/*
Thousand separator stuff not implemented. If you want to implement it, add onKeyPress="return(currencyFormat(this, event))" to field. Errors are that no matter where the cursor is, it adds the key to the end. Also, if you tab to it, and the field is highlighted, it should clear when you start typing. And if you backspace, you may end up with "100,000,00". However, idealista and Finques Toscano have the same problem.
idealista clears automatically when you enter a field -- I should do that if 0. onClick='this.value=""' or better, onFocus=""'

Note: In the currently uploaded version, if you have a field, it will show as "NaN" in the result.
*/

function EvalNum(SS) {
  T = StripNonNum(SS);
  
  return T;
}  

function AddThouSep(SS) {
var T='', S=String(SS), L=S.length-1, C, j;

  for (j=0; j<=L; j++) {
    T += C=S.charAt(j);
    if ((j < L) && ((L-j)%3 == 0) && (C != '-'))
	  T+=',';
	}
	
return T;
}

function StripThouSep(SS) {
  T = SS;
  T = T.replace(/,/g, '');
  T = T.split(',').join();
return T;
}  

function StripNonNum(SS) {
var strCheck = '0123456789';
var i = 0;
var T = '';

  for(i = 0; i < SS.length; i++)
    if (strCheck.indexOf(SS.charAt(i)) != -1)
	  T += SS.charAt(i);
	  
return T;  
}  

function currencyFormat(fld, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var S = aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
  if (whichCode == 13)
    return true;  // Enter
  key = String.fromCharCode(whichCode);  // Get key value from key code
  if (strCheck.indexOf(key) == -1)
    return false;  // Not a valid key
  fld.value += key;

  //strip non-numbers	
  S = fld.value;
  S = StripNonNum(S);
  
  fld.value = AddThouSep(S);
	  
return false;
}

function currencyFormatOld(fld, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var milSep = ',';
var whichCode = (window.Event) ? e.which : e.keyCode;
  if (whichCode == 13)
    return true;  // Enter
  key = String.fromCharCode(whichCode);  // Get key value from key code
  if (strCheck.indexOf(key) == -1)
    return false;  // Not a valid key
  fld.value += key;

  //strip non-numbers	
  len = fld.value.length;
  for(i = 0; i < len; i++)
    if (strCheck.indexOf(fld.value.charAt(i)) != -1)
	  aux += fld.value.charAt(i);
	  
  fld.value = aux;
  len = aux.length;
  if (len > 2) {
    aux2 = '';
    for (j = 0, i = len - 1; i >= 0; i--) {
      if (j == 3) {
        aux2 += milSep;
        j = 0;
        }
      aux2 += aux.charAt(i);
      j++;
      }
    fld.value = '';
    len2 = aux2.length;
    for (i = len2 - 1; i >= 0; i--)
      fld.value += aux2.charAt(i);
    }
return false;
}

function currencyFormatOrig(fld, milSep, decSep, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
  if (whichCode == 13)
    return true;  // Enter
  key = String.fromCharCode(whichCode);  // Get key value from key code
  if (strCheck.indexOf(key) == -1)
    return false;  // Not a valid key
  len = fld.value.length;
  for(i = 0; i < len; i++)
    if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
  aux = '';
  for(; i < len; i++)
    if (strCheck.indexOf(fld.value.charAt(i))!=-1)
	  aux += fld.value.charAt(i);
  aux += key;
  len = aux.length;
  if (len == 0)
    fld.value = '';
  if (len == 1)
    fld.value = '0'+ decSep + '0' + aux;
  if (len == 2)
    fld.value = '0'+ decSep + aux;
  if (len > 2) {
    aux2 = '';
    for (j = 0, i = len - 3; i >= 0; i--) {
      if (j == 3) {
        aux2 += milSep;
        j = 0;
        }
      aux2 += aux.charAt(i);
      j++;
      }
    fld.value = '';
    len2 = aux2.length;
    for (i = len2 - 1; i >= 0; i--)
      fld.value += aux2.charAt(i);
    fld.value += decSep + aux.substr(len - 2, len);
    }
return false;
}


var isIE=document.all?true:false;
var isDOM=document.getElementById?true:false;
var isNS4=document.layers?true:false;

function toggleMort(_h) {
  if (isDOM)
  {
    if (_h=='y') {
	  document.getElementById("bankpct").style.display = 'inline';
	  document.getElementById("appraisal").style.display = 'inline';
	  document.getElementById("bankfees").style.display = 'inline';
	  document.getElementById("youpaylit").firstChild.nodeValue = 'Portion outside mortgage:';
	  document.getElementById("dueyou").firstChild.nodeValue = 'due to the bank a week before the closing';
	  document.getElementById("duetaxes").firstChild.nodeValue = 'due to the bank a week before the closing';
	  document.getElementById("duenotary").firstChild.nodeValue = 'due to the bank a week before the closing';
	  document.getElementById("duereg").firstChild.nodeValue = 'due to the bank a week before the closing';
    }
    else {
	  document.getElementById("bankpct").style.display = 'none';
	  document.getElementById("appraisal").style.display = 'none';
	  document.getElementById("bankfees").style.display = 'none';
	  document.getElementById("youpaylit").firstChild.nodeValue = 'Payment for house:';
	  document.getElementById("dueyou").firstChild.nodeValue = 'due to the seller at the closing';
	  document.getElementById("duetaxes").firstChild.nodeValue = 'due after the closing';
	  document.getElementById("duenotary").firstChild.nodeValue = 'due after the closing';
	  document.getElementById("duereg").firstChild.nodeValue = 'due after the closing';
	  }
  }
  
  getyou();
}

