﻿// a.js
var new_rate=3.25; // April 21 2009
var monthly_factor=.003837985674810; // 4.65 % qualifying rate
var amount=0.00; //sum of all non mortgage debt.
var new_pay=0.00;
var sav_pay=0.00;
var car_pay=0.00;
var car_baL=0.00;
var personal_pay=0.00;
var personal_baL=0.00;
var visa_pay=0.00;
var visa_baL=0.00;
var mc_pay=0.00;
var mc_baL=0.00;
var capital_pay=0.00;
var capitaL_baL=0.00;
var ct_pay=0.00;
var ct_baL=0.00;
var other_pay=0.00;
var other_baL=0.00;
var total_pay=0.00;
var total_baL=0.00;
var mtg_pmt=0.00;
var mtg_baL=0.00;
var ltv_ratio=0.00;
var home_value=0.00;
var family_income=0.00;
var totaL_debt=0.00; // sum of mortgage + amount.
var qualified_new_pmt=0.00;
var tds_ratio=0.00;
var i=1; // global counter for colour_on functions
var timerID, timerID2; //process ID's for setTimeouts
function stripZeros(inputStr) {
	var result = inputStr
	while (result.substring(0,1) == "0") {
		result = result.substring(1,result.length)
}
	return result
}

function insert_commas(numberString)	{

var currentString = numberString;
var reverseString = "";
var i = 1;
var displayString = "";
var isDecimal = "false";
var Decimalpoint ;
var tempString = "";
var offset;

currentString = currentString.toString();
currentString = stripZeros(currentString);
Decimalpoint = currentString.indexOf(".");

if (Decimalpoint != -1) {
			isDecimal = "true";
offset = currentString.length - Decimalpoint;
switch (offset) {
	case 1:
{
	tempString = currentString.substring(0,Decimalpoint+1);
	tempString = tempString+"0"+"0";
	break;
}
	case 2:
 {
	tempString = currentString.substring(0,Decimalpoint+2)
	tempString = tempString+"0";
	break;
}
	default :
{
tempString = currentString.substring(0,Decimalpoint+3)
}
}
var cnt = Decimalpoint+3;
			for (var n=0; n>=cnt; n++) {
				var Char = tempString.charAt(cnt);
 				reverseString = reverseString + Char;
				cnt = cnt-1;
				}
			currentString = "";
			currentString = tempString;
}
for (var counter = currentString.length; counter >= 0; counter--) {
			if (isDecimal == "true") {
			i = -2;
			isDecimal = "false"
}
			var currentChar = currentString.charAt(counter);
			reverseString = reverseString + currentChar;
			if ((i==4) && (counter != 0)) {
			reverseString = reverseString + "," ;
			i=1;
	}
			i=i+1;
}


	for (var count = reverseString.length-1; count >=0; count--) {
	var curChar = reverseString.charAt(count);
	displayString = displayString + curChar;
}
numberString = displayString;
return numberString
}
function isNumeric(myString) {
        var dontCalculateFlag = 0
        var decimalFlag = 0
        for (var counter = 0; counter < eval(myString.length); counter++) {
                var myCurChar = myString.charAt(counter);
                if ((myCurChar < "0" || "9" < myCurChar) && myCurChar != ".") {
                        dontCalculateFlag = 1
                }
                if (myCurChar == "." && decimalFlag== 1) {
                        dontCalculateFlag = 1
                }
                if (myCurChar == "."){
                        decimalFlag = 1
                }
        }
        if (myString == "") {
                dontCalculateFlag = 1
        }
        if (dontCalculateFlag == 1)  {
                return false
        }
        else {
                return true
        }
}
function deFormat(myField) {
        var myString = myField; //grab the contents of the current field
        var myFinalString = ""; //declare the variable that holds the final string
        var doParseString = "false";
        var decimalFlag = "false";
        for (var counter = 0; counter < myString.length; counter++) {
                var myCurChar = myString.charAt(counter);
                if (decimalFlag == "false") {
                        myCharsAllowed="1234567890./";
                } else {
                        myCharsAllowed="1234567890/";
                }
                if ((myCurChar != "0") && (isNumeric(myCurChar))) {
                        doParseString = "true"
                }
                if (myCurChar == ".") {
                        decimalFlag = "true"
                }
                if (myCharsAllowed.indexOf(myCurChar) != -1 && doParseString == "true") {
                        myFinalString=myFinalString + myCurChar
                }
        }
        if ((myFinalString == "") || (myFinalString == ".")) {
                myFinalString="0";
        }
        myField=myFinalString;
return myField
}
function getObject(obj) {
	var theObj
	if (document.layers) {
		if (typeof obj == "string") {
			return document.layers[obj]
		} else {
			return obj
		}
	}
	if (document.all) {
		if (typeof obj == "string") {
			return document.all(obj).style
		} else {
			return obj.style
		}
	}
	if (document.getElementById) {
		if (typeof obj == "string") {
			return document.getElementById(obj).style
		} else {
			return obj.style
		}
	}
	return null
}
function setBGColor(obj, color) {
	var theObj = getObject(obj)
	if (theObj.bgColor) {
		theObj.bgColor = color
	} else if (typeof theObj.backgroundColor != "undefined") {
		theObj.backgroundColor = color
	}
}
function colour_on() {
if ( i < 7) {
setBGColor(document.forms[0].elements[i],"orange");
// 1750 msec delay before turning off
timerID=setTimeout("colour_off()",1750);
}
return
}
function colour_off(){
if (timerID) {
// kill colour_on process
clearTimeout(timerID)
}
if (timerID2) {
// kill colour_off process
clearTimeout(timerID2)
}
// reset object to default
setBGColor(document.forms[0].elements[i],"");
// Calculate Savings button found at 0
if ( i == 0 ) {
return
}
if ( i < 7 ) {
i = i + 1 ;
// final call
if ( i == 7 ){
i = 0;
}
timerID2 = setTimeout("colour_on()",700);
}
return
}
function document_Debt_Form_Car_Pay_onfocus() {
var obj = document.Debt_Form.Car_Pay;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Car_Pay.value = "";
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Car_BaL_onfocus() {
var obj = document.Debt_Form.Car_BaL;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Car_BaL.value = "";
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Car_Pay_onblur() {
var obj = document.Debt_Form.Car_Pay;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Car_Pay.blur();
document.Debt_Form.Car_Pay.value = deFormat(this.value);
total_pay = total_pay - car_pay; // subtract previous car payment
car_pay = parseFloat(this.value);
total_pay = total_pay + car_pay;
this.value = insert_commas(car_pay.toFixed(2));
document.Debt_Form.Total_Pay.value = insert_commas(total_pay.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Car_BaL_onblur() {
var obj = document.Debt_Form.Car_BaL;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Car_BaL.blur();
document.Debt_Form.Car_BaL.value = deFormat(this.value);
amount = amount - car_baL; // subtract previous car balance
car_baL = parseFloat(this.value);
amount = amount + car_baL;
this.value = insert_commas(car_baL.toFixed(2));
document.Debt_Form.Amount.value = insert_commas(amount.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Personal_Pay_onblur() {
var obj = document.Debt_Form.Personal_Pay;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Personal_Pay.blur();
document.Debt_Form.Personal_Pay.value = deFormat(this.value);
total_pay = total_pay - personal_pay;
personal_pay = parseFloat(this.value);
total_pay = total_pay + personal_pay;
this.value = insert_commas(personal_pay.toFixed(2));
document.Debt_Form.Total_Pay.value = insert_commas(total_pay.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Personal_BaL_onblur() {
var obj = document.Debt_Form.Personal_BaL;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Personal_BaL.blur();
document.Debt_Form.Personal_BaL.value = deFormat(this.value);
amount = amount - personal_baL;
personal_baL = parseFloat(this.value);
amount = amount + personal_baL;
this.value = insert_commas(personal_baL.toFixed(2));
document.Debt_Form.Amount.value = insert_commas(amount.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Visa_Pay_onblur() {
var obj = document.Debt_Form.Visa_Pay;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Visa_Pay.blur();
document.Debt_Form.Visa_Pay.value = deFormat(this.value);
total_pay = total_pay - visa_pay;
visa_pay = parseFloat(this.value);
total_pay = total_pay + visa_pay;
this.value = insert_commas(visa_pay.toFixed(2));
document.Debt_Form.Total_Pay.value=insert_commas(total_pay.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Visa_BaL_onblur() {
var obj = document.Debt_Form.Visa_BaL;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Visa_BaL.blur();
document.Debt_Form.Visa_BaL.value = deFormat(this.value);
amount = amount - visa_baL;
visa_baL = parseFloat(this.value);
amount = amount + visa_baL;
this.value = insert_commas(visa_baL.toFixed(2));
document.Debt_Form.Amount.value = insert_commas(amount.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_MC_Pay_onblur() {
var obj = document.Debt_Form.MC_Pay;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.MC_Pay.blur();
document.Debt_Form.MC_Pay.value = deFormat(this.value);
total_pay = total_pay - mc_pay;
mc_pay = parseFloat(this.value);
total_pay = total_pay + mc_pay;
this.value = insert_commas(mc_pay.toFixed(2));
document.Debt_Form.Total_Pay.value=insert_commas(total_pay.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_MC_BaL_onblur() {
var obj = document.Debt_Form.MC_BaL;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.MC_BaL.blur();
document.Debt_Form.MC_BaL.value = deFormat(this.value);
amount = amount - mc_baL;
mc_baL = parseFloat(this.value);
amount = amount + mc_baL;
this.value = insert_commas(mc_baL.toFixed(2));
document.Debt_Form.Amount.value = insert_commas(amount.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_CapitaL_Pay_onblur() {
var obj = document.Debt_Form.CapitaL_Pay;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.CapitaL_Pay.blur();
document.Debt_Form.CapitaL_Pay.value = deFormat(this.value);
total_pay = total_pay - capital_pay;
capital_pay = parseFloat(this.value);
total_pay = total_pay + capital_pay;
this.value = insert_commas(capital_pay.toFixed(2));
document.Debt_Form.Total_Pay.value=insert_commas(total_pay.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_CapitaL_BaL_onblur() {
var obj = document.Debt_Form.CapitaL_BaL;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.CapitaL_BaL.blur();
document.Debt_Form.CapitaL_BaL.value = deFormat(this.value);
amount = amount - capitaL_baL;
capitaL_baL = parseFloat(this.value);
amount = amount + capitaL_baL;
this.value = insert_commas(capitaL_baL.toFixed(2));
document.Debt_Form.Amount.value = insert_commas(amount.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_CT_Pay_onblur() {
var obj = document.Debt_Form.CT_Pay;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.CT_Pay.blur();
document.Debt_Form.CT_Pay.value = deFormat(this.value);
total_pay = total_pay - ct_pay;
ct_pay = parseFloat(this.value);
total_pay = total_pay + ct_pay;
this.value = insert_commas(ct_pay.toFixed(2));
document.Debt_Form.Total_Pay.value = insert_commas(total_pay.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_CT_BaL_onblur() {
var obj = document.Debt_Form.CT_BaL;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.CT_BaL.blur();
document.Debt_Form.CT_BaL.value = deFormat(this.value);
amount = amount - ct_baL;
ct_baL = parseFloat(this.value);
amount = amount + ct_baL;
this.value = insert_commas(ct_baL.toFixed(2));
document.Debt_Form.Amount.value = insert_commas(amount.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Other_Pay_onblur() {
var obj = document.Debt_Form.Other_Pay;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Other_Pay.blur();
document.Debt_Form.Other_Pay.value = deFormat(this.value);
total_pay = total_pay - other_pay;
other_pay = parseFloat(this.value);
total_pay = total_pay + other_pay;
this.value = insert_commas(other_pay.toFixed(2));
document.Debt_Form.Total_Pay.value = insert_commas(total_pay.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Other_BaL_onblur() {
var obj = document.Debt_Form.Other_BaL;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Other_BaL.blur();
document.Debt_Form.Other_BaL.value = deFormat(this.value);
amount = amount - other_baL;
other_baL = parseFloat(this.value);
amount = amount + other_baL;
this.value = insert_commas(other_baL.toFixed(2));
document.Debt_Form.Amount.value = insert_commas(amount.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Mtg_Pmt_onfocus() {
var obj = document.Debt_Form.Mtg_Pmt;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Mtg_Pmt.value = "";
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Mtg_Pmt_onblur() {
var obj = document.Debt_Form.Mtg_Pmt;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Mtg_Pmt.value = deFormat(this.value);
mtg_pmt = parseFloat(this.value);
this.value = insert_commas(mtg_pmt.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Mtg_BaL_onfocus() {
var obj = document.Debt_Form.Mtg_BaL;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Mtg_BaL.value ="";
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Mtg_BaL_onblur() {
var obj = document.Debt_Form.Mtg_BaL;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Mtg_BaL.value = deFormat(this.value);
mtg_baL = parseFloat(this.value);
this.value = insert_commas(mtg_baL.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Home_Value_onfocus() {
var obj = document.Debt_Form.Home_Value;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Home_Value.value = "";
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Home_Value_onblur() {
var obj = document.Debt_Form.Home_Value;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Home_Value.value = deFormat(this.value);
home_value = parseFloat(this.value);
this.value = insert_commas(home_value.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Income_onfocus() {
var obj = document.Debt_Form.Income;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Income.value = "";
document.Debt_Form.Step3_Done.value ="";
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Income_onblur() {
var obj = document.Debt_Form.Income;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
document.Debt_Form.Income.value = deFormat(this.value);
family_income = parseFloat(this.value);
this.value = insert_commas(family_income.toFixed(2));
//!!//</_FRAGMENT_>
}
function document_Debt_Form_Button_Print_onclick() {
var obj = document.Debt_Form.Button_Print;
//!!//<_FRAGMENT_ ID="_CONNECTOR_1">
parent.print();
//!!//</_FRAGMENT_>
}
function CaLc_Savings() {
new_pay = amount*((new_rate/100)/12);
document.Debt_Form.New_Pay.value = insert_commas(new_pay.toFixed(2));
sav_pay = total_pay - new_pay;
document.Debt_Form.Sav_Pay.value = insert_commas(sav_pay.toFixed(2));
document.Debt_Form.Table_Sav.value = insert_commas(sav_pay.toFixed(2));
if ( sav_pay < 0) {
alert("Please check each statement, minimum payment & balances.");
}
if ( sav_pay > 0 ) {
document.Debt_Form.Step1_Done.value = "Done";
}
else document.Debt_Form.Step1_Done.value ="";
// for too lazy to enter corresponding statement balances
if (( new_pay == 0 )&& ( sav_pay != 0 )){
alert("Please enter BALANCE(s) from last month statements for each corresponding monthly payment.");
}
}
function CaLc_LTV() {
totaL_debt = amount + mtg_baL;
ltv_ratio = 100*(totaL_debt/home_value);
document.Debt_Form.LTV.value = insert_commas(ltv_ratio.toFixed(2));
document.Debt_Form.Table_LTV.value = insert_commas(ltv_ratio.toFixed(2));
if (ltv_ratio > 81) {
alert("Call for other solutions when Loan to Value ratio is greater than 80 %.");
}
if (ltv_ratio < 81) {
document.Debt_Form.Step2_Done.value = "Done";
}
else document.Debt_Form.Step2_Done.value = "";
}
function CaLc_TDS() {
if ((sav_pay==0) && (mtg_pmt==0)) {
alert("Missing debt payments or mortgage payment.");
}
if (family_income==0) {
alert("Please enter total family income from Tax Returns.");
}
qualified_new_pmt = (75000)*(monthly_factor);
if ((sav_pay!=0) || (mtg_pmt!=0)) {
tds_ratio = 100*(qualified_new_pmt + mtg_pmt + 250) / (family_income/12);
}
else tds_ratio=0;
// estimate prop taxes + heating totals $ 250
document.Debt_Form.TDSR.value = insert_commas(tds_ratio.toFixed(2));
document.Debt_Form.Table_TDS.value = insert_commas(tds_ratio.toFixed(2));
if (tds_ratio > 40) {
alert("Contact for other solutions, since Total Debt Ratio is greater than 40 %.");
}
if (( tds_ratio !=0 ) && (tds_ratio <41)) {
document.Debt_Form.Step3_Done.value = "Done";
}
else document.Debt_Form.Step3_Done.value ="";
}
// End of a.js