function fieldStrip(objText,fldType) {
	var fldData=trim(objText);
	var iLen=String(fldData).length;
	var fmt='';
	if(iLen>0) {
		switch(fldType) {
			case 0: //numbers
				fmt=fldData.replace(/[^0-9]+/g,'');
				break;
			case 1: //floating point numbers
				fmt=fldData.replace(/[^0-9\.]+/g,'');
				break;
			case 2: //characters only
				fmt=fldData.replace(/[^a-zA-Z ]+/g,'');
				break;
			case 3: //alpha numeric
				fmt=fldData.replace(/[^a-zA-Z 0-9]+/g,'');
				break;
			default:
				fmt=fldData;
		}
	}
	return fmt;
}
function floor(num) {
//round a number to 2 decimal points
	return Math.round(num*100)/100;
}
function floor4(num) {
//round a number to 4 decimal points
	return Math.round(num*10000)/10000;
}
function checkNum(objText) { //forces empty values to zero
	var n2s=fieldStrip(objText.toString(),0);
	if (n2s.length==0 || n2s=='NaN' || isNaN(n2s)) return 0;
	return parseInt(n2s);
}
function checkFloat(objText) { //forces empty values to zero
	var n2s=fieldStrip(objText.toString(),1);
	if (n2s.length==0 || n2s=='NaN' || isNaN(n2s)) return 0;
	return parseFloat(n2s);
}
function dosum() {
	var objForm=document.calcform;
	
	var la=checkFloat(objForm.la.value);
	var mir=checkFloat(objForm.ir.value)/1200;
	var dur=checkNum(objForm.yr.value)*12;
	var at=checkFloat(objForm.at.value)/12;
	var ai=checkFloat(objForm.ai.value)/12;
	
	var base=1,mbase=1+mir;
	for (var i=0; i<dur; i++) base*=mbase;
	
	objForm.pi.value=formatCommas(floor(la*mir/(1-(1/base))));
	objForm.mt.value=formatCommas(floor(at));
	objForm.mi.value=formatCommas(floor(ai));
	var dasum=la*mir/(1-(1/base))+(at+ai);
	objForm.mp.value=formatCommas(floor(dasum));
	
	objForm.loan.value=la;
	objForm.term.value=dur;
	objForm.rate.value=checkFloat(objForm.ir.value);
	objForm.tax.value=at;
	objForm.ins.value=ai;
	objForm.pmi.value=0;
	objForm.pmnt.value=floor(dasum);
	
	this_process();
	
	var objiFrames=document.getElementsByTagName("iframe");
	objiFrames[0].style.height='380px';
	objiFrames[0].src='biweekly_chart.asp';
}

function dosum_fha() {
	var objForm=document.calcform;
	
	var la=checkFloat(objForm.la3.value);
	var mir=checkFloat(objForm.ir3.value)/1200;
	var dur=checkNum(objForm.yr3.value)*12;
	var at=checkFloat(objForm.at3.value)/12;
	var ai=checkFloat(objForm.ai3.value)/12;
	
	var fha_la=la+(la*0.0175);
	var fha_mi=la*0.0055;
	
	objForm.mmi3.value=formatCommas(floor(fha_la));
	
	var base=1,mbase=1+mir;
	for (var i=0; i<dur; i++) base*=mbase;
	
	objForm.pi3.value=formatCommas(floor(fha_la*mir/(1-(1/base))));
	objForm.mti3.value=formatCommas(floor(fha_mi/12));
	objForm.mt3.value=formatCommas(floor(at));
	objForm.mi3.value=formatCommas(floor(ai));
	
	var dasum=fha_la*mir/(1-(1/base))+(at+ai);
	objForm.mp3.value=formatCommas(floor(dasum+floor(fha_mi/12)));
	
	objForm.loan.value=la;
	objForm.term.value=dur;
	objForm.rate.value=checkFloat(objForm.ir3.value);
	objForm.tax.value=at;
	objForm.ins.value=ai;
	objForm.pmi.value=floor(fha_mi/12);
	objForm.pmnt.value=floor(floor(dasum+floor(fha_mi/12)));
	
	this_process();
	
	var objiFrames=document.getElementsByTagName("iframe");
	objiFrames[0].style.height='380px';
	objiFrames[0].src='biweekly_chart.asp';
}

