/**
 * Rekent het bedrag uit dat er per maand betaald moet worden
 *
 * @access public
 * @param  integer i - Start bedrag
 * @param  integer l - Looptijd contract in maanden
 */
function amountToMonth(i,l){
  //return (Math.round(i*1.05/l*0.58 * 100, 2) / 100);
  a = Math.round (i * 0.05 / 12 * 58, 2);
  return formatNumber (a);
}

function populateElementTwo(v, v2, t){
  d=document;
  d.getElementById(t+"_1").value=" € "+amountToMonth(v, 60);
  d.getElementById(t+"_2").value=" € "+amountToMonth(v2, 120);
  recalculateTotalAmount();
}
function populateElement(v, t){
  d=document;
  d.getElementById(t+"_1").value=" € "+amountToMonth(v, 60);
  d.getElementById(t+"_2").value=" € "+amountToMonth(v, 120);
  recalculateTotalAmount();
}

function populateYears(y, e){
  for(var i=y;i>e;i--){
    document.write("<option value='"+i+"'>"+i+"</option>\n");
  }
}
function populateKetels(){
  for(var i=0;i<aKetelTypes.length;i++){
    document.write("<option value='1'>"+aKetelTypes[i]+"</option>\n");
  }
}

function _o(s){
  return(document.getElementById(s));
}

function recalculateKetel(){
  var contract = getCheckboxValue("typeContract");
  var year = _o("yearKetel");
  var type = _o("typeKetel");
  if (type.options[type.selectedIndex].value=="0") {
    contract = "1";
    setCheckboxValue("typeContract", "1");
  }
  if (contract == "0") {
    if (iYear - year.options[year.selectedIndex].value > aBreakYear) {
      var iYearInGarantie = 0;
      var iYearInOnderhoud = 10;
    } else {
      var iYearInGarantie = aMaxGarantie - (iYear - year.options[year.selectedIndex].value);
      var iYearInOnderhoud = (iYear - year.options[year.selectedIndex].value);
    }
  } else {
      var iYearInGarantie = 0;
      var iYearInOnderhoud = 10;
  }

  var a = 0;
  var b = getCheckboxValue("boiler");

  a += _calculateGarantiePlan(iYearInGarantie, b);
  a += _calculateOnderhoudPlan(iYearInGarantie, iYearInOnderhoud, b);

  var aTien = a;
  a = 0;
  if (contract == "0") {
    if (iYear - year.options[year.selectedIndex].value > 5) {
      var iYearInGarantie = 0;
      var iYearInOnderhoud = 5;
      setCheckboxValue("typeContract", "1");
    } else {
      var iYearInGarantie = 5;
      var iYearInOnderhoud = 0;
    }
  } else {
      var iYearInGarantie = 0;
      var iYearInOnderhoud = 5;
  }

  a += _calculateGarantiePlan(iYearInGarantie, b);
  a += _calculateOnderhoudPlan(iYearInGarantie, iYearInOnderhoud, b);
  var aVijf = a;
  //alert(aVijf+"\n\n"+aTien);
  foo = getCheckboxValue ("verwarmingsinstallatie");
  if (foo == "1") { 
	  populateElementTwo(aVijf, aTien, "verwarmingsinstallatie");
  } else {
	  populateElementTwo(0, 0, "verwarmingsinstallatie");
  } 
  populateElementTwo(aVijf, aTien, "sub_verwarmingsinstallatie");

}

function _calculateGarantiePlan(g, b){
  var r = 0;
  for (var i = 1; i < g+1; i++) {
    r += aKetel[0][b] * Math.pow(fIndex, i - 1);
  }
  return (r);
}

function _calculateOnderhoudPlan(g, o, b){
  var r = 0;
  for (var i = g+1; i < o+1; i++) {
    r += aKetel[1][b] * Math.pow(fIndex, i-1);
  }
  return (r);
}


