    
    //cookieValue : categoryId=valCatId&prodCode1=prodName1&prodCode2=prodName2&prodCode3=prodName3&prodCode4=prodName4

    var minNumberOfProducts = 2;
    var maxNumberOfProducts = 4;
    var putDelete = true;    
    var htmlOriginale = "";

    function verifyCompareCookie() {
    
        var cookieValue = readCookie(compareCookie);
        
        if (cookieValue) {
            var cookieCategoryId = getCategoryFromCompareCookie(cookieValue);
            
            if (cookieCategoryId != categoryId) {
            	//alert('found another category cookie; will be deleted ...');
                deleteCookieFromSession(compareCookie);
            }
            else {
                //alert('found compare cookie; updating checkboxes ...');
                // check the right checkboxes to honor the cookie
                updateCompareCheckboxes(cookieValue);
            }
        }
        else {
            //alert('We found no compare cookie');
        }
    }
    
    function handleCompareCheckbox(form) {
        if (form.productCheckbox.checked) {
            appendToCompareCookie(form);
            if(putDelete == true) {                        
            	if(htmlOriginale == "") htmlOriginale = document.getElementById('deleteCookie').innerHTML;
               	document.getElementById('deleteCookie').innerHTML+='&nbsp;<a href="javascript:deleteCookiesAndUpdateChecks(compareCookie);" style="vertical-align:top; font-family: verdana; font-size: 11px; color: #000000">Deseleziona tutti</a>';
               	putDelete = false;
            }            
        }
        else {
            var productId = form.catEntryId.value;
            removeFromCompareCookie(productId);
            var valore = readCookie(compareCookie);
            if(valore.indexOf("&") < 0) {
            	document.getElementById('deleteCookie').innerHTML=htmlOriginale;
				putDelete = true;  
            }
        }
    }
    
    function appendToCompareCookie(form) {
    
        var productId = form.catEntryId.value;
        var productName = form.prodName.value.replace(/&/g, '<%26>');
        
        var cookieValue = readCookie(compareCookie);
        if (cookieValue) {
            if (checkCompareCounter()) {
                // append to the cookie
                cookieValue += "&" + productId + "=" + productName;                        
            }
            else {
                // uncheck the checkbox
                form.productCheckbox.checked = false;

                // build the custom dialog window
                buildCompareDialog(cookieValue);
            }
        }
        else {
            // recreate the new cookie for the current category
            cookieValue = "categoryId=" + categoryId;
            cookieValue += "&" + productId + "=" + productName;    
        }
        
        writeCookieInSession(compareCookie, cookieValue);
    }
    
    function removeFromCompareCookie(productId) {
    
        var cookieValue = readCookie(compareCookie);
        
        if (cookieValue) {
        
            var arrNVP = cookieValue.split("&");
            var newCookieValue = arrNVP[0]; // category attribute
            
            for (i = 1; i < arrNVP.length; i++) {
            
                var cookieProductId = arrNVP[i].split("=")[0];
                
                if (productId != cookieProductId) {
                    newCookieValue += "&" + arrNVP[i];
                }
            }
            
            writeCookieInSession(compareCookie, newCookieValue);
        }     
    }
    
    function removeProductFromCompareCookie(productId) {
    
        var cookieValue = readCookie(compareCookie);
        
        if (cookieValue) {
        
            var arrNVP = cookieValue.split("&");
            var newCookieValue = arrNVP[0]; // category attribute
            
            for (i = 1; i < arrNVP.length; i++) {
            
                var cookieProductId = arrNVP[i].split("=")[0];
                
                if (productId != cookieProductId) {
                    newCookieValue += "&" + arrNVP[i];
                }
            }
            
            writeCookieInSession(compareCookie, newCookieValue);
            updateCompareCheckboxes(newCookieValue);
        }     
    }
    
    function updateCompareCheckboxes(cookieValue) {
    
        var arrNVP = cookieValue.split("&");
        
        for (i = 0; i < document.forms.length; i++) {
        
            var form = document.forms[i];
            var formName = form.name;
            
            if (formName.indexOf("OrderItemAddForm") >= 0) {
            
                form.productCheckbox.checked = false;
                var productId = form.catEntryId.value;
                
                for (j = 1; j < arrNVP.length; j++) { // skip the categoryId nvp
                    var cookieProductId = arrNVP[j].split("=")[0];

                    if (productId == cookieProductId) {
                        form.productCheckbox.checked = true;
                        if(putDelete == true) {         
	                        if(htmlOriginale == "") htmlOriginale = document.getElementById('deleteCookie').innerHTML;               
                        	document.getElementById('deleteCookie').innerHTML+='&nbsp;<a href="javascript:deleteCookiesAndUpdateChecks(compareCookie);" style="vertical-align:top; font-family: verdana; font-size: 11px; color: #000000">Deseleziona tutti</a>';
                        	putDelete = false;
                        }
                        break;
                    }
                }
            }
        }
    }
    
    function getCategoryFromCompareCookie(cookieValue) {
        var arrNVP = cookieValue.split("&");
        // by design the first value is always the category
        return arrNVP[0].split("=")[1];
    }
    
    function checkCompareCounter() {
    
        var cookieValue = readCookie(compareCookie);
        
        if (cookieValue) {
            arrNVP = cookieValue.split("&");
        
            if (arrNVP.length >= maxNumberOfProducts + 1) {
                return false;
            }
        }
        
        return true;
    }
    
    function checkCompareMinLimit() {
    
        var checkedProducts = 0;
        var cookieValue = readCookie(compareCookie);
            
        if (cookieValue) {
            arrNVP = cookieValue.split("&");
            checkedProducts = arrNVP.length;
        }
        
        if (checkedProducts <= minNumberOfProducts) {
            buildCompareDialog(cookieValue);
            return false;
        }
        
        return true;
    }
    
    function buildCompareDialog(cookieValue) {
    
        var compareWindow = window.open('','name','width=500,height=200,toolbar=no,scrollbars=no,resizable=no,modal=yes');
        var doc = compareWindow.document;
        
        doc.write("<HTML>");
		doc.write("<HEAD>");
        doc.write("  <TITLE>Dialogo confronto</TITLE>");
        doc.write("  <style type=\"text/css\">");
        doc.write("	   .textnormal {");
		doc.write("		text-decoration:none;");
		doc.write("		font-family: Verdana;");
		doc.write("		color:#000000;");
		doc.write("		font-size:10;");
		doc.write("	   }");
        doc.write("  </style>");
		doc.write("</HEAD>");
		doc.write("<BODY>");
        doc.write("<font class=\"textnormal\" >");
		doc.write("<b>Selezionare al minimo " + minNumberOfProducts + " e al massimo " + maxNumberOfProducts + " prodotti per il confronto.</b>");
        doc.write("</font>");
        doc.write("<br><br>");
        
        var arrNVP;
        
        if (cookieValue && (arrNVP = cookieValue.split("&")).length > 1) {
        
          doc.write("<table width=\"100%\">");
          
          for (i = 1; i < arrNVP.length; i++) {
        
            var productId = arrNVP[i].split("=")[0];
            var productName = arrNVP[i].split("=")[1];
            productName = productName.replace(/<%26>/g, '&');
            
            doc.write("<tr>");
            doc.write("  <td>");
            doc.write("    <font face=\"verdana\" size=\"1\" >");
            doc.write("      <b>" + productName + "</b>");
            doc.write("    </font>");
            doc.write("  </td>");
            doc.write("  <td>");
            doc.write("    <input type=\"button\" class=\"textnormal\" onclick=\"window.opener.removeProductFromCompareCookie(" + productId + ");this.disabled=true;\" name=\"" + productId + "\" id=\"" + productId + "\" value=\"Elimina\">");
            doc.write("  </td>");
            doc.write("</tr>");
          }
          
          doc.write("</table>");
        }
        else {
            doc.write("<font class=\"textnormal\" >");
            doc.write("<b>Nessun prodotto selezionato.</b>");
            doc.write("</font>");
            doc.write("<br>");
        }
        
        doc.write("<br>");
        doc.write("<input type=\"button\" class=\"textnormal\" onClick=\"window.close();\" value=\"Chiudi\">");
		doc.write("</BODY>");
		doc.write("</HTML>");
		doc.close();
	}
	
	function deleteCookiesAndUpdateChecks(cookieValue) {
		deleteCookieFromSession(compareCookie);
		updateCompareCheckboxes(cookieValue);
		document.getElementById('deleteCookie').innerHTML=htmlOriginale;
		putDelete = true;    
	}
    