//(C) 2003 Di-Namic Solutions 2:54 PM 10/28/2005

function IsNumeric(inputVal) 
{	
	if (isNaN(inputVal))
		return (false);
	else
		return (true);
}

function formatCurrency(num) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";

	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
		cents = "0" + cents;

	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3)) + num.substring(num.length-(4*i+3));
	
	return (num + '.' + cents);
}


function ZeroQuan()
{
	var objForm = document.myform;
	//alert("running clear");
	objForm.txtCBook.value = 0;
	objForm.txtFBook.value = 0;
	objForm.txtCBookTot.value = 0;
	objForm.txtFBookTot.value = 0;
	objForm.txtGST.value = 0;
	objForm.txtPST.value = 0;
	objForm.txtSubTotal.value = 0;
	objForm.txtTotal.value = "";
}

function BillShip () 
{
	var objForm = document.myform;
	objForm.txtSHConame.value = objForm.txtCoName.value;
	objForm.SHTitle.value = objForm.cmbTitle.value;
	objForm.txtSHFname.value = objForm.txtFname.value; 
	objForm.txtSHLname.value = objForm.txtLname.value;
	objForm.txtSHAddress.value = objForm.txtAddress.value;
	objForm.txtSHCity.value = objForm.txtCity.value;
	objForm.txtSHStateProv.value = objForm.txtStateProv.value;
	objForm.txtSHZipPostal.value = objForm.txtZipPostal.value;
	objForm.txtSHCountry.value = objForm.txtCountry.value;
	objForm.txtSHTele.value = objForm.txtTele.value;
	alert ("Your Shipping Information has been updated"); 
}


function UpdatePage()
{
	var quan1, quan2, subtotal1, subtotal2, subtotal1tmp, subtotal2tmp, subtotal3tmp, StrSubtotal3, total, PST, Province;
	var discF, discC, priceF, priceC, subTotalC, subTotalF;
	var objForm = document.myform;
	
	var QuantityNumFlag = true;
	
	subtotal1 = 0;
	subtotal2 = 0;
	discount = 0;
	discF = 0;
	discC = 0;
	total = 0;

	
	Province = (objForm.txtStateProv.value.toUpperCase());

	//Check to see if any quantities have been entered for any booklets
	
	if ((objForm.txtCBook.value != "") && (objForm.txtFBook.value != ""))
	{	
		shipCost = 7.95;
		if ((!IsNumeric (objForm.txtCBook.value)) && (!IsNumeric(objForm.txtFBook.value)))
		{
			QuantityNumFlag = false;
			ZeroQuan();
			alert ("Please enter in numeric values for quantity");			
		}
			
		if (QuantityNumFlag == true)
		{			

			if (objForm.txtCBook.value != "") 
			{	
				quan1 = objForm.txtCBook.value;
				subtotal1 = (quan1 * 10);
				
				if ((quan1 < 20) && (quan1 > 0)) {
					discC = (quan1 * 10) - (quan1 * 10);	
					priceC = 10;
					
				}
				if (quan1 >= 20) {
					discC = (quan1 * 8) - (quan1 * 10);
					priceC = 8;
				}
								
				objForm.subTotalC.value = (quan1 * 10) + discC;
				objForm.priceC.value = priceC;
				subtotal1tmp = subtotal1 + ".00";
				objForm.txtCbookTot.value = subtotal1tmp;
			}
	

			//Calculations for Fbook values 101-1000 = 4, 1001+ = 3, else 5$
		
			if (objForm.txtFBook.value != "")
			{	
				quan2 = objForm.txtFBook.value; 
				subtotal2 = (quan2 * 6); 
				if (quan2 >= 20) {
					discF = (quan2 * 5) - (quan2 * 6);
					priceF = 5;
				}
				if ((quan2 < 20) && (quan2 > 0)) {
					discF = (quan2 * 6) - (quan2 * 6);	
					priceF = 6;
				}
				
				objForm.subTotalF.value = (quan2 * 6) + discF;
				objForm.priceF.value = priceF;
				subtotal2tmp = subtotal2 + ".00";
				objForm.txtFBookTot.value = subtotal2tmp;
			}

			if ((Province == "ON") || (Province == "ONT") || (Province == "ONTARIO"))
			{
				//PST = Math.round (((subtotal1 + subtotal2 + discC + discF)* 100) * 0.08) / 100;
				//objForm.txtPST.value = PST;
			}
			else
			{
				//PST = 0;
				//objForm.txtPST.value = PST;
			}
		

			//Start final total calculations 

			subtotal3tmp = Math.round (((subtotal1 + subtotal2 + discC + discF)* 100 )) / 100;
			StrSubtotal3 = subtotal3tmp + ".00"; 
			objForm.txtSubTotal.value = StrSubtotal3;

			//GST = Math.round (((subtotal3tmp)* 100) * 0.07) / 100; //Rounded to two decimal places
			

				objForm.txtCDisc.value = discC + ".00";
				objForm.txtFDisc.value = discF + ".00";

				//objForm.txtGST.value = GST;
				objForm.txtTotal.value = Math.round (((subtotal3tmp + shipCost)* 100)) / 100 ;		
		}
	}
	//additonal part
	//else if ((objForm.txtCBook.value == "") && (objForm.txtCBook.value == "")) 
	//{
	//	alert ("Please enter in numeric quantities for the required booklets");
	//	QuantityNumFlag = false;
	//}

	else
	{	
		alert ("Please enter 0 in Quantity box if none required");
		QuantityNumFlag = false;
	}
		
}


