getCheckedProductCount = function()
{
	return getCheckedProducts().length;
}

getCheckedProducts = function()
{
	var checkedProducts = Array();	
	ids = getCookie('products_ids');	
	if (ids)
	{
		arr = ids.split('&');
		for (i=0; i<arr.length; i++)
		{
			if (arr[i])
			{
				checkedProducts[checkedProducts.length] = arr[i];
			}	
		}
	}
	
	return checkedProducts;
}

 function btnCompared()
 {
	count = countProducts();
	
	var checkedProducts = getCheckedProducts()

	if (count < 2)
	{
		alert('Choose two or more products!');
		return false;
	}
	
	//var url = 'http://h2hreviews.com/comparison/index.html?';
	var url = '/comparison/index.html?';
	//alert(checkedProducts);
	for (i=0; i<checkedProducts.length; i++)
	{
		url += 'product' + i + 'Id=' + checkedProducts[i] + '&';
	}
	
	//alert(url);
	//window.location = url;
	document.location = url;
}

function uncheckedProducts()
{
	$("input[@name=compare]").each(function(){		
		$(this).attr('checked', false);
	})	
}

function setCookie(cookieName,cookieValue,nDays) 
{
	var today = new Date();
	var expire = new Date();
	 
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}
 

function getCookie(name) 
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);

    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }

    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name)
{
	document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
} 

function addInCookie(x) 
{	
	var new_el = false;
	var ids = getCookie('products_ids');
	var ids_new = '';
	
	if (ids)
	{
		arr = ids.split('&');
		ids_new = '';
		for (i=0; i<arr.length; i++)
		{
			if (arr[i])
			{
				if (arr[i] == x)
				{
					new_el = true; 
				}
				else
				{
					ids_new += arr[i] + '&';
				}				
			}
		}
		if (new_el == false)
		{
			ids_new += x + '&';
		}
	}
	else
	{
		ids_new = x + '&';
	}
	//alert(ids_new);
	setCookie('products_ids', ids_new, 1);
}

function removeFromCookie(x) 
{	
	var new_el = false;
	var ids = getCookie('products_ids');
	var ids_new = '';
	
	if (ids)
	{
		arr = ids.split('&');
		ids_new = '';
		for (i=0; i<arr.length; i++)
		{
			if (arr[i])
			{
				if (arr[i] == x)
				{
					new_el = true; 
				}
				else
				{
					ids_new += arr[i] + '&';
				}				
			}
		}
	}
	else
	{
		ids_new = x + '&';
	}
	setCookie('products_ids', ids_new, 1);
}

function countProducts()
{
	ids = getCookie('products_ids');
//	alert(ids);
	if (ids)
	{
		arr = ids.split('&');
		return (arr.length - 1);
	}
	else return 0;
}

function isCheckedProduct(str, x) 
{	
	if (str)
	{
		arr = str.split('&');
		for (i=0; i<arr.length; i++)
		{
			if (arr[i] == x)
			{
				return true; 
			}
		}
	}

	return false;
}


$(document).ready(function(){

	$('.products_selected').html(countProducts() + ' products selected');
	
	var ids = getCookie('products_ids')||'';
	$("input[@name=compare]").each(function(){		
		if (isCheckedProduct(ids, $(this).val()) == true)
		{
			$(this).attr('checked', true);
		}
		else
		{
			$(this).attr('checked', false);
		}
	})
	
	$("input[@name=compare]").bind("click", function(){		
			
		if ($(this).attr('checked'))
		{
			count = countProducts();
			if (count >= 3)
			{
				$(this).attr('checked', false);
				alert('You can not choose more than three products!');
			}
			else
			{
				addInCookie($(this).val());	
			}				
		}
		else
		{
			removeFromCookie($(this).val());
		}			
		$('.products_selected').html(countProducts() + ' products selected');
	});
	
	$(".clear_products").bind("click", {}, function(){
		uncheckedProducts();
		deleteCookie('products_ids');
		$('.products_selected').html('0 products selected');
	});			

	$("#btnCompare").bind("click", {}, btnCompared);
	$("#btnCompare_bottom").bind("click", {}, btnCompared);			
});



