
// Calculate the Principal & Interest value depending on the Total Amount Owed  
// calculation based on 6.25% of interest rate and 30 year of loan - Sema Atavi 06/01/2005

// Calculate Total Debt Consolidation
function calDebtCon(f){
	 
	total = 0;
	form = document.debtCon;
	
	amtOwe01 = strip_commas(form.amtOwe01.value)
	amtOwe02 = strip_commas(form.amtOwe02.value)
	amtOwe03 = strip_commas(form.amtOwe03.value)
	amtOwe04 = strip_commas(form.amtOwe04.value)
	amtOwe05 = strip_commas(form.amtOwe05.value)
	amtOwe06 = strip_commas(form.amtOwe06.value)
	amtOwe07 = strip_commas(form.amtOwe07.value)	
		
	if (amtOwe01 && !isNaN(amtOwe01))
	total += round(amtOwe01);
	if (amtOwe02 && !isNaN(amtOwe02))
	total += round(amtOwe02);
	if (amtOwe03 && !isNaN(amtOwe03))
	total += round(amtOwe03);
	if (amtOwe04 && !isNaN(amtOwe04))
	total += round(amtOwe04);
	if (amtOwe05 && !isNaN(amtOwe05))
	total += round(amtOwe05);	
	if (amtOwe06 && !isNaN(amtOwe06))
	total += round(amtOwe06);			
	if (amtOwe07 && !isNaN(amtOwe07))
	total += round(amtOwe07);		
	form.amtOweTotal.value=total;
	form.amtOweTotal.value=formatCurrency(form.amtOweTotal.value); 
	document.getElementById("amtOweTotal").innerHTML = formatCurrency(form.amtOweTotal.value); 
	
}

// Calculate Total Montly Payment
function calMonPay(f) {
	
	total = 0;
	form = document.debtCon
	monPay01 = strip_commas(form.monPay01.value)
	monPay02 = strip_commas(form.monPay02.value)
	monPay04 = strip_commas(form.monPay04.value)
	monPay05 = strip_commas(form.monPay05.value)
	monPay06 = strip_commas(form.monPay06.value)
	monPay07 = strip_commas(form.monPay07.value)
	
	if (monPay01 && !isNaN(monPay01))
	total += round(monPay01);
	if (monPay02 && !isNaN(monPay02))
	total += round(monPay02);
	if (monPay04 && !isNaN(monPay04))
	total += round(monPay04);
	if (monPay05 && !isNaN(monPay05))
	total += round(monPay05);	
	if (monPay06 && !isNaN(monPay06))
	total += round(monPay06);			
	if (monPay07 && !isNaN(monPay07))
	total += round(monPay07);		
	form.monPayTotal.value=total;
	form.monPayTotal.value=formatCurrency(form.monPayTotal.value);
	document.getElementById("monPayTotal").innerHTML = formatCurrency(form.monPayTotal.value); 
	
}

//Calculate Principal & Interest Value
function calcPI()
{  
  form = document.debtCon;

  var monInt = form.intRate.value / 1200;
  var base = 1;
  var mBase = 1 + monInt;
  for (i=0; i<form.year.value * 12; i++)
  {
    base = base * mBase
  }
	
	if (((form.amtOwe01.value) && (form.amtOwe01.value > "0.00")) && ((form.monPay01.value) && (form.monPay01.value > "0.00")) && ((form.intRate.value) && (form.intRate.value > 0)) && ((form.year.value) && (form.year.value > 0))) {
  form.PI.value = floor(strip_commas(form.amtOweTotal.value) * monInt / ( 1 - (1/base)))
  form.PI.value = formatCurrency(form.PI.value);
  document.getElementById("PI").innerHTML = formatCurrency(form.PI.value); 
	
  
  form.monSavings.value = (strip_commas(form.monPayTotal.value) - strip_commas(form.PI.value)) 
	form.monSavings.value = formatCurrency(form.monSavings.value);
 	document.getElementById("monSavings").innerHTML = formatCurrency(form.monSavings.value); 
	
	}	
}


