NS4 = (document.layers) ? true : false;

function checkEnter1(event)
{ 	
	var code = 0;
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	if (code==13)
		Calculate('Calculator1');
}
function checkEnter2(event)
{ 	
	var code = 0;
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	if (code==13)
		Calculate('Calculator2');
}


function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	return num;
}
function dollarValue(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}

function percentValue(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	i=i*100
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s+'%';
}

function decimalValue(strValue,places)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);
	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	return dblValue + '.' + strCents;
	
}

function writeSpan(formName,field,value) {
	field=formName+field;
	value="<B>" + value + "</B>";
	if (document.layers) {
		document.layers[field].document.write(value);
		document.layers[field].document.close();
	}
	else if (document.all) {
		document.all[field].innerHTML = value;
	}
	else {
		//alert(field);
		document.getElementById(field).innerHTML=value;
	}
	return true;
}

function adjustPrice(formName) {
	if (!isFloat(document.forms[formName].elements['CapitalizationRate'].value)) {
		alert("Please enter a value for capitalization rate (i.e. 6.5).");
		return false;
	}
	else {
		CapitalizationRateValue=document.forms[formName].elements['CapitalizationRate'].value/100;
	}
	PropertyPriceValue=formatCurrency(parseFloat(AnnualRentValue)/parseFloat(CapitalizationRateValue));
	document.forms[formName].elements['PropertyPrice'].value=dollarValue(PropertyPriceValue);
}

function adjustPrice2(formName) {
	if (!isFloat(formatCurrency(document.forms[formName].elements['PropertyPrice'].value))) {
		alert("Please enter a value for property price in dollars.");
		return false;
	}
	else {
		PropertyPriceValue=formatCurrency(document.forms[formName].elements['PropertyPrice'].value);
	}
	CapitalizationRateValue=(parseFloat(AnnualRentValue)/parseFloat(PropertyPriceValue))*100;
	document.forms[formName].elements['CapitalizationRate'].value=decimalValue(CapitalizationRateValue);
}

function adjustDown(formName) {
	if (!isFloat(formatCurrency(document.forms[formName].elements['PropertyPrice'].value))) {
		alert("Please enter a value for property price in dollars.");
		return false;
	}
	else {
		PropertyPriceValue=formatCurrency(document.forms[formName].elements['PropertyPrice'].value);
	}
	if (!isFloat(document.forms[formName].elements['LoanToValueRate'].value)) {
		alert("Please enter a value for the loan to value ratio (i.e. 25).");
		return false;
	}
	else {
		LoanToValueRateValue=document.forms[formName].elements['LoanToValueRate'].value/100;
	}
	DownPaymentValue=formatCurrency(parseFloat(PropertyPriceValue)*(1-parseFloat(LoanToValueRateValue)));
	document.forms[formName].elements['DownPayment'].value=dollarValue(DownPaymentValue);
}

function adjustDown2(formName) {
	if (!isFloat(formatCurrency(document.forms[formName].elements['PropertyPrice'].value))) {
		alert("Please enter a value for property price in dollars.");
		return false;
	}
	else {
		PropertyPriceValue=formatCurrency(document.forms[formName].elements['PropertyPrice'].value);
	}
	if (!isFloat(formatCurrency(document.forms[formName].elements['DownPayment'].value))) {
		alert("Please enter a value for down payment in dollars.");
		return false;
	}
	else {
		DownPaymentValue=formatCurrency(document.forms[formName].elements['DownPayment'].value);
	}
	LoanToValueRateValue=((parseFloat(PropertyPriceValue)-parseFloat(DownPaymentValue))/parseFloat(PropertyPriceValue))*100;
	//LoanToValueRateValue=1-LoanToValueRateValue;
	document.forms[formName].elements['LoanToValueRate'].value=decimalValue(LoanToValueRateValue);
}

