var admin = shop.admin.value;

function Dollar (val) {  // force to valid dollar amount
	var str,pos,rnd=0;
	if (val < .995) rnd = 1;  // for old Netscape browsers
	str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
	pos = str.indexOf (".");
	if (pos > 0) str = str.substring (rnd, pos + 3);
	return str;
}

function checkvalue()
{
	var value = Number(shop.pack.value);
	
	if (isNaN(value)) 
	{
		alert("Please enter a valid number.");
		shop.pack.focus();
		//shop.pack.value = 1;
	}
	
	if (shop.pack.value <= 0)
	{
		shop.pack.value = 1;
	}
}

function calctotal() {

	if (shop.xshiploc.value == "-1")
	{
		shop.xshiploc.selectedIndex = shop.xshiploc.selectedIndex + 1;
	}
	
	var location = shop.xshiploc.options[shop.xshiploc.selectedIndex].text;
	var price = 0;
	var shipping = 0;
	var handling = 0;
	
	var qty = shop.pack.value;
	
	switch(true)
	{
		case (qty >= 1 && qty <=5): handling = 1.50;break;
		case (qty >= 6 && qty <=10): handling = 2.50;break;
		case (qty >= 11 && qty <=15): handling = 3.00;break;
		case (qty >= 16 && qty <=20): handling = 4.00;break;
	}
			
	switch(true)
	{
		case (qty >= 1 && qty <= 4): price = 4.99;break;
		case (qty >= 5 && qty <= 9): price = 4.75;break;
		case (qty >= 10 && qty <= 20): price = 4.50;break;
	}
	
	switch(location)
	{
		case "É.-U.": 
			switch(true)
			{
				case (qty == 1): shipping = 2.50;break;
				case (qty == 2): shipping = 3.25;break;
				case (qty == 3): shipping = 3.50;break;
				case (qty == 4): shipping = 6.00;break;
				case (qty == 5): shipping = 6.50;break;
				case (qty >= 6 && qty <= 10): shipping = 8.00;break;
				case (qty >= 11 && qty <= 20): shipping = 13.25;break;
			}
			break;
			
		case "International":
			switch(true)
			{
				case (qty == 1): shipping = 3.50;break;
				case (qty == 2): shipping = 4.00;break;
				case (qty >= 3 && qty <= 4): shipping = 6.50;break;
				case (qty >= 5 && qty <= 10): shipping = 14.00;break;
				case (qty >= 11 && qty <= 20): shipping = 27.00;break;
			}
			break;
			
		default: {
			switch(true)
			{
				case (qty == 1): shipping = 1.50;break;
				case (qty >= 2 && qty <= 3): shipping = 2.25;break;
				case (qty >= 4 && qty <= 7): shipping = 3.00;break;
				case (qty >= 8 && qty <= 10): {
					if (location == "Colombie-Britannique" || location == "Alberta" || location == "Manitoba")
					{
						shipping = 9.00;
					}
					else
					{
						shipping = 10.00;
					}
					break;
				}
				case (qty >= 11 && qty <= 20): {
					if (location == "Colombie-Britannique" || location == "Alberta" || location == "Manitoba")
					{
						shipping = 9.00;
					}
					else
					{
						shipping = 11.50;
					}
					break;
				}
			break;
			}
		}
	}
	
	shipping = (shipping - 2);
	if (shipping < 0) shipping = 0;
	
	var subtotal = shop.pack.value * price;
	var tax = subtotal * shop.xshiploc.value;
	var total = subtotal + tax + shipping + handling;
	
	xprice.innerText = "$ " + Dollar(price);
	xsubtotal.innerText = "$ " + Dollar(subtotal);
	xtax.innerText = "$ " + Dollar(tax);
	xshipping.innerText = "$ " + Dollar(shipping + handling);
	//xhandling.innerText = "$ " + Dollar(handling);
	xtotal.innerText = "$ " + Dollar(total);
	//xusdtotal.innerText = "$ " + Dollar(total * 0.88);
	
	shop.shiploc.value = location;
	shop.price.value = price;	
	shop.subtotal.value = xsubtotal.innerText;
	shop.tax.value = xtax.innerText;
	shop.shipping.value = xshipping.innerText;
	shop.handling.value = 0;
	//shop.handling.value = xhandling.innerText;
	shop.total.value = xtotal.innerText;
}
