//note possible bug when product numbers don't start with letter?

//globals
var arrayOffset;
var fontcolor="#000000";

function initGlobals(){
	//will init globals based on browser
	var browser = navigator.appName;
	if(browser == "Netscape"){
		arrayOffset = 0;
	}
	else{
		arrayOffset=1;
	}
}

function setCookie(name, value) {
	var expires = new Date();
	//expires = (expires.getTime() + 7 * 24 * 60 * 60);//plus 7 days
	//expiresTwo = expires.toGMTString();
	var curCookie = name + "=" + escape(value) + ";path=/;";
	document.cookie = curCookie;
}

function getCookie(name) {
	var thecookie = document.cookie;
	var prefix = name + "=";
	var begin = thecookie.indexOf("; " + prefix);
	if (begin == -1) {
		begin = thecookie.indexOf(prefix);
		if (begin != 0) return null;
	} else
	begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = thecookie.length;
	return unescape(thecookie.substring(begin + prefix.length, end));
}

function addProduct(item){
	//item is productId:Quantity:Description:Price/next ... 
	var itemPassedIn = escape(item);
	item += "/";// add / to seperate items in cart
	var thecookie = document.cookie;
	var index = thecookie.indexOf("ITEMS");
	if(index == -1){//can't find ITEMS and so this is the first item to add	
		initGlobals();
		setCookie("ITEMS",item);
	}
	else{
		//test to see if item has been previously added
		initGlobals();
		var thisProduct = item.split(":");
		var location = thecookie.indexOf(thisProduct[0]);
		if(location == -1){//if not found, then add
			var itemsInCart = getCookie("ITEMS");
			if(itemsInCart == null){//cookie is empty
				itemsInCart="";
			}
			itemsInCart += item;
			setCookie("ITEMS",itemsInCart);	
		}
		
	}
}

function removeProduct(itemNum){
	//remove item with itemNum and all details
	var newcontent="";
	var itemPassedIn = itemNum + ":";
	var thecookie = getCookie("ITEMS");
	if(thecookie ==null){	
		return;
	}
	initGlobals();
	var index = thecookie.indexOf(itemPassedIn);
	if(index != -1){//item was in cookie
		var allitems = thecookie.split("/");//split all items into array
		for(loop=0;loop<allitems.length-arrayOffset;loop++){
			if(allitems[loop].indexOf(itemPassedIn)==-1){//if this is not item to delete
				if(allitems[loop])//if it exists
					newcontent += (allitems[loop] + "/");//add item for returning to cookie
			}
		}
		setCookie("ITEMS",newcontent);	
	}		
}

function adjustQuantity(item, number){
	var itemPassedIn = item, newcontent="";
	var newNum = number;
	if(!isInteger(number) || number == "0" || number == "00" || number == "" || number == " "){
		alert("Quantity must be the number one or greater.");
		return;
	}
	var thecookie = getCookie("ITEMS");
	if(thecookie ==null){
		return;
	}
	if(number.charAt(0) == '0'){//this test for numbers like 01,02,etc.
		newNum = number.charAt(1);//turns them into 1,2,3,etc
	}
	initGlobals();
	var index = thecookie.indexOf(itemPassedIn);	
	if(index != -1){//item was in cookie
		var allitems = thecookie.split("/");//split all items into array
		for(loop=0;loop<allitems.length-arrayOffset;loop++){
			if(allitems[loop].indexOf(itemPassedIn)!=-1){//if this is the item to adjust
				//should be in form itemnum:quantity:description:price
				var thisItem = allitems[loop].split(":");
				allitems[loop] = thisItem[0] + ":" + newNum + ":" + thisItem[2] + ":" + thisItem[3];
				break;
			}
		}
		for(i=0;i<allitems.length-arrayOffset;i++){//rebuild data for cookie
			if(allitems[i]){
				newcontent += (allitems[i] + "/");
			}
		}
		setCookie("ITEMS",newcontent);	
	}		
}

