var country;
var province;
var chargeExpress;
var chargeFlorida;
var chargeGST;
var priceGirl, priceBoy;
var floridaRate = 0.065;
var gstRate = 0.05;
var postageRate;
var qtyGirl = 0;
var qtyBoy = 0;
var subtotalGirl = 0.00;
var subtotalBoy = 0.00;
var subtotal = 0.00;
var postageCharge = 0.00;
var expressCharge = 0.00;
var floridaCharge = 0.00;
var gstCharge = 0.00;
var total = 0.00;

function initPage()
{
	updateVariables();
	updateForm();
}

function updateVariables()
{
	country = $('AddrCountry').value;
	
	switch(country)
	{
		case "Canada":
			province = $('State_Province').value;
			priceBoy = priceGirl = 34.95;
			postageRate = 12.00;
			break;
		case "United States":
			province = $('State_Province').value;
			priceBoy = priceGirl = 29.95;
			postageRate = 7.00;
			break;
		default:
			province = $('State_ProvinceText').value;
			priceBoy = priceGirl = 29.95;
			postageRate = 14.50;
			break;
	}

	chargeExpress = (country == "United States" && $('express_bool').checked);
	chargeFlorida = (country == "United States" && province == "Florida");
	chargeGST = (country == "Canada");
}

function updateForm()
{
	updateVariables();
	
	switch(country)
	{
		case "Canada":
			$('labelProvState').innerHTML = 'Province';
			$('labelPostalZip').innerHTML = 'Postal Code';
			$('State_ProvinceText').style.display = 'none';
			$('State_Province').style.display = 'block';
			$('gst_div').style.display = 'block';
			$('price_label').innerHTML = "Price (CAD)";
			$('express_div').style.display = 'none';
			$('express_bool').checked = false;
			break;
		case "United States":
			$('labelProvState').innerHTML = 'State';
			$('labelPostalZip').innerHTML = 'Zip Code';
			$('State_ProvinceText').style.display = 'none';
			$('State_Province').style.display = 'block';
	 		$('gst_div').style.display = 'none';
			$('price_label').innerHTML = "Price (USD)";
			$('express_div').style.display = 'block';
			break;
		default:
			$('labelProvState').innerHTML = 'Region';
			$('labelPostalZip').innerHTML = 'Postal Code';
			$('State_ProvinceText').style.display = 'block';
			$('State_Province').style.display = 'none';
			$('gst_div').style.display = 'none';
			$('price_label').innerHTML = "Price (USD)";
			$('express_div').style.display = 'none';
			$('express_bool').checked = false;
			break;
	}

	$('Price1').value = priceGirl;
	$('Price2').value = priceBoy;
	
	if (chargeExpress)
		$('express_surcharge').style.color = "black";
	else
		$('express_surcharge').style.color = "#999";

	if(chargeFlorida)
		$('flatax_div').style.display = 'block';
	else
		$('flatax_div').style.display = 'none';

	if(chargeGST)
		$('gst_div').style.display = 'block';
	else
		$('gst_div').style.display = 'none';
	updateCart();
}

