var i = 0;
var dayofyear = 0;
var dayofmonth = 0;
var totaldays = 0;
var totalSettlement = 0;
var commissionDollar = 0;
var documentStamps = 0;
var other = 0;
var firstLoan = 0;
var secondLoan = 0;
var otherLoan = 0;
var payoffs = 0;
var taxAccrual = 0;
var netProceeds = 0;
var dailyTaxRate = 0;
var commissionPercent = 0;
var salesPrice = 0;
var taxesPriorYear = 0;

var TitleInsuranceSearch = 250;
var RecordingAndCourier = 30;
var DocStampPercentage = .007;


//

var closingFee=150;
var searchFee=75;
var examFee=125;
var titlePremium;
	
function CalcAll(){
   
   		var_loanAmount = parseFloat(numberFilter(document.data.salesPrice.value));
		var_prevLoan = parseFloat(numberFilter(document.data.CurrentCoverageAmt.value));
   		//if (var_loanAmount == 0){
		//	alert("You must enter a sales price!");
		//	document.data.salesPrice.select();
		//	return false;
		//}
   		//var_newAmount = document.data.salesPrice.value*1;
		
				if (var_loanAmount<=1000000){
			//if the price is under $100 thousand
			if (var_loanAmount<=100000){
				var originalPremium = 0.00575*var_loanAmount;
			//otherwise the price is over $100 thousand but under $1 million
			} else {
				var originalPremium = (0.00575*100000) + ( (var_loanAmount-100000) * 0.005);
			}
		//otherwise if the price is over $1 million 
		} else if (var_loanAmount>1000000 && var_loanAmount<=5000000){
			var_break3=var_loanAmount-1000000;
			var_break2=900000;
			var_break1=100000;
			var originalPremium = (0.00575*var_break1) + (var_break2*0.005) + (var_break3*0.00250);
		}
		
	if (var_prevLoan > var_loanAmount)
		var_prevLoan = var_loanAmount;
	if (var_prevLoan < 100001)
		var totalCredit = ((var_prevLoan/1000) * 2.45);
	if (var_prevLoan > 100000)
		var totalCredit = ((((var_prevLoan - 100000)/1000)  * 2) + 245);
		
		totalCredit=roundOff(totalCredit,2);
		
		// rates on new policy amount
		var_newAmount=var_loanAmount-var_prevLoan;
		
		//reissue rates
		if (var_prevLoan<=1000000){
			//if the price is under $100 thousand
			if (var_prevLoan<=100000){
				reissuePremium = 0.0033*var_prevLoan;
			//otherwise the price is over $100 thousand but under $1 million
			} else {
				reissuePremium = (0.0033*100000) + ( (var_prevLoan-100000) * 0.003);
			}
		//otherwise if the price is over $1 million 
		} else if (var_prevLoan>1000000 && var_prevLoan<=10000000){
			var_break3=var_prevLoan-1000000;
			var_break2=900000;
			var_break1=100000;
			reissuePremium = (0.0033*var_break1) + (var_break2*0.003) + (var_break3*0.002);
		}
		
		
		//original rates on new policy amount
		if (var_newAmount<=1000000){
			//if the price is under $100 thousand
			if (var_newAmount<=100000){
				titlePremium = 0.00575*var_newAmount;
			//otherwise the price is over $100 thousand but under $1 million
			} else {
				titlePremium = (0.00575*100000) + ( (var_newAmount-100000) * 0.005);
			}
		//otherwise if the price is over $1 million 
		} else if (var_newAmount>1000000 && var_newAmount<=5000000){
			var_break3=var_newAmount-1000000;
			var_break2=900000;
			var_break1=100000;
			titlePremium = (0.00575*var_break1) + (var_break2*0.005) + (var_break3*0.00250);
		}
		var newTitlePremium = originalPremium-totalCredit;
		//alert("Title Premium + Policy Premium: "+(parseInt(titlePremium)+parseInt(ownersPolicy)));
   		//=10% of the combined Owner's Title Policy Premium and Lender's Policy Premium.
		var ownersPolicy=newTitlePremium;
		document.data.reissueCredit.value = currency(roundOff(totalCredit, 2));
		document.data.newPolicyPremium.value = currency(roundOff(newTitlePremium, 2));
		document.data.CurrentCoverageAmt.value = currency(var_prevLoan);
}


