function shipsubmit()
{
	if(document.getElementById)
	{
		document.getElementById("shipping").submit();
	}
	else
	{
		document.all["shipping"].submit();
	}
}

function AddCart(txtPartNo)
{
	var qty, price, data, newdata;
	var index = -1;
	var splitcookie = new Array();
	var parseitem = new Array();
	
	document.getElementById? qty = document.getElementById(txtPartNo).value : qty = document.all[txtPartNo].value;
	document.getElementById? price = document.getElementById(txtPartNo + "Price").value : price = document.all[txtPartNo + "Price"].value;
	newdata = txtPartNo + "," + qty + "," + price + "|";
	data = readCookie("cart");
	if(data == null)//there is no cookie yet
		createCookie("cart",newdata,1);
	else
	{
		splitcookie = data.split("|");//0=PN, 1=Qty, 2=Price
		for(var i=0; i < splitcookie.length; i++)//look for this product already in the cookie
		{
			parseitem[i] = splitcookie[i].substr(0, splitcookie[i].indexOf(','));
			if(parseitem[i] == txtPartNo)
			{
				index = i;
				break;
			}
		}
		if(index != -1)//if the product was found in the cookie
		{
			parseitem = splitcookie[index].split(",");
			parseitem[1] = parseInt(parseitem[1]) + parseInt(qty);
			parseitem[2] = price;
			data = parseitem[0] + "," + parseitem[1] + "," + parseitem[2] + "|";
			for(var i=0; i < splitcookie.length-1; i++)
			{
				if(i != index)
					data += splitcookie[i] + "|";
			}
		}
		else//the product was not found in the cookie
		{
			data += newdata;
		}
		createCookie("cart",data,1);
	}
	location.href="/cart/cart.php"
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function RemoveItem(txtPartNo)
{
	var data;
	var index = -1;
	var splitcookie = new Array();
	var parseitem = new Array();
	
	data = readCookie("cart");
	if(data == null)
		return;
	else
	{
		splitcookie = data.split("|");
		for(var i=0; i < splitcookie.length; i++)
		{
			parseitem[i] = splitcookie[i].substr(0, splitcookie[i].indexOf(','));
			if(parseitem[i] == txtPartNo)
			{
				index = i;
				break;
			}
		}
		if(index != -1)
		{
			data = "";
			for(var i=0; i < splitcookie.length-1; i++)
			{
				if(i != index)
					data += splitcookie[i] + "|";
			}
		}
		else
		{
			return;
		}
		createCookie("cart",data,1);
	}
	RefreshCart();
}

function UpdateSubtotal(txtPartNo)
{
	var data, qty;
	var newdata = "";
	var index = -1;
	var splitcookie = new Array();
	var parseitem = new Array();
	
	data = readCookie("cart");
	if(data == null)
		return;
	else
	{
		splitcookie = data.split("|");
		for(var i=0; i < splitcookie.length; i++)
		{
			parseitem = splitcookie[i].split(",");
			if(parseitem[0] == txtPartNo)
			{
				document.getElementById? qty = document.getElementById(txtPartNo).value : qty = document.all[txtPartNo].value;
				newdata += txtPartNo + "," + qty + "," + parseitem[2] + "|";
			}
			else if(parseitem[0] != "")
			{
				newdata += splitcookie[i] + "|";
			}
		}
		createCookie("cart",newdata,1);
	}
	RefreshCart();
}

function EmptyCart()
{
	eraseCookie("cart");
	RefreshCart();
}

function RefreshCart()
{
	//document.location.href=document.location.href;
	location.replace("/cart/cart.php");
}

function CheckOut()
{
	var total, shipping, insurance, discount, subtotal, terms;
	
	if(confirm("You are about to Begin the Checkout Process. Make sure you have selected your Country and Shipping Speed Before Proceeding. Proceed? (OK = Yes   Cancel = No)"))
	{
		document.getElementById ?  terms=document.getElementById("terms").checked: terms=document.all["terms"].checked;
		if(terms == false)
		{
			alert("You Must Read, Comprehend, and Agree to the Terms and Conditions.");
			RefreshCart();
			return;
		}
		if(document.getElementById)
		{
			//total = eval(document.getElementById("total").value);
			shipping = eval(document.getElementById("shipping").value);
			insurance = eval(document.getElementById("insurance").value);
			discount = eval(document.getElementById("discount").value);
			subtotal = eval(document.getElementById("subtotal").value);
			Country = ship.document.getElementById("Country").options[ship.document.getElementById("Country").selectedIndex].value;
			document.getElementById("on0").value = Country;

			if(Country == "USA")
				ShipSpeed = ship.document.getElementById("ShipSpeed").options[ship.document.getElementById("ShipSpeed").selectedIndex].value;
			else
				ShipSpeed = "USPS Airmail";
			if(ShipSpeed == "SelectOne")
			{
				alert("You Must Select a Shipping Speed.");
				RefreshCart();
				return;
			}
			document.getElementById("os0").value = ShipSpeed;
			//document.getElementById("textbox1").value = ship.document.getElementById("Country").options[ship.document.getElementById("Country").selectedIndex].value;
			
			document.getElementById("amount").value = (subtotal-discount).toFixed(2);
			document.getElementById("shipping_amount").value = (shipping + insurance).toFixed(2);
			document.getElementById("custom_field").value = readCookie("cart");
			document.getElementById("send2paypal").submit();
		}
		else
		{
			//total = eval(document.all["total"].value);
			shipping = eval(document.all["shipping"].value);
			insurance = eval(document.all["insurance"].value);
			discount = eval(document.all["discount"].value);
			subtotal = eval(document.all["subtotal"].value);
			Country = ship.document.all["Country"].options[ship.document.all["Country"].selectedIndex].value;
			document.all["on0"].value = Country;

			if(Country == "USA")
				ShipSpeed = ship.document.all["ShipSpeed"].options[ship.document.all["ShipSpeed"].selectedIndex].value;
			else
				ShipSpeed = "USPS Airmail";
			document.all["os0"].value = ShipSpeed;
			//document.all["textbox1"].value = ship.document.all["Country"].options[ship.document.all["Country"].selectedIndex].value;

			document.all["amount"].value = (subtotal-discount).toFixed(2);
			document.all["shipping_amount"].value = (shipping + insurance).toFixed(2);
			document.all["custom_field"].value = readCookie("cart");
			document.all["send2paypal"].submit();
		}
	}
	else
		RefreshCart();
}

function CalcTotal()
{
	var total, shipping, insurance, discount, subtotal, terms;
		if(document.getElementById)
		{
			//total = eval(document.getElementById("total").value);
			subtotal = eval(document.getElementById("subtotal").value);
			shipping = eval(document.getElementById("shipping").value);
			insurance = eval(document.getElementById("insurance").value);
			discount = eval(document.getElementById("discount").value);
		}
		else
		{
			//total = eval(document.all["total"].value);
			subtotal = eval(document.all["subtotal"].value);
			shipping = eval(document.all["shipping"].value);
			insurance = eval(document.all["insurance"].value);
			discount = eval(document.all["discount"].value);
		}
		total = (subtotal+shipping+insurance-discount).toFixed(2);
		if(total == "" || total == "NaN")
			total = "";
		
		if(document.getElementById)
			document.getElementById("total").value = total;
		else
			document.all["total"].value = total;
}