

function updateExpressCharge()
{
	var form = document.Order;
	
	if (getNumberValue(form.total.value) > 0)
	{
		calculateTotal();
	}
	
	if (form.express_bool.checked == true)
		$('express_input').style.color = "black";
	else
		$('express_input').style.color = "#999";
}

/*<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="41406">
<input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynow_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>*/


function SetForm()
{
	 
	var total = calculateTotal();
	var country = getCheckedValue(document.Order.region);
 
	document.Order.action = "https://www.paypal.com/cgi-bin/webscr";
	//document.Order.action = "https://www.sandbox.paypal.com/cgi-bin/webscr";
	
	
	document.Order.amount.value = money(eval(getTotalBeforeExtra()));//getSubtotal
	document.Order.shipping.value =  document.Order.postage.value;
	document.Order.os5.value = document.Order.shipping.value;//postage&handling
	
	var item_desc = "";
	if (checkQuantity('girl'))
		item_desc +=  document.Order.Quantity1.value + " Baby helmet(s) for girl."
	if (checkQuantity('boy'))
		item_desc +=  document.Order.Quantity2.value + " Baby helmet(s) for boy."
	
	if (document.Order.comments.value.length != 0 )
	{
		document.Order.os0.value =  document.Order.comments.value;
	}
	
	if (document.Order.hear.value.length != 0 )
	{
		document.Order.os1.value = document.Order.hear.value;
	}
	
	//always there so don't need to check
	document.Order.os2.value = document.Order.Phone.value;
	
	if (document.Order.State_Province.value == 'Florida' || document.Order.State_Province.value == 'florida')
	{
		 
		var florida_tax = parseFloat(document.Order.flor_tax.value);
	}
	else
	{
		 
		var florida_tax = 0;
	}
 
    document.Order.tax.value = florida_tax;
 	
	alert ("Florida tax: " + florida_tax);


	if ( country == "canada")
	{
		document.Order.tax.value = money(parseFloat(florida_tax) + parseFloat(document.Order.gst.value));
		document.Order.os4.value = document.Order.gst.value;
	}
	
	document.Order.os3.value = florida_tax;
	 

	if (country == "usa")
	{
		if (document.Order.express_bool.checked == true)
		{
			item_desc +=  " Rush order:YES"
			document.Order.shipping.value =  money(parseFloat(document.Order.postage.value) +  eval(getRushAmount()));
			document.Order.os6.value = money(eval(getRushAmount()));
		}
		else
		{
			item_desc +=  " Rush order:NO"
		}
	}
	 
	document.Order.item_name.value = item_desc;
	

	if (country == "canada")
		document.Order.currency_code.value = "CAD";
	else
		document.Order.currency_code.value = "USD";
	
	
}

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 getTotalBeforeExtra()
{
	var total =0;
	var form = document.Order;
	total = getNumberValue(form.SubTotal1.value) + getNumberValue(form.SubTotal2.value);
	return total;
	
}

//Amount of charge if order rushed (us only)
function getRushAmount()
{
	var form = document.Order;
	var country = getCheckedValue(form.region);
	var total = 0;
	if (country == "usa")
	{
		if ($('express_div').style.display != 'none')
			if (form.express_bool.checked == true)
				total += parseFloat(form.express_surcharge.value);
	}
	return total;
}

function calculateTotal()
{
	 
	var form = document.Order;
	var country = getCheckedValue(form.region);
	 
	var total =0;
 
	total = getTotalBeforeExtra()
	

	total += parseFloat(form.postage.value);
	
	if (country == "usa")
	{
		if ($('express_div').style.display != 'none')
			if (form.express_bool.checked == true)
				total += parseFloat(form.express_surcharge.value);
	}
		
	//if ($('international_div').style.display != 'none' )
		//total +=  parseFloat(form.international.value);
		
	var florida_tax = total * 0.065;
 
	form.flor_tax.value = money(florida_tax);
	total += florida_tax;
	
	if ( country == "canada")
	{
		var gst_val = total  * 0.05;
		form.gst.value = money(gst_val);
		total += gst_val;
		florida_tax = florida_tax  + (florida_tax/ 0.86);
	}
	 
	form.total.value = money(total); 
	return form.total.value;
}

 
function onCountryChange(form)
{
	var form = document.Order;
	var country = getCheckedValue(form.region);

	if (country != "canada")
	{
 		$('gst_div').style.display = 'none';
		form.Price1.value = form.Price2.value ="29.95";
		$('price_label').innerHTML = "Price (USD)";
	}
		
	if (country == "canada")
	{
 		$('gst_div').style.display = 'block';
		form.postage.value =  "12.00";
		form.Price1.value = form.Price2.value ="34.95";
		$('price_label').innerHTML = "Price (CAD)";
	}
	
	if (country != "usa")
	{
		$('express_div').style.display = 'none';
		
		//$('international_div').style.display = 'block';
		//form.international.value  = "12.50";
	}
	
	if ( country == "europe")
	{
		form.postage.value =  "14.50";
	}
	
	if (country == "usa")
	{
		form.postage.value =  "7.00";
		form.express_surcharge.value =  "12.00";
		$('express_div').style.display = 'block';
		///$('international_div').style.display = 'none';
	}
	
	//Need to update subtotals, because prices may change based on country.
	 
	if (checkQuantity('girl'))
		updateSubtotal('girl');
	if (checkQuantity('boy'))
		updateSubtotal('boy');
	
	if (document.Order.Quantity1.value != '' || document.Order.Quantity2.value != '')
		calculateTotal();
}


