
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 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;
}

var numItems = GetCookie('numItems');
	if (numItems == null) {
		expireDate = new Date;
		expireDate.setHours(expireDate.getHours()+5);
		document.cookie = "numItems=0;expires="+ expireDate.toGMTString()+";path=/";
	}



function back() {
		parent.Body.history.back();
		setTimeout('parent.Body.location=parent.Body.location.href;',1000);
	}


function add(item, quantity, price, shipping) {
	// set cookie expiration date
	expireDate = new Date;
	expireDate.setHours(expireDate.getHours()+5);
	numItems++;

//alert(item+"\n"+quantity+"\n"+price+"\n"+shipping);

	// set strings to hold currently selected items
	if (GetCookie('items') != null) {
		currentItems = new String(GetCookie('items'));
	} else {
		currentItems = new String(' ');
	}
	if (GetCookie('quantities') != null) {
		currentQtys = new String(GetCookie('quantities'));
	} else {
		currentQtys = new String(' ');
	}
		if (GetCookie('prices') != null) {
		currentPrices = new String(GetCookie('prices'));
	} else {
		currentPrices = new String(' ');
	}
		if (GetCookie('shipping') != null) {
		currentShipping = new String(GetCookie('shipping'));
	} else {
		currentShipping = new String(' ');
	}


//alert(currentItems+"\n"+currentQtys+"\n"+currentPrices+"\n"+currentShipping);


	// add new item and separator to strings
	currentItems += (item + '|');
	currentQtys += (quantity + '|');
	currentPrices += (price + '|');
	currentShipping += (shipping + '|');


//alert(currentItems+"\n"+currentQtys+"\n"+currentPrices+"\n"+currentShipping);

	// rewrite cookies
	document.cookie = "numItems="+numItems+";expires="+expireDate.toGMTString()+";path=/";
	document.cookie = "items="+currentItems+";expires="+expireDate.toGMTString()+";path=/";
	document.cookie = "quantities="+currentQtys+";expires="+expireDate.toGMTString()+";path=/";
	document.cookie = "prices="+currentPrices+";expires="+expireDate.toGMTString()+";path=/";
	document.cookie = "shipping="+currentShipping+";expires="+expireDate.toGMTString()+";path=/";


}



function setVal(j) {

	// get the new quantity from the form field
	var newVal = eval("document.shopcart.qty"+j+".value");

	// get the current quantities from the 'quantities' cookie
	temp = new String(GetCookie('quantities'));

	// split the cookie value at the '|' separator
	var oldVals = temp.split('|');
	temp = '';

	// if the new value is greater than 0 then change the value
	if (newVal > 0) {
		oldVals[j-1] = newVal;
		// write the new array to a string for cookie storage
		for (var i=0; i < numItems; i++) {
			temp += oldVals[i] + '|';
		}
		// set expiration date to plus 5 hours
		expireDate = new Date;
		expireDate.setHours(expireDate.getHours()+5);

		// write the new quantity string to the 'quantities' cookie
		document.cookie = "quantities="+temp+";expires="+expireDate.toGMTString()+";path=/";

	// if the new value is 0 or less delete the entry
	} else {
		deleteItem(j);
	}
}


function deleteItem(h) {

	// set strings to hold cookie values
	var currentItems = GetCookie('items');
	var currentQtys = GetCookie('quantities');
	var currentPrices = GetCookie('prices');
	var currentShipping = GetCookie('shipping');

	// set arrays to hold the split cookie values
	var Items = currentItems.split('|');
	var Qtys = currentQtys.split('|');
	var Prices = currentPrices.split('|');
	var Shipping = currentShipping.split('|');

	// delete value and shift all values down array
	for (var i=h; i < numItems; i++) {
		Items[i-1] = Items[i];
		Qtys[i-1] = Qtys[i];
		Prices[i-1] = Prices[i];
		Shipping[i-1] = Shipping[i];
	}

	// decrese the number of items by 1
	numItems = numItems - 1;

	// clear the old strings
	currentItems = '';
	currentQtys = '';
	currentPrices = '';
	currentShipping = '';

	// write the array values back to strings
	for (var i=0; i < numItems; i++) {
		currentItems += Items[i]+'|';
		currentQtys += Qtys[i]+'|';
		currentPrices += Prices[i]+'|';
		currentShipping += Shipping[i]+'|';
	}

	// write strings back to cookies
	expireDate = new Date;
	expireDate.setHours(expireDate.getHours()+5);
	document.cookie = "numItems="+numItems+";expires="+expireDate.toGMTString()+";path=/";
	document.cookie = "items="+currentItems+";expires="+expireDate.toGMTString()+";path=/";
	document.cookie = "quantities="+currentQtys+";expires="+expireDate.toGMTString()+";path=/";
	document.cookie = "prices="+currentPrices+";expires="+expireDate.toGMTString()+";path=/";
	document.cookie = "shipping="+currentShipping+";expires="+expireDate.toGMTString()+";path=/";

}




 
/* -------------------------------------------------------------------------------------

	This function is called everytime you want to convert a number into a currency.
   For instance, if you send it 8.87634 it will return 8.88. If you send it 45.5 it
   will return 45.50. */
   
function roundToCents(n)
{
cents = n * 100;
cents = Math.round(cents);
strCents = "" + cents;
len = strCents.length;
	
first = strCents.substring(0, len - 2) + ".";
last = strCents.substring(len - 2, len);
	
	if (first == "."){
	   first = "0.";
  	}
	
	if (last.length == 1){
	   last += "0";
	}
	
return first+last;
}

