var cookieName = "productlist";
var prefix = "";
var compareicon = "imgCompare";

 
 function compare()
 {
 	value = getCookie(cookieName);
 	var url = location.href.toString();
 	var index = url.indexOf("/");
 	for(var j = 0; j<2 ; j++) 
	{
		index = url.indexOf("/", index + 1);
	}
	url = url.substring(0,index + 1);
	url = url + "Products/ProductCompare.aspx?pid=";
	if(value != null)
 	{
	var offerIds = stringToArray(value);
	var len = offerIds.length;
	var result = "";
	var counter = 0;
	for(var i=0;i<len;i++) {
			if(counter == 0) {
			  result = offerIds[i];
			} else {
			  result = result + "x" + offerIds[i];
			}
			counter++;
	}
    url = url + result;
    }
    location.href = url;
    return false;
 }
 
function SetCookie(name, value)
{
	document.cookie = name + "=" + value  + "; path=/";
}

 function addOfferId(arg) {
	arg  = arg + prefix;
	if(hasOfferId(arg)) {
		return;
	}
	if(arg == "") {
		return;
	}
	value = getCookie(cookieName);
	if(value == null || value == "null" ) {
		value = arg ;
	} else {
	   value=value+"x"+ arg;
	}
	SetCookie(cookieName, value);
	ShowCompareIcon();
  }

  function delOfferId(arg) {
	arg = arg + prefix;

	if(!hasOfferId(arg)) {
	   return;
	}

	value = getCookie(cookieName);
	var offerIds = stringToArray(value);
	var len = offerIds.length;
	var result = "";
	var counter = 0;
	for(var i=0;i<len;i++) {
	  if(offerIds[i] != arg) {
			if(counter == 0) {
			  result = offerIds[i];
			} else {
			  result = result + "x" + offerIds[i];
			}
			counter++;
	  }
	}
	SetCookie(cookieName, value);

	ShowCompareIcon();
  }
  
  function ShowCompareIcon()
  {
   var i = getOfferIdCount();
   var icon = document.getElementById(compareicon);
   if(i > 0)
   {
	icon.style.display = "";
	icon.alt = "View Compare List "+ i +" items";
   }
   else
   {
	icon.style.display = "none"; 
   }
 }
  
  
 window.onload =  function Init()
  {
	ShowCompareIcon();
  	var value = getCookie(cookieName);
  	if(value != null)
  	{
	var offerIds = stringToArray(value);
	var len = offerIds.length;
	var result = "";
	var counter = 0;
	for(var i=0;i<len;i++)
	{	
		var check = document.getElementById("check"+offerIds[i]);
		if(check != null)
		{
		check.checked = true;
		}
	}
	}
 }
  
  
  function getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i=0;
	while(i<clen) {
		var j=i+alen;
		if(document.cookie.substring(i,j)==arg)
			return www_helpor_net(j);
		i=document.cookie.indexOf(" ",i)+1;if(i==0)
			break;
	}
	return null;
  }

  function www_helpor_net(offset) {
	  var endstr=document.cookie.indexOf(";",offset);
	  if(endstr==-1)
	  endstr=document.cookie.length;
	  return unescape(document.cookie.substring(offset,endstr));
  }

  function hasOfferId(arg) {
	var value = getCookie(cookieName);
	if(value == null || value == "null" || value.indexOf(arg) == -1 ) {
		return false;
	} else {
		return true;
	}
  }

  function getOfferIdCount() {

	value = getCookie(cookieName);
	
	if(value == null || value == 'null') {
	  return 0;
	}
	
	var count = 1;
	while(value.indexOf("x") != -1) {
	   var position = value.indexOf("x");
	   var len = value.length;
	   value =  value.substring(position+1,len);
	   count++;
	}
	return count;
  }
  
  function stringToArray(str) {
	   var temp = str;
	   var strArray = new Array();
	   var count = 0;
	   while(temp.indexOf("x") != -1) {

		 var position = temp.indexOf("x");
		 
		 strArray[count] = temp.substring(0,position);
		 count++;
		 var len = temp.length;
		 temp =  temp.substring(position+1,len);

	   }
	   strArray[count] = temp;
	   return strArray;
  }
  
  function clickcompareBox(id,box) {
	if(box.checked){
	  if(getOfferIdCount()>20){
		alert('Sorry only 20 products per list');
		box.checked=false;
	  }else{
		addOfferId(id);
		
	  }
	} else {
	  delOfferId(id);
	}
  }
 //清除本页面的 