function adjustFee(formName) {
	if (!isFloat(document.forms[formName].elements['MortgageFeePercent'].value)) {
		alert("Please enter a value for the mortgage fee percent (i.e. 0.75).");
		return false;
	}
	else {
		MortgageFeePercentValue=document.forms[formName].elements['MortgageFeePercent'].value/100;
	}
	MortgageFeeDollarValue=formatCurrency(parseFloat(MortgageBalanceValue)*parseFloat(MortgageFeePercentValue));
	document.forms[formName].elements['MortgageFeeDollar'].value=dollarValue(MortgageFeeDollarValue);
}

function adjustFee2(formName) {
	if (!isFloat(formatCurrency(document.forms[formName].elements['MortgageFeeDollar'].value))) {
		alert("Please enter a value for mortgage value in dollars.");
		return false;
	}
	else {
		MortgageFeeDollarValue=formatCurrency(document.forms[formName].elements['MortgageFeeDollar'].value);
	}
	
	MortgageFeePercentValue=(parseFloat(MortgageFeeDollarValue)/parseFloat(MortgageBalanceValue))*100;
	document.forms[formName].elements['MortgageFeePercent'].value=decimalValue(MortgageFeePercentValue);
}


function Calculate(formName) {

	writeSpan(formName,'MortgageBalance','');
	writeSpan(formName,'LoanConstant','');
	writeSpan(formName,'PandI','');
	//writeSpan(formName,'MortgageFee','');
	writeSpan(formName,'TotalDownPayment','');
	//writeSpan(formName,'ReturnOnEquity','');
	writeSpan(formName,'DebtCoverageRatio','');
	writeSpan(formName,'AnnualCashReturn','');
	writeSpan(formName,'CashOnCashReturn','');
	
	if (!isFloat(formatCurrency(document.forms[formName].elements['PropertyPrice'].value))) {
		alert("Please enter a value for property price in dollars.");
		return false;
	}
	else {
		PropertyPriceValue=formatCurrency(document.forms[formName].elements['PropertyPrice'].value);
	}
	
	if (!isFloat(document.forms[formName].elements['CapitalizationRate'].value)) {
		alert("Please enter a value for capitalization rate (i.e. 6.5).");
		return false;
	}
	else {
		CapitalizationRateValue=document.forms[formName].elements['CapitalizationRate'].value;
	}
	
	if (document.forms[formName].elements['DownPayment'].value.length == 0) {
		DownPaymentValue=0;
	}
	else {
		if (!isFloat(formatCurrency(document.forms[formName].elements['DownPayment'].value))) {
			alert("Please enter a value for down payment in dollars.");
			return false;
		}
		else {
			DownPaymentValue=formatCurrency(document.forms[formName].elements['DownPayment'].value);
		}
	}
	
	if (!isInteger(document.forms[formName].elements['Amortization'].value)) {
		alert("Please enter a whole value for amoritzation (i.e. 30).");
		return false;
	}
	else {
		AmortizationValue=document.forms[formName].elements['Amortization'].value;
	}
	
	if (!isFloat(document.forms[formName].elements['InterestRate'].value)) {
		alert("Please enter a value for interest rate (i.e. 6.5).");
		return false;
	}
	else {
		InterestRateValue=document.forms[formName].elements['InterestRate'].value;
	}
	
	if (!isFloat(formatCurrency(document.forms[formName].elements['LendersLegal'].value))) {
		alert("Please enter a valid dollar amount for lender's legal fees.");
		return false;
	}
	else {
		LendersLegalValue=formatCurrency(document.forms[formName].elements['LendersLegal'].value);
		document.forms[formName].elements['LendersLegal'].value=dollarValue(LendersLegalValue);
	}
	
	if (!isFloat(formatCurrency(document.forms[formName].elements['Appraisal'].value))) {
		alert("Please enter a value dollar amount for the appraisal fees.");
		return false;
	}
	else {
		AppraisalValue=formatCurrency(document.forms[formName].elements['Appraisal'].value);
		document.forms[formName].elements['Appraisal'].value=dollarValue(AppraisalValue);
	}
	
	document.forms[formName].elements['PropertyPrice'].value=dollarValue(PropertyPriceValue);
	document.forms[formName].elements['DownPayment'].value=dollarValue(DownPaymentValue);
	document.forms[formName].elements['MortgageFeeDollar'].value=dollarValue(MortgageFeeDollarValue);
	
	MortgageBalanceValue=formatCurrency(parseFloat(PropertyPriceValue)-parseFloat(DownPaymentValue));
	writeSpan(formName,'MortgageBalance',dollarValue(MortgageBalanceValue));
	
	pv=1;
	interestRate=parseFloat(InterestRateValue)/100;
	n=12*AmortizationValue;
	i=interestRate/12;
	
	pmt=((pv*i)/(1-Math.pow((1+i),(0-n))))*12;
	
	writeSpan(formName,'LoanConstant',percentValue(pmt));
	
	PandIValue=MortgageBalanceValue*pmt;
	writeSpan(formName,'PandI',dollarValue(PandIValue));
	
	//MortgageFeeValue=MortgageBalanceValue*0.0075;
	//writeSpan(formName,'MortgageFee',dollarValue(MortgageFeeValue));
	
	//TotalDownPaymentValue= MortgageFeeValue+LendersLegalValue+AppraisalValue;
	TotalDownPaymentValue= formatCurrency(parseFloat(DownPaymentValue)+parseFloat(MortgageFeeDollarValue)+parseFloat(AppraisalValue)+parseFloat(LendersLegalValue));
	writeSpan(formName,'TotalDownPayment',dollarValue(TotalDownPaymentValue));
	
	//ReturnOnEquityValue=AnnualRentValue/TotalDownPaymentValue;
	//writeSpan(formName,'ReturnOnEquity',percentValue(ReturnOnEquityValue));
	
	DebtCoverageRatioValue=AnnualRentValue/PandIValue;
	writeSpan(formName,'DebtCoverageRatio',decimalValue(DebtCoverageRatioValue));
	
	AnnualCashReturnValue=AnnualRentValue-PandIValue;
	writeSpan(formName,'AnnualCashReturn',dollarValue(AnnualCashReturnValue));
		
	CashOnCashReturnValue=AnnualCashReturnValue/TotalDownPaymentValue;
	writeSpan(formName,'CashOnCashReturn',percentValue(CashOnCashReturnValue));
}

