//----------------------------------------------------------------------------------------------------------------------
//JavaScript functions file for purchase.html
//Copyright (c) 2009 HJP Associates, Inc. All Rights Reserved.
//----------------------------------------------------------------------------------------------------------------------
function calcTotal(Total,Qty,Price) 
{
	if (Qty.value == "") {
		Total.value = ""; 
	}
	else {
		Total.value = Qty.value	* Price;
		document.PurchaseForm.Grand_Total.value = eval(document.PurchaseForm.Grand_Total.value) +
			eval(Total.value);
	}
}
//----------------------------------------------------------------------------------------------------------------------
function formatTotal(num) 
{
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num)) num = "0";
  cents = Math.floor((num*100+0.5)%100);
  num = Math.floor((num*100+0.5)/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 processForm() 
{
	if(isNaN(document.PurchaseForm.PayCalc_Qty.value) || 
		document.PurchaseForm.PayCalc_Qty.value.indexOf("-") > -1) document.PurchaseForm.PayCalc_Qty.value = 0;
	if(isNaN(document.PurchaseForm.PayCalc_Up_Qty.value) ||
		document.PurchaseForm.PayCalc_Up_Qty.value.indexOf("-") > -1)
			document.PurchaseForm.PayCalc_Up_Qty.value = 0;
	if(isNaN(document.PurchaseForm.PayCalc_SL_Qty.value) ||
		document.PurchaseForm.PayCalc_SL_Qty.value.indexOf("-") > -1)
			document.PurchaseForm.PayCalc_SL_Qty.value = 0;
	if(isNaN(document.PurchaseForm.FSG_Qty.value) || document.PurchaseForm.FSG_Qty.value.indexOf("-") > -1)
		document.PurchaseForm.FSG_Qty.value = 0;
	if(isNaN(document.PurchaseForm.TB1099_Qty.value) || 
		document.PurchaseForm.TB1099_Qty.value.indexOf("-") > -1) document.PurchaseForm.TB1099_Qty.value = 0;
	if(isNaN(document.PurchaseForm.TBW2_Qty.value) || document.PurchaseForm.TBW2_Qty.value.indexOf("-") > -1)
		document.PurchaseForm.TBW2_Qty.value = 0;	
	if(isNaN(document.PurchaseForm.DeprCalc_Qty.value) || document.PurchaseForm.DeprCalc_Qty.value.indexOf("-") > -1)
		document.PurchaseForm.DeprCalc_Qty.value = 0;
	document.PurchaseForm.Grand_Total.value = 0;		
	calcTotal(document.PurchaseForm.PayCalc_Total,document.PurchaseForm.PayCalc_Qty,79);
	calcTotal(document.PurchaseForm.PayCalc_Up_Total,document.PurchaseForm.PayCalc_Up_Qty,49);
	calcTotal(document.PurchaseForm.PayCalc_SL_Total,document.PurchaseForm.PayCalc_SL_Qty,29);
	calcTotal(document.PurchaseForm.FSG_Total,document.PurchaseForm.FSG_Qty,79);		
	calcTotal(document.PurchaseForm.TB1099_Total,document.PurchaseForm.TB1099_Qty,29);
	calcTotal(document.PurchaseForm.TBW2_Total,document.PurchaseForm.TBW2_Qty,29);
	calcTotal(document.PurchaseForm.DeprCalc_Total,document.PurchaseForm.DeprCalc_Qty,20);
	//calculate NJ sales tax if applicable
	if(document.PurchaseForm.Taxable.checked) {
		document.PurchaseForm.NJ_Sales_Tax.value = eval(document.PurchaseForm.Grand_Total.value) * 0.07; 
		document.PurchaseForm.Taxable.value = "Yes"; 
	}
	else {
		document.PurchaseForm.NJ_Sales_Tax.value = 0;
		document.PurchaseForm.Taxable.value = "No";
	}
	document.PurchaseForm.Grand_Total.value = eval(document.PurchaseForm.Grand_Total.value) +
		eval(document.PurchaseForm.NJ_Sales_Tax.value);
	//format dollar amount fields		
	if(document.PurchaseForm.PayCalc_Total.value != 0) {
		document.PurchaseForm.PayCalc_Total.value = formatTotal(document.PurchaseForm.PayCalc_Total.value);
	}
	else {
		document.PurchaseForm.PayCalc_Total.value = "";
	}
	if(document.PurchaseForm.PayCalc_Up_Total.value != 0) {
		document.PurchaseForm.PayCalc_Up_Total.value =formatTotal(document.PurchaseForm.PayCalc_Up_Total.value);
	}
	else {
		document.PurchaseForm.PayCalc_Up_Total.value = "";
	}
	if(document.PurchaseForm.PayCalc_SL_Total.value != 0) {
		document.PurchaseForm.PayCalc_SL_Total.value =formatTotal(document.PurchaseForm.PayCalc_SL_Total.value);
	}
	else {
		document.PurchaseForm.PayCalc_SL_Total.value = "";
	}
	if(document.PurchaseForm.FSG_Total.value != 0) {
		document.PurchaseForm.FSG_Total.value = formatTotal(document.PurchaseForm.FSG_Total.value);
	}
	else {
		document.PurchaseForm.FSG_Total.value = "";
	}
	if(document.PurchaseForm.TB1099_Total.value != 0) {
		document.PurchaseForm.TB1099_Total.value = formatTotal(document.PurchaseForm.TB1099_Total.value);
	}
	else {
		document.PurchaseForm.TB1099_Total.value = "";
	}
	if(document.PurchaseForm.TBW2_Total.value != 0) {
		document.PurchaseForm.TBW2_Total.value = formatTotal(document.PurchaseForm.TBW2_Total.value);
	}
	else {
		document.PurchaseForm.TBW2_Total.value = "";
	}
	if(document.PurchaseForm.DeprCalc_Total.value != 0) {
		document.PurchaseForm.DeprCalc_Total.value = formatTotal(document.PurchaseForm.DeprCalc_Total.value);
	}
	else {
		document.PurchaseForm.DeprCalc_Total.value = "";
	}
	if(document.PurchaseForm.NJ_Sales_Tax.value != 0) {
  		document.PurchaseForm.NJ_Sales_Tax.value = formatTotal(document.PurchaseForm.NJ_Sales_Tax.value);
	}
	else {
		document.PurchaseForm.NJ_Sales_Tax.value = "";
	}
	if(document.PurchaseForm.Grand_Total.value != 0) {
		document.PurchaseForm.Grand_Total.value = formatTotal(document.PurchaseForm.Grand_Total.value);
	}
	else {
		document.PurchaseForm.Grand_Total.value = "";
	}
}
//----------------------------------------------------------------------------------------------------------------------
function processCardNum() 
{
	temp = document.PurchaseForm.Card_Number.value;
	while (temp.indexOf(" ") > -1) {
		temp = temp.substring(0, temp.indexOf(" ")) + temp.substring(temp.indexOf(" ") + 1, temp.length);
	}
	if (isNaN(temp)) temp = ""; 
	if(temp.length == 16) {
		temp = temp.substring(0, 4) + " " +
					 temp.substring(4, 8) + " " +
					 temp.substring(8, 12) + " " +
					 temp.substring(12, 16);
	}
	else {
		temp = "INVALID";
	}
	document.PurchaseForm.Card_Number.value = temp;
}
//----------------------------------------------------------------------------------------------------------------------
function processSecurityCode() 
{	
	if (isNaN(document.PurchaseForm.Security_Code.value) ||
		document.PurchaseForm.Security_Code.value.indexOf("-") > -1 || 
		document.PurchaseForm.Security_Code.value.indexOf("+") > -1) document.PurchaseForm.Security_Code.value = "INVALID";
		
	if (document.PurchaseForm.Security_Code.value.length != 3)
		document.PurchaseForm.Security_Code.value = "INVALID";
}
//----------------------------------------------------------------------------------------------------------------------
function processZipCode(zipIndex) 
{	
	if (zipIndex == 1) {
		work = document.PurchaseForm.Billing_Zip_Code;
	}
	else {
		work = document.PurchaseForm.Zip_Code;
	}
	if (isNaN(work.value) || work.value.indexOf("-") > -1 || work.value.indexOf("+") > -1) work.value = "INVALID";
		
	if (work.value.length != 5) work.value = "INVALID";
}
//----------------------------------------------------------------------------------------------------------------------
function sameInfo() 
{
	if (document.PurchaseForm.Same_Info.checked) {
		document.PurchaseForm.Customer_Name.value = document.PurchaseForm.Card_Holder.value;
		document.PurchaseForm.Address.value = document.PurchaseForm.Billing_Address.value;
		document.PurchaseForm.City.value = document.PurchaseForm.Billing_City.value;
		document.PurchaseForm.State.value = document.PurchaseForm.Billing_State.value;
		document.PurchaseForm.Zip_Code.value = document.PurchaseForm.Billing_Zip_Code.value;
	}
	else {
		document.PurchaseForm.Customer_Name.value = "";
		document.PurchaseForm.Address.value = "";
		document.PurchaseForm.City.value = "";
		document.PurchaseForm.State.value = "0";
		document.PurchaseForm.Zip_Code.value = "";	
	}
}
//----------------------------------------------------------------------------------------------------------------------
function submitForm() 
{
	if (confirm("Select Ok to process your order.\nSelect Cancel to return to the form."))
		document.PurchaseForm.submit();
}
//----------------------------------------------------------------------------------------------------------------------
function cancelForm() 
{
	if (confirm("Select Ok to cancel your order.\nSelect Cancel to return to the form.")) {
		document.PurchaseForm.reset();
		window.location = "http://www.hjpassociates.com/";
	}
}
