// JavaScript Document

function stopRKey(evt) { 
  var evt = (evt) ? evt : ((event) ? event : null); 
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
} 

function CheckShip_State()
{
	if($('#Ship_State').val() == "selectstate") {
		return [[$('#Ship_State'), "Please select the shipping state."]];
	}
	if($('#Ship_State').val() == "CA" && $('#ContainsCARestricted').val() == "1") {
		return [[$('#Ship_State'), "Yamaha 2-stroke outboards cannot ship to CA due to EPA restrictions. Please choose to ship to a different state or remove this motor from your order."]];
	}
	return true;
}

function CheckBill_State()
{
	if($('#Bill_State').val() == "selectstate") {
		return [[$('#Bill_State'), "Please select your billing state."]];
	}
	return true;
}

function CheckCard_Type()
{
	if($('#Card_Type').val() == "none") {
		return [[$('#Card_Type'), "Please select credit card type."]];
	}
	return true;
}

function CheckExpMonth()
{
	if($('#Exp_Month').val() == "----") {
		return [[$('#Exp_Month'), "Please select credit card expiration month."]];
	}
	return true;
}

function CheckExpYear()
{
	if($('#Exp_Year').val() == "----") {
		return [[$('#Exp_Year'), "Please select credit card expiration year."]];
	}
	return true;
}

function ComputeForm(form)
{
	if(document.form1.Ship_State.value != "selectstate")
	{
		ComputeShipCost();
		ComputeTax();
		ComputeTotal(); 	
	}
	else 
	{
		setSpanText('Ship_Cost_Display', "Please enter shipping address");
		document.getElementById('Ship_Cost').value = null;
		setSpanText('Sales_Tax_Display', "Please enter shipping address");
		document.getElementById('Sales_Tax').value = null;
		setSpanText('Total_Display', "Please enter shipping address");
		document.getElementById('Total').value = null;
	}
}

function ComputeShipCost()
{
	var shipcost = 0.00;
	
	if(document.form1.Ship_State.value == "AK")
	{
		shipcost = Number(document.form1.ShipTotalAK.value);
		setSpanText('Ship_Cost_Display', "$ " + shipcost.toFixed(2));
	}
	else if(document.form1.Ship_State.value == "HI")
	{
		shipcost = Number(document.form1.ShipTotalHI.value);
		setSpanText('Ship_Cost_Display', "$ " + shipcost.toFixed(2));
	}
	else if(document.form1.Ship_State.value == "CA" && (document.form1.ContainsCARestricted.value == 1))
	{
		shipcost = 0;
		setSpanText('Ship_Cost_Display', "Error: Yamaha 2-Stroke Cannot Ship to CA");
	}
	else 
	{
		setSpanText('Ship_Cost_Display', "FREE");
	}
		
	document.getElementById('Ship_Cost').value = shipcost;

}

function ComputeTax() 
{
	if(document.form1.Ship_State.value == "TN")
	{
  		var taxpercent = 0.0975;
		var taxamount = taxpercent * document.form1.Subtotal.value;
		setSpanText('Sales_Tax_Display', "$ " + taxamount.toFixed(2));

	}
	else if(document.form1.Ship_State.value == "CA" && (document.form1.ContainsCARestricted.value == 1))
	{
		shipcost = 0;
		setSpanText('Sales_Tax_Display', "Please Select A Different Ship-To State or");
	}
	else 
	{
		var taxamount = 0.00;
		setSpanText('Sales_Tax_Display', "N/A");
	}
	document.getElementById('Sales_Tax').value = taxamount;

}

function ComputeTotal()
{
	if(document.form1.Ship_State.value == "CA" && (document.form1.ContainsCARestricted.value == 1))
	{
		setSpanText('Total_Display', "Remove This Motor From Your Order");
		document.form1.HoldDueToRestriction.value = 1;
	}
	else 
	{
		var total = Number(document.getElementById('Subtotal').value) + Number(document.getElementById('Ship_Cost').value) + Number(document.getElementById('Sales_Tax').value);
		setSpanText('Total_Display', "$ " + total.toFixed(2));
		document.getElementById('Total').value = total;
		document.form1.HoldDueToRestriction.value = 0;
	}
}

function setSpanText(elementID, newValue)
{
		if(document.all){
			document.getElementById(elementID).innerText = newValue;
		} else{
			document.getElementById(elementID).textContent = newValue;
		}
}

function FillShipping(form)
{
	if(form.Ship_SameAsBilling.checked)
	{
		form.Ship_Address.value = form.Bill_Address.value;
		form.Ship_City.value = form.Bill_City.value;
		form.Ship_State.value = form.Bill_State.value;
		form.Ship_ZIP.value = form.Bill_ZIP.value;
	}
	else
	{
		form.Ship_Address.value = "";
		form.Ship_City.value = "";
		form.Ship_State.value = "";
		form.Ship_ZIP.value = "";
	}
	ComputeForm(form);
}