function toggleCapRate(formName) {
	for (var i=0; i<document.forms[formName].UsePrice.length; i++) {
		if (document.forms[formName].UsePrice[i].value=="0") {
			document.forms[formName].UsePrice[i].checked=1;
		}
	}
	adjustPrice(formName);
}

function togglePrice(formName) {
	for (var i=0; i<document.forms[formName].UsePrice.length; i++) {
		if (document.forms[formName].UsePrice[i].value=="1") {
			document.forms[formName].UsePrice[i].checked=1;
		}
	}
	adjustPrice2(formName);
}

function toggleLoanValueRate(formName) {
	for (var i=0; i<document.forms[formName].UseDown.length; i++) {
		if (document.forms[formName].UseDown[i].value=="0") {
			document.forms[formName].UseDown[i].checked=1;
		}
	}
	adjustDown(formName);
}

function toggleDown(formName) {
	for (var i=0; i<document.forms[formName].UseDown.length; i++) {
		if (document.forms[formName].UseDown[i].value=="1") {
			document.forms[formName].UseDown[i].checked=1;
		}
	}
	adjustDown2(formName);
}

function toggleFeeDollar(formName) {
	for (var i=0; i<document.forms[formName].UseFeeDollar.length; i++) {
		if (document.forms[formName].UseFeeDollar[i].value=="1") {
			document.forms[formName].UseFeeDollar[i].checked=1;
		}
	}
	adjustFee2(formName);
}

function toggleFeePercent(formName) {
	for (var i=0; i<document.forms[formName].UseFeeDollar.length; i++) {
		if (document.forms[formName].UseFeeDollar[i].value=="0") {
			document.forms[formName].UseFeeDollar[i].checked=1;
		}
	}
	adjustFee(formName);
}