//

function calculate(){
	clearVars();
	CalcAll();
	CalcAll();
	if(document.data.salesPrice.value == 0 || document.data.salesPrice.value == "$0.00"){  
		alert("Please enter a sales price!");
		document.data.salesPrice.select();
		return false; 
	}
	if (document.data.commissionPercent.value == "") {  
		alert("Please enter the commission as a percent.");
	    document.data.commissionPercent.select();
	    return false;  
	}
	
	premium =  parseFloat(numberFilter(document.data.newPolicyPremium.value));
	salesPrice =  parseFloat(numberFilter(document.data.salesPrice.value));
	taxesPriorYear = parseFloat(numberFilter(document.data.taxesPriorYear.value));

	if (document.data.commissionPercent.value == "") { 
		commissionDollar = parseFloat(numberFilter(document.data.commissionDollar.value));
		commissionPercent = parseFloat((commissionDollar / salesPrice) * 100);  
		document.data.commissionPercent.value = "%" + commissionPercent;
	}
	
	ReissueCredit = parseFloat(numberFilter(document.data.reissueCredit.value));
	document.data.reissueCredit.value = currency(ReissueCredit);
	
	var TotalTitleFees = premium+closingFee+searchFee+examFee;
	document.data.totalTitleFees.value = currency(TotalTitleFees);
	
	ClerkOfCourtFees = parseFloat(numberFilter(document.data.clerkOfCourtFees.value));
	document.data.clerkOfCourtFees.value = currency(ClerkOfCourtFees);
  
	  
	documentStamps = parseFloat(salesPrice * DocStampPercentage);
	
	var RecordingFees = 10;
	document.data.clerkOfCourtFees.value = currency(RecordingFees + documentStamps);
	
	commissionPercent = parseFloat(numberFilter(document.data.commissionPercent.value));
	commissionDollar = parseFloat((commissionPercent * .01) * salesPrice); 
	document.data.commissionPercent.value = numberFilter(document.data.commissionPercent.value);
	totalSettlement = (commissionDollar + documentStamps + premium + closingFee + searchFee + examFee + RecordingFees);
	firstLoan = numberFilter(document.data.firstLoan.value);
	firstLoan = parseFloat(firstLoan);
	secondLoan = parseFloat(numberFilter(document.data.secondLoan.value));
	payoffs = firstLoan + secondLoan;
		if (document.data.month.value == 1) dayofyear = 0;
	    else if (document.data.month.value == 2) dayofyear =31;
		else if (document.data.month.value == 3) dayofyear = 59;
		else if (document.data.month.value == 4) dayofyear = 90; 
		else if (document.data.month.value == 5) dayofyear = 120; 
		else if (document.data.month.value == 6) dayofyear = 151;
		else if (document.data.month.value == 7) dayofyear = 181;
		else if (document.data.month.value == 8) dayofyear = 212;
		else if (document.data.month.value == 9) dayofyear = 243;
		else if (document.data.month.value == 10) dayofyear = 273;
		else if (document.data.month.value == 11) dayofyear = 304;
		else if (document.data.month.value == 12) dayofyear = 334;
	
	for (i = 0; i<=365; i++) { 
		if (document.data.day.value == i)
			dayofmonth = i; 
	}
	
	totaldays = dayofyear + dayofmonth;	
	//if (totaldays>0){
	//	totaldays=totaldays-1;
	//}
	
	dailyTaxRate = (taxesPriorYear / 365);
	
	taxAccrual = parseFloat(dailyTaxRate * totaldays);
	
	totalDeduction = totalSettlement + payoffs + taxAccrual;
	netProceeds = salesPrice - totalDeduction;
	
	
	//Round Numbers and Format Currency
	document.data.newPolicyPremium.value = currency(numberFilter(document.data.newPolicyPremium.value));
	document.data.titleIns.value = currency(numberFilter(document.data.titleIns.value));
	document.data.recordingFees.value = currency(numberFilter(document.data.recordingFees.value));
	documentStamps = roundOff(documentStamps,2);
	document.data.documentStamps.value = currency(documentStamps);
	
	taxesPriorYear = roundOff(taxesPriorYear,2);
	document.data.taxesPriorYear.value = currency(taxesPriorYear);
	
	document.data.firstLoan.value = currency(firstLoan);
	document.data.secondLoan.value = currency(secondLoan);
	
	salesPrice = currency(roundOff(salesPrice,2));
	document.data.salesPrice.value = salesPrice;
	
	commissionDollar = currency(roundOff(commissionDollar,2));
	document.data.totalCommissions.value = commissionDollar;
	
	documentStamps = currency(roundOff(documentStamps,2));
	document.data.documentStamps.value =documentStamps;
	
	totalSettlement =  currency(roundOff(totalSettlement,2));
	document.data.totalSettlement.value =totalSettlement;
	
	payoffs = currency(roundOff(payoffs,2));
	document.data.totalPayoffs.value = payoffs;
	
	dailyTaxRate = currency(roundOff(dailyTaxRate,2));
	document.data.dailyTaxRate.value = dailyTaxRate;
	
	totalDeduction = currency(roundOff(totalDeduction,2));
	document.data.totalDeduction.value = totalDeduction;
	
	taxAccrual = currency(roundOff(taxAccrual,2));
	document.data.taxAccrual.value = taxAccrual;
	
	netProceeds = currency(roundOff(netProceeds,2));
	document.data.netProceeds.value = netProceeds;
}

