// Cookie functions

//Define global variable(s)

	var valueString = "";
	var itemNbr = new Array();
	var itemQty = new Array();
	var itemSize = new Array();

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" : "");
		//document.write ("document.cookie = " + document.cookie);
}

function getCookieVal(offset){
	var endstr=document.cookie.indexOf(";",offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function getCookie(name){
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var h = 0;
	while (h < clen){
		var g = h + alen;
		if (document.cookie.substring(h,g) == arg)
			return getCookieVal(g);
			h = document.cookie.indexOf(" ",h) + 1;
			if(h == 0) break;
	}
	return null;
}

// This function builds and fills an array with all of the items
// in the shopping basket.
function loadBasket(name){
	valueString = getCookie(name);
	if (valueString != ""){
 		var slen = valueString.length;
		var bDelim = 0;
		var eDelim;
		var i = 0;
		while (bDelim < slen) {
			eDelim = valueString.indexOf("~", bDelim);
			itemNbr[i] = valueString.substring(bDelim,eDelim);
			//grab the qty too.
			bDelim = eDelim + 1;
			eDelim = valueString.indexOf("~", bDelim);
			itemQty[i] = valueString.substring(bDelim, eDelim);
			//now grab the size.
			bDelim = eDelim + 1;
			eDelim = valueString.indexOf("~", bDelim);
			itemSize[i] = valueString.substring(bDelim, eDelim);
			bDelim = eDelim + 1;
			i++;
		}
	}
}

// This function reads back the item array, builds a value string and writes the cookie
function writeCookie(name){
	//build the value string.
	var j = 0;
	valueString = "";
	while (j < itemNbr.length){
		if (itemQty[j] > "0") {
			valueString += itemNbr[j] + "~" + itemQty[j] + "~" + itemSize[j] + "~";
		}
		j++;
	}
	var expdate = new Date ( );
	expdate.setTime (expdate.getTime( ) + ( 24 * 60 * 60 * 1000)); // 24 hours from now
	setCookie(name,valueString, expdate);
}

//This function will either increase item quantity by 1 (if item is already in basket)
// or add the item to the basket with a quantity of 1
function add_to_basket(name,item,qty,size){
	var itemString;
	itemString = getCookie(name);
	qty = eval(document.forms[0].itemQty.value);
	size = eval(document.forms[0].itemSize[document.forms[0].itemSize.selectedIndex].value); 
	
	if (itemString == null) {
		itemString = item + "~" + qty + "~" + size + "~";
		setCookie(name,itemString);
	} else {
		// load the array with the shopping basket
		loadBasket(name);
		// search the array for the item being added
		var itemQtyN;
		var itemSizeN;
		var itemFound = "False";
		var k = 0;
		while (k < itemNbr.length){
			if ((itemNbr[k] == item) && (itemSize[k] == size)){
				itemQtyN = Number(itemQty[k]) + Number(qty);
				itemQty[k] = String(itemQtyN);
				var itemFound = "True";
			}
			k++;
		}
		if (itemFound == "False"){
			itemNbr[itemNbr.length] = item;
			itemQty[itemQty.length] = qty;
			itemSize[itemSize.length] = size;
		}
		writeCookie(name);
	}
	history.go(-1);
}
                                                                                                                                                                                                                                                                                                                                                              	
function display_items(name){
	load_basket(name);
	var l = 0;
	while (l < itemNbr.length){
		document.shopping_basket.qty.value = itemQty[l];
		document.shopping_basket.item
	}
}

function deleteCookie(name,path,domain){
	if (getCookie(name)) {
		document.cookie = name + "=" + 
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function cancelOrder(name){
	deleteCookie(name);
	window.location.href = "home.cfm"
}
 function UpdateQty(name,item,qty,size){
	var itemString;
	itemString = getCookie(name);
	if (itemString == null) {
		itemString = item + "~" + qty + "~" + size + "~";
		setCookie(name,itemString);
	} else {
		// load the array with the shopping basket
		loadBasket(name);
		// search the array for the item being added
		var itemQtyN;
		var itemFound = "False";
		var k = 0;
		while (k < itemNbr.length){
			if (itemNbr[k] == item) {
				itemQty[k] = qty
				itemSize[k] = size;
				var itemFound = "True";
			}
			k++;
		}
		writeCookie(name);		
	}
	location.reload();
}                                                                                                                                                                                                                                                                                                                                                             	

function updateItem(qtyname){
	// the user has changed the qty of an item.  Derive the item number and qty value from this information,
	// use these values as parameter to update the cookie and then reload the form.
	// build the value reference string for the qty field
	var valueRef = "document.forms[0]." + qtyname + ".value";
	var qtyValue = eval(valueRef);
	
	//build the name reference of hidden field so that  it can be referenced.
	var qtyNameLen = qtyname.length;
	var itemIx = qtyname.charAt(qtyNameLen - 1);
	var itemName = "Item" + itemIx;
	var sizeName = "Size" + itemIx;
	
	//build the value reference string for the item field.
	var valueRef = "document.forms[0]." + itemName + ".value";
	var itemValue = eval(valueRef);
		
	//build the value reference string for the size field.
	var valueRef = "document.forms[0]." + sizeName + ".value";
	var sizeValue = eval(valueRef);
	
	var cookieName = "radarcart";
	
	//update the cookie
	UpdateQty(cookieName,itemValue,qtyValue,sizeValue);
}
	
	





