	// QN: this function loads the comparison cookie that contains the category name and the list of model numbers to be compared
	function loadCookieSettings(categoryName)
	{
		currentCookieUserName=getCookieCategory("CompareGrid","EMPTY");
		if (categoryName==currentCookieUserName )
	  	{
			skuList=getCookie("CompareGrid","NA");
			startIndex=skuList.indexOf("#");
			if (startIndex<0) 
				return true;

			skuList=skuList.substring(startIndex+1,skuList.length);
			skuArr=skuList.split("|");
			var i=0;
			for(i=0;i<skuArr.length;i++)
			{
				if (document.getElementById(skuArr[i])!=null) 	
					document.getElementById(skuArr[i]).checked=true;
			}
		}
		return true;    
	}
	
	// Add CATENTDESC.NAME (model number) to the comparison cookie
	function addCheckBox(name,sku,categoryName)
	{
		var newCookieValue="";	

		if (document.getElementById(name).checked)
		{ 
			currentCookieUserName=getCookieCategory("CompareGrid","EMPTY");
			// QN If there's no cookie set already, create one with the name of the category that u're currently in,
			// then add the sku to that, separate with a # sign (example: Camera Phones#SRSM30)
			if ("EMPTY"==currentCookieUserName )
			{
				setCategoryCookie("CompareGrid",categoryName);			
				newCookieValue=getCookieCategory("CompareGrid","NA");
				newCookieValue +="#"+ sku;
				setCookie("CompareGrid",newCookieValue);
			// QN If cookie exists, but for a different category (not the same one you're on),
			// then remove it, create a new one with the current category, and add sku to that
			} else if(categoryName!=currentCookieUserName) {
				removeCookie("CompareGrid"); 
				setCategoryCookie("CompareGrid",categoryName);
				newCookieValue=getCookieCategory("CompareGrid","NA");
				newCookieValue +="#"+ sku;
				setCookie("CompareGrid",newCookieValue);
			// QN Cookie exists for the current category, just add sku to it, prepend with a pipe
			} else {
				cookieValue=getCookie("CompareGrid","NA");
				newCookieValue = cookieValue + "|"+ sku;
				setCookie("CompareGrid",newCookieValue);		
			}
			
		} else {
			newList=removeCookieSKU("CompareGrid",name); // removeCookieSKU() is in cookies.js
		}
	}
	
	// Create the sku list to be passed to Advizia
	function addSKUList()
	{
		cookieValue=getCookie("CompareGrid","NA");
		if (cookieValue == null) return "";
		currentCookieUserName=getCookieCategory("CompareGrid","EMPTY");
	 	skuListStartPostion=cookieValue.indexOf(currentCookieUserName+"#");
	 	if (skuListStartPostion < 0) return "";
	 	skuListStartPostion += currentCookieUserName.length + 1;
	 	skuList=cookieValue.substring(skuListStartPostion);

	 	return skuList;
	}
	
	function submitCompareProducts()
	{
	  document.compareProductForm.submit(); 
	} 	
	
	function compareProducts() 
	{   
		var cmpimg=document.getElementById("WC_CachedCategoriesDisplay_Compare1");
		var skuList=addSKUList();
		var compare_arr=skuList.split("|");
		if(compare_arr.length<2)
		{
			alert ("Please select at least 2 products for comparison");
			return;
		} else { 
			document.compareProductForm.compareSKUList.value=skuList;
			setTimeout('submitCompareProducts()',30);
            
		}
	}		

