//should be removed, check the "id-" for 5 prod cookies
function storeID(theElement){
  var cookiename = "sbsids=";
  var oldcookie = readCookie(cookiename);
  var theProdID = "," + theElement.value + "#";
  oldcookie = removeIDstr(oldcookie, theProdID);
		//form["sidebysidecompare"]

  var pos = theElement.value.indexOf("c");
  if(pos == -1) {
	  pos = theElement.value.length-2;
  }
  var regstr = theElement.value.substr(pos)+'#';
  var regstrlen = regstr.length;
  if (theElement.checked){
  	  var countstr = oldcookie;
	  while (countstr.indexOf(regstr)!=-1) {
	    countstr = countstr.replace(regstr,'');
	  }	  
	  var cookienum = (oldcookie.length - countstr.length) / regstrlen;  
//alert(regstr);
//alert(theProdID);
//alert(cookienum);
//alert(oldcookie);
//alert(countstr);

	  if(cookienum >=5){  		  		
	  		//alert("Please select no more than 5 products to compare features!");  		
			alert("您已经选择了5个商品，最多只能比较5个商品。谢谢!");  		
	  		theElement.checked = false;
	 		return false;	
	  }
	  
  	  oldcookie = oldcookie + theProdID;  	 
  }

  document.cookie = cookiename + oldcookie + getCookieKey();

  //alert(document.cookie);

}

function removeIDstr(myString, pattern) {

   var newString = myString.replace(pattern,"");

   return(newString);
}



function removeID(theID) {

  var cookiename = "sbsids=";
  var oldcookie = readCookie(cookiename);
  var theProdID = "," + theID + "#";

  oldcookie = removeIDstr(oldcookie, theProdID);
  document.cookie = cookiename + oldcookie + getCookieKey();
}

function removeAllID() {

  var cookiename = "sbsids=";

  document.cookie = cookiename + getCookieKey();
}



function readCookie(theCookie) {

  var allcookies = document.cookie;
  var value = "";
  var pos = allcookies.indexOf(theCookie);
  if (pos != -1) {
    var start = pos + theCookie.length;
	var end = allcookies.indexOf(";", start);

	if (end == -1) end = allcookies.length;
	value = allcookies.substring(start, end);
	value = unescape(value);
  }

  return (value);
}

function get_prodid_type(s_id) {
	var id_len = s_id.length;
	return eval(s_id.substring(id_len - 2,id_len));
}

function set_action_url(id,form_obj) {
	form_obj.action = "/goto_sidebyside.php";
}

function check_form_sebs(form_obj) {
	var elArr = form_obj.elements;
	var patrn_name = /^sidebyside.+$/;
	var old_type = 0;
	var sbs_count = 0;
	for(var i = 0; i < elArr.length; i++) {
		if(patrn_name.exec(elArr[i].name) && elArr[i].type == "checkbox" && elArr[i].checked) {
			id_type = get_prodid_type(elArr[i].value);
			if(id_type > 0) {
				if(old_type == 0) old_type = id_type;
				else if(old_type != id_type) {
					alert("Please compare products in one channel, thank you!");
					return false;
				}
				sbs_count ++;
			}
		}
	}
	if(sbs_count > 0) {
		set_action_url(old_type,form_obj);
		return true;
	}
	else return false;
}

function gotoSidebyside(form) {
	var url = "";
	var prodn = 0;
	for(loop=0; loop<form.length; loop++) {
		name = form.elements[loop].name;
		if(name == "chid"
			|| name == "catid"
			|| name == "retpage") {
			value = form.elements[loop].value;
		} else if(name == "prodid[]") {
			if(form.elements[loop].checked == false) {
				continue;
			}
			prodn++;
			name = "prodid" + prodn;
			value = form.elements[loop].value;
		} else {
			continue;
		}
		if(url != "") {
			url += "&";
		}
		url += name + "=" + value;
	}
	url += "&prodn=" + prodn;
	var pos = form.action.indexOf("?");
	if(pos == -1) {
		url = form.action + "?" + url;
	} else {
		url = form.action + "&" + url;
	}
	//alert(url);
	document.location = url;
	return false;
}

function getCookieKey() {
	domain = location.host;

	if(domain.indexOf("www") == 0) {
		domain = domain.substring(3);
	}

	return "; path=/; domain=" + domain;
}

