//left pane id = pc-1, pc-2 ,pc-3

function compare_tray_add(id,name,link,image)
{
	var empty_space = -1;
	var item_exist = false;
	
	for(var i=0;i<3;i++)
	{		
		if(readCookie("compare-product-" + i) == null && empty_space == -1)
		{
			empty_space = i;
		}
		else
		{
			if(parseInt(readCookie("compare-product-" + i)) == id)
			{
				item_exist = true;
				break;
			}
		}		
	}
	
	if(!item_exist)
	{
		if(empty_space == -1)
		{
			alert("No more than 3 products can be added to tray");
		}
		else
		{
			createCookie("compare-product-" + empty_space, id);
			if(document.getElementById("pc-" + (empty_space+1)))
			{
				var list = document.getElementById("pc-" + (empty_space+1));
				if(image.indexOf("<img") != -1)
				{
					list.innerHTML = "<a href='" + link + "'>" + image + name + "</a><div style='clear:both'></div>";	
				}
				else
				{
					list.innerHTML = "<a href='" + link + "'><img src='" + image + "' alt='' border='0' />" + name + "</a><div style='clear:both'></div>";	
				}
				add_select();
				//alert("Product added to tray");
			}
		}
	}
	else
	{
		//alert("Product already added to tray");
	}
	return false;
}

function add_select()
{
	var empty_space = -1;	
	
	for(var i=0;i<3;i++)
	{		
		if(readCookie("compare-product-" + i) == null && empty_space == -1)
		{
			empty_space = i;
		}		
	}
	
	if(empty_space != -1)
	{
		if(document.getElementById("pc-" + (empty_space+1)))
		{
			var list = document.getElementById("pc-" + (empty_space+1));		
			list.innerHTML = build_option_left(empty_space+1);
		}
	}
	
	return false;
}

function compare_tray_erase_all()
{
	for(var i=0;i<3;i++)
	{		
		eraseCookie("compare-product-" + i);
		if(document.getElementById("pc-" + (i+1)))
		{
			var list = document.getElementById("pc-" + (i+1));
			list.innerHTML = build_option_left(i+1);
		}
	}
	return false;
}

function erase_item(id)
{	
	for(var i=0;i<3;i++)
	{	
		if(readCookie("compare-product-" + i) != null)
		{
			if(parseInt(readCookie("compare-product-" + i)) == parseInt(id))
			{				
				eraseCookie("compare-product-" + i);
				if(document.getElementById("pc-" + (i+1)))
				{
					var list = document.getElementById("pc-" + (i+1));
					list.innerHTML = build_option_left(i+1);	
				}
			}
		}
	}
	
	return false;	
}

function build_option_left(id)
{
	var option_shown = false;
	for(var i=0; i< 3;i++)
	{		
		if(document.getElementById("pc-" + (i+1)))
		{
			if(document.getElementById("pc-" + (i+1)))
			{
				var list = document.getElementById("pc-" + (i+1))
				
				if(list.innerHTML.indexOf("<select") > -1 && parseInt(id)!= (i+1))
				{
					option_shown = true;
					break;
				}
			}
		}
		
	}
	
	var _select = "";
	if(!option_shown)
	{
		_select += "<select style=\"width:150px;\" onchange=\"changeSelect(this.options[this.selectedIndex].value)\">";
		_select += "<option>Please select</option>";
		for(var i=0; i< dataLeft.product.length;i++)
		{
			if(!compare_item_exist(dataLeft.product[i].id))
				_select += "<option value='" + dataLeft.product[i].id + "'>" + dataLeft.product[i].name + "</option>"; 
		}
		_select += "</select>";	
	}
	return _select
}

function compare_item_exist(id)
{
	var item_exist = false;
	for(var i=0;i<3;i++)
	{		
		if(readCookie("compare-product-" + i) != null)
		{
			if(parseInt(readCookie("compare-product-" + i)) == parseInt(id))
			{
				item_exist = true;
				break;
			}
		}		
	}
	return item_exist;
}

function compare_item_number()
{
	var items = 0;
	for(var i=0;i<3;i++)
	{		
		if(readCookie("compare-product-" + i) != null)
		{
			items++;
		}		
	}
	return items;
}