function calcpremium() {
	
	var salesPrice = 0;
	var ownersPolicy = 0;
	
	if(document.data.salesPrice.value == 0) {  
		alert("Please enter a sales price!");
		document.data.salesPrice.focus();
		return false; 
	}
	salesPrice =  parseFloat(numberFilter(document.data.salesPrice.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

	if(salesPrice>0) taxable[0]=1;
	if(salesPrice>100000) taxable[1]=1;
	if(salesPrice>1000000) taxable[2]=1;
	if(salesPrice>5000000) taxable[3]=1;
	if(salesPrice>10000000) taxable[4]=1;
	
	rate[0] = 0.00575;
	rate[1] = 0.005;
	rate[2] = 0.0025;
	rate[3] = 0.00225;
	rate[4] = 0.002;
	
	if (taxable[1] > 0) {taxableamount[0] = 100000} else {taxableamount[0] = salesPrice}
	if (taxable[2] > 0) {taxableamount[1] = 1000000 - 100000} else {taxableamount[1] = salesPrice - taxableamount[0]}
	if (taxable[3] > 0) {taxableamount[2] = 5000000 - 1000000} else {taxableamount[2] = salesPrice - taxableamount[0] - taxableamount[1]}
	if (taxable[4] > 0) {taxableamount[3] = 10000000 - 5000000} else {taxableamount[3] = salesPrice - taxableamount[0] - taxableamount[1] - taxableamount[2]}
	if (taxable[4] > 0) {taxableamount[4] = salesPrice - 10000000} else {taxableamount[4] = salesPrice - 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]) }
	
	var searchFee = parseFloat(numberFilter(document.data.search.value));
	var examinationFee = parseFloat(numberFilter(document.data.examination.value));
	var closingFee = parseFloat(numberFilter(document.data.closingFee.value));
	var totalFees = searchFee + examinationFee + closingFee;
	
	if (ownersPolicy < totalFees)
		{ ownersPolicy = totalFees; }

	ownersPolicy = roundOff(ownersPolicy,2);	
	document.data.newPolicyPremium.value = currency(ownersPolicy);

}


function applyVars () {

	if (!isNaN(numberFilter(url['ownersPolicy'])))
		document.data.newPolicyPremium.value = numberFilter(url['ownersPolicy']); 
	if (!isNaN(numberFilter(url['salesPrice'])))
		document.data.salesPrice.value = numberFilter(url['salesPrice']); 		
}

function clearVars() {
	i = 0;
	dayofyear = 0;
	dayofmonth = 0;
	totaldays = 0;
	totalSettlement = 0;
	commissionDollar = 0;
	documentStamps = 0;
	other = 0;
	firstLoan = 0;
	secondLoan = 0;
	otherLoan = 0;
	payoffs = 0;
	taxAccrual = 0;
	netProceeds = 0;
	dailyTaxRate = 0;
	commissionPercent = 0;
	salesPrice = 0;
	taxesPriorYear = 0;
}