// ********************************************** CHECK AND BUILD COOKIE
function testAndBuild(theString) {
	var exp = new Date();
	var oneYear = exp.getTime() + (1 * 24 *60 * 60 * 1000);
	exp.setTime(oneYear);
	
	theString = theString.replace("||", "|");
	aCheck = theString.charAt(0);
	if (aCheck == "|"){
		theString = theString.substr(1);
	}
	SetCookie(theCookieName, theString , exp, "/");
}

// ********************************************************* CALL ADD TO BASKET and GO TO CHECKOUT

function delPic(theVal) {
	theAr = GetCookie(theCookieName);
	picArray = theAr.split("|");
	for (i =0; i<picArray.length;i++){
		if (picArray[i] == theVal){
			delete(picArray[i]);
			//alert("item: " + i + " which equals " + theVal + " is being deleted");
		}
	}
	newCookie = picArray.join("|");
	testAndBuild(newCookie);
	document.orderForm.method = "GET";
	document.orderForm.action = "";
}

// ********************************************** GET COOKIE VALUE
function GetCookie(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 getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

// ********************************************************* GO TO BASKET
function goToCheckout(){
	theTotalItems = GetCookie(theCookieName);
	if (theTotalItems ==null) {
		alert("Cannot checkout. Basket is empty.");
	} else {
		document.orderForm.action = theCheckoutPage;
	}
}

// ********************************************************* BUTTON RETURN
function showValue(){
	theCurrent = GetCookie(theCookieName);
	alert("cookie: " + theCurrent);
}

// ********************************************************* CLEAR BASKET
function clearBasket(){
	if (confirm("Are you sure you want to empty the basket?")) {
		var exp = new Date();
		var oneYear = exp.getTime() - (365 * 24 *60 * 60 * 1000);
		exp.setTime(oneYear);
		SetCookie(theCookieName, "" , exp, "/");
		window.location.href = '/boutique/index.php';
	} else {
		return null;
	}	
}

// ********************************************************* EMPTY BASKET (NO CHOICE!)
function completeEmptyBasket(){
		var exp = new Date();
		var oneYear = exp.getTime() - (365 * 24 *60 * 60 * 1000);
		exp.setTime(oneYear);
		SetCookie(theCookieName, "" , exp, "/");
}

// ********************************************************* ADD TO BASKET

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function cycleForm(bNumber){ // Cycle through all items in the form. Submit to cookie subroutine
	anItem = 0; // Begin with nothing being added. Something needs to be over 0 to change this.
	theList = document.orderForm.theList.value;
	listArray = theList.split("|");
	for (j=0; j<listArray.length-1; j++){
		addToOrder(listArray[j]);
	}
	if (anItem >0){
		document.orderForm.action = theCheckoutPage;
	} else {
		alert("No items were added to basket.\nPlease ensure quantity is \"1\" or more on at least on item.");
	}
}

function updateBasket(bNumber){ // Update the items (cycle through all of them on the checkOut page)
	anItem = 0; // Begin with nothing being added. Something needs to be over 0 to change this.
	theList = document.orderForm.theList.value;
	listArray = theList.split("|");
	for (j=0; j<listArray.length-1; j++){
		updateOrder(listArray[j]);
	}
	if (anItem >0){
		document.orderForm.action = theCheckoutPage;
	} else {
		alert("No items were adjusted.");
		document.orderForm.action = theCheckoutPage;
	}
}

// ********************************************************* CALL ADD TO BASKET
function addToOrder(bNumber) {
	numSelected = eval("document.orderForm." + bNumber + ".value"); // field # must be legit
	numSelected = parseInt(numSelected); // turn to an integer.
	if (numSelected >0 && numSelected <9999){ // if it is good, we go! (FIRST LOOP)
		anItem = 1; // Make a mark to let the loop know something was actually over 0!
		theOrder = bNumber + "*" + numSelected; // this is what we might add (e.g  b_1*3)
		theCurrent = GetCookie(theCookieName); // let us see if this cookie even exists.
		if (theCurrent != null ){// Already are bands, so ADD (SECOND LOOP)
			theCurrentArray = theCurrent.split("|"); // Split up the cookie
			task = 0;
			for (i=0; i<theCurrentArray.length; i++){ // Cycle through that cookie pieces
				theItem = theCurrentArray[i]; 
				theUnits = theItem.split("*"); // Even further, we split the piece up
				theBand = theUnits[0]; // Here is the BAND number already stored
				theNumber = theUnits[1]; // Here is the NUMBER of those bands.
				// With that in mind, we check if a LIKE band has been added.
				// If so, we ADD the new number to the old number.
				if (bNumber == theBand){ // A match? Let us add the numbers.
					theNumber = parseFloat(theNumber) + parseFloat(numSelected);
					theCurrentArray[i] = bNumber + "*" + theNumber;
					task = 1;
				}
			}
			if (task == 1) {
				theCurrent = theCurrentArray.join("|");
				theCookie = theCurrent;
			} else {
				theCookie = theCurrent + "|" + theOrder;
			}
			testAndBuild(theCookie);
		} else { // (fork SECOND) SET a NEW cookie!
			testAndBuild(theOrder);
		} // (END SECOND LOOP) whether bands were already set.
	} // (END FIRST LOOP) had to have been legit; otherwise nothing is done.
	document.orderForm.action = theCheckoutPage;
}

// ********************************************************* CALL ADD TO BASKET
function updateOrder(bNumber) {
	numSelected = eval("document.orderForm." + bNumber + ".value"); // field # must be legit
	numSelected = parseInt(numSelected); // turn to an integer.
	if (numSelected >0 && numSelected <9999){ // if it is good, we go! (FIRST LOOP)
		anItem = 1;
		var exp = new Date(); // todays date
		var oneYear = exp.getTime() + (1 * 24 *60 * 60 * 1000); // cookie expires in...
		exp.setTime(oneYear); // a time has been set!
		theOrder = bNumber + "*" + numSelected; // this is what we might add (e.g  b_1*3)
		theCurrent = GetCookie(theCookieName); // let us see if this cookie even exists.
		if (theCurrent != null ){// Already are bands, so ADD (SECOND LOOP)
			theCurrentArray = theCurrent.split("|"); // Split up the cookie
			task = 0;
			for (i=0; i<theCurrentArray.length; i++){ // Cycle through that cookie pieces
				theItem = theCurrentArray[i]; 
				theUnits = theItem.split("*"); // Even further, we split the piece up
				theBand = theUnits[0]; // Here is the BAND number already stored
				theNumber = theUnits[1]; // Here is the NUMBER of those bands.
				// With that in mind, we check if a LIKE band has been added.
				// If so, we ADD the new number to the old number.
				if (bNumber == theBand){ // A match? Let us add the numbers.
					theNumber = parseFloat(numSelected);
					theCurrentArray[i] = bNumber + "*" + theNumber;
					task = 1;
				}
			}
			if (task == 1) {
				theCurrent = theCurrentArray.join("|");
				theCookie = theCurrent;
			} else {
				theCookie = theCurrent + "|" + theOrder;
			}
			SetCookie(theCookieName, theCookie , exp, "/");
		} else { // (fork SECOND) SET a NEW cookie!
			SetCookie(theCookieName, theOrder , exp, "/");
		} // (END SECOND LOOP) whether bands were already set.
	}// (END FIRST LOOP) had to have been legit; otherwise nothing is done.
}

// ********************************************************* END ORDER
function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}