function printItems(){
	var thecookie = getCookie("ITEMS");
	if(thecookie==null || thecookie==""){
		document.writeln("<tr><td colspan=2><p><font face=\"arial\"><B>Your Cart is Empty</B></font></td><td>&nbsp;</td></table>");
	}
	else{
		initGlobals();
		var arrayOfItems = thecookie.split("/");//split all items into array
		var grandTotal=0;
		//document.writeln("<form name=\"theform\">");
		//document.writeln("<input type=\"hidden\" value=\"for future use\">");
		for(i=0;i<arrayOfItems.length;i++){
			var thisItem = arrayOfItems[i].split(":");
			//note: assume cookie info in form productid:quantity:description:price
			if(arrayOfItems[i])
				document.writeln("<tr>");
			for(j=1;j<thisItem.length;j++){//starting at 1 will not print out item number
				if(thisItem[j]){
					if(j==1){//this is the quantity field
						//assumes quantity comes right after item number
						document.writeln("<td valign=\"top\"><input type=\"text\" maxlength=\"2\" onchange=\"adjustQuantity('" + thisItem[j-1] + "',theform." + "quantity" + thisItem[j-1] + ".value)\";top.location='mycart.html';\" name=\"" + "quantity" + thisItem[j-1] + "\" size=\"2\" value=\"" + thisItem[j] + "\"> \
							<a href=\"mycart.html\" onclick=\"adjustQuantity('" + thisItem[j-1] + "',theform." + "quantity" + thisItem[j-1] + ".value)\"><img src=\"../images/update.gif\" alt=\"Update Quantity\" border=0 align=absmiddle></a></td>");

					}
					else if(j==2){//this is the description field
						document.writeln("<td valign=\"top\"><p><font color=\"" + fontcolor + "\">" + thisItem[j] + " <BR><font size=\"-1\">Price: $" + thisItem[3] + "</font></font></td>");
					}
					else if(j==3){//this is the price field
						var total = thisItem[j]*thisItem[1];//add price * quantity to total
						grandTotal += total;
						document.writeln("<td valign=\"top\"><p>$" + cent(total) + "</td>");

					}
					else{	
						document.writeln("<td valign=\"top\">" + thisItem[j] + "</td>");
					}
				}
			}
			if(thisItem[0])
				document.writeln("<td><a href=\"mycart.html\" onclick=\"removeProduct('" + thisItem[0] + "')\"><img src=\"../images/removeitem.gif\" alt=\"Remove this item\" border=0 align=absmiddle></a><BR>&nbsp;<BR>&nbsp;<BR></td>");
		}
		document.writeln("<tr><td>&nbsp;</td><td align=\"right\"><p><b>Subtotal:</b></td><td><p><b>$" + cent(grandTotal) + "</b></td><td>&nbsp;</td>");
		document.writeln("</table>");
		document.writeln("</form>");
		document.writeln("<form action=\"https://id3494.securedata.net/~magcosports/shop/php/checkout.php\" method=\"POST\">");
		document.writeln("<input type=\"hidden\" name=\"cart\" value=\"" + getCookie("ITEMS") + "\">");
		document.writeln("<p><input type=\"image\" border=\"0\" src=\"../images/checkout.gif\" alt=\"ready to checkout\"></a></p>");
		document.writeln("</form>");
	}
}

function goTo(page){
	browser.location = page;
}

function isDigit(c){
	return ((c >= "0") && (c <= "9"))
}

function isInteger(s){
	var i;
	for (i = 0; i < s.length; i++){   
		// Check that current character is number.
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}
	// All characters are numbers.
	return true;
}

function cent(amount) {
	// returns the amount in the .99 format
 	amount -= 0;
	amount = (Math.round(amount*100))/100; return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
} 




function showCookie(){
	alert("COOKIE: " + unescape(document.cookie));
}

