
function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789-");
}

function allSSNDigits(str)
{
	return inValidCharSet(str,"0123456789-");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}

function validPhoneNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter a valid 10 digits phone number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
		else if(formField.value.length!=10)
		{
 			alert('Please enter a valid 10 digits phone number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
		
	} 
	
	return result;
}

function validZipCode(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter a valid 5 digits zip code for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
		else if(formField.value.length!=5)
		{
 			alert('Please enter a valid 5 digits zip code for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
		
	} 
	
	return result;
}

function validSSN(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
	if (result)
 	{
 		if (!allSSNDigits(formField.value) )//&& !allDigits(second.value)  && !allDigits(third.value))
 		{
 			alert('Please enter a valid SSN number ***-**-**** for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
		
		else if(formField.value.length!=11)
		{
 			alert('Please enter a valid SSN number ***-**-**** for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
		
	} 
	
	return result;
}

function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("/");
 		
 		result = (elems.length == 3); // should be three components
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	
	return result;
}

function validateForm1(theForm)
{
		
    if (!validRequired(theForm.first_name,"First Name"))
		return false;
	
	if (!validRequired(theForm.last_name,"Last Name"))
		return false;
	
	if (!validRequired(theForm.title,"Title"))
		return false;
		
	if (!validNum(theForm.SSN,"SSN",false))
		return false;

	if (!validRequired(theForm.street,"Address"))
		return false;
	
	if (!validRequired(theForm.city,"City"))
		return false;
	
	if (!validRequired(theForm.state,"State"))
		return false;
	
	if (!validNum(theForm.zip,"ZIP",true))
		return false;
		
	if (!validEmail(theForm.email,"Email Address",true))
		return false;
	
	if (!validNum(theForm.home_phone_1,"Home Phone Area Code",true))
		return false;
	if (!validNum(theForm.home_phone_2,"Home Phone",true))
		return false;
	if (!validNum(theForm.home_phone_3,"Home Phone",true))
		return false;
		
	if (!validNum(theForm.fax_1,"Fax",false))
		return false;
	if (!validNum(theForm.fax_2,"Fax",false))
		return false;
	if (!validNum(theForm.fax_3,"Fax",false))
		return false;	
		
	if (!validRequired(theForm.capital,"Capital"))
		return false;
	
	return true;
}

function validateForm2(theForm)
{
		
    if (!validRequired(theForm.first_name,"First Name"))
		return false;
	
	if (!validRequired(theForm.last_name,"Last Name"))
		return false;

	if (!validRequired(theForm.street,"Address"))
		return false;
	
	if (!validRequired(theForm.city,"City"))
		return false;
	
	if (!validRequired(theForm.state,"State"))
		return false;
	
		if (!validNum(theForm.home_phone_1,"Home Phone Area Code",true))
		return false;
	if (!validNum(theForm.home_phone_2,"Home Phone",true))
		return false;
	if (!validNum(theForm.home_phone_3,"Home Phone",true))
		return false;
	
	if (!validNum(theForm.zip,"ZIP",true))
		return false;
		
	if (!validEmail(theForm.email,"Email Address",true))
		return false;
			
	return true;
}

function validateForm3(theForm)
{
		
    if (!validRequired(theForm.first_name,"First Name"))
		return false;
	
	if (!validRequired(theForm.last_name,"Last Name"))
		return false;

		
	if (!validEmail(theForm.email,"Email Address",true))
		return false;
	
	if (!validNum(theForm.home_phone_1,"Home Phone Area Code",true))
		return false;
	if (!validNum(theForm.home_phone_2,"Home Phone",true))
		return false;
	if (!validNum(theForm.home_phone_3,"Home Phone",true))
		return false;		
	
	return true;
}

function validateForm4(theForm)
{
		
   	if (!validRequired(theForm.date,"Data"))
		return false;
		
    if (!validRequired(theForm.printName,"Your Name"))
		return false;
	
	if (!validRequired(theForm.address1,"Address Line 1"))
		return false;
	
	if (!validRequired(theForm.address2,"Address Line 2"))
		return false;

	if (!validRequired(theForm.homePhone,"Home Phone"))
		return false;

		
	if (!validEmail(theForm.email,"Email Address",true))
		return false;
			
	return true;
}



function validateForm(theForm)
{
   
	// Customize these calls for your form

	// Start ------->
	if (!validRequired(theForm.first_name,"First Name"))
		return false;
	
	if (!validRequired(theForm.last_name,"Last Name"))
		return false;
	
	/*if (!validNum(theForm.DOB_month,"Month of Birth"), true)
		return false;
	
	
	if (!validNum(theForm.DOB_date,"Date of Birth"), true)
		return false;
		
	if (!validNum(theForm.DOB_year,"Year of Birth"), true)
		return false;
	*/
	
	if (!validNum(theForm.SSN,"SSN",false))
		return false;
	
	if (!validNum(theForm.spouse_SSN,"Spouse's SSN",false))
		return false;
	
		
	if (!validRequired(theForm.address,"Address"))
		return false;
	
	if (!validRequired(theForm.city,"City"))
		return false;
	
	if (!validRequired(theForm.state,"State"))
		return false;
	
	if (!validNum(theForm.zip,"ZIP",true))
		return false;
	
	if (!validNum(theForm.R_phone_1,"Home Phone",true))
		return false;
	if (!validNum(theForm.R_phone_2,"Home Phone",true))
		return false;
	if (!validNum(theForm.R_phone_3,"Home Phone",true))
		return false;
	
	if (!validNum(theForm.B_phone_1,"Business Phone",false))
		return false;
	if (!validNum(theForm.B_phone_2,"Business Phone",false))
		return false;
	if (!validNum(theForm.B_phone_3,"Business Phone",false))
		return false;
		
	if (!validEmail(theForm.email,"Email Address",true))
		return false;
		
	if (!validNum(theForm.fax_1,"Fax",false))
		return false;	
	if (!validNum(theForm.fax_2,"Fax",false))
		return false;	
	if (!validNum(theForm.fax_3,"Fax",false))
		return false;	
		
	/*if (!validRequired(theForm.how_soon,"How Soon to Start?"))
		return false;
	
	if (!validRequired(theForm.GAI_1,"General Area of Intrest choice 1"))
		return false;
	
	if (!validRequired(theForm.GAI_2,"General Area of Intrest choice 1"))
		return false;
		*/
	
	if (!validRequired(theForm.R1_first_name,"Reference 1 First Name"))
		return false;
	
	if (!validRequired(theForm.R1_last_name,"Reference 1 Last Name"))
		return false;
	
	if (!validRequired(theForm.R1_addr,"Reference 1 Address"))
		return false;
	
	if (!validNum(theForm.R1_phone_1,"Reference 1 Phone",true))
		return false;
	if (!validNum(theForm.R1_phone_2,"Reference 1 Phone",true))
		return false;
	if (!validNum(theForm.R1_phone_3,"Reference 1 Phone",true))
		return false;
		
	if (!validRequired(theForm.R1_year,"Reference 1 Years Known"))
		return false;
		
	if (!validRequired(theForm.R1_rls,"Reference 1 Relastionship"))
		return false;
	
	if (!validRequired(theForm.R2_first_name,"Reference 2 First Name"))
		return false;
	
	if (!validRequired(theForm.R2_last_name,"Reference 2 Last Name"))
		return false;
		
	if (!validRequired(theForm.R2_addr,"Reference 2 Address"))
		return false;
	
	if (!validNum(theForm.R2_phone_1,"Reference 2 Phone",true))
		return false;
	if (!validNum(theForm.R2_phone_2,"Reference 2 Phone",true))
		return false;
	if (!validNum(theForm.R2_phone_3,"Reference 2 Phone",true))
		return false;
	
	if (!validRequired(theForm.R2_year,"Reference 2 Years Known"))
		return false;
		
	if (!validRequired(theForm.R2_rls,"Reference 2 Relastionship"))
		return false;
	
	//Employment_Data Validation
/*
	if (!validRequired(theForm.employer,"Your Employer"))
		return false;
	
	if (!validRequired(theForm.A_salary,"Annual_Salary"))
		return false;
		
	if (!validRequired(theForm.position,"Position"))
		return false;
	
	if (!validRequired(theForm.work_how_long,"How Long"))
		return false;
	
	if (!validRequired(theForm.responsibility,"Responsibility"))
		return false;
	*/	
	//Validation for Financial_Data

	if (!validRequired(theForm.cash_checkingAccount,"Cash in Checking Accounts"))
		return false;
	
	if (!validRequired(theForm.cash_savingAccount,"Cash in Saving Accounts"))
		return false;
	
	if (!validRequired(theForm.stocks_bonds,"Stocks and Bonds"))
		return false;
		
	if (!validRequired(theForm.IRA_RetirementPlan,"Date of Birth"))
		return false;
		
	if (!validRequired(theForm.RE_Home,"Real Estate Home"))
		return false;
	
	if (!validRequired(theForm.RE_Other,"Real Estate Other"))
		return false;
	
	if (!validRequired(theForm.automobile,"Automobile(s)"))
		return false;
	
	if (!validRequired(theForm.life_insurance,"Life Insurance Cash Value"))
		return false;
	
	if (!validRequired(theForm.money_due_you,"Money Due You"))
		return false;
	
	if (!validRequired(theForm.Pay_Bank,"Notes Payable to Banks"))
		return false;
	
	if (!validRequired(theForm.Pay_Others,"Notes Payable to Others"))
		return false;
		
	if (!validRequired(theForm.RE_Indetedness,"Real Estate Indebtedness"))
		return false;
		
	if (!validRequired(theForm.Automobile_Indebtedness,"Automobile(s) Indebtedness"))
		return false;
	
	if (!validRequired(theForm.Charge_Accounts,"Charge_Accounts"))
		return false;
	
	if (!validRequired(theForm.Credit_Cards,"Credit_Cards"))
		return false;
		
	
	if (!validRequired(theForm.Tax,"Taxes Payable"))
		return false;
	
	if (!validRequired(theForm.Owing_Life_Insurance,"Owing on Life Insurace"))
		return false;
	
	//check if all number
	
	/*if (!validNum(theForm.cash_checkingAccount,"Cash in Checking Accounts", true))
		return false;
	
	if (!validNum(theForm.cash_savingAccount,"Cash in Saving Accounts", true))
		return false;
	
	if (!validNum(theForm.stocks_bonds,"Stocks and Bonds", true))
		return false;
		
	if (!validNum(theForm.IRA_RetirementPlan,"Date of Birth", true))
		return false;
		
	if (!validNum(theForm.RE_Home,"Real Estate Home", true))
		return false;
	
	if (!validNum(theForm.RE_Other,"Real Estate Other", true))
		return false;
	
	if (!validNum(theForm.automobile,"Automobile(s)", true))
		return false;
	
	if (!validNum(theForm.life_insurance,"Life Insurance Cash Value", true))
		return false;
	
	if (!validNum(theForm.money_due_you,"Money Due You", true))
		return false;
	
	if (!validNum(theForm.Pay_Bank,"Notes Payable to Banks", true))
		return false;
	
	if (!validNum(theForm.Pay_Others,"Notes Payable to Others", true))
		return false;
		
	if (!validNum(theForm.RE_Indetedness,"Real Estate Indebtedness", true))
		return false;
		
	if (!validNum(theForm.Automobile_Indebtedness,"Automobile(s) Indebtedness", true))
		return false;
	
	if (!validNum(theForm.Charge_Accounts,"Charge_Accounts", true))
		return false;
	
	if (!validNum(theForm.Credit_Cards,"Credit_Cards", true))
		return false;
		
	
	if (!validNum(theForm.Tax,"Taxes Payable", true))
		return false;
	
	if (!validNum(theForm.Owing_Life_Insurance,"Owing on Life Insurace", true))
		return false;
	
	if (!validNum(theForm.other_asset1_value,"Other Assets1",false))
		return false;
		
	if (!validNum(theForm.other_asset2_value,"Other Assets1", false))
		return false;
		
	if (!validNum(theForm.other_asset3_value,"Other Assets1", false))
		return false;
		
	if (!validNum(theForm.Other_Liabilities1_Value,"Other Liablities1", false))
		return false;
		
	if (!validNum(theForm.Other_Liabilities2_Value,"Other Liablities2", false))
		return false;
		
	if (!validNum(theForm.Other_Liabilities3_Value,"Other Liablities3", false))
		return false;
		
	if (!validNum(theForm.Other_Liabilities4_Value,"Other Liablities4", false))
		return false;
		
	if (!validNum(theForm.Liquid_Capital,"Liquid Capital", true))
		return false;
	//end of checking special requirement for financial data	
	/*
	if (!validRequired(theForm.Partner_PersonalSource_Investment,"Do you have a financial Parter or any other personal source of investment capital?"))
		return false;
	
	if (!validRequired(theForm.How_Hear_Us,"How did you find SHIELD Security Systems?"))
		return false;
		*/
	return true;
}

function calculate_total1()
{

var cash_checkingAccount	= window.document.theForm.cash_checkingAccount.value;
var cash_savingAccount		= window.document.theForm.cash_savingAccount.value;
var stocks_bonds 			= window.document.theForm.stocks_bonds.value;
var IRA_RetirementPlan 		= window.document.theForm.IRA_RetirementPlan.value;	
var RE_Home					= window.document.theForm.RE_Home.value;
var RE_Other				= window.document.theForm.RE_Other.value;
var automobile				= window.document.theForm.automobile.value;
var life_insurance			= window.document.theForm.life_insurance.value;
var money_due_you			= window.document.theForm.money_due_you.value;
var other_asset1_value		= window.document.theForm.other_asset1_value.value;
var other_asset2_value		= window.document.theForm.other_asset2_value.value;
var other_asset3_value		= window.document.theForm.other_asset3_value.value;

cash_checkingAccount	-= 0; //the magic bit - 0  
cash_savingAccount 		-= 0; //the magic bit - 0  
stocks_bonds 		 	-= 0; //the magic bit - 0  
IRA_RetirementPlan		-= 0; //the magic bit - 0  
RE_Home					-= 0; //the magic bit - 0  
RE_Other				-= 0; //the magic bit - 0  
automobile				-= 0; //the magic bit - 0  
life_insurance			-= 0; //the magic bit - 0  
money_due_you			-= 0; //the magic bit - 0  
other_asset1_value			-= 0; //the magic bit - 0  
other_asset2_value			-= 0; //the magic bit - 0  
other_asset3_value			-= 0; //the magic bit - 0  

var total_asset	= cash_checkingAccount + cash_savingAccount + stocks_bonds + IRA_RetirementPlan	+ RE_Home + RE_Other + automobile +life_insurance + money_due_you + other_asset1_value + other_asset2_value + other_asset3_value;

window.document.theForm.total_asset.value = total_asset;
}

function calculate_total2()
{ 
var Pay_Bank				= window.document.theForm.Pay_Bank.value;
var Pay_Others 				= window.document.theForm.Pay_Others.value;
var RE_Indebtedness 		= window.document.theForm.RE_Indebtedness.value;
var Automobile_Indebtedness = window.document.theForm.Automobile_Indebtedness.value;
var Charge_Accounts			 = window.document.theForm.Charge_Accounts.value;
var Credit_Cards			 = window.document.theForm.Credit_Cards.value;
var Tax						 = window.document.theForm.Tax.value;
var Owing_Life_Insurance	 = window.document.theForm.Owing_Life_Insurance.value;
var Other_Liabilities1_Value	 = window.document.theForm.Other_Liabilities1_Value.value;
var Other_Liabilities2_Value	 = window.document.theForm.Other_Liabilities2_Value.value;
var Other_Liabilities3_Value	 = window.document.theForm.Other_Liabilities3_Value.value;
var Other_Liabilities4_Value	 = window.document.theForm.Other_Liabilities4_Value.value;

Pay_Bank 	-= 0;
Pay_Others 	-= 0;
RE_Indebtedness -=0;
Automobile_Indebtedness -=0;
Charge_Accounts -=0;
Credit_Cards -=0;
Tax -=0;
Owing_Life_Insurance -=0; 
Other_Liabilities1_Value -=0;
Other_Liabilities2_Value -=0;
Other_Liabilities3_Value -=0;
Other_Liabilities4_Value -=0;

var total_liability = Pay_Bank + Pay_Others + RE_Indebtedness + Automobile_Indebtedness + Charge_Accounts + Credit_Cards + Tax + Owing_Life_Insurance  + Other_Liabilities1_Value + Other_Liabilities2_Value + Other_Liabilities3_Value  + Other_Liabilities4_Value;
window.document.theForm.Total_Liability.value = total_liability;
}

function calculate_networth()
{
  //var total;
  var total1 = window.document.theForm.total_asset.value;
  var total2 = window.document.theForm.Total_Liability.value;
  
  total1 -=0;
  total2 -=0;
  
  var total = total1 - total2;

  window.document.theForm.Net_Worth.value = total;
}
