// Copyright 2007  WaterRocketPop.com all rights reserved
//  Direct all inquires to WaterRocketPop.com
//  Author of all except the IsNumbersOnly() function and formatCurrency() - Mike Passerotti
//
var PayPalForm;
var ErrorMessage;
var BasicPrice = 45.00;

function GetZone(InputZip, ShippingCompany)
{
	var ZipFirstThree;
	var return_zone = 5;

	ZipFirstThree = InputZip.substring(0,3);
	if (ShippingCompany == "USPS")
	{
		for (var i = 0; i < usps_zone_array.length; i++)
		{
			if ((ZipFirstThree >= usps_zone_array[i].range_low) && (ZipFirstThree <= usps_zone_array[i].range_high))
			{
				// found the right zone
				return_zone = usps_zone_array[i].zones;
				i = usps_zone_array.length;
			}
		}
	}
	else if (ShippingCompany == "UPS")
	{
		for (var i = 0; i < ups_zone_array.length; i++)
		{
			if ((ZipFirstThree >= ups_zone_array[i].range_low) && (ZipFirstThree <= ups_zone_array[i].range_high))
			{
				// found the right zone
				return_zone = ups_zone_array[i].zones;
				i = ups_zone_array.length;
			}
		}
	}
	else if (ShippingCompany == "FEDEX")
	{
		for (var i = 0; i < fedex_zone_array.length; i++)
		{
			if ((ZipFirstThree >= fedex_zone_array[i].range_low) && (ZipFirstThree <= fedex_zone_array[i].range_high))
			{
				// found the right zone
				return_zone = fedex_zone_array[i].zones;
				i = fedex_zone_array.length;
			}
		}
	}

	return return_zone;
}

function GetRate(InputWeightOunces, InputZone, ShippingCompany)
{
	var ZoneIndex;
	var RoundUpPounds;
	var return_rate = 13.45;
	var remainder;
	var UPS_Adjust = 2.40;

	RoundUpPounds = Math.floor(InputWeightOunces / 16);
	remainder = InputWeightOunces - (RoundUpPounds * 16);
	if (remainder > 0)
	{
		RoundUpPounds = RoundUpPounds + 1;
	}

	if (RoundUpPounds < 1)
	{
		RoundUpPounds = 1;
	}
	// change for zero based index
	RoundUpPounds = RoundUpPounds - 1;

	if (ShippingCompany == "USPS")
	{
		if ((InputZone == 1) || (InputZone == 2))
		{
			return_rate = usps_rate_array[RoundUpPounds].zone1_2;
		}
		else if (InputZone == 3)
		{
			return_rate = usps_rate_array[RoundUpPounds].zone3;
		}
		else if (InputZone == 4)
		{
			return_rate = usps_rate_array[RoundUpPounds].zone4;
		}
		else if (InputZone == 5)
		{
			return_rate = usps_rate_array[RoundUpPounds].zone5;
		}
		else if (InputZone == 6)
		{
			return_rate = usps_rate_array[RoundUpPounds].zone6;
		}
		else if (InputZone == 7)
		{
			return_rate = usps_rate_array[RoundUpPounds].zone7;
		}
		else if (InputZone == 8)
		{
			return_rate = usps_rate_array[RoundUpPounds].zone8;
		}
		else
		{
			return_rate = usps_rate_array[RoundUpPounds].zone5;
		}
	}
	else if (ShippingCompany == "UPS")
	{
		if ((InputZone == 1) || (InputZone == 2))
		{
			return_rate = ups_rate_array[RoundUpPounds].zone1_2;
		}
		else if (InputZone == 3)
		{
			return_rate = ups_rate_array[RoundUpPounds].zone3;
		}
		else if (InputZone == 4)
		{
			return_rate = ups_rate_array[RoundUpPounds].zone4;
		}
		else if (InputZone == 5)
		{
			return_rate = ups_rate_array[RoundUpPounds].zone5;
		}
		else if (InputZone == 6)
		{
			return_rate = ups_rate_array[RoundUpPounds].zone6;
		}
		else if (InputZone == 7)
		{
			return_rate = ups_rate_array[RoundUpPounds].zone7;
		}
		else if (InputZone == 8)
		{
			return_rate = ups_rate_array[RoundUpPounds].zone8;
		}
		else
		{
			return_rate = ups_rate_array[RoundUpPounds].zone5;
		}
		return_rate = return_rate + UPS_Adjust;
	}
	else if (ShippingCompany == "FEDEX")
	{
		if ((InputZone == 1) || (InputZone == 2))
		{
			return_rate = fedex_rate_array[RoundUpPounds].zone1_2;
		}
		else if (InputZone == 3)
		{
			return_rate = fedex_rate_array[RoundUpPounds].zone3;
		}
		else if (InputZone == 4)
		{
			return_rate = fedex_rate_array[RoundUpPounds].zone4;
		}
		else if (InputZone == 5)
		{
			return_rate = fedex_rate_array[RoundUpPounds].zone5;
		}
		else if (InputZone == 6)
		{
			return_rate = fedex_rate_array[RoundUpPounds].zone6;
		}
		else if (InputZone == 7)
		{
			return_rate = fedex_rate_array[RoundUpPounds].zone7;
		}
		else if (InputZone == 8)
		{
			return_rate = fedex_rate_array[RoundUpPounds].zone8;
		}
		else
		{
			return_rate = fedex_rate_array[RoundUpPounds].zone5;
		}
	}
	return return_rate;
}

