// JavaScript Document

	function Validate(theForm)
{
	var error = "";

	
	// validate the purchase price is entered and with the correct format
	var digits = "0123456789";
	
		if (theForm.purchase_price.value == "")
		{
			error += "Please enter a purchase price.\n";
		}
		
		for (var i = 0; i < theForm.purchase_price.value.length; i++)
		{
			temp = theForm.purchase_price.value.substring(i, i+1)
			
			if (digits.indexOf(temp) == -1 &&
			theForm.purchase_price.value != "")
			{
			error += "Please enter numbers only for Purchase Price. Do not enter dollar sign, commas or decimal.\n";
			}
		}
	//end validation for purchase price
	
	// validate a selection for loan purpose
		if (theForm.loan_purpose.options[0].selected == true)
		{
			error += "Please select a Loan Purpose from the drop-down list.\n";
		}
	// end validation for loan purpose
	
	// validate the hazard insurance is entered and with the correct format
	var digits = "0123456789.";
	
		if (theForm.hazard_ins.value == "")
		{
			error += "Please enter an estimated rate for Hazard Insurance. The default rate is .45%.\n";
		}
		
		for (var i = 0; i < theForm.hazard_ins.value.length; i++)
		{
			temp = theForm.hazard_ins.value.substring(i, i+1)
			
			if (digits.indexOf(temp) == -1 &&
			theForm.hazard_ins.value != "")
			{
			error += "Please enter numbers and decimal only for Hazard Insurance rate. Do not enter the percentage sign.\n";
			break;
			}
		}
	//end validation for hazard insurance rate
	
	// validate the tax information is entered and with the correct format
	var digits = "0123456789";
	
		if (theForm.taxes.value == "")
		{
			error += "Please enter an tax rate for Property Taxes. The default rate is 1%.\n";
		}
		
		for (var i = 0; i < theForm.taxes.value.length; i++)
		{
			temp = theForm.taxes.value.substring(i, i+1)
			
			if (digits.indexOf(temp) == -1 &&
			theForm.taxes.value != "")
			{
			error += "Please enter numbers and decimal only for the Property Tax rate. Do not enter the percentage sign.\n";
			break;
			}
	}
	//end validation for tax rate

	// validate a selection for state
		if (theForm.state.options[0].selected == true)
		{
			error += "Please select a State from the drop-down list.\n";
		}
	// end validation for state
	
	// validate the loan amount is entered and with the correct format
	
	var digits = "0123456789";
	
		if (theForm.loan_amount.value == "")
		{
			error += "Please enter a Loan Amount.\n";
		}
		
		for (var i = 0; i < theForm.loan_amount.value.length; i++)
		{
			temp = theForm.loan_amount.value.substring(i, i+1)
			
			if (digits.indexOf(temp) == -1 &&
			theForm.loan_amount.value != "")
			{
			error += "Please enter numbers only for Loan Amount. Do not enter dollar sign, commas or decimal.\n";
			break;
			}
		}
	//end validation for loan amount format

	// validate the interest rate is entered and with the correct format
	var digits = "0123456789.";
	
		if (theForm.rate.value == "")
		{
			error += "Please enter an Interest Rate.\n";
		}
		
		for (var i = 0; i < theForm.rate.value.length; i++)
		{
			temp = theForm.rate.value.substring(i, i+1)
			
			if (digits.indexOf(temp) == -1 &&
			theForm.rate.value != "")
			{
			error += "Please enter numbers and decimal only for Interest Rate. Do not enter the percentage sign.\n";
			break;
			}
		}
	//end validation for interest rate
	
	// validate the term is entered and with the correct format
	
	var digits = "0123456789";
	
		if (theForm.term.value == "")
		{
			error += "Please enter a Term.\n";
		}
		
		for (var i = 0; i < theForm.term.value.length; i++)
		{
			temp = theForm.term.value.substring(i, i+1)
			
			if (digits.indexOf(temp) == -1 &&
			theForm.term.value != "")
			{
			error += "Please enter numbers only for Term.\n";
			break;
			}
		}
		
	var months = theForm.term.value;
		if (months < 120)
		{
			error += "Minimum term is 10 years (120 months). Multiply the years by 12.\n";
		}
		
		if (months > 480)
		{
			error += "Maximum term is 40 years (480 months). Multiply the years by 12.\n";
		}
	//end validation for term
	
	// validate the credit score is entered and with the correct format
	
	var digits = "0123456789";
	
		if (theForm.credit_score.value == "")
		{
			error += "Please enter a Credit Score.\n";
		}
		
		for (var i = 0; i < theForm.credit_score.value.length; i++)
		{
			temp = theForm.credit_score.value.substring(i, i+1)
			
			if (digits.indexOf(temp) == -1 &&
			theForm.credit_score.value != "")
			{
			error += "Please enter numbers only for Credit Score.\n";
			break;
			}
		}
		
	var months = theForm.credit_score.value;
		if (months < 300)
		{
			error += "Minimum Credit Score is 300.\n";
		}
		
		if (months > 850)
		{
			error += "Maximum Credit Score is 850.\n";
		}
	//end validation for credit score

	//trap entry errors
	if (error != "")
	{
		alert(error);
		return (false);
	} 	
	
	//end entry errors
	
	var errorLoan = "";
	var LTV = ((theForm.loan_amount.value) / (theForm.purchase_price.value));

	//determine the max LTV
	//Arizona
	if (theForm.loan_amount.value <= 50000)
	{
		maxLTV = .9875;
	}
	
	if ((theForm.state.value == "az") && (theForm.loan_amount.value > 50000) && (theForm.loan_amount.value <= 125000))
	{
		maxLTV = .9765;
	}
	
	if ((theForm.state.value == "az") && (theForm.loan_amount.value > 125000))
	{
		maxLTV = .9715;
	}
	
	//California
	if ((theForm.state.value == "ca") && (theForm.loan_amount.value > 50000) && (theForm.loan_amount.value <= 125000))
	{
		maxLTV = .9765;
	}
	
	if ((theForm.state.value == "ca") && (theForm.loan_amount.value > 125000))
	{
		maxLTV = .9715;
	}
	
	//Colorado
	if ((theForm.state.value == "co") && (theForm.loan_amount.value > 50000) && (theForm.loan_amount.value <= 125000))
	{
		maxLTV = .9765;
	}
	
	if ((theForm.state.value == "co") && (theForm.loan_amount.value > 125000))
	{
		maxLTV = .9715;
	}
	
	//Idaho
	if ((theForm.state.value == "id") && (theForm.loan_amount.value > 50000) && (theForm.loan_amount.value <= 125000))
	{
		maxLTV = .9765;
	}
	
	if ((theForm.state.value == "id") && (theForm.loan_amount.value > 125000))
	{
		maxLTV = .9715;
	}

	//Illinois
	if ((theForm.state.value == "il") && (theForm.loan_amount.value > 50000) && (theForm.loan_amount.value <= 125000))
	{
		maxLTV = .9765;
	}
	
	if ((theForm.state.value == "il") && (theForm.loan_amount.value > 125000))
	{
		maxLTV = .9715;
	}

	//Indiana
	if ((theForm.state.value == "in") && (theForm.loan_amount.value > 50000) && (theForm.loan_amount.value <= 125000))
	{
		maxLTV = .9765;
	}
	
	if ((theForm.state.value == "in") && (theForm.loan_amount.value > 125000))
	{
		maxLTV = .9715;
	}
	
	//New Mexico
	if ((theForm.state.value == "nm") && (theForm.loan_amount.value > 50000) && (theForm.loan_amount.value <= 125000))
	{
		maxLTV = .9765;
	}
	
	if ((theForm.state.value == "nm") && (theForm.loan_amount.value > 125000))
	{
		maxLTV = .9715;
	}
	
	//Nevada
	if ((theForm.state.value == "nv") && (theForm.loan_amount.value > 50000) && (theForm.loan_amount.value <= 125000))
	{
		maxLTV = .9765;
	}
	
	if ((theForm.state.value == "nv") && (theForm.loan_amount.value > 125000))
	{
		maxLTV = .9715;
	}

	//Oregon
	if ((theForm.state.value == "or") && (theForm.loan_amount.value > 50000) && (theForm.loan_amount.value <= 125000))
	{
		maxLTV = .9765;
	}
	
	if ((theForm.state.value == "or") && (theForm.loan_amount.value > 125000))
	{
		maxLTV = .9715;
	}
	
	//Utah
	if ((theForm.state.value == "ut") && (theForm.loan_amount.value > 50000) && (theForm.loan_amount.value <= 125000))
	{
		maxLTV = .9765;
	}
	
	if ((theForm.state.value == "ut") && (theForm.loan_amount.value > 125000))
	{
		maxLTV = .9715;
	}
	
	//Washington
	if ((theForm.state.value == "wa") && (theForm.loan_amount.value > 50000) && (theForm.loan_amount.value <= 125000))
	{
		maxLTV = .9765;
	}
	
	if ((theForm.state.value == "wa") && (theForm.loan_amount.value > 125000))
	{
		maxLTV = .9715;
	}
	
	//Wisconsin
	if ((theForm.state.value == "wi") && (theForm.loan_amount.value > 50000) && (theForm.loan_amount.value <= 125000))
	{
		maxLTV = .9765;
	}
	
	if ((theForm.state.value == "wi") && (theForm.loan_amount.value > 125000))
	{
		maxLTV = .9715;
	}
	
	//Wyoming
	if ((theForm.state.value == "wy") && (theForm.loan_amount.value > 50000) && (theForm.loan_amount.value <= 125000))
	{
		maxLTV = .9765;
	}
	
	if ((theForm.state.value == "wy") && (theForm.loan_amount.value > 125000))
	{
		maxLTV = .9715;
	}
	
	//Alabama
	if ((theForm.state.value == "al") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Alaska
	if ((theForm.state.value == "ak") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Arkansas
	if ((theForm.state.value == "ar") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Connecticut
	if ((theForm.state.value == "ct") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Delaware
	if ((theForm.state.value == "de") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Florida
	if ((theForm.state.value == "fl") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Georgia
	if ((theForm.state.value == "ga") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Hawaii
	if ((theForm.state.value == "hi") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Iowa
	if ((theForm.state.value == "ia") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Kansas
	if ((theForm.state.value == "ks") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Kentucky
	if ((theForm.state.value == "ky") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Louisiana
	if ((theForm.state.value == "la") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Maine
	if ((theForm.state.value == "me") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Maryland
	if ((theForm.state.value == "md") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Massachusetts
	if ((theForm.state.value == "ma") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Michigan
	if ((theForm.state.value == "mi") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Minnesota
	if ((theForm.state.value == "mn") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Mississippi
	if ((theForm.state.value == "ms") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Missouri
	if ((theForm.state.value == "mo") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Montana
	if ((theForm.state.value == "mt") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Nebraska
	if ((theForm.state.value == "ne") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//New Hampshire
	if ((theForm.state.value == "nh") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//New Jersey
	if ((theForm.state.value == "nj") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//New York
	if ((theForm.state.value == "ny") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//North Carolina
	if ((theForm.state.value == "nc") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//North Dakota
	if ((theForm.state.value == "nd") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Ohio
	if ((theForm.state.value == "oh") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Oklahoma
	if ((theForm.state.value == "ok") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Pennsylvania
	if ((theForm.state.value == "pa") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Rhode Island
	if ((theForm.state.value == "ri") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//South Carolina
	if ((theForm.state.value == "sc") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//South Dakota
	if ((theForm.state.value == "sd") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Tennessee
	if ((theForm.state.value == "tn") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Texas
	if ((theForm.state.value == "tx") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Vermont
	if ((theForm.state.value == "vt") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Virginia
	if ((theForm.state.value == "va") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//West Virginia
	if ((theForm.state.value == "wv") && (theForm.loan_amount.value > 50000))
	{
		maxLTV = .9775;
	}
	
	//Rate/Term Refinance
	if (theForm.loan_purpose.value == "refi")
	{
		maxLTV = .97;
	}
	
	//Cashout Refinance
	if (theForm.loan_purpose.value == "cashout")
	{
		maxLTV = .95;
	}
	//end max LTV
	
	
	//determine if loan exceeds max LTV
	if (LTV > maxLTV)
	{
		var maxLoan = ((theForm.purchase_price.value) * (maxLTV))
		var capState = (theForm.state.value.toUpperCase())
		errorLoan += "The maximum loan amount for a purchase price of $"+(theForm.purchase_price.value)+" in "+(capState)+" is $"+maxLoan+".\n";
	}
	//end determing if loan exceeds max LTV
	
	//trap loan size error
	if (errorLoan != "")
	{
		alert(errorLoan);
		return (false);
	} 	
	else
	{
		return (true);
	}
}