//-----------------------------------------
// 
// This function performs the form validation
//
//-----------------------------------------

function Validate ()
{

	//Declare variables
	var msgArray = new Array ("Company Name","Title", "First Name", "Last Name", "Address", "City", "State", "Zip Code", 
			"Telephone", "Fax Number", "Email Address")
	
	var ShipMsgArray = new Array ("Title", "First Name", "Last Name", "Address", "City", "State", "Telephone", "ZIP Code");
	
	var missingFields = "Billing Info\n\nPlease fill in the required fields: \n";
	var ShipMissingFields = "Shipping Info\n\nPlease fill in the required fields: \n";
	var PayMissing = "Payment Info\n\nPlease fill in the required fields: \n";	
	var NumericMsg = "The following fields must be numeric: \n";
	var DateMsg;
	
	var returnFlag = true;
	var FormFlag = true;
	var ShipFormFlag = true;
	var MailFlag = true;
	var PayFlag = true;

	var objForm = document.myform;

	//-----------------------------------------------------------
	
	//Validate the first 11 text fields to see if they are NULL or not.	
	for (i=0; i < 11; i++)
	{
		if (objForm.elements[i].value == "")
		{	
			missingFields += msgArray [i] + "\n";
			FormFlag = false;
		}	
	}

	//----Non optional fax field-------------------------------------------------------	
	fax = objForm.elements["txtFax"].value
	if (fax == "")
		{
			FormFlag = true;
		}

	//Validate the email field to see if it's a valid email structure or not
	mail = objForm.elements["txtEmail"].value
	
	if (mail.indexOf ("@") == -1)
		MailFlag = false;
	if (mail.indexOf (".") == -1)
		MailFlag = true;

	//-----------------------------------------------------------
	//Begin Shipping Details Validation (for null values)
	
	for (k=12; k < 19; k++)
	{
		if (objForm.elements[k].value == "")
		{
			ShipMissingFields += ShipMsgArray [k-12] + "\n";
			ShipFormFlag = false;
		}
	}

	if ((!objForm.rdoCC[0].checked) && (!objForm.rdoCC[1].checked))
		PayFlag = false;

	if (FormFlag == false)
	{
		alert (missingFields);
		return (false);
	}

	if (ShipFormFlag == false)
	{
		alert(ShipMissingFields);
		return (false);
	}
	if (PayFlag == false)
	{
		alert ("Please select a method of payment");
		return (false);
	}
	
	return (true);
}

//This function performs validation upon the file Form3.asp