function ChangePrice(FormName)
{
	var SubTotal;
	var ShippingTotal;
	var Total;
	var FormExtension = '0';
	if (FormName == 'paypal_0')
	{
		BasicPrice = 38.00;
		PayPalForm = document.forms[0];
		FormExtension = '0';
	}
	else if (FormName == 'paypal_1')
	{
		BasicPrice = 45.00;
		PayPalForm = document.forms[2];
		FormExtension = '1';
	}
	if (InputValidation() == true)
	{
		SubTotal = GetPadPrice(FormExtension);
		ShippingTotal = GetShippingPrice(FormExtension);
		Total = SubTotal + ShippingTotal;
		document.getElementById('OutputText_' + FormExtension).innerHTML = "";
		document.getElementById('TOTAL_' + FormExtension).innerHTML = "<b>$" + formatCurrency(Total) + "</b>";
	}
	else
	{
		document.getElementById('OutputText_' + FormExtension).innerHTML = '<p align="center"><font color="red"><b>' + ErrorMessage + '</b></font></p>';
		SubTotal = GetPadPrice();
		ShippingTotal = GetDefaultShippingPrice(FormExtension);
		Total = SubTotal + ShippingTotal;
		document.getElementById('TOTAL_' + FormExtension).innerHTML = "$" + formatCurrency(Total);
	}
	return true;
}

