function checkAll(categoryId,url) {
	var checkBox = document.categoryDisplayThreeForm.comparison;
	var productCompareCookieName = 'productCompare'+categoryId;
	var checkedFields = getCookie(productCompareCookieName);
	var checkedFieldsArr = checkedFields.split(",");
	var productCount = checkedFieldsArr.length-1;
	
	if (productCount >= 2 && productCount <= 10) {
		setCookie(productCompareCookieName,checkedFields,1);
		window.location.href = url;
	}
	else {
		writeErrorMsg('Please select between two and ten products.');
	}
}

function checkBox(catentryId,categoryId,url) {
	var checkBox = document.categoryDisplayThreeForm.comparison;
	var productCompareCookieName = 'productCompare'+categoryId;
	var productCount = 0;
	var compareCookie = getCookie(productCompareCookieName);
	var checkedFieldsArr = compareCookie.split(",");
	var checkedCount = checkedFieldsArr.length-1;
	
	if (checkedCount >= 10) {
		if (document.getElementById(catentryId).checked) {
			writeErrorMsg('Maximum number of products reached');
			window.location.hash="errorMsg";
			// alert('Maximum number of products reached');
			document.getElementById(catentryId).checked=false;
			return false;
		}
	}
	
	for (var i = 0; i < checkBox.length; i++) {
		if (checkBox[i].checked) {
			productCount++;
			if (compareCookie.indexOf(checkBox[i].value)<0) {
				compareCookie += checkBox[i].value+',';
			}
		}
		else {
			if (compareCookie.indexOf(checkBox[i].value)>=0) {
				compareCookie = compareCookie.replace(checkBox[i].value+',',"");
			}
		}
	}
	
	if (productCount <= 10) {
		setCookie(productCompareCookieName,compareCookie,1);
	}
}

function setCookie(c_name,value,expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			
			if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
	    }
	}
	return "";
}

function writeErrorMsg(errorMessage) {
	var topErrorMessages = document.getElementById('topErrorMessages');
	topErrorMessages.innerHTML = errorMessage;	
	topErrorMessages.className = 'error';
	topErrorMessages.style.display = 'block';
	return false;
}

function resetErrorMsg() {
	var topErrorMessages = document.getElementById('topErrorMessages');
	topErrorMessages.innerHTML = '';	
	topErrorMessages.className = '';
	topErrorMessages.style.display = 'none';

}

function doCheckboxes(cookieName) {
	var products = getCookie(cookieName);
	var productArr = products.split(',');
	for (var j = 0; j < productArr.length; j++) {
		var productCheckbox = document.getElementById(productArr[j]);
		if (productCheckbox != null)
			productCheckbox.checked = 'checked';
	}
}