﻿window.addEvent('domready',function() {
  // PNG fix
  $$('img[src$=.png]').setStyle('behavior','url(/_include/pngFix.htc)');

  // E-mail fix
  $$('a[href^=mailto:]').each(function(link) {
    link.setProperty('href',link.getProperty('href').replace(/#/,'@'));
  });

  $$('a[rel=external]').setProperty('target', '_blank');

  $$('fieldset > br').dispose();

  if($('aspnetForm')) {
    if(Browser.Engine.trident) {
      $$('input[readonly]').setStyles({
        'background-color':'#EBEBE4',
        'border':'1px solid #A5ACB2'
      });
    }
      
    var FormCheck = new Form($('aspnetForm'));
    FormCheck.requiredFields();
    FormCheck.checkEmail();
  
    if($('aspnetForm').getElement('input[value=BuyOrRent]')) {
      var myForm = new buyVSrent($('aspnetForm'));
    }
    
    if($('aspnetForm').getElement('input[value=SellRentback]')) {
      var myForm = new SellRentback($('aspnetForm'));
    }
    
    // Extra partner informatie indien er een partner is
    if($('ctl00_mainText_ctl09_partnerinfo')) {
      $('ctl00_mainText_ctl09_partneryes').addEvent('click',function() {
        if(this.getProperty('checked') == true) {
          $('ctl00_mainText_ctl09_partnerinfo').setStyle('display','block');
          if(!$('ctl00_mainText_ctl09_partnername').hasClass('reqFld')) $('ctl00_mainText_ctl09_partnername').addClass('reqFld');
          if(!$('ctl00_mainText_ctl09_partnerbirthdate').hasClass('reqFld')) $('ctl00_mainText_ctl09_partnerbirthdate').addClass('reqFld');
        }
      });
      $('ctl00_mainText_ctl09_partnerno').addEvent('click',function() {
        if(this.getProperty('checked') == true) {
          $('ctl00_mainText_ctl09_partnerinfo').setStyle('display','none');
          if($('ctl00_mainText_ctl09_partnername').hasClass('reqFld')) $('ctl00_mainText_ctl09_partnername').removeClass('reqFld');
          if($('ctl00_mainText_ctl09_partnerbirthdate').hasClass('reqFld')) $('ctl00_mainText_ctl09_partnerbirthdate').removeClass('reqFld');
        }
      });
    }
    
    // Bij selecteren Zeehos, extra formulier velden tonen
    if($('ctl00_mainText_ctl09_zeehos')) {
      if(location.href.test('zeehos')) {
        $('ctl00_mainText_ctl09_zeehos').setProperty('checked',true);
      } else {
        $('ctl00_mainText_ctl09_extraDiv').setStyle('display','none');
      }
      
      $('ctl00_mainText_ctl09_zeehos').addEvent('click',function () {
        if(this.getProperty('checked') == true) {
          $('ctl00_mainText_ctl09_extraDiv').setStyle('display','block');
        } else {
          $('ctl00_mainText_ctl09_extraDiv').setStyle('display','none');
        }
      });
    }
    
    // Tooltips bij formulieren
    var myTips = new Tips('.tooltip',{className:'myToolTip'});
  }

});


var tableArr = new Array();
tableArr[55] = {'L':75, 'P':5.98};
tableArr[56] = {'L':76, 'P':6.09};
tableArr[57] = {'L':76, 'P':6.2};
tableArr[58] = {'L':77, 'P':6.31};
tableArr[59] = {'L':78, 'P':6.42};
tableArr[60] = {'L':78, 'P':6.533};
tableArr[61] = {'L':79, 'P':6.65};
tableArr[62] = {'L':80, 'P':6.77};
tableArr[63] = {'L':80, 'P':6.89};
tableArr[64] = {'L':81, 'P':7.01};
tableArr[65] = {'L':82, 'P':7.135};
tableArr[66] = {'L':82, 'P':7.28};
tableArr[67] = {'L':83, 'P':7.42};
tableArr[68] = {'L':84, 'P':7.56};
tableArr[69] = {'L':84, 'P':7.71};
tableArr[70] = {'L':85, 'P':7.87};
tableArr[71] = {'L':85, 'P':8.07};
tableArr[72] = {'L':86, 'P':8.27};
tableArr[73] = {'L':87, 'P':8.49};
tableArr[74] = {'L':88, 'P':8.71};
tableArr[75] = {'L':89, 'P':8.93};
tableArr[76] = {'L':89, 'P':9.17};
tableArr[77] = {'L':90, 'P':9.41};
tableArr[78] = {'L':90, 'P':9.65};
tableArr[79] = {'L':91, 'P':9.89};
tableArr[80] = {'L':92, 'P':10.14};

var SellRentback = new Class({
  initialize: function(form) {
    this.form = form;
    
    this.A = $empty; // Koopprijs woning
    this.C = $empty; // Afkoop levenslange huur
    this.B = $empty; // Huidige hypotheek
    this.EL = $empty; // Gem. extra te besteden per jaar bij levenslang
    this.H = $empty; // Hypotheekrente (na belastingvoordeel)
    this.L = 75; // Verwachte eindleeftijd
    this.P = $percent(5.98); // Uitkeringspercentage per 100000 euro
    this.R = $percent(5); // Rente percentage bank/rendement
    this.X = 55; // Leeftijd verkoper/huurder
    this.Y = $empty; // Gewenste looptijd
    this.Z = $percent(6.5); // Huur als percentage van taxatiewaarde
    
    this.tmp = $empty; // Temp values
    this.O = $empty; // Overwaarde
    
    this.H = this.R * $percent(80);
    $('ctl00_mainText_ctl09_age').set('value',this.X) // Vul de leeftijd alvast in
    
    this.setEvents();
  },
  
  setEvents: function() {
//    $('ctl00_mainText_ctl09_housevalue').addEvent('keyup',this.setA.bindWithEvent(this,$('ctl00_mainText_ctl09_housevalue')));
//    $('ctl00_mainText_ctl09_borrow').addEvent('keyup',this.setB.bindWithEvent(this,$('ctl00_mainText_ctl09_borrow')));
//    $('ctl00_mainText_ctl09_age').addEvent('keyup',this.setX.bindWithEvent(this,$('ctl00_mainText_ctl09_age')));
//    $('ctl00_mainText_ctl09_time').addEvent('keyup',this.setY.bindWithEvent(this,$('ctl00_mainText_ctl09_time')));
    
  var myInputs = [$('ctl00_mainText_ctl09_housevalue'),$('ctl00_mainText_ctl09_borrow'),$('ctl00_mainText_ctl09_age'),$('ctl00_mainText_ctl09_time')];
    
    myInputs.each(function(input) {
      input.addEvent('keyup',this.calculateForm.bind(this));
    },this);
  },
  
  setA: function(e,inputElement) {
    this.A = inputElement.get('value');
    this.A = GetOnlyInts(this.A);
    inputElement.set('value',this.A);
  },
    
  setB: function(e,inputElement) {
    this.B = inputElement.get('value');
    this.B = GetOnlyInts(this.B);
    inputElement.set('value',this.B);
  },
  
  setC: function() {
    this.C = (this.A * 1.08) * this.Z / this.P;
  },
  
  setX: function(e,inputElement) {
    this.X = inputElement.get('value');
    this.X = GetOnlyInts(this.X);
    inputElement.set('value',this.X);
    this.setL(this.X);
  },
  
  // Selecteer de verwachte eindleeftijd bij de huidige leeftijd
  setL: function(x) {
    if(x >= 55 && x <= 80) {
      this.L = tableArr[x].L;
      this.P = $percent(tableArr[x].P);
    } else {
      this.L = tableArr[55].L;
      this.P = $percent(tableArr[55].P);
    }
  },
  
  setY: function(e,inputElement) {
    this.Y = inputElement.get('value');
    this.Y = GetOnlyInts(this.Y);
    inputElement.set('value',this.Y);
  },
  
  calculateForm: function() {
    this.setA(this,$('ctl00_mainText_ctl09_housevalue'));
    this.setB(this,$('ctl00_mainText_ctl09_borrow'));
    this.setX(this,$('ctl00_mainText_ctl09_age'));
    this.setY(this,$('ctl00_mainText_ctl09_time'));
  
    this.setO();
    this.setC();
    if(this.Y == $empty || this.Y == '') {
      this.setExtraMoneyLifetime();
    } else {
      this.setExtraMoney();
    }
  },
  
  setO: function() {
    this.O = (this.A - this.B).round(2);
    $('ctl00_mainText_ctl09_plus').set('value',this.O);
  },
  
  setExtraMoney: function() {
    //this.tmp = (((this.O / this.Y) + (this.O * $percent(2))) - (this.A * this.Z) + (this.B * this.H)).round(2);
    //this.tmp = (((this.O / this.Y) + (this.O * $percent(2))) - (this.A * this.Z) + (this.B * this.H)).round(2);
    this.tmp = ((this.O - this.C * this.Y / (this.L - this.X) + this.B * this.H * this.Y) / this.Y).round(0);
    $('ctl00_mainText_ctl09_extramoney').set('value',this.tmp);
  },
  
  setExtraMoneyLifetime: function() {
    //this.tmp = (((this.O / (this.L - this.X)) + (this.O * $percent(2))) - (this.A * this.Z) + (this.B * this.H)).round(2);
    this.EL = ((this.O - this.C + this.B * this.H * (this.L - this.X)) / (this.L - this.X)).round(0);
    $('ctl00_mainText_ctl09_extramoney').set('value',this.EL);
  }
});


var buyVSrent = new Class({
  initialize: function(form) {  
    this.form = form;
    
    this.A = $empty; // Taxatiewaarde/koopprijs woning
    this.B = $empty; // Huidige hypotheek
    this.C = $empty; // Afkoop levenslange huur
    this.D = 0.8; // Disconteringsfactor (in procenten)
    this.F = $percent(2); // In huur verdisconteerde kosten (in procenten)
    this.H = $empty; // Hypotheekrente (na belastingvoordeel)
    this.J = $empty; // Huur minus renteopbrengst niet betaalde koopprijs
    this.K = $empty; // Kosten per maand in geval van koop
    this.L = 75; // Verwachte eindleeftijd
    this.P = $percent(5.98); // Uitkeringspercentage per 100000 euro
    this.R = $percent(5); // Rente percentage bank/rendement
    this.X = 55; // Leeftijd verkoper/huurder
    this.Y = $empty; // Gewenste looptijd
    this.Z = $percent(6.5); // Huur als percentage van taxatiewaarde
    
    this.H = this.R * $percent(80);
    this.tmp = $empty; // Temp values
    this.RentMonth = $empty; // Tussenberekening voor de huur per maand (A * Z% / 12)
    
    $('ctl00_mainText_ctl09_age').set('value',this.X) // Vul de leeftijd alvast in
    //$('ctl00_mainText_ctl09_percent').set('value',this.R); // Vul het rentepercentage alvast in
    
    // Container with a suggestion toward the client
    this.suggestion = new Element('div',{
      'id':'suggestion'
    });
    
    this.setEvents();
  },
  
  setEvents: function() {
//    $('ctl00_mainText_ctl09_houseprice').addEvent('keyup',this.setA.bindWithEvent(this,$('ctl00_mainText_ctl09_houseprice')));
//    $('ctl00_mainText_ctl09_age').addEvent('keyup',this.setX.bindWithEvent(this,$('ctl00_mainText_ctl09_age')));
    //$('ctl00_mainText_ctl09_percent').addEvent('keyup',this.setR.bindWithEvent(this,$('ctl00_mainText_ctl09_percent')));
    //var myInputs = [$('ctl00_mainText_ctl09_houseprice'),$('ctl00_mainText_ctl09_age'),$('ctl00_mainText_ctl09_percent')];
    var myInputs = [$('ctl00_mainText_ctl09_houseprice'),$('ctl00_mainText_ctl09_age')];
    
    myInputs.each(function(input) {
      input.addEvent('keyup',this.calculateForm.bind(this));
    },this);
  },
  
  setA: function(e,inputElement) {
    this.A = inputElement.get('value');
    this.A = GetOnlyInts(this.A);
    inputElement.set('value',this.A);
    this.A = this.A * 1.08; // 2008 07 09 - Toevoeging n.a.l. nieuwe berekening
  },
  
  setX: function(e,inputElement) {
    this.X = inputElement.get('value');
    this.X = GetOnlyInts(this.X);
    inputElement.set('value',this.X);
    this.setL(this.X);
  },
  
  // Selecteer de verwachte eindleeftijd bij de huidige leeftijd
  setL: function(x) {
    if(x >= 55 && x <= 80) {
      this.L = tableArr[x].L;
      this.P = $percent(tableArr[x].P);
    } else {
      this.L = tableArr[55].L;
      this.P = $percent(tableArr[55].P);
    }
  },
  
  setR: function(e,inputElement) {
    this.R = inputElement.get('value');
    this.R = GetOnlyInts(this.R);
    inputElement.set('value',this.R);
    this.R = $percent(this.R);
  },
  
  /**
   * Calculate all values
   */
  calculateForm: function() {
    this.setA(this,$('ctl00_mainText_ctl09_houseprice'));
    this.setX(this,$('ctl00_mainText_ctl09_age'));
  
    this.setRentMonth();
    this.setBuyRentLifetime();
    this.setRentMinus();
    this.setCosts();
    this.setDifference();
  },
  
  setRentMonth: function() {
    this.RentMonth = (this.A * (this.Z / 12)).round(0);
    $('ctl00_mainText_ctl09_rent').set('value',this.RentMonth);
  },
  
  setBuyRentLifetime: function() {
    //this.tmp = (((this.RentMonth * 12 * (this.L - this.X)) + (this.RentMonth * 6 * (this.L - this.X) * this.R)) / (1 + this.R * (this.L - this.X))).round(2);
    this.C = (this.A * this.Z / this.P).round(0);
    $('ctl00_mainText_ctl09_buyrentlifetime').set('value',this.C);
  },
  
  setRentMinus: function() {
    this.J = (this.RentMonth - (this.A * (this.H / 12))).round(0);
    $('ctl00_mainText_ctl09_rentminus').set('value',this.J);
  },
  
  setCosts: function() {
    this.K = (this.A * (this.F / 12)).round(0);
    $('ctl00_mainText_ctl09_costs').set('value',this.K);
  },
  
  setDifference: function() {
    this.tmp = (this.J - this.K).round(0);
    $('ctl00_mainText_ctl09_difference').set('value',this.tmp);
    
    if(this.tmp == 0) { //Er is geen verschil
      this.suggestion.set('html','Er is geen verschil tussen koop en huur.');
    }
    else if(this.tmp < 0) { // Huur is de goedkoopste oplossing
      this.suggestion.set('html','De goedkoopste oplossing is in dit geval huren.');
    } else { // Koop is de goedkoopste oplossing
      this.suggestion.set('html','De goedkoopste oplossing is in dit geval kopen.');
    }
    
    this.suggestion.inject(this.form.getElements('fieldset')[1],'bottom');
  }
  
});


/**
 * Calculates the percent of a value so you can use it in calculations like:
 * 10 * 10% ---> 10 * $percent(10)
 *
 * @param value   Value to convert to a percent
 * @return        Integer as 0.xxxxxx so you can use it in a calculation
 */
function $percent(value) {
  return (value / 100);
}

/**
 * Only allow numeric values.
 * When a non-numeric character is entered, it will be deleted
 *
 * @param amount  The amount entered by the user
 * @return        The new amount without non-numeric characters
 */
function GetOnlyInts(amount) {
  var numbers = new RegExp('[0-9]');
  var newAmount = '';

  if (amount.test(numbers) != false) newAmount = amount.toInt();
  return newAmount;
}