function ValMultiForm ()
{
	boolFlag = false;
	unitFlag = true;

	subTotal = 0;
	cGmpQty = 0;
	cGmp11Qty = 0; 
	CombinedQty = 0;
	CustomQty= 0;

	cGmpPrice = 6;
	cGmpDisPrice = 5;
	cGmp11Price = 7;
	cGmp11DisPrice = 6;
	CombinedPrice = 10;
	CombinedDisPrice = 8;
	CustomPrice = 4.5;
	CustomDisPrice = 3.5;

	ShippingPrice = 7.95;
	
	//Variables for discount qualifier
	gmpQualifier = 20;
	CustomQualifier = 2000;

	var objForm = document.myform;
	objForm.subCustom.value = 0;
	
	//Check to see that the four quantity fields are not empty

	if ((objForm.cGmpQty.value == "") && (objForm.cGmp11Qty.value == "") && (objForm.CombinedQty.value == "") && (objForm.CustomQty.value == ""))
	{
		alert ("Please enter a quantity");
	}

	if (IsNumeric(objForm.cGmpQty.value))
	{
		cGmpQty = objForm.cGmpQty.value;
		if (cGmpQty >= gmpQualifier) //Discount applies
		{			
			objForm.subTotalcGmp.value = formatCurrency(cGmpQty * cGmpDisPrice);
			objForm.discCGMP.value = formatCurrency((cGmpQty * cGmpDisPrice) - (cGmpQty * cGmpPrice));
			objForm.Price1.value = formatCurrency(cGmpDisPrice);
		}
		else
		{
			objForm.subTotalcGmp.value = formatCurrency(cGmpQty * cGmpPrice);
			objForm.discCGMP.value = 0.00;
			objForm.Price1.value = formatCurrency(cGmpPrice);
		}
	}
	else
		boolFlag = false;		
	

	if (IsNumeric(objForm.cGmp11Qty.value))
	{
		cGmp11Qty = objForm.cGmp11Qty.value;
		if (cGmp11Qty >= gmpQualifier) //Discount applies
		{			
			objForm.subTotalcGmp11.value = formatCurrency(cGmp11Qty * cGmp11DisPrice);
			objForm.discCGMP11.value = formatCurrency((cGmp11Qty * cGmp11DisPrice) - (cGmp11Qty * cGmp11Price));
			objForm.Price2.value = formatCurrency(cGmp11DisPrice);
		}
		else
		{
			objForm.subTotalcGmp11.value = formatCurrency(cGmp11Qty * cGmp11Price);
			objForm.discCGMP11.value = 0.00;
			objForm.Price2.value = formatCurrency(cGmp11Price);
			
		}
	}
	else
		boolFlag = false;


	if (IsNumeric(objForm.CombinedQty.value))
	{
		CombinedQty = objForm.CombinedQty.value;
		if (CombinedQty >= gmpQualifier) //Discount applies
		{			
			objForm.subCombined.value = formatCurrency(CombinedQty * CombinedDisPrice);
			objForm.discCombined.value = formatCurrency((CombinedQty * CombinedDisPrice) - (CombinedQty * CombinedPrice));
			objForm.Price3.value = formatCurrency(CombinedDisPrice);

		}
		else
		{
			objForm.subCombined.value = formatCurrency(CombinedQty * CombinedPrice);
			objForm.discCombined.value = 0.00;
			objForm.Price3.value = formatCurrency(CombinedPrice);
		}
	}
	else
		boolFlag = false;

	
	if (IsNumeric(objForm.CustomQty.value))
	{
		CustomQty = objForm.CustomQty.value;
		if (CustomQty != "")
		{
			//Check to see if the minimum number is met
			if (CustomQty >= 1000)
			{
			
				if (CustomQty >= CustomQualifier) //Discount applies
				{
					objForm.subCustom.value = formatCurrency(CustomQty * CustomDisPrice);
					objForm.discCustom.value = formatCurrency((CustomQty * CustomDisPrice) - (CustomQty * CustomPrice));
					objForm.Price4.value = formatCurrency(CustomDisPrice);
				}
				else
				{
					objForm.subCustom.value = formatCurrency(CustomQty * CustomPrice);
					objForm.discCustom.value = 0.00;
					objForm.Price4.value = formatCurrency(CustomPrice);
				
				}
			}
			else
			{
				alert ("You must order 1000 units or more of the custom booklets");
				objForm.discCustom.value = "";
				objForm.subCustom.value = 0;
				unitFlag = false;
			}
		}
	}
	else
	{
		boolFlag = false;	
	}
	
	
	if (cGmpQty + cGmp11Qty + CombinedQty == 0)
		objForm.ShippingCost.value = 0.00;
	else
		objForm.ShippingCost.value = ShippingPrice;


	//Sum up the subtotals
	objForm.Total2.value = formatCurrency(parseFloat(objForm.subTotalcGmp.value) + parseFloat(objForm.subTotalcGmp11.value) + parseFloat(objForm.subCombined.value) + parseFloat(objForm.ShippingCost.value));

	if (objForm.subCustom.value != "")
	{
		objForm.gst.value = formatCurrency((parseFloat (objForm.Total2.value) + 0) * 0.05);
		objForm.pst.value = formatCurrency((parseFloat (objForm.Total2.value) + 0) * 0.08);
		objForm.finalTotal.value = formatCurrency(parseFloat (objForm.Total2.value) + parseFloat(objForm.subCustom.value) + parseFloat (objForm.gst.value) + parseFloat (objForm.pst.value));

	}
	else
	{
		objForm.gst.value = formatCurrency((parseFloat (objForm.Total2.value)) * 0.05);
		objForm.pst.value = formatCurrency((parseFloat (objForm.Total2.value) + parseFloat(objForm.subCustom.value)) * 0.08);
		objForm.finalTotal.value = formatCurrency(parseFloat (objForm.Total2.value) + parseFloat (objForm.gst.value) + parseFloat (objForm.pst.value));
	}

	objForm.Total2.value = formatCurrency(parseFloat(objForm.Total2.value) + parseFloat(objForm.gst.value)+ parseFloat(objForm.pst.value));

	return (unitFlag);
}

function SubmitForm()
{
	unitFlag = ValMultiForm ();
	
	submitFlag = true;
	if (document.myform.finalTotal.value != "")
	{
		if (!IsNumeric(document.myform.finalTotal.value) || (document.myform.finalTotal.value == 0))
			submitFlag = false;
	}
	
	if (unitFlag == false)
		submitFlag = false;

	return (submitFlag);
}