function GetPadPrice(FormExtension)
{
	var WaterPrice = 10.00;
	var PressureGaugePrice = 15.00;
	var IndustrialAirPrice = 10.00;
	var AutomotiveAirPrice = 10.00;

	var Quantity = PayPalForm.quantity.value;

	var BasicExtPrice = BasicPrice ;
	var PressureExtPrice = 0;
	var WaterExtPrice = 0;
	var IndustrialAirExtPrice = 0;
	var AutomotiveAirExtPrice = 0;
	var PayPalSubTotal = BasicPrice;
	var SubTotal = BasicExtPrice;

	var ItemDescription = "Basic Water Rocket Launch Pad";
	var ItemNum = "BASICPAD";
	var SelectedShipper = "";

	if (FormExtension == "0")
	{
		ItemDescription = "2008 Basic Water Rocket Launch Pad";
		ItemNum = "2008BASICPAD";
	}
	else
	{
		ItemDescription = "2007 Basic Water Rocket Launch Pad";
		ItemNum = "2007BASICPAD";
	}

	if (PayPalForm.os0[1].checked == true)
		SelectedShipper = "UPS";
	else if (PayPalForm.os0[2].checked == true)
		SelectedShipper = "FEDEX";

	BasicExtPrice = BasicPrice * Quantity;
	if (PayPalForm.PressureGauge.checked == true)
	{
		PressureExtPrice = PressureGaugePrice * Quantity;
		PayPalSubTotal = PayPalSubTotal + PressureGaugePrice;
		ItemDescription = ItemDescription + " + Pressure Gauge";
		ItemNum = ItemNum + "_PRESS";
	}
	if (PayPalForm.GardenHose.checked == true)
	{
		WaterExtPrice = WaterPrice * Quantity;
		PayPalSubTotal = PayPalSubTotal + WaterPrice;
		ItemDescription = ItemDescription + " + Garden Hose Adaptor";
		ItemNum = ItemNum + "_WATER";
	}
	if (PayPalForm.AirConnector[1].checked == true)
	{
		IndustrialAirExtPrice = IndustrialAirPrice  * Quantity;
		PayPalSubTotal = PayPalSubTotal + IndustrialAirPrice;
		ItemDescription = ItemDescription + " + Industrial Air Connector";
		ItemNum = ItemNum + "_INDUSTRIALAIR";
	}
	if (PayPalForm.AirConnector[2].checked == true)
	{
		AutomotiveAirExtPrice = AutomotiveAirPrice * Quantity;
		PayPalSubTotal = PayPalSubTotal + AutomotiveAirPrice;
		ItemDescription = ItemDescription + " + Automotive Air Connector";
		ItemNum = ItemNum + "_AUTOMOTIVEAIR";
	}

	SubTotal = BasicExtPrice + PressureExtPrice + WaterExtPrice + IndustrialAirExtPrice + AutomotiveAirExtPrice;
	PayPalForm.amount.value = PayPalSubTotal;
	PayPalForm.item_name.value = ItemDescription;
	PayPalForm.item_number.value = ItemNum;

	document.getElementById('BASIC_PRICE_' + FormExtension).innerHTML = "$" + formatCurrency(BasicExtPrice);
	document.getElementById('GAUGE_PRICE_' + FormExtension).innerHTML = "$" + formatCurrency(PressureExtPrice);
	document.getElementById('WATER_PRICE_' + FormExtension).innerHTML = "$" + formatCurrency(WaterExtPrice);
	document.getElementById('INDUSTRIAL_AIR_PRICE_' + FormExtension).innerHTML = "$" + formatCurrency(IndustrialAirExtPrice);
	document.getElementById('AUTOMOTIVE_AIR_PRICE_' + FormExtension).innerHTML = "<u>$" + formatCurrency(AutomotiveAirExtPrice) + "</u>";
	document.getElementById('SUBTOTAL_' + FormExtension).innerHTML = "$" + formatCurrency(SubTotal);

	return SubTotal;
}

function GetDefaultShippingPrice(FormExtension)
{
	var ShippingSubTotal = 18.75;
	PayPalForm.quantity.value = "1";
	PayPalForm.ZipCode.value = "02747";
	ShippingTotal = GetShippingPrice(FormExtension);

	return ShippingSubTotal;
}

