function calculatorDays()
{
  this.sSelectYear = null;
  this.sSelectMonth = null;
  this.sSelectDay = null;
  this.type = null;

  this.nMonthValue = null;
  this.sYear = null;
  this.sMonth = null;
  this.nDays = null;

  this.td = new Date();
  this.todayMonth = dateToday.month;
}


calculatorDays.prototype = {

    addElement : function (num)
    {
      var new_element = new Option(num, num, false, false);
      this.sSelectDay.options[num] = new_element;
      this.sSelectDay.options[num].value = num;
    },

    calculateOrigin : function ()
    {
      calculator.calculate("origin");
    },

    calculateDestination : function ()
    {
      calculator.calculate("destination");
    },

    calculate : function (type)
    {
      switch (type)
      {
        case "origin":
          this.sSelectYear = document.forms['form_flying'].elements["name_flying_originYear"];
          this.sSelectMonth = document.forms['form_flying'].elements["name_flying_originMonth"];
          this.sSelectDay = document.forms['form_flying'].elements["name_flying_originDay"];
          break;

        case "destination":
          this.sSelectYear = document.forms['form_flying'].elements["name_flying_destinationYear"];
          this.sSelectMonth = document.forms['form_flying'].elements["name_flying_destinationMonth"];
          this.sSelectDay = document.forms['form_flying'].elements["name_flying_destinationDay"];
          break;

        default : alert('error : "type" in the function calculatorDays.calculate');
      }

      this.nMonthValue = "20" + this.sSelectYear.value + this.sSelectMonth.options[this.sSelectMonth.selectedIndex].value;
      this.sYear = this.nMonthValue.toString().substr(2, 4);
      this.sMonth = this.nMonthValue.toString().substr(6, 2);
      this.nDays = 31;

      if (this.sSelectMonth.options[this.sSelectMonth.selectedIndex].value < this.todayMonth) {
        var newYear = parseInt(this.sSelectYear.options[this.sSelectYear.selectedIndex].value) + parseInt(1);
        //this.sSelectYear.value = newYear;
        this.sSelectYear.options[1].selected = true;
      }

      switch (this.sMonth)
      {
        case "04":
        case "06":
        case "09":
        case "11":
          if(this.sSelectDay.options[28] == null) this.addElement('28');
          if(this.sSelectDay.options[29] == null) this.addElement('29');

          this.nDays = 30;
          this.sSelectDay.options[30] = null;

          break;

        case "22":
          this.nDays = ( (this.sYear % 4) == 0 ? 29: 28);
          for (var less = 30; less >= this.nDays; less --) this.sSelectDay.options[less] = null;
          break;

        default:
          if(this.sSelectDay.options[28] == null) this.addElement('28');
          if(this.sSelectDay.options[29] == null) this.addElement('29');
          if(this.sSelectDay.options[30] == null) this.addElement('30');
      }

      for (var count = 0; count < this.nDays; count++) {
        var check = this.sSelectDay.options[count] || false;
        if (check) {
          this.sSelectDay.options[count].value = this.LZ(count + 1);
          this.sSelectDay.options[count].text = count + 1;
  	    }
      }

      this.checkDate('form_flying', type);
      return;
    },


    calculateDays : function (num, d, m, y)
    {
      var m = (m.substr(0,1) == "0" ? m.substr(1,1) : m);
      var d = (d.substr(0,1) == "0" ? d.substr(1,1) : d);
      var y = y;

      var month = new Array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
      var now = new Date(" " + month[m] + " " + d + ", " + y + "");
      var today = now.getDay();
      var weekdays = new Array("Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi");

      if (document.getElementById("flying_today_" + num)) {
        document.getElementById("flying_today_" + num).innerHTML = weekdays[today];
      }
      return;
    },


    LZ : function (x)
    {
      var x = parseInt(x, 10);
      return (x<0 || x>9 ? "" : "0") + x;
    },


    checkDate : function (form, type)
    {
      if(type == "origin") {
        var num = 1;
        var d = document.forms[form].elements["name_flying_originDay"].value;
        var m = document.forms[form].elements["name_flying_originMonth"].value;
        var y = document.forms[form].elements["name_flying_originYear"].value;
      } else {
        var num = 2;
        var d = document.forms[form].elements["name_flying_destinationDay"].value;
        var m = document.forms[form].elements["name_flying_destinationMonth"].value;
        var y = document.forms[form].elements["name_flying_destinationYear"].value;
      }
      this.calculateDays(num, d, m, y);
    }
}

var calculator = new calculatorDays();