var cookiename = "ENYcart";
function deletecookie() {
document.cookie = cookiename + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
function addtocart(indata) {
   if (navigator.cookieEnabled) {
/* SEE IF COOKIE EXISTS AND GET VALUE*/
      var cookieval = "";   
      if(document.cookie.length > 0) {
         startC = document.cookie.indexOf(cookiename+"=");
	     if (startC != -1) {
	        startC += cookiename.length + 1;
		    endC = document.cookie.indexOf(";",startC);
            if (endC == -1) {
		       endC = document.cookie.length;
		    }
		    cookieval = document.cookie.substring(startC, endC);
/*DELETE THE COOKIE IF IT EXISTS*/
 		    document.cookie = cookiename + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	     }
	  }
	  var newcookieval = "";
	  if (cookieval.length > 0) {
		 var tmp = new Array();
		 var tindex = cookieval.indexOf("~~~");
		 var morethanone = 0;
		 if (tindex > 0) {
		    morethanone = 1;
		 }
		 if (morethanone == 1) {
		   tmp = cookieval.split('~~~');
		 } else {
		   tmp[0] = cookieval; 
		 }
       /*SEE IF ITEM EXISTS AND ADD 1 TO QUANTITY OR ADD TO END AND MAKE newcookieval*/
		 var itemexists = 0;
		 for(var i = 0; i < tmp.length; i++) {
		   var tmp1 = new Array();
			tmp1 = tmp[i].split(',');
			if (tmp1[0] == indata) {
			   itemexists = 1;
			   tmpnum = parseInt(tmp1[1]) + 1;
			   tmp1[1] = tmpnum;
			}
			if (i == 0) {
			   newcookieval += tmp1[0] + "," + tmp1[1];
			} else {
			   newcookieval += "~~~" + tmp1[0] + "," + tmp1[1];
			} 
			if (i == 100) return;

		 }
		 if (itemexists == 0) {
         newcookieval += "~~~" + indata + ",1";
		 }
	  } else {
	     newcookieval += indata + ",1";
      }	  
      /*REWRITE THE COOKIE AND RESET THE EXPIRATION*/   
      var myDate=new Date();
      myDate.setDate(myDate.getDate()+5);
      document.cookie = "ENYcart=" + newcookieval + "; expires=" + myDate.toGMTString() +  "; path=/";
	  setcart();
   } else {
      alert('Please enable cookies to shop');
   }
}

function setcart() {
   var showcart = 0;
   if(document.cookie.length > 0) {
      startC = document.cookie.indexOf(cookiename+"=");
	  if (startC != -1) {
	     startC += cookiename.length + 1;
		 endC = document.cookie.indexOf(";",startC);
		 if (endC == -1) {
		    endC = document.cookie.length;
		 }
		 var cookieval = document.cookie.substring(startC, endC);
		 if (cookieval.length > 0) {
		    showcart = 1;
          var tmp = new Array();
		    var tindex = cookieval.indexOf("~~~");
			 var morethanone = 0;
		    if (tindex > 0) {
		       morethanone = 1;
		    }
		    var morethanone = 0;
		    if (tindex > 0) {
		       morethanone = 1;
		    }
		    if (morethanone == 1) {
		      tmp = cookieval.split('~~~');
		    } else {
		      tmp[0] = cookieval; 
		    }
			 var itemcount = 0;
			 for(var i = 0; i < tmp.length; i++) {
				 var tmp1 = new Array();
				 tmp1 = tmp[i].split(',');
				 itemcount = parseInt(itemcount) + parseInt(tmp1[1]);
			 }
		 }
	  }
   }
   if (showcart == 1) {
     document.getElementById('topcart').innerHTML ="<img src='images/carto.gif' width='76' height='56' alt='Cart' border='0'>"
     document.getElementById('itemcount').innerHTML = itemcount;
   } else {
     document.getElementById('topcart').innerHTML ="<img src='images/cart.gif' width='76' height='56' alt='Cart' border='0'>"   
   }
}
function removeitem(indata) {
   var startC = document.cookie.indexOf(cookiename+"=");
   startC += cookiename.length + 1;
   var endC = document.cookie.indexOf(";",startC);
   if (endC == -1) {
      endC = document.cookie.length;
   }
   var cookieval = document.cookie.substring(startC, endC);
/*DELETE THE COOKIE IF IT EXISTS*/
   document.cookie = cookiename + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   var newcookieval = "";
   if (cookieval.length > 0) {
     var tmp = new Array();
     var tindex = cookieval.indexOf("~~~");
     var morethanone = 0;
     if (tindex > 0) {
	morethanone = 1;
     }
     if (morethanone == 1) {
	tmp = cookieval.split('~~~');
     } else {
        tmp[0] = cookieval; 
     }
   }
   for(var i = 0; i < tmp.length; i++) {
     var tmp1 = new Array();
     tmp1 = tmp[i].split(',');
     var itemremoved = 0;
     if (tmp1[0] == indata && itemremoved == 0) {
	if (tmp1[1] > 1) {
	   tmpnum = parseInt(tmp1[1]) - 1;
	   tmp1[1] = tmpnum;
           if (i == 0) {
              newcookieval += tmp1[0] + "," + tmp1[1];
	   } else {
	      newcookieval += "~~~" + tmp1[0] + "," + tmp1[1];
	   } 
	}
        itemremoved = 1
     } else {
        if (i == 0) {
           newcookieval += tmp1[0] + "," + tmp1[1];
	} else {
	   newcookieval += "~~~" + tmp1[0] + "," + tmp1[1];
	} 
     }
   }
   var tchk = newcookieval.substr(0,3);
   if (tchk == "~~~") {
      var clen = newcookieval.length - 3;
      newcookieval = newcookieval.substr(3,clen);		   
   }
   /*REWRITE THE COOKIE AND RESET THE EXPIRATION*/   
   var myDate=new Date();
   myDate.setDate(myDate.getDate()+5);
   document.cookie = "ENYcart=" + newcookieval + "; expires=" + myDate.toGMTString() +  "; path=/"; 
   /*RELOAD THE PAGE*/
   window.location.reload();
}
