/*****************************************************************
********************* COPTRIGHT NOTICE ***************************
(C) COPYRIGHT UNION TITLE CO 2002
WRITTEN BY JAMES EHLY FOR LINCOLN WEBDESIGN 
*****************************************************************/

//changes the Refinance Rate
function changeRate(x,type) {
	document.calculator.RatePrcnt.value = x;
	premium("Refinance");
}

//formats for Currency $'s and ,'s
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

//calculates the Premium rate based on the highest amount given and the refinace rate
function premium(type) {
	//get values
	CalcType = type;
	SalePrice = document.calculator.Sale.value;
	LoanAmount = document.calculator.Loan.value;
	Prcnt = document.calculator.RatePrcnt.value;
	County = document.calculator.County[1].checked;
		
	//strip numbers of currencyFormat
	SalePrice = parseFloat(SalePrice.toString().replace(/\$|\,/g,''));
	LoanAmount = parseFloat(LoanAmount.toString().replace(/\$|\,/g,''));
	
	//Keep our original values stored in vars that are unaltered
	OrgSalePrice = SalePrice;
	OrgLoanAmount = LoanAmount;
	
	//set currency values
	document.calculator.Sale.value = formatCurrency(SalePrice);
	document.calculator.Loan.value = formatCurrency(LoanAmount);

	// see which amount is higher to base premium off of 
	if (CalcType == 'Sale') {
		if (LoanAmount >= SalePrice) {
			HighAmount = LoanAmount;
		} else {
			HighAmount = SalePrice;
		}
	} else if (CalcType == 'Refinance') {
		HighAmount = LoanAmount;
	}
	
	// round up the higher amount to the nearest thousand
	num = 1000;
	HighAmount = Math.ceil(HighAmount / num) * num
	LoanAmount = Math.ceil(LoanAmount / num) * num
	SalePrice = Math.ceil(SalePrice / num) * num
	
	if (CalcType == 'Refinance' && LoanAmount > SalePrice && SalePrice > 0 && Prcnt == .7) {
	// special refinance rate for new loan amt exeeding old loan amt and 0-5 years
		RefLimit = SalePrice;
		PastLimit = LoanAmount;
		
		// find rate for RefLimit @ 70%
		if (RefLimit <= 0.0) {
			premRate1 = "Invalid Amount";
		} else if (RefLimit > 0.0 && RefLimit <= 20000.0) {
			premRate1 = 100;
		} else if (RefLimit > 20000.0 && RefLimit <= 50000.0) {
			premRate1 = ((RefLimit - 20000.0) / 1000) * 3.5 + 100;
		} else if (RefLimit > 50000.0 && RefLimit <= 100000.0) {
			premRate1 = ((RefLimit - 50000.0) / 1000) * 3 + 205;
		} else if (RefLimit > 100000.0 && RefLimit <= 1000000.0) {
			premRate1 = ((RefLimit - 100000.0) / 1000) * 2 + 355;
		} else if (RefLimit > 1000000.0 && RefLimit <= 5000000.0) {
			premRate1 = ((RefLimit - 1000000.0) / 1000) * 1.5 + 2155;
		} else if (RefLimit > 5000000.0 && RefLimit <= 10000000.0) {
			premRate1 = ((RefLimit - 5000000.0) / 1000) * 1.1 + 8155;
		} else if (RefLimit > 10000000.0 && RefLimit <= 15000000.0) {
			premRate1 = ((RefLimit - 10000000.0) / 1000) * 1 + 13655;
        } else if (RefLimit > 15000000.0) {
			premRate1 = ((RefLimit - 15000000.0) / 1000) * .9 + 18655;
		}
		
		premRateA = premRate1 * .7;
		premRateB = premRate1 * 1;
		
		//if other County is selected add $52.50 fee
		if (County) {
			premRateA += (52.5 * .7);
		}
			
		//find rate for PastLimit @ 100%	
		if (PastLimit <= 0.0) {
			premRate2 = "Invalid Amount";
		} else if (PastLimit > 0.0 && PastLimit <= 20000.0) {
			premRate2 = 100;
		} else if (PastLimit > 20000.0 && PastLimit <= 50000.0) {
			premRate2 = ((PastLimit - 20000.0) / 1000) * 3.5 + 100;
		} else if (PastLimit > 50000.0 && PastLimit <= 100000.0) {
			premRate2 = ((PastLimit - 50000.0) / 1000) * 3 + 205;
		} else if (PastLimit > 100000.0 && PastLimit <= 1000000.0) {
			premRate2 = ((PastLimit - 100000.0) / 1000) * 2 + 355;
		} else if (PastLimit > 1000000.0 && PastLimit <= 5000000.0) {
			premRate2 = ((PastLimit - 1000000.0) / 1000) * 1.5 + 2155;
		} else if (PastLimit > 5000000.0 && PastLimit <= 10000000.0) {
			premRate2 = ((PastLimit - 5000000.0) / 1000) * 1.1 + 8155;
		} else if (PastLimit > 10000000.0 && PastLimit <= 15000000.0) {
			premRate2 = ((PastLimit - 10000000.0) / 1000) * 1 + 13655;
        } else if (PastLimit > 15000000.0) {
			premRate2 = ((PastLimit - 15000000.0) / 1000) * .9 + 18655;
		}
		
		premRateC = premRate2 * 1;
		premRate = premRateA + (premRateC - premRateB); // calc rates to get premRate
		
	} else {
	// calculate premium base on the higher amount
		if (HighAmount <= 0.0) {
			premRate = "Invalid Amount";
		} else if (HighAmount > 0.0 && HighAmount <= 20000.0) {
			premRate = 100;
		} else if (HighAmount > 20000.0 && HighAmount <= 50000.0) {
			premRate = ((HighAmount - 20000.0) / 1000) * 3.5 + 100;
		} else if (HighAmount > 50000.0 && HighAmount <= 100000.0) {
			premRate = ((HighAmount - 50000.0) / 1000) * 3 + 205;
		} else if (HighAmount > 100000.0 && HighAmount <= 1000000.0) {
			premRate = ((HighAmount - 100000.0) / 1000) * 2 + 355;
		} else if (HighAmount > 1000000.0 && HighAmount <= 5000000.0) {
			premRate = ((HighAmount - 1000000.0) / 1000) * 1.5 + 2155;
		} else if (HighAmount > 5000000.0 && HighAmount <= 10000000.0) {
			premRate = ((HighAmount - 5000000.0) / 1000) * 1.1 + 8155;
		} else if (HighAmount > 10000000.0 && HighAmount <= 15000000.0) {
			premRate = ((HighAmount - 10000000.0) / 1000) * 1 + 13655;
        } else if (HighAmount > 15000000.0) {
			premRate = ((HighAmount - 15000000.0) / 1000) * .9 + 18655;
		}
		
		//if other County is selected add $52.50 fee
		if (County) {
			premRate += 52.5;
		}
		
		premRate = premRate * Prcnt;
		
	}
	
	//can't have a value lower than 100
	if (premRate < 100.0) {
		premRate = 100;
	}
	
	//if sale calc and SalePrice > 0 and LoanAmount > 0 then add $75
	if (CalcType == "Sale" && SalePrice > 0 && LoanAmount > 0) {
		premRate += 75;
	}
	
	//if endorsement checkbox has been checked
	if(document.calculator.Endorsement.checked){
		premRate+=25;
	}
	
	//print the premium rate
	if (CalcType == "Sale" && (OrgSalePrice + OrgLoanAmount) < 20000.0) {
		document.calculator.Premium.value = "Call for Quote";
	} else if (CalcType == "Refinance" && (OrgSalePrice < 20000.0 || OrgLoanAmount < 20000.0)) {
		document.calculator.Premium.value = "Call for Quote";
	} else {
		document.calculator.Premium.value = formatCurrency(premRate);
	}	
}
