// JavaScript Document

//** Uses rescore_1.0.js**//

function roundVal(val){
	var dec = 2;
	var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
	var retResult = formatNumber(result,2);
	return retResult;
}

function formatNumber(myNum, numOfDec) {
	var decimal = 2
	for(i=1; i<=numOfDec;i++)
    decimal = decimal *10
	var myFormattedNum = (Math.round(myNum * decimal)/decimal).toFixed(numOfDec)
    //alert(myNum + "   " +  myFormattedNum)
	return(myFormattedNum);
} 

function roundUp(iNumber) {
	var fNumber = Math.ceil(iNumber/1000);
	var cNumber = 1000*fNumber;
	return cNumber;
}

//calculate total amt & state deed tax
function calculate() {

	var countySelect
	
	if (document.resBuyForm.hennepin[0].checked == true)  {
		countySelect = document.resBuyForm.hennepin[0].value
	} else {
		countySelect = document.resBuyForm.hennepin[1].value
	}
	
	//get mortage tax amt
	mortgageAmt = document.getElementById('mortgageAmt').value;	
	mortgageAmtNoComma = mortgageAmt.replace(/\,/g,'');
	// N.U. newMortgageAmt = roundUp(mortgageAmtNoComma);
	newMortgageAmt = parseFloat(mortgageAmtNoComma);

	if(countySelect == "yes") {
		mortRegTax = (newMortgageAmt * 0.24) / 100;
	} else {
		mortRegTax = (newMortgageAmt * 0.23) / 100;	
	}
	
	//mortTax = roundVal(mortRegTax).toString()
    //document.getElementById('mortRegTax').value = roundVal(mortTax);
    document.getElementById('mortRegTax').value = roundVal_v2_1(mortRegTax);

	//Get Lender Title Insurance Policy
	
	/*
	First $250,000, Policy Premium = (Loan Amount * 3 /1000) 
	Amount from $250,000.01 to $500,000, add ((Loan Amount-250,000) * 1.75/1000) 
	Amount from $500,000.01 to $1,000,000, add ((Loan Amount -500,000) * 1.50/1000) 
	Amount greater than $1,000,000, add ((Loan Amount -1,000,000) * 1.25/1000) 
	Keep doing this until the loan amount is covered 
	Round up to the next highest dollar 
	Minimum premium = $100.00 
	Example, loan amount of 175,000 would be:  (250,000 * 3/1000) + (250,000 * 1.75/1000) + (250,000) * 1.25/1000)  = 525 
	*/

	
	
	if (newMortgageAmt > 250000) {
		policyPremium = (250000 * 3) / 1000
		//250,000 - 500,000
		if (newMortgageAmt > 500000) {
			policyPremium = policyPremium + (250000 * 1.75)/1000
			//500,000 - 1,000,000
			if (newMortgageAmt > 1000000) {
				policyPremium = policyPremium + (500000 * 1.50)/1000
						//remainder
						if (newMortgageAmt > 1000000) {
							policyPremium = policyPremium + ((newMortgageAmt - 1000000)*1.25)/1000
						}
			//<=1,000,000
			} else {
				policyPremium = policyPremium + ((newMortgageAmt - 500000)*1.50)/1000
			}
		//<=500,000
		} else {
			policyPremium = policyPremium + ((newMortgageAmt - 250000)*1.75)/1000
		}
	// <250,000
	} else {
		policyPremium = (newMortgageAmt * 3) / 1000
	}

    if (policyPremium < 100 && policyPremium > 0) {
		lenderPolicyRnd = 100;
	} else {
		lenderPolicyRnd = policyPremium;
    }
	
	
	lenderPolicy = roundVal(lenderPolicyRnd).toString();	
	document.getElementById('lenderTitlePolicy').value = roundVal(lenderPolicy);

	
	
	
	//Get owner's policy premium
	/*
	Owner's Policy Premium 
	First $250,000, Policy Premium = (Sale Price* 3.25/1000) 
	Amount from $250,000.01 to $500,000, add ((Sale Price-100,000) * 2/1000) 
	Amount from $500,000.01 to $1,000,000, add ((Sale Price-150,000) * 1.75/1000) 
	Amount greater than $1,000,000, add ((Sale Price-1,000,000) * 1.5/1000) 
	Keep doing this until the sale price is covered 
	Round up to the next highest dollar 
	Minimum premium = $100.00 
	Example, Sale Price of 750,000 would be:  (250,000 * 3.25/1000) + (250,000 * 2/1000) + (250,000) * 1.75/1000)  = 525.00 
	*/
	
	salesAmt = document.getElementById('salesAmt').value;	
	salesAmtNoComma = salesAmt.replace(/\,/g,'');

	parseSalesAmt = parseInt(salesAmtNoComma);
	newSalesAmt = roundUp(salesAmtNoComma);


	if (newSalesAmt > 250000) {
		ownPolicyPremium = (250000 * 3.25) / 1000
		//250000-500,000
		if (newSalesAmt > 500000) {
			ownPolicyPremium = ownPolicyPremium + (250000 * 2)/1000
			//500,000-1,000,000
			if (newSalesAmt > 1000000) {
				ownPolicyPremium = ownPolicyPremium + (500000 * 1.75)/1000
						//remainder
						if (newSalesAmt > 1000000) {
							ownPolicyPremium = ownPolicyPremium + ((newSalesAmt - 1000000)*1.5)/1000
						}
			//<=1,000,000
			} else {
				ownPolicyPremium = ownPolicyPremium + ((newSalesAmt - 500000)*1.75)/1000
			}
		//<=500,000
		} else {
			ownPolicyPremium = ownPolicyPremium + ((newSalesAmt - 250000)*2)/1000
		}
	// <250,000
	} else {
		ownPolicyPremium = (newSalesAmt * 3.25) / 1000
	}
	
	if (ownPolicyPremium < 100) {
		ownPolicyRnd = 100;
	} else {
		ownPolicyRnd = ownPolicyPremium;

	}
	
	
	ownerPolicy = roundVal(ownPolicyRnd).toString();
	
	
	
	
	//get combination policy
	/*
	Combination Policy 
	Owners's Policy Premium calculation + $100.00  - LenderPolicy
	*/

	if (newMortgageAmt == 0)
	    ownerPolicy = parseFloat(ownerPolicy);
	else
	    ownerPolicy = ownerPolicy - lenderPolicy + 100;	

	// JMJ, 1/23/11:  override for owner Policy calculation
	/* If SaleAmount > 0 and MortgageAmount > SalesAmount, the owner policy will be set to 0
	   If SaleAmount = 0 and MortageAmount > 0, the owner policy will be set to 0 	
	*/

	//document.getElementByID('optionalPolicyComment').value = "";
	
	if (newSalesAmt > 0 && newMortgageAmt > newSalesAmt) {
			ownerPolicy = 0;
			//document.getElementByID('optionalPolicyComment').value = "Please call for a quote";
	}
	if (newSalesAmt == 0 && newMortgageAmt > 0) 
			ownerPolicy = 0;



    //	ownerPolicyRnd = Math.round(ownPolicyPremium);
	
    //	if (policyPremium > 0) {
    //		comboRate = ownerPolicyRnd + 100;
    //	} else {
    //		comboRate = ownerPolicyRnd;
    //	}
	
	
	
    //	ownerPolicy = comboRate - lenderPolicyRnd
	
	//ownerPolicyRnd = Math.round(ownPolicyPremium);
	//ownerPolicy = ownerPolicyRnd.toString() + ".00";
	
	
	document.getElementById('optionalPolicy').value = roundVal(ownerPolicy);
	
	
	//get total closing costs
	
	totalCost = formatNumber(ownerPolicy + 870 -(lenderPolicy * -1), 2);
	totalCostStr = totalCost.toString();
	
	document.getElementById('totalCosts').value = totalCostStr;
	
	gvtCost = mortRegTax + 97;
	gvtCostStr = roundVal(gvtCost).toString();
	
	document.getElementById('governmentCost').value = gvtCostStr;	

}
