function eraseCookie(name) {
	SetCookie(name,"",-1);
}

function SetCookie(sName, sValue, when)

{
  	if (when == 0)
  	{
  		document.cookie = sName + "=" + escape(sValue);
  	} 
  	else
  	{
  		date = new Date()  ;

 		var m = date.getMinutes ();

  		m += when;

  		date.setMinutes (m);
  
  		document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
  	}

}

function GetCookie(sName)

{

  // cookies are separated by semicolons

  var aCookie = document.cookie.split("; ");

  for (var i=0; i < aCookie.length; i++)

  {

    // a name/value pair (a crumb) is separated by an equal sign

    var aCrumb = aCookie[i].split("=");

    if (sName == aCrumb[0]) 

      return unescape(aCrumb[1]);

  }

  // a cookie with the requested name does not exist

  return null;

}

function incrementOne(tbox)

{
    box = document.getElementById (tbox);  

    val = box.value  ;
    val++ ;
    box.value = val;

	updateCookie (tbox, val, 0);


}

function decrementOne(tbox)

{

    box = document.getElementById (tbox);    

    if ((box.value - 1) <= 0 )

    {

        box.value = 0;

    }

    else

    {

        box.value = box.value - 1;

    }

    updateCookie (tbox, box.value, 0);

}

function updateCookie (id, value, increment)
{	
	var cookieString = new String(GetCookie ("productdata"));
    var newcookieString = new String("");
    //var remove = 0;
	var match = 0;    
	
     if (cookieString == "null" || cookieString == "undefined")
    {
	    newcookieString = id + "~" + value ;
    }
    else
    {   
		arProds = cookieString.split(",");
    	for (i=0;i < arProds.length; i++)
    	{
        	arProdQ = arProds[i].split("~");
        	pid = new String(arProdQ[0]);
        	pquan = new String(arProdQ[1]);

        	if (id == pid) // match
        	{
            	match = 1;
				intValue = parseInt(pquan);
				if (increment && intValue >= 0)
				{
					intValue++;
					pquan = intValue;
				}
				else
				{
					pquan = value;
				}
        	}        

 //      		if (value >= 0)
 //       	{ // append to new cookie       
            	newcookieString += pid + "~" + pquan + ",";
 //       	}
	    }

    	if (match !== 1)
    	{
     		newcookieString += id + "~" + value ;
     	}
     	else
     	{
     		newcookieString = newcookieString.substring(0,newcookieString.length - 1);
     	}
    }
    
    SetCookie("productdata",newcookieString, 0);
}