function recalculateTotalAmount(){
  var a=new Array('buitenschilderwerk_', 'binnenschilderwerk_', 'verwarmingsinstallatie_', 'nieuweketel_', 'veiligheidsinspectie_', 'klusservice_');
  var t1=0; var t2 = 0;
  for(i=0;i<a.length;i++){
    s1 =document.getElementById(a[i] + '1').value;
    s2 =document.getElementById(a[i] + '2').value;
    f1 = s1.substr(3);
    f2 = s2.substr(3);
    //f2= DeleteFormat (s2.substr(3)) * 100;
    
    //f2 = DeleteFormat (f2);
        

    //if (!isNaN(f1))
      t1+= DeleteFormat (f1) * 100;
    //if (!isNaN (f2))
      t2+= DeleteFormat (f2) * 100;

  }
  //alert(t1/100);
  document.getElementById('totaal_1').value=" € "+ formatNumber (t1);
  document.getElementById('totaal_2').value=" € "+ formatNumber (t2);
}

function ChangeWopRecalculate ()
{
  var woptype=document.getElementById("woptype");
  woptypeV=woptype.options[woptype.selectedIndex].value
  var data=aWoningen['types'][woptypeV];
  var vijf=data['vijf'];
  var tien=data['tien'];
  var totVijf=vijf[0];
  var totTien=tien[0];
  totVijf+=vijf[0]*vijf[1][parseInt(getCheckboxValue("staatonderhoud"))];
  totTien+=tien[0]*tien[1][parseInt(getCheckboxValue("staatonderhoud"))];
  totVijf+=vijf[0]*vijf[3][parseInt(getCheckboxValue("houtrot"))];
  totTien+=tien[0]*tien[3][parseInt(getCheckboxValue("houtrot"))];
  totVijf+=parseInt(document.getElementById("numDak").options[document.getElementById("numDak").selectedIndex].value)*vijf[4];
  totTien+=parseInt(document.getElementById("numDak").options[document.getElementById("numDak").selectedIndex].value)*tien[4];
  totVijf+=parseInt(getCheckboxValue("goothout"))*vijf[5];
  totTien+=parseInt(getCheckboxValue("goothout"))*tien[5];
  totVijf+=parseInt(getCheckboxValue("garagedeur"))*vijf[6];
  totTien+=parseInt(getCheckboxValue("garagedeur"))*tien[6];
  totVijf+=parseInt(getCheckboxValue("carport"))*vijf[7];
  totTien+=parseInt(getCheckboxValue("carport"))*tien[7];
  totVijf+=parseInt(getCheckboxValue("schuifpui"))*vijf[8];
  totTien+=parseInt(getCheckboxValue("schuifpui"))*tien[8];
  totVijf+=parseInt(getCheckboxValue("kleurwijziging"))*vijf[9];
  totTien+=parseInt(getCheckboxValue("kleurwijziging"))*tien[9];
  if (getCheckboxValue("buitenschilderwerk")=="0"){
    totVijf=0;
    totTien=0;
  }

  // Verhoging bedragen
  totVijf *= increase;
  totTien *= increase;

  populateElementTwo(totVijf, totTien, "buitenschilderwerk");
}

function getCheckboxValue(s){
  var o=eval("document.form."+s);
  for (var i=0;i<o.length;i++){
    if (o[i].checked){
      return (o[i].value);
    }
  }
  return 0;
}
 
function setCheckboxValue(s, v){
  var o=eval("document.form."+s);
  for (var i=0;i<o.length;i++){
    if (o[i].value == v){
      o[i].checked = true;
    }
  }
  return 0;
}
function formatNumber(number) {
  if (number==0){
    return "0,00";
  }
  // convert to string
  number=Math.round(number);
  var fnumber = number+'';
  // formatting
  if (fnumber.length == 2) {
    return ("0," + fnumber);
  }
  var len = fnumber.length - 2;
  fnumber = fnumber.substring(0, len) + ',' + fnumber.substring(len, fnumber.length);
  len = len - 3;
  while (len > 0) {
    fnumber = fnumber.substring(0, len) + '.' + fnumber.substring(len, fnumber.length);
    len = len - 3;
  }
  return fnumber;
}
 

function DeleteFormat (number) {
    var x = '';
    for (var i=0; i < number.length;i++) {
      if (number.charAt (i) == '.') {

      } else if (number.charAt (i) == ',') {
        x += '.';
      } else {
        x += number.charAt (i);
      } 
    }
    return x;
}