// VC added functions for search results comparision
    function loadSearchResultsCompareCookie()
    {
    	var compareCheckBox ;
   	if ( compareProducts && compareProducts.length > 0 )
    	{
    	   for ( uncheckElements = 0 ; uncheckElements < compareProducts.length ; uncheckElements++ )
    	   {
    	   	compareCheckBox = document.getElementById(compareProducts[uncheckElements]);
    	   	if ( compareCheckBox )
    	   	compareCheckBox.checked = false;
    	   }
    	}
            var skuElement;
            var skuElementArr ;
            // Get the cookie
            skuList=getCookie("SearchResultsCompareCookie","NA");

            if (skuList == null || skuList == '') 
                return true;
            skuArr=skuList.split("````");
            var i=0;
            for(i=0;i<skuArr.length;i++)
            { 
                skuElement = skuArr[i] ;
                skuElementArr = skuElement.split("~~~~");
                if (document.getElementById(skuElementArr[0])!=null)   
                    document.getElementById(skuElementArr[0]).checked=true;
            }
    }
    
    function addSearchResultsCheckBox(name,sku,damCategoryName)
    {
        var newCookieValue="";  

        if (document.getElementById(name).checked)
        { 
            currentCookieUserName=getCookie("SearchResultsCompareCookie","EMPTY");
            // QN If there's no cookie set already, create one with the name of the category that u're currently in,
            // then add the sku to that, separate with a # sign (example: Camera Phones#SRSM30)
            if (! currentCookieUserName || "EMPTY"==currentCookieUserName || currentCookieUserName == '')
            {
                newCookieValue == "";
                newCookieValue += sku + "~~~~" + damCategoryName;

                setCookie("SearchResultsCompareCookie",newCookieValue);
            // QN If cookie exists, then add values to the existing cookie
            }  else {
                cookieValue=getCookie("SearchResultsCompareCookie","NA");
                newCookieValue = cookieValue + "````"+ sku + "~~~~" + damCategoryName;;
                setCookie("SearchResultsCompareCookie",newCookieValue);        
            }
            
        } else {
            newList=removeSearchResultsCookieSKU("SearchResultsCompareCookie",name,damCategoryName);
        }
       
    }

    function removeSearchResultsCookieSKU(name,skuName, damCategoryName)
    {
        fallback="EMPTY";
        var cookieToRemove = skuName + "~~~~" + damCategoryName;
        var cookieVal=trimSearchCookie(getCookie(name,fallback));
        var newCookieVal = "";
        
        var cookies_arr=cookieVal.split("````");
        var currentSku = "";
            
	for ( i = 0 ; i < cookies_arr.length ; i++ ) 
    	{
	    currentSku = cookies_arr[i];
	    if ( currentSku != cookieToRemove)
	    {
	       if ( newCookieVal == "" )
	           newCookieVal = currentSku;
	       else
	       	   newCookieVal = newCookieVal + "````" + currentSku;
	    }
	        
        }  
        setCookie(name,newCookieVal);

        return document.cookie;

    }    
    
    
    function compareSearchResultsProducts() 
    {
    
  	var skuList= trimSearchCookie(getCookie("SearchResultsCompareCookie",""));
  
  
  	if ( ! skuList  || skuList == "" ) 
  	{
  	     alert ("Please select at least 2 products for comparison");
  	     return false;
  	}        
          
          var compare_arr=skuList.split("````");
          var skuListForSubmit = "";
          var currSkuInfo = "";
          var skuElements_arr;
          var damCategoryName = "";
          if(compare_arr.length<2)
          {
              alert ("Please select at least 2 products for comparison");
              return false;
          } else {
              
              for ( i = 0 ; i < compare_arr.length ; i++ ) 
              {
                  currSkuInfo = compare_arr[i];
                  skuElements_arr = currSkuInfo.split("~~~~");
                  // create the list of partnumber seperated by | for submitting to command
                  if ( i ==0 )
                      skuListForSubmit = skuElements_arr[0];
                  else
                      skuListForSubmit = skuListForSubmit + "|" + skuElements_arr[0] ;
                  // if we do not have damcategory name populate the values    
                  if (! damCategoryName || damCategoryName == "" )
                      damCategoryName = skuElements_arr[1];
              }
              // Populate the elements values and category name and submit the form
              document.compareProductForm.compareSKUList.value=skuListForSubmit;
              if ( damCategoryName == "" )
              {
                  alert ("Selected products are not eligible for comparsion. Please select two similar products for comparision");
                  return false;                
              }
              document.compareProductForm.compareUser.value=damCategoryName;
              document.compareProductForm.submit();
          }
        return false;
    }       
    
    function removeSearchResultsCompareCookie()
    {

        removeCookie("SearchResultsCompareCookie");
    }
    
    function trimSearchCookie(sInString) {
    if ( sInString == null || sInString == '')
    	return sInString;
      sInString = sInString.replace( /^\s+/g, "" );// strip leading
      return sInString.replace( /\s+$/g, "" );// strip trailing
    }
    
    function noProductsToCompare()
    {
    	alert ("No products available for comparision");
    }
    
            