
function GetCookie(sName)
{
	var aCookie=document.cookie.split("; ");
	for(var i=0;i<aCookie.length;i++)
	{
		var aCrumb=aCookie[i].split("=");

		if(sName==aCrumb[0])
			return unescape(aCrumb[1]);
	}
	
	return null;
}
function ProcessCompCookie(CompCookie){if(!CompCookie)return;var aCookie=CompCookie.split("|");var sProdList="";for(var i=0;i<aCookie.length;i++)
{if(i==0){document.getElementById('LastCatID').value=aCookie[i];}
else{document.getElementById('compare'+i).value=aCookie[i];sProdList=sProdList+aCookie[i]+',';if(document.getElementById('checked_'+aCookie[i])!=null)
{loopcheck(aCookie[i],true);}}}
return sProdList;}
function GetCompItems(ProdIds)
{
	new Ajax.Request('/searchresultslist.phtml',{method:'get',parameters:{ajaxRequest:'getCompItems='+ProdIds,1:1},onSuccess:CompItemsCallBack});
}
function CompItemsCallBack(transport)
{
	eval(transport.responseText);
	var i;
	for(i=0;i<=old_items.length-1;i++)
	{
		buildItem(old_items[i][0],old_items[i][1],old_items[i][2]);
	}
}
function rememberItems(val)
{dml=document.searchForm;if(dml.elements.length)
len=dml.elements.length;else
len=0;var i=0;for(i=0;i<len;i++)
{var match=/^checked_(\d*)$/i.exec(dml.elements[i].id);if(match)
{if(dml.elements[i].checked)
updateComp(dml.elements[i]);}}}
function switchDiv(hideDiv,showDiv)
{
	$(hideDiv).toggle();
	$(hideDiv+1).toggle();
	$(hideDiv+2).toggle();
	$(showDiv).toggle();
	$(showDiv+1).toggle();
	$(showDiv+2).toggle();
	new Ajax.Request('/searchresultslist.phtml',{method:'get',parameters:'ajax=1&vw='+showDiv});
	return true;
}
function updateComp(obj,form)
{
	val=obj.value;
	if(obj.checked)
	{
		if(count>=3)
			return;
			
		imgobj=document.getElementById("img"+val);
		
		isrc=imgobj.getAttribute("src");
		
		pTitle=imgobj.getAttribute("alt");
		
		for(i=0;i<item_val.length;i++)
		{
			if(item_val[i]==val)
				return;
			else
				item_val.push(val);
		}
	
		count++;
		
		var compareBox=document.getElementById('compare_box');
		
		compareBox.style.display='block';
		
		var indexNumber=document.getElementById('index');
		var number=(document.getElementById("index").value-1)+2;
		indexNumber.value=number;
		var newDivId=val;
		tbl=document.getElementById("parent");
		var newDiv=document.createElement('div');
		newDiv.setAttribute("id",newDivId);
		if(pTitle.length>38)
			pTitle=pTitle.substr(0,35)+"...";
			
		newDiv.innerHTML="<div style='display:inline;width:170px'><div style='float:left; margin:7px;'><a href='/product.html?postId="+val+"'><img width=\"50\" src='"+isrc+"' alt='"+pTitle+"'/></a> <br /><a href='javascript:removeLocation(\""+newDivId+"\")' style='color:#663;text-decoration:underline'>remove</a></div> <div style='float:right;width:90px;margin:2px;' > <a  href='/product.html?postId="+val+"' style='color:#5C85D6;font-weight:normal;'>"+pTitle+"</a></div> <br clear='all'></div> ";
		
		if(count>1)
			newDiv.innerHTML="<div style='margin:4px; border-top:2px solid #cccccc'></div>"+newDiv.innerHTML;tbl.appendChild(newDiv);
	}
}
function buildItem(image,title,ID)
{if(!$('compare_box'))return;count++;var compareBox=document.getElementById('compare_box');compareBox.style.display='block';var indexNumber=document.getElementById('index');var number=(document.getElementById("index").value-1)+2;indexNumber.value=number;var newDivId=ID;tbl=document.getElementById("parent");var newDiv=document.createElement('div');newDiv.setAttribute("id",newDivId);if(title.length>38)title=title.substr(0,35)+"...";newDiv.innerHTML="<div style='display:inline;width:170px'><div style='float:left; margin:7px;'><a href='/product.html?postId="+ID+"'><img width=\"50\" src='"+image+"' alt='"+title+"'/></a> <br /><a href='javascript:removeLocation(\""+newDivId+"\")' style='color:#663;text-decoration:underline' >remove</a></div> <div style='float:right;width:90px;margin:2px;' > <a  href='/product.html?postId="+ID+"' style='color:#5C85D6;font-weight:normal;'>"+title+"</a></div> <br clear='all'></div> ";if(count>1)newDiv.innerHTML="<div style='margin:4px; border-top:2px solid #cccccc'></div>"+newDiv.innerHTML;tbl.appendChild(newDiv);}
function removeLocation(divNum)
{
	var d=$('parent');
	var olddiv=$(divNum);
	var chkBox=$('checked_'+divNum);
	if(olddiv)
		d.removeChild(olddiv);
		
	itemUnChecked(chkBox,divNum);
	count--;
	writeCookie();
	
	if(count<=0)
	{
		var compareBox=document.getElementById('compare_box');
		compareBox.style.display='none';
	}
}
function submitForm()
{if(count>1)
{return true;}
else
{alert("You must select more than one product to compare.");}
return false;}
var compareLimit=3;
function checkCompare(e,sCatID)
{
	if(e.checked)
	{
		itemChecked(e,sCatID);
	}
	else
	{
		itemUnChecked(e,0);
		removeLocation(e.value);
	}
	writeCookie();
}
function itemChecked(e,sCatID)
{
	var isOpen;
	var sValue;
	
	if(sCatID!=document.getElementById("LastCatID").value)
	{
		if(document.getElementById("compare1").value!=""){removeLocation(document.getElementById("compare1").value)};
		if(document.getElementById("compare2").value!=""){removeLocation(document.getElementById("compare2").value)};
		if(document.getElementById("compare3").value!=""){removeLocation(document.getElementById("compare3").value)};
		
		document.getElementById("compare1").value="";
		document.getElementById("compare2").value="";
		document.getElementById("compare3").value="";
		
		UncheckAll();
		
		e.checked=true;
		
		document.getElementById("LastCatID").value=sCatID;
	}

	isOpen=false;
	for(var i=1;i<=compareLimit;i++)
	{
		sValue=eval("document.getElementById('compare"+i+"').value");
		if(sValue==""){isOpen=true;break;}
	}

	if(isOpen)
	{
		switch(i)
		{
			case 1:
				document.getElementById("compare1").value=e.value;
				break;
			case 2:
				document.getElementById("compare2").value=e.value;
				break;
			case 3:
				document.getElementById("compare3").value=e.value;
				break;
		}
	
		updateComp(e);
		loopcheck(e.value,true)
	}
	else
	{
		alert("You can select only up to "+compareLimit+" products to compare.  Please remove a product first.");
		e.checked=false;
		return;
	}
	
	return;
}
function itemUnChecked(e,ID){var sValue;if(e!=null){ID==e.value;}
for(var i=1;i<=compareLimit;i++){sValue=eval("document.getElementById('compare"+i+"').value");if(ID==sValue){break;}}
switch(i){case 1:document.getElementById("compare1").value="";break;case 2:document.getElementById("compare2").value="";break;case 3:document.getElementById("compare3").value="";break;}
loopcheck(ID,false)
return;}
function loopcheck(ID,checked)
{var allChecks=document.getElementsByName("srch[]")
var i;for(i=0;i<=allChecks.length-1;i++)
{if(allChecks[i].value==ID)
{allChecks[i].checked=checked;}}}
function writeCookie(){var sCookieValue;sCookieValue="";if(!document.getElementById("LastCatID").value==""){sCookieValue=sCookieValue+document.getElementById("LastCatID").value;}
if(!document.getElementById("compare1").value==""){if(!sCookieValue==""){sCookieValue=sCookieValue+"|";}
sCookieValue=sCookieValue+document.getElementById("compare1").value;}
if(!document.getElementById("compare2").value==""){if(!sCookieValue==""){sCookieValue=sCookieValue+"|";}
sCookieValue=sCookieValue+document.getElementById("compare2").value;}
if(!document.getElementById("compare3").value==""){if(!sCookieValue==""){sCookieValue=sCookieValue+"|";}
sCookieValue=sCookieValue+document.getElementById("compare3").value;}
setCookie(cookieName,sCookieValue);}
function UncheckAll()
{var ctr;for(ctr=0;ctr<document.getElementsByName('srch[]').length;ctr++)
{document.getElementsByName('srch[]')[ctr].checked=false;}
return;}
function UncheckByValue(value)
{var ctr;for(ctr=0;ctr<document.getElementsByName('srch[]').length;ctr++)
{if(document.getElementsByName('srch[]')[ctr].value==value)
{document.getElementsByName('srch[]')[ctr].checked=false;}}
return;}
function ClearAllProducts()
{document.getElementById("LastCatID").value="";document.getElementById("compare1").value="";document.getElementById("compare2").value="";document.getElementById("compare3").value="";UncheckAll();writeCookie();return;}
function GetCookie(sName)
{var aCookie=document.cookie.split("; ");for(var i=0;i<aCookie.length;i++)
{var aCrumb=aCookie[i].split("=");if(sName==aCrumb[0])
return unescape(aCrumb[1]);}
return null;}
function setCookie(name,value,expires,path,domain,secure){var curCookie=name+"="+value+";expires=Thu, 2 Aug 2030 20:47:11 UTC; path=/";document.cookie=curCookie;}
function expireCookie(name,value,expires,path,domain,secure){var curCookie=name+"=expires=Thu, 2 Aug 2001 20:47:11 UTC;path=/";document.cookie=curCookie;}