//If amount owe value exists, make sure user enters a monthly payment value greater than $0.00 - Sema Atavi 06-14-2005
function checkFields(){
		 
		form = document.debtCon;
		
  	//Make sure user enters a positive dollar value before the calculation. - Sema Atavi 06/14/2005
  	if ((!form.amtOwe01.value) || (form.amtOwe01.value <= "0.00")) {
			alert("Please enter at least one mortgage value greater than $0.00");
			form.amtOwe01.focus();	
			form.amtOwe01.select();				
		}
  	else if ((!form.monPay01.value) || (form.monPay01.value <= "0.00")) {
			alert("Please enter at least one monthly payment value greater than $0.00");
			form.monPay01.focus();	
			form.monPay01.select();				
		}				
		else if ((form.amtOwe01.value > "0.00") && ((!form.monPay01.value) || (form.monPay01.value <= "0.00")))
		{
			alert("Please enter monthly payment value greater than $0.00");	
			form.monPay01.focus();	
			form.monPay01.select();			
		}
		else if ((form.amtOwe02.value > "0.00") && ((!form.monPay02.value) || (form.monPay02.value <= "0.00")))
		{
			alert("Please enter monthly payment value greater than $0.00");	
			form.monPay02.focus();	
			form.monPay02.select();			
		}	
		else if ((form.monPay02.value > "0.00") && ((!form.amtOwe02.value) || (form.amtOwe02.value <= "0.00")))
		{
			alert("Please enter mortgage value greater than $0.00");	
			form.amtOwe02.focus();	
			form.amtOwe02.select();			
		}					
		else if ((form.amtOwe04.value > "0.00") && ((!form.monPay04.value) || (form.monPay04.value <= "0.00")))
		{
			alert("Please enter monthly payment value greater than $0.00");	
			form.monPay04.focus();	
			form.monPay04.select();							
		}	
		else if ((form.monPay04.value > "0.00") && ((!form.amtOwe04.value) || (form.amtOwe04.value <= "0.00")))
		{
			alert("Please enter mortgage value greater than $0.00");	
			form.amtOwe04.focus();	
			form.amtOwe04.select();			
		}				
	
		else if ((form.amtOwe05.value > "0.00") && ((!form.monPay05.value) || (form.monPay05.value <= "0.00")))
		{
			alert("Please enter monthly payment value greater than $0.00");	
			form.monPay05.focus();	
			form.monPay05.select();			
		}	
		else if ((form.monPay05.value > "0.00") && ((!form.amtOwe05.value) || (form.amtOwe05.value <= "0.00")))
		{
			alert("Please enter mortgage value greater than $0.00");	
			form.amtOwe05.focus();	
			form.amtOwe05.select();			
		}							
		else if ((form.amtOwe06.value > "0.00") && ((!form.monPay06.value) || (form.monPay06.value <= "0.00")))
		{
			alert("Please enter monthly payment value greater than $0.00");	
			form.monPay06.focus();	
			form.monPay06.select();				
		}	
		else if ((form.monPay06.value > "0.00") && ((!form.amtOwe06.value) || (form.amtOwe06.value <= "0.00")))
		{
			alert("Please enter mortgage value greater than $0.00");	
			form.amtOwe06.focus();	
			form.amtOwe06.select();			
		}					
		else if ((form.amtOwe07.value > "0.00") && ((!form.monPay07.value) || (form.monPay07.value <= "0.00")))
		{
			alert("Please enter monthly payment value greater than $0.00");	
			form.monPay07.focus();	
			form.monPay07.select();					
		}
		else if ((form.monPay07.value > "0.00") && ((!form.amtOwe07.value) || (form.amtOwe07.value <= "0.00")))
		{
			alert("Please enter mortgage value greater than $0.00");	
			form.amtOwe07.focus();	
			form.amtOwe07.select();			
		}			
	 // If the values are valid calculate Principal & Interest Value
	 else { 
	 	calDebtCon(this.form);
	 	calMonPay(this.form);
	 	calcPI(); 
	 	
	 	
	 	}								
}


 function numericCheck(txtObj)
   {
      if((txtObj.value.search(/^\d*$/) == -1))
      {
         alert("Valid Characters Are Numbers [0-9] Only");
         txtObj.focus();
         txtObj.select();
         return false;
      }
      else
      {
         return true;
      }
   } // End of numericCheck
   
   
// Remove all leading and trailing spaces
   function trimSpace(strObj)
   {
		inputString = strObj.value;
		var lre = /^\s*/;	// remove leading
		var rre = /\s*$/;	// remove trailing
		inputString = inputString.replace(lre, "");
		inputString = inputString.replace(rre, "");
		strObj.value = inputString;
		
   } // End of trimSpace
   
//Display amount in money format.
<!--

function displayAmt(number) {
    number = '' + number
      
	    if (number.length > 3) {
	        var mod = number.length%3;
	        var output = (mod > 0 ? (number.substring(0,mod)) : '');
	        for (i=0 ; i < Math.floor(number.length/3) ; i++) {
	            if ((mod ==0) && (i ==0))
	                output+= number.substring(mod+3*i,mod+3*i+3);
	            else
	                output+= ',' + number.substring(mod+3*i,mod+3*i+3);
	        }
	        return (output);
	    }
    else return number;
} // End of displayAmt


