//Todo: change form validation to something nicer looking
var hideARMDiv = 0;

function formatLoanAnalyzerAmount(/*Form input obj*/ i){
	var amt = formatDollarAmount(i.value);
	if(amt.substring(0,1) == "$"){
		amt = amt.substring(1, amt.length);
	}
	i.value = amt;
}

function toggleARMFields(sel, hideARM){
	var val = sel.options[sel.selectedIndex].text;
	var el = document.getElementById("ARM");
	hideARMDiv = hideARM;

	if(val.indexOf("ARM") >= 0){
		el.style.display = "block";
		if (fel["index_rate"].selectedIndex == 0) {
			fel["margin"].disabled = true;
			fel["margin"].style.backgroundColor = '#ededee';
			fel["margin"].value = '';
		} else {
			fel["margin"].disabled = false;
			fel["margin"].style.backgroundColor = '#ffffff';
		}
	} else {
		if (hideARMDiv > 0)
		{
			el.style.display = "none";
			hideARMDiv = 1;
		}
	}
}

function validateAnalyzerForm(){
	var validated = true;
	if(fel["state"].selectedIndex == 0){
		document.getElementById('errorState').innerHTML = 'Please select State';
		validated = false;
	}
	if(fel["product"].selectedIndex == 0){
		document.getElementById('errorProduct').innerHTML = 'Please select Loan Type';
		validated = false;
	}

	if(fel["product"].options[fel["product"].selectedIndex].text.indexOf("ARM") >= 0){
		if(fel["margin"].value.length == 0 && fel["index_rate"].selectedIndex != 0){
			document.getElementById('errorMargin').innerHTML = 'Please enter Margin';
			validated = false;
		}
	}
	if(fel["interest"].value.length == 0 || isNaN(parseFloat(fel["interest"].value))){
		document.getElementById('errorInterest').innerHTML = 'Please enter Interest Rate';
		validated = false;
	}
	if(fel["principal"].value.replace(/[^0-9]/g, "").length == 0){
		document.getElementById('errorPrincipal').innerHTML = 'Please enter Loan Amount';
		validated = false;
	}
	if(fel["fees"].value.replace(/[^0-9]/g, "").length == 0){
		document.getElementById('errorFees').innerHTML = 'Please enter Fees';
		validated = false;
	}
	if(fel["points"].value.length == 0 || isNaN(parseFloat(fel["points"].value))){
		document.getElementById('errorPoints').innerHTML = 'Please enter Points';
		validated = false;
	}
	if(fel["property_value"].value.replace(/[^0-9]/g, "").length == 0){
		document.getElementById('errorPropertyValue').innerHTML = 'Please enter Property Value';
		validated = false;
	}
	if(fel["ownershipPeriod"].selectedIndex == 0){
		document.getElementById('errorPeriod').innerHTML = 'Please select years expected to keep this loan';
		validated = false;
	}
	var LTV = parseInt(fel["principal"].value) / parseInt(fel["property_value"].value);
	if(!isNaN(LTV) && LTV > 0.8){
		document.getElementById('property_value').innerHTML = 'The following offers are based on a Loan to Value (LTV) ratio of 80% or less. For loans with higher LTV check with lender for accurate pricing.';
		validated = false;
	}

	return validated;
}

function SetupTooltips() {
	if ($("MonthlyPmtTip")) { // quick and dirty if statement...
		var MonthlyPmt = $("MonthlyPmtTip");
		MonthlyPmt.setAttribute("title", "The estimated monthly payment on principle and interest based on loan amount entered and the listed Rate.  Please note that actual monthly payment may be higher due to escrows for tax and insurance.");
		var Rate = $("RateTip");
		Rate.setAttribute("title", "The note rate. This is what the monthly payments are based on.  Loan.com rates are valid for purchase and rate &amp; term refinance transactions. Rates may differ for cash-out refinance transactions.");
		var Points = $("PointsTip");
		Points.setAttribute("title", "Points are up-front interest paid to reduce the interest rate. 1 point is 1% of the loan amount. On a $100,000 loan, 1 point is $1,000. Loan.com points quoted include both discount and origination points.");
		var Fees = $("FeesTip");
		Fees.setAttribute("title", "Loan.com Fees quoted include all broker and/or lender fees as well as appraisal and credit report fees. Fees quoted do not include fees charged by the third parties.");
		var APR = $("APRTip");
		APR.setAttribute("title", "APR is an acronym for annual percentage rate. The APR is designed to measure the effective or actual cost of a loan over its life. Different lenders and brokers include different fees and use different formulas when computing APRs, so do not compare loans solely on the basis of APRs.");

		var AllTips = Array(MonthlyPmt, Rate, Points, Fees, APR);
		var ret = new Tips(AllTips, {offsets: {x:20, y:10}});
	}
}

window.addEvent("domready", function(){
	f = document.forms["Analyzer"];
	fel = f.elements;
	toggleARMFields(fel["product"], hideARMDiv);
	if (window.location.href.indexOf("mortgage-loan-calculator") != -1) {
	highlightFieldVal(f);
	formatLoanAmount(fel["principal"]);
	formatLoanAmount(fel["fees"]);
	formatLoanAmount(fel["property_value"]);
	SetupTooltips();
	}
});