function clearAll() {
	var y =document.getElementsByTagName("input");
	for(var i =0;i<y.length;i++)
	{
		if(y[i].type =="checkbox" && y[i].id.substring(0,5) == "check")
		{
			y[i].checked = false;
			delOfferId(y[i].id.substring(5));
		}
	}
return false;
}

function selectAll()
{
	var y =document.getElementsByTagName("input");
	for(var i =0;i<y.length;i++)
	{
		if(y[i].type =="checkbox" && y[i].id.substring(0,5) == "check")
		{
			if(y[i].checked == false)
			{
				y[i].checked = true;
				addOfferId(y[i].id.substring(5));
			}
		}
	}
	return false;
}

  function delDisplay(id)
  {
   delOfferId(id);
   var display = document.getElementById("Display"+id);
   display.style.display = "none";
   display.innerHTML = "";
   return false;
  }
  //上移
  function upDisplay(arg)
  {
	arg = arg + prefix;
	if(!hasOfferId(arg)) {
		alert("");
		return;
	}

	value = getCookie(cookieName);
	var offerIds = stringToArray(value);
	var len = offerIds.length;
	var result = "";
	var counter = 0;
	for(var i=0;i<len;i++)
	{
		if(offerIds[i] == arg && i>0 )
		{
		 var html1 = document.getElementById("Display"+offerIds[i]).innerHTML;
		 var html2 = document.getElementById("Display"+offerIds[i-1]).innerHTML;
		 var e1 = document.getElementById("Display"+offerIds[i]);
		 var e2 = document.getElementById("Display"+offerIds[i-1]);
		
		 e2.innerHTML = html1;
		 e1.innerHTML = html2;	 
		 e2.id = "Display"+offerIds[i];
		 e1.id = "Display"+offerIds[i-1];
		 
		 var temp = offerIds[i-1];
		 offerIds[i-1] = offerIds[i];
		 offerIds[i] = temp;
		 break;
		}
	}
	for(var i=0;i<len;i++) {
	if(counter == 0) 
	{
	result = offerIds[i];
	} else 
	{
	result = result + "x" + offerIds[i];
	}
	counter++;
	}
	SetCookie(cookieName, result);
	return false;
  }
  //下移
  function downDisplay(arg)
  {
	arg = arg + prefix;
	if(!hasOfferId(arg)) {
		alert("");
		return;
	}

	value = getCookie(cookieName);
	var offerIds = stringToArray(value);
	var len = offerIds.length;
	var result = "";
	var counter = 0;
	for(var i=0;i<len;i++)
	{
		if(offerIds[i] == arg && i< len-1 )
		{
		 var html1 = document.getElementById("Display"+offerIds[i]).innerHTML;
		 var html2 = document.getElementById("Display"+offerIds[i+1]).innerHTML;
		 var e1 = document.getElementById("Display"+offerIds[i]);
		 var e2 = document.getElementById("Display"+offerIds[i+1]);
		
		 e2.innerHTML = html1;
		 e1.innerHTML = html2;	 
		 e2.id = "Display"+offerIds[i];
		 e1.id = "Display"+offerIds[i+1];
		 
		 var temp = offerIds[i+1];
		 offerIds[i+1] = offerIds[i];
		 offerIds[i] = temp;
		 break;
		}
	}
	for(var i=0;i<len;i++) {
	if(counter == 0) 
	{
	result = offerIds[i];
	} else 
	{
	result = result + "x" + offerIds[i];
	}
	counter++;
	}
	SetCookie(cookieName, result);
	return false;
  }
 //清除所有页面的
function clearAll1() {
	var value = "null";
	SetCookie(cookieName, value);
	location.href = "ProductCompare.aspx";
}

