 //Global Vars
 var vAsking_price = 0;  
 var vIntrest_rate = 0;
 var vDown_percent = 0;
 var vTerm = 0;
 
 var vDown_amount = 0;
 var vPayment = 0;
 
//  var c_name = new Array();
//  c_name['USD'] = 'U.S. Dollars';
//  c_name['CAD'] = 'Canadian Dollars';
//  c_name['EUR'] = 'European Euros';
//  c_name['DEM'] = 'German Marks';
//  c_name['FRF'] = 'French Francs';
//  c_name['JPY'] = 'Japanese Yen';
//  c_name['GBP'] = 'British Pounds';
//  c_name['CHF'] = 'Swiss Francs';
//  c_name['AUD'] = 'Australian Dollars';
//  c_name['HKD'] = 'Hong Kong Dollars';
//  c_name['NZD'] = 'New Zealand Dollars';
 
  
 function initMorgage() {
   vDown_amount = (parseFloat(vAsking_price) * parseFloat(vDown_percent) / 100);
   document.getElementById("down_amount").value = wholeNumber(vDown_amount);
   document.getElementById("term").value = vTerm;
   document.getElementById("rate").value = vIntrest_rate;   
   document.getElementById("propVal").innerHTML = vAsking_price;     
 }
 
 function updateTerm(){
   vTerm = wholeNumber(document.getElementById("term").value);
 }
 
 function updateRate(){
   vIntrest_rate = document.getElementById("rate").value;
 }
 
 function updateDownPayment(changedField)
 {   
    var itemValue, newValue;            
    if (changedField == "money") {
       itemValue = wholeNumber(document.getElementById("down_amount").value);
       newValue = wholeNumber((itemValue / vAsking_price) * 100);
       //document.getElementById("down_percent").value = newValue;   
       vDown_amount = itemValue;
       vDown_percent = newValue;             
    } else {
	//itemValue = wholeNumber(document.getElementById("down_percent").value);
	//newValue = wholeNumber(vAsking_price * (itemValue / 100));
	//document.getElementById("down_amount").value = newValue;
	//vDown_percent = itemValue;
	//vDown_amount = newValue;     
    }   
 }
 
 function calcPayments()
 {
   var iRate, iTerm, iPrincipal;
   
   //calc Rate (% per month)
   iRate = (vIntrest_rate / 100) / 12;
   
   //Term to Months
   iTerm = vTerm * 12;
   
   //Principal
   iPrincipal = vAsking_price - vDown_amount;
   
   //calc payment
   vPayment = Math.floor((iPrincipal*iRate)/(1-Math.pow((1+iRate),(-1*iTerm)))*100)/100;
 }
 
 function displayData(){
   var msg = vTerm + " year bond at " + vIntrest_rate + "% with an optional " + formatRands(wholeNumber(vDown_amount)) + " down";  
   msg = vTerm + " year bond at " + vIntrest_rate + "% with an optional " + vDown_percent + 
   	"% down-Payment";  
   document.getElementById("askingPrice").innerHTML = formatRands(wholeNumber(vAsking_price));
   document.getElementById("propVal").innerHTML = formatRands(wholeNumber(vAsking_price));   
   document.getElementById("monthlyInstallment").innerHTML = formatRands(wholeNumber(vPayment));
   document.getElementById("downpayment").innerHTML = formatRands(wholeNumber(vDown_amount));
   document.getElementById("CalcedRate").innerHTML = msg; // vIntrest_rate + "% with " + vDown_percent + "% "; 
 }
 
 function recalc(){
   document.getElementById("calculator").style.display = "none";
   calcPayments();
   displayData();   
 }
 
 function showCalc(){
   document.getElementById("calculator").style.display = "";
 }
 function wholeNumber(AVal)
 {
  var num = new String(AVal);
  
  if ((i=num.indexOf(".")) >= 0) {
    num = num.substring(0,i);
  }     
  while((i=num.indexOf(",")) >= 0) {
    t1 = num.substring(0,i);
    t2 = num.substring(i+1,num.length);
    num = t1 + t2;
  }     
   return num;
 }
 
 function formatRands(AVal) {
   return "R " + formatNumber(AVal);
 }
 
 function formatNumber(AVal) {
   var oldVal = new String(AVal);
   var newVal = "";
      
  for (i = oldVal.length-1; i >= 0; i--) {
    if (((oldVal.length-i-1) % 3) == 0) {
      if ((oldVal.length-i-1) > 0) {
        newVal = " " + newVal;
      }
    }
    newVal = oldVal.substring(i,i+1) + newVal;
  } 
   return newVal;
 }

//  function mOver(Ident)
//  {
//    document.getElementById("m" + Ident).style.backgroundColor = "#D50000";  
//  }

//  function mOut(Ident)
//  {
//   document.getElementById("m" + Ident).style.backgroundColor = "#E8E8E8";
//  }

//  function setSymbol(Symbol){
//    var exp = new Date();
//    var nowPlusWeek = exp.getTime() + (7 * 24 * 60 * 60 * 1000);
//    exp.setTime(nowPlusWeek);
//    document.cookie = "SYM=" + Symbol + "; expires=" + exp.toGMTString() + ";path=/";
//  }

//  function getSymbol()
//  {
//    if (document.cookie.length > 0) 
//    { 
//      begin = document.cookie.indexOf("SYM="); 
//      if (begin != -1)
//     { 
//        begin += 4;
//        end = document.cookie.indexOf(";", begin);
//        if (end == -1) end = document.cookie.length;
//        return document.cookie.substring(begin, end);       
//      } 
//    }
//    return "NONE";  
//  }

//  function doCurrency(){ 
//    setSymbol(document.getElementById("currList").value);
//    if (document.currencyProc != null) 
//    {
//     document.currencyProc(document.getElementById("currList").value);
//    } 
//  }

//  function setCurrency()
//  {
//    var symbol = getSymbol(); 
//    var i;
//    var  list = document.getElementById("currList");
//    for (i=0;i<list.options.length;i++)
//  	{
//  	  if (list.options[i].value == symbol)
//  	  {	    
//  	    list.selectedIndex = i;
//  	    return;
//  	  }
//  	}
//  }

//  function enterDirectory(){
//    var URI = document.getElementById("dirList").value;
//    if (URI != 'NULL') { document.location = URI;}
//  }

//  function searchCatalog(){
//    var URI = "../search/fulltextsearch.asp?txtPhrase=" + document.getElementById("txtPhrase").value;
//    document.location = URI;
//  }

