//      Code to use 'products' cookie as store for form checkbox settings.
//              cbtest.js by Steven@phelum.com 2006-03-25

function        getCookieVal    (offset)                // get ent for cookie
 {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape (document.cookie.substring (offset, endstr));
 }


function        GetCookie       (name)                  // find cookie by 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 getCookieVal (j);
    i = document.cookie.indexOf (" ", i) + 1;
    if (i == 0) break;
   }
  return null;
 }


function        SetCookie       (cookieName, cookieValue, nDays)
 {
  var today  = new Date ();
  var expire = new Date ();
  if (nDays==null || nDays==0) nDays=1;
  expire.setTime (today.getTime () + 3600000*1*nDays);
  document.cookie = cookieName + "=" + escape (cookieValue)
                    + ";expires=" + expire.toGMTString ();
  return;
 }


function        DeleteCookie    (name)                  // mark as dead
 {
  var exp = new Date ();
  exp.setTime (exp.getTime () - 1);
  var cval = GetCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString ();
  return;
 }

function        BoxChange       (obj)                   // checkbox change
 {
var CookieVal = GetCookie ('products');
  if (CookieVal == null)
        CookieVal = '';
 	keyword=CookieVal.split (';');
    len=keyword.length-1;   
	if(obj.checked)
       {
			if(len==4){
				obj.checked=false;
				alert("Max 4");
				return;
			}
	   }
	 BoxChange2       (obj);
}

function        BoxCheck       ()                   // checkbox change
 {
var CookieVal = GetCookie ('products');
  if (CookieVal == null)
        CookieVal = '';
 	keyword=CookieVal.split (';');
    len=keyword.length-1;   
		if(len<2){
				
				alert("Minimal 2 Item yang dipilih");
				return;
			}
	 
	window.location="compare.php";
}

function        BoxChange2       (obj)                   // checkbox change
 {

  var cookieVal;
  var optionEnt;
  var x;
  var y;
  var z;

  if (obj.value != null)
   {
    optionEnt = obj.value + ';';                        // build entry
    cookieVal = GetCookie ('products');
    if (cookieVal == null)
      cookieVal = '';
    while (0 <= (x = cookieVal.indexOf (optionEnt)))    // delete entry
     {                                                  // from cookie
      y = x + optionEnt.length;
      z = cookieVal.length;
      cookieVal = cookieVal.substring (0, x) +
                  cookieVal.substring (y, z);
     }
    if (obj.checked)                                    // if selected,
      cookieVal += optionEnt;                           // append ent to cookie
    SetCookie ('products', cookieVal);
    if (cookieVal.length == 0)                          // if no ents,
      DeleteCookie ('products');                        // delete cookie
   }
  return;
 }


function        LoadBoxes       (FormName)              // load checkboxes
 {
  var Group     = document.getElementById (FormName);
//var Group     = document.getElementsByTagName (FormName); bad in IE 5.5
  var cookieVal = GetCookie ('products');
  var optionEnt;

  if (cookieVal == null)
    cookieVal = '';
  for (var x = 0; x < Group.length; x++)                // for each checkbox,
   {                                                    // checked = entry
    if (Group [x].type == 'checkbox')                   // in cookie
     {
      optionEnt = Group [x].value + ';';
      Group [x].checked = (cookieVal.indexOf (optionEnt) >= 0);
     }
   }
  return;
 }


function        ResetReact      (Group)                 // when form reset
 {
  for (var x = 0; x < Group.length; x++)
   {
    if (Group [x].type == 'checkbox')                   // for each checkbox,
     {                                                  // reset box,
      Group [x].checked = 0;                            // update cookie
      BoxChange (Group [x]);
     }
   }
  return;
 }