// check max number of characters for text size
   function maxLength(textObj,textLen)
   {
      str = textObj.value;
      if(str.length > textLen)
      {
         alert("Text Can Not Be More Than " + textLen + " Characters.\nPlease Shorten Your Message.");
         textObj.focus();
         return false;
      }
   } // end of maxLength
   

 // rounds number to X decimal places, defaults to 2
function round(number,X) 
{
   X = (!X ? 2 : X);
   return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
} // End of round
   
   
//The floor function returns the largest (closest to positive infinity) 
//number that is not greater than the argument and that is an integer. 
function floor(number)
{
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
} // End of floor
   
     
   // function for pop up windows
   function winPop(mypage,myname,w,h,t,scroll,resizable,screenX,screenY,left,top)
{
   var winl = (screen.width - w) / 2;
   var wint = (screen.height - h) / 2;
   winprops = 'height=' + h + ',width=' + w + ',top=' + wint + ',left=' + winl + ',toolbar=' + t + ',scrollbars=' + scroll + ',resizable=' + resizable + ',screenX=' + screenX + ',screenY=' + screenY + ',left=' + left + ',top=' + top;
   win      = window.open(mypage,myname,winprops);
   if((navigator.appVersion * 1) >= 4) { win.window.focus(); }
   
}//End of winPop


// check for valid amount value
function validateAmount(txtObj)
{
   if(txtObj.value.search(/^(\d+)?(\.?\d{1,2})?$/) == -1) {
     alert("Invalid Amount, Numbers OR Decimal Only");
     txtObj.focus();
     txtObj.select();
     return false;
  }

if((txtObj.value) && (txtObj.value > 0))
{
  if(txtObj.value.search(/\./) == -1)  // No Decimal, No Cents
  {
     txtObj.value = txtObj.value + ".00";
  }
  else  // Put 2 Digit Cents After Decimal
  {
	   var amount = txtObj.value.split('.');
	   if(amount[1].length != 2)
	   {
	      txtObj.value = amount[0] + "." + amount[1] + "0";
	   }
  }
   
 }
  return true;
} // End of check for valid amount value


//Display money values in Currency Format
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}

//REMOVE THE COMMAS IN THE MONEY VALUES
function strip_commas(num) {
    var newnum;
    var ch;

    newnum ="";
    for(var i=0; i<num.length; i++) {
            ch = num.charAt(i);
            if(ch != ",") {
                    newnum += ch;
            }
    }
    return parseFloat(newnum);
}


//javasript for Calculation Selection Navigation
//Includes all calculation pages. - Sema Atavi 06/03/2005
function selCalc()
{
var list = document.forms[0].findCal;	
location = list.options[list.selectedIndex].value;
}

//javasript for Mortgage Shopping Guide Selection 
//Includes all pages - Sema Atavi 06/14/2005
function selShopGuide()
{
var listSG = document.forms[0].shopGuide;	
location = listSG.options[listSG.selectedIndex].value;

}
//javasript for FAQs Selection 
//Includes all pages - Sema Atavi 06/14/2005
function selFaqs()
{
var listFaq = document.forms[0].faqs;	
location = listFaq.options[listFaq.selectedIndex].value;
}

//function to goBack to previous page
function goBack() {
  if (navigator.appVersion.indexOf("MSIE") > 0)
    history.back(-1);
  else
    if (navigator.appVersion.indexOf("Macintosh") > 0)
      top.window.back();
    else
      window.back();
}

//capitalise the first letter of the sentence
function toUpper(strObj) {
    var pattern = /(\w)(\w*)/; // a letter, and then one, none or more letters 

		a = strObj.value.split(/\s+/g); // split the sentence into an array of words;
		
    //var a = document.forms[0].text1.value.split(/\s+/g); // split the sentence into an array of words

    for (i = 0 ; i < a.length ; i ++ ) {
        var parts = a[i].match(pattern); // just a temp variable to store the fragments in.

        var firstLetter = parts[1].toUpperCase();
        var restOfWord = parts[2].toLowerCase();

        a[i] = firstLetter + restOfWord; // re-assign it back to the array and move on
    }
    
    strObj.value = a.join(' '); // join it back together
}


// Function to auto-tab to a field
<!-- Begin
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
		if(arr[index] == ele)
		found = true;
		else
		index++;
		return found;
	}
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
		else i++;
		return index;
	}
	return true;
}
//  End -->


