//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 regstr = theElement.value.substr((theElement.value.length-2),2)+'#';
	if (theElement.checked)
	{
		var countstr = oldcookie;
		while (countstr.indexOf(regstr)!=-1) 
		{
		countstr = countstr.replace(regstr, '');
		}	  
		var cookienum = (oldcookie.length - countstr.length)/3;  
		if(cookienum >= 5)
		{
			alert("5つまで商品を選んで比較をして下さい。");
			theElement.checked = false;
			return false;
		}
		oldcookie = oldcookie + theProdID;  	 
	}
	document.cookie = cookiename + oldcookie + "; path=/; domain=.smarter.co.jp";
}

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 + "; path=/; domain=.smarter.co.jp";
}

function removeAllID()
{
  var cookiename = "sbsids=";
  document.cookie = cookiename + "; path=/; domain=.smarter.co.jp";
}

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;
}
