

var closingFee = 500;
var titleSearch = 60;
var titleExam = 0;
var endorse81 = 50;
var Form9EndorsementPercentage = 0.2;

var loanAmount = 0;
var ownersPolicy = 0;
var total = 0;
var mtgPremium = 0;
var endorse9 = 0;
var netPremium = 0;
var totalPremium = 0;
var current = 0;
var totalCredit = 0;

function calculate() {

	loanAmount = 0;
	ownersPolicy = 0;
	total = 0;
	mtgPremium = 0;
	endorse9 = 0;
	netPremium = 0;
	totalPremium = 0;
	current = 0;
	totalCredit = 0;

	if(stringFilter(document.refinance.loanAmount.value) == 0) {
		alert("Please enter the loan amount!");
		document.refinance.loanAmount.focus();
		return false; 
	}
	loanAmount = parseFloat(stringFilter(document.refinance.loanAmount.value));
	var taxable = new Array(5)
	var taxableamount = new Array(5)
	var rate = new Array(5)
	for(var i=0;i<=5;i++)
		taxable[i]=0
	
	rate[0] = .00575
	rate[1] = .005
	rate[2] = .0025
	rate[3] = .00225
	rate[4] = .002

	if(loanAmount>0) taxable[0]=1
	if(loanAmount>100000) taxable[1]=1
	if(loanAmount>1000000) taxable[2]=1
	if(loanAmount>5000000) taxable[3]=1
	if(loanAmount>10000000) taxable[4]=1
	
	if (taxable[1] > 0) {taxableamount[0] = 100000} else {taxableamount[0] = loanAmount}
	if (taxable[2] > 0) {taxableamount[1] = 1000000 - 100000} else {taxableamount[1] = loanAmount - taxableamount[0]}
	if (taxable[3] > 0) {taxableamount[2] = 5000000 - 1000000} else {taxableamount[2] = loanAmount - taxableamount[0] - taxableamount[1]}
	if (taxable[4] > 0) {taxableamount[3] = 10000000 - 5000000} else {taxableamount[3] = loanAmount - taxableamount[0] - taxableamount[1] - taxableamount[2]}
	if (taxable[4] > 0) {taxableamount[4] = loanAmount - 10000000} else {taxableamount[4] = loanAmount - taxableamount[0] - taxableamount[1] - taxableamount[2] - taxableamount[3]}
	
	for(var i=0;i<=5;i++) {
//		alert('taxable[' + i + '] = ' + taxable[i] + ', taxableamount = ' + taxableamount[i])
		if (!isNaN(taxableamount[i]))
			ownersPolicy = ownersPolicy + (taxableamount[i] * rate[i]) }
	
	current = parseFloat(stringFilter(document.refinance.current.value));
	if (current > loanAmount)
		current = loanAmount;
	if (current < 100001)
		totalCredit = ((current/1000) * 2.45);
	if (current > 100000)
		totalCredit = ((((current - 100000)/1000)  * 2) + 245);
	//if (totalCredit < 300)		//minimum?
	//  { totalCredit = 300; }
	
	netPremium = ownersPolicy - totalCredit; //same as mtg title prem.
	
	endorse9 = parseFloat((netPremium) * Form9EndorsementPercentage);
	//if (endorse9 > 0)
	//	endorse9 = 25;
	
	if (document.refinance.balloon.checked == true)
		total = total + 50;
	if (document.refinance.adjustable.checked == true)
		total = total + 50; 
	if (document.refinance.condo.checked == true)
		total = total + 50; 
	if (document.refinance.master.checked == true)
		total = total + 50; 
	
	totalPremium = netPremium + closingFee + titleSearch + titleExam + endorse81 + total + endorse9;
	//ownersPolicy = roundOff(ownersPolicy);
	endorse9 = roundOff(endorse9);
	totalCredit = roundOff(totalCredit);
	
	document.refinance.loanAmount.value = currency(loanAmount);
	document.refinance.premium.value = currency(ownersPolicy);
	document.refinance.current.value = currency(stringFilter(document.refinance.current.value));
	document.refinance.totalCredit.value = currency(totalCredit);
	document.refinance.netPremium.value = currency(netPremium);
	document.refinance.total.value = currency(total);
	document.refinance.mtgPremium.value = currency(netPremium);	
	document.refinance.endorse9.value = currency(endorse9);
	document.refinance.endorseOther.value = currency(total);
	document.refinance.totalPremium.value = currency(totalPremium);
}

function clearVars() {
	loanAmount = 0;
	ownersPolicy = 0;
	total = 0;
	mtgPremium = 0;

	endorse9 = 0;
	netPremium = 0;
	totalPremium = 0;
	current = 0;
	totalCredit = 0;
}
// JavaScript Document