//Copyright, 1999, by Digital Webspan, All Rights Reserved. 
//None of the material on this webpage can be reproduced in 
//any manner without the written permission of the copyright holder.  
//http://www.digitalwebspan.com
//info@digitalwebspan.com

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;


function calculate()  {
	clearVars();
	calcpremium();
	if(document.data.sales.value == 0) {  
		alert("Please enter a sales price!");
		document.data.sales.focus();
		return false; 
	}
	if (document.data.commissionPercent.value == "") {  
		alert("Please enter the commission as a percent.");
	    document.data.commissionPercent.focus();
	    return false;  
	}
	
	premium =  parseFloat(stringFilter(document.data.insPremium.value));
	salesPrice =  parseFloat(stringFilter(document.data.sales.value));
	taxesPriorYear = parseFloat(stringFilter(document.data.taxesPriorYear.value));
	
	if (document.data.commissionPercent.value == "") { 
		commissionDollar = parseFloat(stringFilter(document.data.commissionDollar.value));
		commissionPercent = parseFloat((commissionDollar / salesPrice) * 100);  
		document.data.commissionPercent.value = "%" + commissionPercent
	}
	
	commissionPercent = parseFloat(stringFilter(document.data.commissionPercent.value));
	commissionDollar = parseFloat((commissionPercent * .01) * salesPrice); 
	document.data.commissionPercent.value = stringFilter(document.data.commissionPercent.value);
  
	  
	documentStamps = parseFloat(salesPrice * DocStampPercentage);
	other = parseFloat(stringFilter(document.data.other.value));
	totalSettlement = (commissionDollar + documentStamps + other + premium + TitleInsuranceSearch + RecordingAndCourier);
	
	firstLoan = parseFloat(stringFilter(document.data.firstLoan.value));
	secondLoan = parseFloat(stringFilter(document.data.secondLoan.value));
	otherLoan = parseFloat(stringFilter(document.data.otherLoan.value));
	payoffs = firstLoan + secondLoan + otherLoan;
	
	dailyTaxRate = (taxesPriorYear / 365);
		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<366; i++) { 
		if (document.data.day.value == i)
			dayofmonth = i; 
	}
	totaldays = dayofyear + dayofmonth;	
	
	taxAccrual = parseFloat(dailyTaxRate * totaldays);
	totalDeduction = totalSettlement + payoffs + taxAccrual;
	netProceeds = salesPrice - totalDeduction;
	
	
	//Round Numbers and Format Currency
	document.data.insPremium.value = currency(stringFilter(document.data.insPremium.value));
	document.data.titleIns.value = currency(stringFilter(document.data.titleIns.value));
	document.data.recordingFees.value = currency(stringFilter(document.data.recordingFees.value));
	documentStamps = roundOff(documentStamps);
	document.data.documentStamps.value = currency(documentStamps);
	
	other = roundOff(other);
	document.data.other.value = currency(other);
	
	taxesPriorYear = roundOff(taxesPriorYear);
	document.data.taxesPriorYear.value = currency(taxesPriorYear);
	
	document.data.otherLoan.value = currency(otherLoan);
	document.data.firstLoan.value = currency(firstLoan);
	document.data.secondLoan.value = currency(secondLoan);
	
	salesPrice = roundOff(salesPrice);
	document.data.sales.value = currency(salesPrice);
	
	commissionDollar = roundOff(commissionDollar);
	documentStamps = roundOff(documentStamps);
	document.data.documentStamps.value =currency(documentStamps);
	
	totalSettlement =  roundOff(totalSettlement);
	document.data.totalSettlement.value =currency(totalSettlement);
	
	payoffs = roundOff(payoffs);
	document.data.totalPayoffs.value = currency(payoffs);
	
	dailyTaxRate = roundOff(dailyTaxRate);
	document.data.dailyTaxRate.value = currency(dailyTaxRate);
	
	totalDeduction = roundOff(totalDeduction);
	document.data.totalDeduction.value = currency(totalDeduction);
	
	taxAccrual = roundOff(taxAccrual);
	document.data.taxAccrual.value =currency(taxAccrual);
	
	netProceeds = roundOff(netProceeds);
	document.data.netProceeds.value = currency(netProceeds);
}

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

	if(salesPrice>0) taxable[0]=1
	if(salesPrice>100000) taxable[1]=1
	if(salesPrice>1000000) taxable[2]=1
	if(salesPrice>10000000) taxable[3]=1
	
	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] = 10000000 - 1000000} else {taxableamount[2] = salesPrice - taxableamount[0] - taxableamount[1]}
	if (taxable[3] > 0) {taxableamount[3] = salesPrice - 10000000} else {taxableamount[3] = salesPrice - taxableamount[0] - taxableamount[1] - taxableamount[2]}
	
	for(var i=0;i<=4;i++) {
//		alert('taxable[' + i + '] = ' + taxable[i] + ', taxableamount = ' + taxableamount[i])
		if (!isNaN(taxableamount[i]))
			ownersPolicy = ownersPolicy + (taxableamount[i] * rate[i]) }
	
	var searchFee = parseFloat(stringFilter(document.data.search.value));
	var examinationFee = parseFloat(stringFilter(document.data.examination.value));
	var closingFee = parseFloat(stringFilter(document.data.closingFee.value));
	var totalFees = searchFee + examinationFee + closingFee;
	
	if (ownersPolicy < totalFees)
		{ ownersPolicy = totalFees; }

	ownersPolicy = roundOff(ownersPolicy);	
	document.data.insPremium.value = currency(ownersPolicy);

}


function applyVars () {

	if (!isNaN(stringFilter(url['ownerspolicy'])))
		document.data.insPremium.value = stringFilter(url['ownerspolicy']); 
	if (!isNaN(stringFilter(url['salesprice'])))
		document.data.sales.value = stringFilter(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;
}