function getIOValues() {
	var objForm=document.calcform;
	var la=checkFloat(objForm.la2.value);
	var ir=checkFloat(objForm.ir2.value);
	var io2=Math.floor(la*ir/12)/100;
	var mt2=floor(checkFloat(objForm.at2.value)/12);
	var mi2=floor(checkFloat(objForm.ai2.value)/12);
	objForm.io2.value=formatCommas(io2);
	objForm.mt2.value=formatCommas(mt2);
	objForm.mi2.value=formatCommas(mi2);
	objForm.mp2.value=formatCommas(io2+mt2+mi2);
	
	objForm.loan.value=0;
	objForm.term.value=0;
	objForm.rate.value=0;
	objForm.tax.value=0;
	objForm.ins.value=0;
	objForm.pmi.value=0;
	
	var objiFrames=document.getElementsByTagName("iframe");
	objiFrames[0].style.height='0';
	objiFrames[0].src='';
}

function blend() {
	var objForm=document.calcform,br=0;
	var tb=(1*objForm.b1.value)+(1*objForm.b2.value);
	if (objForm.b3.value>0) tb+=(1*objForm.b3.value);
	if (objForm.b4.value>0) tb+=(1*objForm.b4.value);
	if (objForm.b5.value>0) tb+=(1*objForm.b5.value);
	for (var c=1; c<5; c++) {
		br+=objForm.elements['r'+c].value*objForm.elements['b'+c].value/tb;
	}
	objForm.tb.value=pround(tb,'00');
	objForm.br.value=pround(br,'000');
	
	objForm.loan.value=0;
	objForm.term.value=0;
	objForm.rate.value=0;
	objForm.tax.value=0;
	objForm.ins.value=0;
	objForm.pmi.value=0;
	
	var objiFrames=document.getElementsByTagName("iframe");
	objiFrames[0].style.height='0';
	objiFrames[0].src='';
}
function prepaypen() {
	var objForm=document.calcform;
	var la=objForm.pla.value;
	var ir=objForm.pir.value;
	var c=ir*100;
	var p=parseFloat(la)+parseFloat(c);
	
	objForm.mpp.value=formatCommas(floor(c));
	objForm.tmpo.value=formatCommas(floor(p));
	
	objForm.loan.value=0;
	objForm.term.value=0;
	objForm.rate.value=0;
	objForm.tax.value=0;
	objForm.ins.value=0;
	objForm.pmi.value=0;
	
	var objiFrames=document.getElementsByTagName("iframe");
	objiFrames[0].style.height='0';
	objiFrames[0].src='';
}

function pround(val,sdec) {
	var dec=parseInt('1'+sdec);
	var newval=Math.round(dec*val);
	var dollars=Math.floor(newval/dec);
	var cents=newval-dollars*dec,centst='';
	if (cents==0) centst=sdec;
	else if (cents<10)  centst=Left(sdec,sdec.length-1)+cents;
	else if (cents<100) centst=Left(sdec,sdec.length-2)+cents;
	else centst=cents;
	return formatCommas(dollars+'.'+centst);
}

function Left(str,l) {
	if (l<=0) return '';
	else if (l>String(str).length) return str;
	else return String(str).substring(0,l);
}
//function formats a number to 2 decimals and can includes commas if full=true
//need to workout limitation of number of decimals
function formatdec(result,full) {
	var answer=floor(result).toString();
	if (answer.indexOf('.')== -1) {
		answer+='.00'; // append a ".00" to results like 123 so that it will be in currency format (123.00)
	} else if (answer.length-answer.indexOf(".")==2) {
		answer+='0'; // append a "0" to results like 123.4 so that it will be in currency format (123.40)
	}
	if (full) {
		var objRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
//check for match to search criteria
		while (objRegExp.test(answer)) {
//replace original string with first group match, a comma, then second group match
			answer=answer.replace(objRegExp, '$1,$2');
		}
	} return answer;
}
function formatCommas(answer) {
	var str=String(answer);
	var objRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
//check for match to search criteria
	while (objRegExp.test(str)) {
//replace original string with first group match, a comma, then second group match
		str=str.replace(objRegExp, '$1,$2');
	} return str;
}