function updateCart()
{
	if(isNaN($('Quantity1').value))
	{
		$('Quantity1').value = '';
		return false;
	}
	if(isNaN($('Quantity2').value))
	{
		$('Quantity2').value = '';
		return false;
	}
	
	qtyGirl = getNumberValue($('Quantity1').value);
	qtyBoy = getNumberValue($('Quantity2').value);
	subtotalGirl = qtyGirl * priceGirl;
	subtotalBoy = qtyBoy * priceBoy;
	subTotal = subtotalGirl + subtotalBoy;

	if(subTotal > 0)
		postageCharge = postageRate;
	else
		postageCharge = 0.00;

	var qtyTotal = qtyBoy + qtyGirl;
	var expressRate;
	if(country == "United States")
	{
		switch(qtyTotal)
		{
			case 0:
				expressRate = 0;
				break;
			case 1:
				expressRate = 21.30;
				break;
			case 2:
				expressRate = 24.50;
				break;
			case 3:
				expressRate = 27.65;
				break;
			default:
				expressRate = 'TBA';
				break;
		}
		if(isNaN(expressRate))
			$('express_surcharge').value = 'billed later...';
		else
			$('express_surcharge').value = formatMoney(expressRate);
	}

	if(chargeExpress)
	{
		if(isNaN($('express_surcharge').value))
			expressCharge = $('express_surcharge').value;
		else
			expressCharge = getNumberValue($('express_surcharge').value);

		postageCharge = 0.00;
	}
	else
		expressCharge = 0.00;

	if(chargeFlorida)
		floridaCharge = subTotal * floridaRate;
	else
		floridaCharge = 0.00;

	if(chargeGST)
		gstCharge = subTotal * gstRate;
	else
		gstCharge = 0.00;

	total = subTotal + postageCharge + floridaCharge + gstCharge;

	if(false == isNaN(expressCharge))
		total = total + expressCharge;

	$('SubTotal1').value = formatMoney(subtotalGirl);
	$('SubTotal2').value = formatMoney(subtotalBoy);
	$('postage').value =  formatMoney(postageCharge);

	if(chargeExpress && false == isNaN(expressCharge))
		$('express_surcharge').value = formatMoney(expressCharge);

	if(chargeFlorida)
		$('flor_tax').value = formatMoney(floridaCharge);

	if(chargeGST)
		$('gst').value = formatMoney(gstCharge);

	$('total').value = formatMoney(total);
}

function getNumberValue(num)
{//num is a string. If invalid, returns 0.
	if ( num == '' || parseInt(num) <= 0)
		return 0;
	else
		return parseFloat(num);
}

function formatMoney(mnt) 
{
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

function formatNumber(num)
{
	var temp = "";
	var output = "";
	
	num *= 100;	// Eliminates decimal point and gives proper amount of decimal points
	num = Math.round(num);
	
	temp = "" + num;
	output = temp.substring(0,(temp.length-2));
	output = output + '.';
	output = output + temp.substring((temp.length-2),temp.length);
		
	return output;
}

function validateForm()
{
    if(qtyGirl == 0 && qtyBoy == 0)
    {
        alert("There is nothing in your cart");
        return false;
    }
    if($('Name').value == '')
    {
        alert("You must enter your name");
        return false;
    }
    if($('Address_Line1').value == '')
    {
        alert("You must enter your address");
        return false;
    }
    if($('City').value == '')
    {
        alert("You must enter your city");
        return false;
    }
    if($('Zip_PostalCode').value == '')
    {
        alert("You must enter your zip/postal code");
        return false;
    }
    if($('Phone').value == '')
    {
        alert("To ensure your little one receives their Baby No Bumps safety helmet as quickly as possible, please provide your phone number in case we have a question about your order. We will keep your number strictly confidential.");
        return false;
    }
    if($('Email').value == '')
    {
        alert("You must enter your email");
        return false;
    }
    
    return sendPayPalForm();
}

function sendPayPalForm()
{
	document.Order.action = "https://www.paypal.com/cgi-bin/webscr";
	//document.Order.action = "https://www.sandbox.paypal.com/cgi-bin/webscr";

	$('os0').value = $('comments').value;
	$('os1').value = $('hear').value;
	$('os2').value = $('Phone').value;
	$('os3').value = formatMoney(floridaCharge);
	$('os4').value = formatMoney(gstCharge);
	$('os5').value = formatMoney(postageCharge);
	$('os6').value = formatMoney(expressCharge);
	$('amount').value = formatMoney(subTotal)
	
	var item_desc = "";
	if (qtyGirl > 0)
		item_desc +=  qtyGirl + " Baby helmet(s) for girl."
	if (qtyBoy > 0)
		item_desc +=  qtyBoy + " Baby helmet(s) for boy."
	
	$('tax').value = formatMoney(floridaCharge + gstCharge);
	$('shipping').value = formatMoney(postageCharge);

	if (country == "United States")
	{
		if (chargeExpress)
		{
			item_desc +=  " Rush order:YES"
			$('shipping').value = formatMoney(expressCharge + postageCharge);
			$('os6').value = formatMoney(expressCharge);
		}
		else
		{
			item_desc +=  " Rush order:NO"
		}
	}

	$('item_name').value = item_desc;

	if (country == "Canada")
		$('currency_code').value = "CAD";
	else
		$('currency_code').value = "USD";
	
	return true;
}