//Returns true if quantity for a gender contains a valid number, false otherwise.
function checkQuantity(gender)
{
	var genderBool;
	if (gender == 'girl')
	{
		genderBool = document.Order.Quantity1.value == '' || parseInt(document.Order.Quantity1.value) <= 0;
	}
	else if (gender == 'boy')
	{
		genderBool = document.Order.Quantity2.value == '' || parseInt(document.Order.Quantity2.value) <= 0;
	}
	
    if(genderBool)
    {
        //alert("You must enter a positive numeric quantity");
        return false;
	}
	else
		return true;
}



function onQuantityChange(gender)
{
	if(getCheckedValue(document.Order.region) == '')
    {
        alert("You must choose a country");
		document.Order.Quantity1.value = document.Order.Quantity1.value = '';
        return false;
    }
	
	
	var quantity = 0;
	if (checkQuantity(gender))
	{
		updateSubtotal(gender);
	}
	else
	{
		alert("You must enter a positive numeric quantity");
	}

	calculateTotal();
}


//PRE: quantity for that gender is valid.
function updateSubtotal(gender)
{
		 
		if (gender == 'girl')
		{
			quantity = document.Order.Quantity1.value;
			document.Order.SubTotal1.value = money(quantity * document.Order.Price1.value);
		}
		else if (gender == 'boy')
		{
			quantity = document.Order.Quantity2.value;
			document.Order.SubTotal2.value = money(quantity * document.Order.Price2.value);
		}
}


 
//num is a string. If invalid, returns 0.
function getNumberValue(num)
{
	if ( num == '' || parseInt(num) <= 0)
		return 0;
	else
		return parseFloat(num);
}
 
function checkRequired(theForm)
{
	 
    if(getCheckedValue(theForm.region) == '')
    {
        return false;
    }
	 
	if(getCheckedValue(theForm.baby_type) == '')
    {
        //alert("You must specify twins, triplets or other.");
       // return false;
    }
	
	var girlBool = document.Order.Quantity1.value == '' || parseInt(document.Order.Quantity1.value) <= 0;
	var boyBool = document.Order.Quantity2.value == '' || parseInt(document.Order.Quantity2.value) <= 0;
	
    if(girlBool && boyBool)
    {
        alert("You must enter a positive numeric quantity");
        return false;
    }
    if(theForm.Name.value == '')
    {
        alert("You must enter your name");
        return false;
    }
    if(theForm.Address_Line1.value == '')
    {
        alert("You must enter your address");
        return false;
    }
    if(theForm.City.value == '')
    {
        alert("You must enter your city");
        return false;
    }
    if(theForm.State_Province.value == '')
    {
        alert("You must enter your state/province");
        return false;
    }
    if(theForm.AddrCountry.value == '')
    {
        alert("You must enter your country");
        return false;
    }
    if(theForm.Zip_PostalCode.value == '')
    {
        alert("You must enter your zip/postal code");
        return false;
    }
    if(theForm.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(theForm.Email.value == '')
    {
        alert("You must enter your email");
        return false;
    }
     /*if(document.Order.sex.value == '')
    {
        alert("You specify wether your baby is a girl or boy.");
        return false;
    }*/
	
	
	
//    if(document.Order.MethodOfPayment.value == '')
//    {
//        alert("You must choose a payment method. Please select Check and enter the check number or click the paypal button.");
//        return false;
//    }
//    if(document.Order.MethodOfPayment.value == 'Check' && document.Order.ChequeNo.value == '')
//    {
//        alert("You must specify check number for payment by check.");
//        return false; 
//    }

    return true;
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) 
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

//Converts a number to a money string.
function money(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);
}