function init()
{
  swapTxt(1);
  document.aspnetForm.borrow.focus();
}


function swapTxt(sID)
{
  var fTxt;

  if (sID == 2) {
    fTxt ="Maximum<br>Borrowed Amount";
    document.aspnetForm.borrow.value = "";
    }
  else {
    fTxt ="Monthly<br>Payment";
    document.aspnetForm.reqmonthlypay.value = "";
    }
  //not needed for netscape, but thought would keep some inital code here, just in case
  NS6 = (document.getElementById?true:false);
  NS4 = (document.layers?true:false);
  IE = (document.all?true:false);

  if (IE||NS6)
    finalTxt.innerHTML = fTxt;
  else {
    document.finalTxt.document.open();
    document.finalTxt.document.write(fTxt);
    document.finalTxt.document.close();
  }

  document.aspnetForm.finalval.value = "";

}


function calcPay()
{
  if (document.aspnetForm.borrow.value != "")
    calcMonthlyPay(document.aspnetForm);
  else if (document.aspnetForm.reqmonthlypay.value != "")
    calcBorrow(document.aspnetForm);
}


function CheckFloatField(field) {
	var val = field.value;

	// lop off trailing "0"s after a decimal point first
	if(val.indexOf(".") != -1) {
		while(val.charAt(val.length-1) == "0")
			val = val.substring(0,val.length-1);
		if(val.charAt(val.length-1) == ".")
			val = val.substring(0,val.length-1);
	}

	if("" + parseFloat(val) != val)
		return false;
	else
		return true;
}


function CheckIntField(field) {
	var val = field.value;

	if(isNaN(val))
		return false;
	else {
		field.value = '' + parseInt(val)
		return true;
	}
}


function CheckDollarField(field) {
	var flt = ReadDollarField(field);

	if(isNaN(flt))
		return false;
	else {
		str = FloatToDollarString(flt);
		field.value = str;
		return true;
	}
}


function ReadDollarField(field) {
	var str = field.value;
	if(str.charAt(0) == "$")
		str = str.substring(1, str.length);

	var pos = str.lastIndexOf(",");
	while(pos != -1) {
		str = str.substring(0,pos) + str.substring(pos+1, str.length);
		pos = str.lastIndexOf(",", pos);
	}

	return parseFloat(str);
}


function FloatToDollarString(flt) {
/*
	var str = "" + flt;

	// check for two decimal places after point
	var pos = str.indexOf(".");
	if(pos == -1)
		str += ".00";
	else {
		if(str.length > pos + 3) {
			str = "" + (parseFloat(Math.round(parseFloat(str) * 100)) / 100.0);
			pos = str.indexOf(".");
		}
		if(pos == -1)
			str += ".00";
		else
			while(pos > str.length - 3)
				str += "0";
	}
*/

	// round off to nearest dollar
	var str = "" + Math.round(flt)

	// add commas
	pos = str.length;  // str.indexOf(".");
	pos -= 4;
	while(pos >= 0) {
		str = str.substring(0,pos+1) + "," + str.substring(pos+1, str.length);
		pos -= 3;
	}

	return str;
}



function calcMonthlyPay(frm) {
        
	if (!CheckDollarField(frm.borrow)) {
		alert("Please enter valid dollar amount");
		frm.borrow.focus();
		return false;
		}
		
	var Principle  = ReadDollarField(frm.borrow);
	var AnnualInt  = parseFloat(frm.intYear.value);
	var MonthlyInt = AnnualInt / (12.0 * 100.0);
	var LenMonths  = parseInt(frm.term.value*12);
         
	if(MonthlyInt == 0)
		var MonthlyPay = Principle / LenMonths;
	else
		var MonthlyPay = Principle * ( MonthlyInt / ( 1 - Math.pow((1 + MonthlyInt), -LenMonths) ) );
	MonthlyPay = (MonthlyPay * 100) / 100;

	frm.finalval.value = Math.round(MonthlyPay);
}

function calcBorrow(frm) {

	if (!CheckDollarField(frm.reqmonthlypay)) {
		alert("Please enter valid dollar amount");
		frm.reqmonthlypay.focus();
		return false;
		}

	var Principle  = ReadDollarField(frm.reqmonthlypay);
	
	var AnnualInt  = parseFloat(frm.intYear.value);
	var MonthlyInt = AnnualInt / (12.0 * 100.0);
	var LenMonths  = parseInt(frm.term.value*12);

	if(MonthlyInt == 0)
		var borrowAmt = Principle * LenMonths;
	else
		var borrowAmt = Principle / ( MonthlyInt / ( 1 - Math.pow((1 + MonthlyInt), -LenMonths) ) );

	frm.finalval.value = Math.round(borrowAmt);
}


