
//
// ***
// * This javascript function when called will create two cookies: SARECENTVIEW  and SARECENTVIEW_COUNTER
// *   1.  SARECENTVIEW cookie will store the most 3 recent viewed product.  There will be no duplication of the product.
// *                    The cookie will have this format:  pid_<i>~~~<productId>@pname_<i>~~~<productName>@purl_<i>~~~<productURL>@
// *				    where <i> can be 1,2, or 3
// *   2.  SARECENTVIEW_COUNTER cookie will store the current <i> value.  This will help us know the number of the products have been stored in the SARECENTVIEW cookie so far
// ***
//
function setRecentViewCookie(pId,pName,pURL)   
{
    //alert("In setRecentViewCookie method now");
   
   	var url = document.location + "";
	var urlStart = url.substring(url.indexOf(".")) + "";
	var domain = urlStart.substring(0, urlStart.indexOf("/"));
	
	if(domain.indexOf(":") != -1){
		domain = domain.substring(0, domain.indexOf(":"));
	}
	
	//alert("domain="+domain);
   
    var cookieValue = getCookie('SARECENTVIEW');
    //alert("cookie value="+cookieValue);
    var counter = getCookie('SARECENTVIEW_COUNTER');
    var counterValue = counter;
    
    var pid_1;var pid_2; var pid_3;
    var pname_1; var pname_2; var pname_3;
    var purl_1; var purl_2; var purl_3;
    
    //alert("counter="+counter);
     var currentProductIdKey;
     var currentProductId ;
     var existed = false;
    if(cookieValue != null && cookieValue != '')
    {
      
          
	      for(i=1 ; i <=counter; i++)
	      {
	          
	          //alert("i="+i+"; counter="+counter);
	          currentProductIdKey = 'pid_'+i;
	          currentProductId = getUserCookieValue('SARECENTVIEW',currentProductIdKey);
	          
	          //alert("pid="+pId+" ; currentProductId="+currentProductId);
	          if(currentProductId != null)
	          {
				  if(pId == currentProductId)
				  {
				      existed=true;
				  }
				 
	          }
	      }  
	       
	       if(!existed)  // Product has not been view yet - add it
		          {
		          	//alert("in if not existed");
		          	//User has not viewed this product yet
		          	if(counter != 3)
		          	{
		          		//alert("in if counter != 3");
		          		var newCounter=parseInt(counter)+1;
		          		//alert("new counter in if = "+newCounter);
		          		cookieValue=cookieValue+"pid_"+newCounter+"~~~"+pId+"@pname_"+newCounter+"~~~"+pName+"@purl_"+newCounter+"~~~"+pURL+"@";
		          		counterValue=parseInt(counter)+1;
		          		//alert("new cookievalue in if ="+cookieValue);
		          		
		          	
		          	}
		          	else if (counter == 3)
		          	{
		          		pid_1=getUserCookieValue('SARECENTVIEW','pid_1');
		          		pid_2=getUserCookieValue('SARECENTVIEW','pid_2');
		          		pid_3=getUserCookieValue('SARECENTVIEW','pid_3');
		          		
		          		pname_1=getUserCookieValue('SARECENTVIEW','pname_1');
		          		pname_2=getUserCookieValue('SARECENTVIEW','pname_2');
		          		pname_3=getUserCookieValue('SARECENTVIEW','pname_3');
		          		
		          		purl_1=getUserCookieValue('SARECENTVIEW','purl_1');
		          		purl_2=getUserCookieValue('SARECENTVIEW','purl_2');
		          		purl_3=getUserCookieValue('SARECENTVIEW','purl_3');
		          		
		          		//move everything up 1     		
		          		pid_1 = pid_2;
		          		pname_1 = pname_2;
		          		purl_1 = purl_2;
		          		
		          		pid_2 = pid_3;
		          		pname_2 = pname_3;
		          		purl_2 = purl_3;
		          		
		          		pid_3 = pId;
		          		pname_3 = pName;
		          		purl_3 = pURL;
		          		
		          		
		          		
		          		cookieValue="pid_1~~~"+pid_1+"@pname_1~~~"+pname_1+"@purl_1~~~"+purl_1+"@"+
		          					"pid_2~~~"+pid_2+"@pname_2~~~"+pname_2+"@purl_2~~~"+purl_2+"@"+
		          					"pid_3~~~"+pid_3+"@pname_3~~~"+pname_3+"@purl_3~~~"+purl_3+"@";
		          	    
		          	    //alert("cookieValue after adding new value ="+cookieValue);
		          	
		          		
		          		//add the new product
		          	}
		         }
    }
    else  //Cookie does not exist
    {
       
		//alert("SARECENTVIEW COOKIE DOES NOT EXIST");
        cookieValue="pid_1~~~"+pId+"@pname_1~~~"+pName+"@purl_1~~~"+pURL+"@";
        counterValue="1";
    
		
       
    }
    
   
 
   document.cookie = "SARECENTVIEW"+"="+cookieValue+"; path=/";
   document.cookie = "SARECENTVIEW_COUNTER"+"="+ counterValue+"; path=/"; 
	
    
}



function getProductId(index)
{
	//alert("IN getProductId method");
	var pid ="pid_"+index;
	var str = null;
	str = getUserCookieValue('SARECENTVIEW','pid');
	if(str != null)
		document.write(str);
	
}

function getProductName(index)
{
	//alert("IN getProductName method. index="+index);   
	var name="pname_"+index;
	
	var str = null;
	str = getUserCookieValue('SARECENTVIEW',name);
	if(str != null)
	{
		document.write(str);
		document.write('</a>');
		document.write('</li>');
	}
	
	
}           

function getProductSEOLink(index)
{
	//alert("IN getProductSEOLink method");
	var url = "purl_"+index
	
	var result;
	 result= getUserCookieValue('SARECENTVIEW',url);  
	if(result == null)
		result="";
	else{
	
	document.write('<li>');
	document.write('<a href="');
	document.write(result);
	document.write('">');
	}
	
}

function displayCartItems()
{
	//alert("IN displayCartItems method");
	var str = "0";
	str = getUserCookieValue('SAMINICART','items');
	if(str == null)
		str = "0";
	document.write(str);
	
	
}

function displayCartTotal()
{
	//alert("IN displayCartItems method");
	var str = "0.00";
	str = getUserCookieValue('SAMINICART','amount');
	if (str != null)
	{
		var iDotIndex = str.indexOf(".");
		if (str.length - iDotIndex > 3)
			str = str.substring(0, iDotIndex + 3);
	}
	else
	{
		str = "0.00";
	}
	
	document.write(str);
	
	
}

function getUserCookieValue(cookieName, keyName)
{
 //alert("in getUserCookieValue");
 var cookieValue = getCookie(cookieName);
 //alert("cookieValue="+cookieValue);
  if ( cookieValue == null )
 	return null;
 var  nameDelimiter = "@" ;
 var pairDelimiter = "~~~" ;
 var matchPattern = keyName + pairDelimiter + '(.*?)(' + nameDelimiter +')';	 	
 //alert("match partern = "+matchPattern);
 var results = cookieValue.match ( matchPattern);
 //alert("resutls="+results);
  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}

function getCookie ( cookieName )
{
  var results = document.cookie.match ( cookieName + '=(.*?)(;|$)' );
  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}