function this_process() {
	var today1 = new Date();
	var today  = new Date(today1.getFullYear(), today1.getMonth(), today1.getDate(), 12, 0, 0, 0);
	var startdate1=Math.floor((today/86400000)-30);
	var startdate;
	var LastCount;
	for (Count=startdate1; Count<=startdate1+14; Count++) {
		if (Count % 14 == 2) startdate=Count;
	}
	
	function IsTripledDebit() {
		var date2 = new Date(LastCount*86400000);
		var currmonth=date2.getMonth();
		var IsTripleDebit;
		do {
			IsTripleDebit=date2.getDate();
			LastCount+=14;
			date2 = new Date(LastCount*86400000);
		} while (date2.getMonth()==currmonth);
		return (IsTripleDebit >= 29);
	}
	
	var objForm=document.calcform;
	var Length=checkNum(objForm.term.value);
	var LoanLengthYears=Length;
	var LoanLengthMonths=(LoanLengthYears*12);
	var origP=checkFloat(objForm.loan.value);
	var AnnualInterestRate=checkFloat(objForm.rate.value);
	var MonthlyInterestRate=AnnualInterestRate/1200.0;
	var CurrBal=0;
	var NumPaymentsMade=12;
	var mt=checkFloat(objForm.tax.value);
	var mi=checkFloat(objForm.ins.value);
	var pmi=checkFloat(objForm.pmi.value);
	var TaxesAndInsurance=mt+mi+pmi;
	var MonthlyPayment=checkFloat(objForm.pmnt.value);
	var PaymentNum=1;
	var Interest      =0.0;
	var TotalInterest =0.0;
	var Principle     =0.0;
	var TotalPrinciple=0.0;
	var RemainingPrinciple=origP;
	do {
		Interest=floor4(RemainingPrinciple*MonthlyInterestRate);
		TotalInterest+=Interest;
		Principle=MonthlyPayment-TaxesAndInsurance-Interest;
		
		RemainingPrinciple-=Principle;
		TotalPrinciple+=Principle;
		
		++PaymentNum;
	} while (RemainingPrinciple > 0 && PaymentNum <= LoanLengthMonths);
	
	var tti =TotalInterest/1; //+0.00000001;
	var mtpo=(PaymentNum-1-NumPaymentsMade);
	var ytpo=mtpo/12;
	
	LastCount=startdate;
	PaymentNum=1;
	TotalInterest =0.0;
	TotalPrinciple=0.0;
	RemainingPrinciple=origP;
	
	do {
		Interest=floor4(RemainingPrinciple*MonthlyInterestRate);
		TotalInterest+=Interest;
		Principle=MonthlyPayment-TaxesAndInsurance-Interest;
		
		if ((IsTripledDebit()) && (PaymentNum > NumPaymentsMade)) {
			RemainingPrinciple-=floor4(MonthlyPayment/2);
		}
		
		if ((CurrBal>0) && (PaymentNum==NumPaymentsMade)) {
			RemainingPrinciple=CurrBal;
		} else {
			RemainingPrinciple-=Principle;
		}
		TotalPrinciple+=Principle;
		
		++PaymentNum;
	} while (RemainingPrinciple > 0 && PaymentNum <= LoanLengthMonths);
	
	var tbwi  =TotalInterest;
	var mtpobw=(PaymentNum-1-NumPaymentsMade);
	var ytpobw=mtpobw/12;
	var InterestSaved=tti-tbwi;
	var PercentSaved =((tti-tbwi)/tti)*100;
	
	objForm.ctr.value=formatdec(ytpobw,false);
	objForm.cer.value=formatdec((tbwi*AnnualInterestRate)/tti,true);
}