//----------------------//
//	                    //    
//   Order Form Code    //
//                      //
//----------------------//


//     CONSTANTS
var rateGST = 0.05;
var ratePST_BC = 0.07;/*				Express Express Airmail	Express Exped.  Airmail
										BC		Canada	USA		USA		USA		INTL		*/
var shippingPrices = new Array(new Array(2,		2.5,	3.5,	15,		25,		15),  // $16.95 & under
							   new Array(6.5,	9,		5,		15,		25,		15),  // $34.50 & under
							   new Array(8.5,	9.5,	7.5,	15,		25,		15),  // $69.00 & under
							   new Array(9.5,	11.5,	10,		15,		25,		15),  // $149.99 & under
							   new Array(0,		0,		0,		15,		25,		25)); // Over $149.99

//     FUNCTIONS

//------------------------------------------------------------------------------
// SHIPPING COSTS
// This function is used to calculate shipping costs.
//------------------------------------------------------------------------------

function Shipping(subtotal)
{
	var shpMethod = document.getElementById("shippingmthd").value;
	var shipping = 0;
	
	if(subtotal <= 0)
	{
		shipping = 0;
	}
	else if(subtotal <= 16.98) 
	{
		shipping = shippingPrices[0][shpMethod - 1];
	}
	else if(subtotal <= 34.5)
	{
		shipping = shippingPrices[1][shpMethod - 1];
	}
	else if(subtotal <= 69)
	{
		shipping = shippingPrices[2][shpMethod - 1];
	}
	else if(subtotal <= 149.99)
	{
		shipping = shippingPrices[3][shpMethod - 1];
	}
	else if(subtotal > 149.99)
	{
		shipping = shippingPrices[4][shpMethod - 1];
	}
	return shipping;
}


//------------------------------------------------------------------------------
//
// Called every time a character changes in one of the editable text boxes
// (the quantities). It updates the subtotals and total, and sets the variables
// that are sent to paypal when submit is clicked.
//------------------------------------------------------------------------------

function CalculateValues(element)
{
	var elmId = parseInt(element.id.replace(/[^0-9]/g, ""));
	var unitPrice = parseFloat(document.getElementById("u"+elmId).value);
	var quantity = parseInt(element.value);

	if(!isNaN(unitPrice) && !isNaN(quantity))
	{
		document.getElementById("p"+elmId).value = "$"+ (unitPrice * quantity).toFixed(2);
	}
	else
	{
		document.getElementById("p"+elmId).value = "";//"$"+ (0).toFixed(2);
	}

	UpdateTotalNoResidentCheck();
	GenPackage();
}



//------------------------------------------------------------------------------
//
// Updates the subtotal, shipping, taxes and total.
//------------------------------------------------------------------------------


function UpdateTotalNoResidentCheck()
{
	// Calculate subtotal
	var subtotal = 0;
	var inputs = document.getElementsByTagName('input');
	for(var i in inputs)
	{
		if((typeof( inputs[i].name ) != "undefined" ) && (inputs[i].name.match(/^p[0-9]+$/)) && inputs[i].value.length > 0)
		{
			subtotal += parseFloat(inputs[i].value.replace(/[^0-9\.]/g, ""));
		}
	}
	document.getElementById('subtotal').value = "$"+subtotal.toFixed(2);
	
	// Calculate shipping based on subtotal
	var shipping = Shipping(subtotal);
	document.getElementById("shipping").value = "$"+shipping.toFixed(2);
	
	// Calculate GST/PST
	var gst = 0;
	var pst = 0;
	

	if(document.getElementById("shippingmthd").value == 1) // Expedited (Within BC)
	{
		pst = subtotal * ratePST_BC;
		gst = subtotal * rateGST;
	} 
	else if(document.getElementById("shippingmthd").value == 2) // Expedited (Within Canada)
	{
		gst = subtotal * rateGST;
	}
	
	document.getElementById("gst").value = "$"+gst.toFixed(2);
	document.getElementById("pst").value = "$"+pst.toFixed(2); 

	document.getElementById("total").value = "$"+(subtotal + gst + pst + shipping).toFixed(2);
	
	// Set values in paypal form
	document.getElementById("pp_item_name").value = GenPackage();
	document.getElementById("pp_amt").value = subtotal.toFixed(2);
	document.getElementById("pp_shipping").value = shipping.toFixed(2);
	document.getElementById("pp_tax").value = (gst + pst).toFixed(2);
}
//------------------------------------------------------------------------------
//
// Generates the item description that appears in PayPal
//------------------------------------------------------------------------------

function GenPackage()
{
	var orderSummary = " ";
	var inputs = document.getElementById("orderTbl").getElementsByTagName("INPUT");
	var nameRegx = /^n[0-9]+$/;
	for(var i in inputs)
	{
		if(nameRegx.test(inputs[i].name))
		{
			var elmId = parseInt(inputs[i].id.replace(/[^0-9]/g, ""));
			//alert(inputs[i].id.replace(/[^0-9]/g, "") + " = " +elmId);
			if(!isNaN(elmId))
			{
				var elmQty = parseInt(document.getElementById("q"+elmId).value);
				if(elmQty > 0)
				{
					orderSummary += "-" + inputs[i].value + " Qty. " + elmQty + " ";
				}
			}
		}
	}
	orderSummary += " ";
	return orderSummary;
	//return orderSummary;
}

//------------------------------------------------------------------------------
//
// Called on the order form's "onsubmit" event. If this function returns
// false the "submit" action is cancelled.
//------------------------------------------------------------------------------

function Validate()
{
	UpdateTotalNoResidentCheck();
	var total = parseFloat(document.getElementById("total").value.replace(/[^0-9\.]/g, ""));	
	if(total <= 0 || isNaN(total))
	{
		alert("Please select one or more items.");
		return false;
	}
	return true;
}

