// CusaProductCookie.js
// Q01871 - 10/27/09 QC-378 Cookie expiration set to 45 days
// newFunction
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function recentlyViewedProductsArrayCookie(productId){
Array.prototype.find = function(searchStr) {
  var returnArray = false;
    for (i=0; i<this.length; i++) {
        if (typeof(searchStr) == 'function') { 
            if (searchStr.test(this[i])) {
                  if (!returnArray) { returnArray = [] }
                          returnArray.push(i);
            }    
        } else { 
             if (this[i]===searchStr) {
                     if (!returnArray) { returnArray = [] }
                             returnArray.push(i);      
             }    
        }  
   }  
   return returnArray;
}

	// Read the cookie to know whether we have anything in it
	var myArrayRead = readCookie("recentlyviewedproductscookie");
	//if null then go and create the cookie with the product id else
	if(myArrayRead!=null){
		var finalArray = [];
		//split the whole string and seperate that using the delimiter. here its pipe
		finalArray = myArrayRead.split('|');
		//alert("array length :: "+finalArray.length);
		var len = finalArray.length;
		//this is a prototype for finding whether we have the product id already present
		var exists = finalArray.find(productId);
		//alert("exists :: "+exists);
		//if doesnt exist - append it to the cookie value
		//else get the index where the product id is located and splice it and add that
		//id to the last position in the array. Make a sting with comma seperated values
		// and add it to the cookie
		if(!exists){
			//check for array not exceeding 5 product ids
			if(finalArray.length>3){
				finalArray.splice(3,1);				
			}
			myArrayRead = "";				
			
			//finalArray[finalArray.length]=productId;
			//finalArray.reverse();
			finalArray.unshift(productId);
			for(z=0;z<finalArray.length;z++){
				//alert("finalArray[z] value :: "+finalArray[z]);
				if(z>0){
					myArrayRead = myArrayRead+"|"+finalArray[z];
				}else{
					myArrayRead = finalArray[z];
				}
			}
			createCookie("recentlyviewedproductscookie", myArrayRead, 45);
		}else{
			//alert("exists"+exists[0]);
			for(a=0;a<exists.length;a++){
				//alert("a :: "+a);
				//alert("exists"+exists[a]);
				finalArray.splice(exists[a],1);
			}			
			var finalArrayLenAfterSplice = finalArray.length;
			//alert("finalArrayLenAfterSplice :: "+finalArrayLenAfterSplice);
			if(finalArray.length>3){
				finalArray.splice(exists[0],1);				
			}
			//finalArray[finalArray.length]=productId;
			//finalArray.reverse();
			finalArray.unshift(productId);
			//alert("myArrayRead value :: "+myArrayRead);
			myArrayRead = "";
			for(k=0;k<finalArray.length;k++){
				//alert("finalArray[k] value :: "+finalArray[k]);
				if(k>0){
					myArrayRead = myArrayRead+"|"+finalArray[k];
				}else{
					myArrayRead = finalArray[k];
				}
			}
			createCookie("recentlyviewedproductscookie", myArrayRead, 45);
		}		
	}else{
		var myArray = [];
		myArray[0] = productId;
		//alert("first array value :: "+myArray[0]);
		myArrayRead = myArray[0];
		createCookie("recentlyviewedproductscookie", myArrayRead, 45);
	}
	
	/*
	var myArrayRead1 = readCookie("abc");
	var finalArray1 = [];
	finalArray1 = myArrayRead1.split(',');
	alert("array length :: "+finalArray1.length);
	for(i=0; i<finalArray1.length;i++){
		alert(finalArray1[i]);
	}
	*/
}