function GetShippingPrice(FormExtension)
{
	var AdditionalHandling = 1.50;
	var PackingWeight = 8;
	var BasicWeight = 48;
	var AddonWeight = 9;
	var Shipping_First = 18.75;
	var Shipping_Additional = 14.37;
	var Shipping_AdditionalSubTotal;
	var ShippingSubTotal = 18.75;
	var Quantity = PayPalForm.quantity.value;
	var addoncount = 0;
	var Zone;
	var TotalWeightOunces;
	var DisplayWeightOunces;
	var DisplayPounds;
	var SelectedShipper = "USPS";

	TotalWeightOunces = BasicWeight + PackingWeight;
	DisplayWeightOunces = (BasicWeight * Quantity) + PackingWeight;
	if (PayPalForm.PressureGauge.checked == true)
	{
		TotalWeightOunces = TotalWeightOunces + AddonWeight;
		DisplayWeightOunces = DisplayWeightOunces + (AddonWeight * Quantity);
		addoncount = addoncount + 1;
	}
	if (PayPalForm.GardenHose.checked == true)
	{
		TotalWeightOunces = TotalWeightOunces + AddonWeight;
		DisplayWeightOunces = DisplayWeightOunces + (AddonWeight * Quantity);
		addoncount = addoncount + 1;
	}
	if (PayPalForm.os0[1].checked == true)
		SelectedShipper = "UPS";
	else if (PayPalForm.os0[2].checked == true)
		SelectedShipper = "FEDEX";

	if (PayPalForm.AirConnector[1].checked == true)
	{
		TotalWeightOunces = TotalWeightOunces + AddonWeight;
		DisplayWeightOunces = DisplayWeightOunces + (AddonWeight * Quantity);
		addoncount = addoncount + 1;
	}
	else if (PayPalForm.AirConnector[2].checked == true)
	{
		TotalWeightOunces = TotalWeightOunces + AddonWeight;
		DisplayWeightOunces = DisplayWeightOunces + (AddonWeight * Quantity);
		addoncount = addoncount + 1;
	}

	Zone = GetZone(PayPalForm.ZipCode.value, SelectedShipper);
	Shipping_First = GetRate(TotalWeightOunces, Zone, SelectedShipper) + AdditionalHandling;

	Shipping_2 = CalculateShipping2(Shipping_First);

	Shipping_Additional = CalculateShipping2(Shipping_First);
	Shipping_AdditionalSubTotal = Shipping_Additional * (Quantity - 1);
	ShippingSubTotal = Shipping_First + Shipping_AdditionalSubTotal;

	document.getElementById('SHIPPING_' + FormExtension).innerHTML = "$" + formatCurrency(Shipping_First);
	document.getElementById('SHIPPING_2_' + FormExtension).innerHTML = "<u>$" + formatCurrency(Shipping_Additional) + "</u>";
	document.getElementById('SHIPPING_2_SUBTOTAL_' + FormExtension).innerHTML = "<u>$" + formatCurrency(Shipping_AdditionalSubTotal) + "</u>";

	DisplayPounds = Math.floor(DisplayWeightOunces / 16);
	if ((DisplayWeightOunces - (Math.floor(DisplayWeightOunces / 16) * 16)) > 0)
		DisplayPounds = DisplayPounds + 1;
	document.getElementById('SHIPPING_WEIGHT_' + FormExtension).innerHTML =  "Est. " + DisplayPounds + " lbs";

	PayPalForm.shipping.value = Shipping_First;
	PayPalForm.shipping2.value = Shipping_Additional;

	return ShippingSubTotal;
}

function CalculateShipping2(InputShip)
{
	return (Math.floor(InputShip * 100 * 0.4) / 100);
}

function InputValidation()
{
	var InputTest = true;
	var InputValid = true;
	ErrorMessage = "";

	InputTest = IsNumbersOnly(PayPalForm.ZipCode.value);
	if (PayPalForm.ZipCode.value.length < 5)
		InputTest = false;
	if (InputTest == false)
	{
		InputValid = false;
		ErrorMessage = ErrorMessage + " Bad Destination Zip Code."
	}
	InputTest = IsNumbersOnly(PayPalForm.quantity.value);
	if (PayPalForm.quantity.value.length < 1)
		InputTest = false;
	if (PayPalForm.quantity.value <= 0)
		InputTest = false;
	if (InputTest == false)
	{
		InputValid = false;
		ErrorMessage = ErrorMessage + " Bad Quantity."
	}
	return InputValid;
}

// Original:  Cyanide_7 (leo7278@hotmail.com) 
// Web Site:  http://www7.ewebcity.com/cyanide7 
// This script and many more are available free online at
// The JavaScript Source!! http://javascript.internet.com
function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + dblValue + '.' + strCents);
}

// Exerpt from:
// Name: USPS Shipping Calculator (java script version) 
// Date: June 17, 2002 
// Author: Nathan Arendt nathan_arendt@yahoo.com
function IsNumbersOnly(val)
{
	var i = 0;

	for(i = 0; i < val.length; i++)
	{
		if(!(val.charCodeAt(i) >= 48 && val.charCodeAt(i) <= 57))
			return false;
	}

	return true;
}

