function Currency(anynum) {
   anynum = eval(anynum);
   workNum = Math.abs((Math.round(anynum*100)/100));
   workStr = "" + workNum;
   if (workStr.indexOf(".") == -1)
    workStr += ".00";
   dStr = workStr.substr(0,workStr.indexOf("."));
   dNum = dStr - 0;
   pStr = workStr.substr(workStr.indexOf("."));
   while (pStr.length < 3)
   {
    pStr += "0";
   }
   retval = dStr + pStr 
   return retval
}

function IsNumeric(strField)
{

  var i = 0;
  var onedec = false;
  for (i = 0; i < strField.length; i++)
  { if (strField.charAt(i)!="." && (strField.charAt(i) < '0' || strField.charAt(i) > '9'))
      return false;
    if (strField.charAt(i)==".")
    { if (onedec)
        return false;
      else
        onedec = true;
    }
  }
  return true;
}
function CheckForm(frm, type)
{
  frm.amount.value = frm.amount.value.replace(",","");
  frm.payments.value = frm.payments.value.replace(",","");
 
  if (!IsNumeric(frm.term.value) || Trim(frm.term.value)=="")
  { //alert("Please enter a valid number for Length of Term");
  	alert(INVALID_LENGTH_OF_TERM);
    frm.term.focus();
    frm.term.select();
    return false;   
  }
  if (!IsNumeric(frm.interestRate.value) || Trim(frm.interestRate.value)=="")
  { //alert("Please enter a valid number for Interest Rate");
  	alert(INVALID_INTEREST_RATE);
    frm.interestRate.focus();
    frm.interestRate.select();
    return false;   
  }
  if (!IsNumeric(frm.amount.value))
  { //alert("Please enter a valid number for Mortgage Amount");
    alert(INVALID_AMOUNT);
    frm.amount.focus();
    frm.amount.select();
    return false;   
  }
  if (!IsNumeric(frm.payments.value))
  { //alert("Please enter a valid number for Monthly Payments");
    alert(INVALID_PAYMENT);
    frm.payments.focus();
    frm.payments.select();
    return false;   
  }
  if(type=="payment" && Trim(frm.amount.value)=="")
  { //alert("Please enter an amount for Mortgage Amount to calculate Monthly Payments");
    alert(EMPTY_AMOUNT);
	frm.amount.focus();
    return false;
  }
  if(type=="amount" && Trim(frm.payments.value)=="")
  { //alert("Please enter an amount for Monthly Payments to calculate Mortgage Amount");
    alert(EMPTY_PAYMENTS);
	frm.payments.focus();
    return false;
  }
  return true;
}
var curr=0;
function CalcMortgage(frm, type)
{

  
  var ans,ans2 ;
  if (CheckForm(frm, type))
  {
  	
    var n = frm.term.value;
    var r = frm.interestRate.value / 200;
    var a = frm.amount.value;
    var p = frm.payments.value;
  	
    if(type == "payment")
	//(P*(((1+i/200)^(1/6)-1))/(1-(((1+i/200)^(1/6)))^-(n*12)))
    { p = a * (Math.pow((1+r),(1/6)) - 1) / (1 - Math.pow(Math.pow((1+r),(1/6)),(n*-12)));
      
	  ans = Currency(p);
	  frm.payments.value= ans;
	  
	  for(var i=0;i<3;i++)
	  {
	  	if(i==curr)				
			document.getElementById("r"+i).bgColor = "#FEAC01";		 
		else
			document.getElementById("r"+i).bgColor = "#f1f1f1";			
	  }
	  
	  frm.MA[curr].value=frm.amount.value;
	  frm.IR[curr].value=frm.interestRate.value;
	  frm.LT[curr].value =frm.term.value;
	  frm.MP[curr].value = ans;
	  
	  curr++;
	  if(curr==3)
	  	curr=0;
	  
	  
    }
    else if(type == "amount")
    {
      //a = (p / ((200 * r) * Math.pow((1 + r),n)) * (200 * (Math.pow((1 + r),n)-1)));
	  a = p / ((Math.pow((1+r),(1/6)) - 1) / (1 - Math.pow(Math.pow((1+r),(1/6)),(n*-12))));
	  
      ans2 = Currency(a);
	  frm.amount.value = ans2;
	  
	  for(var i=0;i<3;i++)
	  {
	  	if(i==curr)		
			document.getElementById("r"+i).bgColor = "#FEAC01";	  
		else
			document.getElementById("r"+i).bgColor = "#f1f1f1";		
	  }
	  
	  frm.MA[curr].value=ans2;
	  frm.IR[curr].value=frm.interestRate.value;
	  frm.LT[curr].value =frm.term.value;
	  frm.MP[curr].value = frm.payments.value;
	  
	  curr++;
	  if(curr==3)
	  	curr=0;
    }
  }
}





        function RTrim(str)
        /***
                PURPOSE: Remove trailing blanks from our string.
                IN: str - the string we want to RTrim

                RETVAL: An RTrimmed string!
        ***/
        {
                // We don't want to trip JUST spaces, but also tabs,
                // line feeds, etc.  Add anything else you want to
                // "trim" here in Whitespace
                var whitespace = new String(" \t\n\r");

                var s = new String(str);

                if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
                    // We have a string with trailing blank(s)...

                    var i = s.length - 1;       // Get length of string

                    // Iterate from the far right of string until we
                    // don't have any more whitespace...
                    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                        i--;


                    // Get the substring from the front of the string to
                    // where the last non-whitespace character is...
                    s = s.substring(0, i+1);
                }

                return s;
        }


        function LTrim(str)
        /***
                PURPOSE: Remove leading blanks from our string.
                IN: str - the string we want to LTrim

                RETVAL: An LTrimmed string!
        ***/
        {
                var whitespace = new String(" \t\n\r");

                var s = new String(str);

                if (whitespace.indexOf(s.charAt(0)) != -1) {
                    // We have a string with leading blank(s)...

                    var j=0, i = s.length;

                    // Iterate from the far left of string until we
                    // don't have any more whitespace...
                    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                        j++;


                    // Get the substring from the first non-whitespace
                    // character to the end of the string...
                    s = s.substring(j, i);
                }

                return s;
        }



        function Trim(str)
        /***
                PURPOSE: Remove trailing and leading blanks from our string.
                IN: str - the string we want to Trim

                RETVAL: A Trimmed string!
        ***/
        {
                return RTrim(LTrim(str));
        }