function toggleNew() {
  getyou();
}

function getformatted(formatnum)
{
	parsestring=Number(formatnum);
	parsedstring=parsestring.toString();
	decimalindex=parsedstring.indexOf(".");
	if(decimalindex=="-1") 
      beforedecimalval=parsedstring.substring(0);
	else	
      beforedecimalval=parsedstring.substring(0,decimalindex);
	var newstr=new String(beforedecimalval);
	newstr = AddThouSep(newstr);
	return newstr;
}

function getlegal()
{	
  legalinpct = eval(document.form1.legalinpct.value);
  if (document.form1.legalinpct.value == "")
      legalinpct = 0;
  legalinamt = eval(document.form1.legalinamt.value);
  if (document.form1.legalinamt.value == "")
      legalinamt = 0;
  if (legalinpct != 0)
    document.form1.legal.value = getformatted((legalinpct * EvalNum(document.form1.houseamt.value)) / 100);
  else 	
    document.form1.legal.value = getformatted(legalinamt);
}

function getdown()
{	
  downinpct = EvalNum(document.form1.downinpct.value);
  if (document.form1.downinpct.value == "")
      downinpct = 0;
  downinamt = EvalNum(document.form1.downinamt.value);
  if (document.form1.downinamt.value == "")
      downinamt = 0;
  if (downinpct != 0)
    document.form1.down.value = getformatted((downinpct * EvalNum(document.form1.houseamt.value)) / 100);
  else 	
    document.form1.down.value = getformatted(downinamt);
}

function getyou()
{	
  houseamt = EvalNum(document.form1.houseamt.value);
  down = EvalNum(document.form1.down.value);
  notarybuy = .0023 * houseamt;
  if (document.form1.radionew[0].checked) 
    taxbuy = .075 * houseamt;
  else
    taxbuy = .07 * houseamt;	
  regbuy = .0012 * houseamt;
  if (document.form1.radiomort[0].checked) {
    mortamt = EvalNum(document.form1.mortamt.value);
    if (mortamt == 0) {
      mortpct = EvalNum(document.form1.mortpct.value);
      mortamt = (mortpct * houseamt) / 100;
      }
    notaryloan = .0035 * mortamt;
    if (document.form1.radionew[0].checked) 
      taxloan = .005 * mortamt;
	else  
      taxloan = .008 * mortamt;
    regloan = .0017 * mortamt;
    gestloan = .0017 * mortamt;
    insurance = 342;
    gestbuy = .0012 * houseamt;
	}
  else {	
    mortpct = 0;
	mortamt = 0;
    notaryloan = 0;
    taxloan = 0;
    regloan = 0;
    gestloan = 0;
    insurance = 0;
	gestbuy = 0;
	}
  you = ((houseamt - mortamt) - down);	
  document.form1.you.value = getformatted(you);
  notaryfees = notarybuy + notaryloan;
  document.form1.notaryfees.value = getformatted(notaryfees);
  regfees = regbuy + regloan;
  document.form1.regfees.value = getformatted(regfees);
  taxes = taxbuy + taxloan;
  document.form1.taxes.value = getformatted(taxes);
  bankfeesv = insurance + gestbuy + gestloan;
  document.form1.bankfees.value = getformatted(bankfeesv);
  
  legalv = EvalNum(document.form1.legal.value);
  downv = EvalNum(document.form1.down.value);
  appraisalv = EvalNum(document.form1.appraisal.value);
  totalv = Number(you) + Number(notaryfees) + Number(regfees) + Number(taxes) + Number(bankfeesv) + Number(legalv) + Number(downv) + Number(appraisalv);
  document.form1.total.value = getformatted(totalv);
}

function calclegalinpct()
{	
  legalinpct = EvalNum(document.form1.legalinpct.value);
  if (legalinpct > 0)
    document.form1.legalinamt.value = "0";
  getlegal();
  getyou();
}

function calclegalinamt()
{	
  legalinamt = EvalNum(document.form1.legalinamt.value);
  if (legalinamt > 0)
    document.form1.legalinpct.value = "0";
  getlegal();
  getyou();
}

function calcdowninpct()
{	
  downinpct = EvalNum(document.form1.downinpct.value);
  //document.form1.downinpct.value = downinpct;
  if (downinpct > 0)
    document.form1.downinamt.value = "0";
  getdown();
  getyou();
}

function calcdowninamt()
{	
  //document.form1.downinamt.value = AddThouSep(EvalNum(document.form1.downinamt.value));
  downinamt = EvalNum(document.form1.downinamt.value);
  if (downinamt > 0)
    document.form1.downinpct.value = "0";
  getdown();
  getyou();
}

function calcmortpct()
{	
  mortpct = EvalNum(document.form1.mortpct.value);
  //document.form1.mortpct.value = mortpct;
  if (mortpct > 0)
    document.form1.mortamt.value = "0";
  getyou();
}

function calcmortamt()
{	
  //document.form1.mortamt.value = AddThouSep(EvalNum(document.form1.mortamt.value));
  mortamt = EvalNum(document.form1.mortamt.value);
  if (mortamt > 0)
    document.form1.mortpct.value = "0";
  getyou();
}

function calcprice()
{	
  //currencyFormat(document.form1.houseamt);
  getlegal();
  getdown();
  getyou();
}

function validprice()
{	
//doing this within calcprice causes the focus to be lost.
  document.form1.houseamt.value = AddThouSep(EvalNum(document.form1.houseamt.value));
}

