JALHotels.ReservationModule.International = function(formId, hotels) {
  this.f = $('#' + formId);
  this.now = new Date();
  this.hotels = {};
  this.hotel = {};
  this.pageParameters;
}

JALHotels.ReservationModule.International.prototype = {

  setMultipleHotels : function(hotels, pageParameters) {
    this.pageParameters = pageParameters;
    $('select[name=country]', this.f).val('none');
    this.resetHotel($('select[name=hotel]', this.f));
    this.setHotelNone($('select[name=hotel]', this.f));
    this.setDates();
    this.setHotels(hotels);
    this.addCountryListener();
    this.addDateListener();
    this.addSubmitListener();
  },

  setSingleHotel : function(hotel, pageParameters) {
    this.hotel = hotel;
    this.pageParameters = pageParameters;
    this.setDates();
    this.setHotel(hotel);
    this.addDateListener();
    this.addSubmitListener();
  },

  setDates : function() {
    var checkIn = new Date(this.now.getFullYear(), this.now.getMonth(), this.now.getDate());
    var checkOut = new Date(this.now.getFullYear(), this.now.getMonth(), this.now.getDate() + 1);
    $('input[name=arriveYear]', this.f).val(checkIn.getFullYear());
    $('select[name=arriveMonth]', this.f).val(checkIn.getMonth() + 1);
    this.setDayOptions($('select[name=arriveDate]', this.f));
    $('select[name=arriveDate]', this.f).val(checkIn.getDate());
    $('input[name=departYear]', this.f).val(checkOut.getFullYear());
    $('select[name=departMonth]', this.f).val(checkOut.getMonth() + 1);
    this.setDayOptions($('select[name=departDate]', this.f));
    $('select[name=departDate]', this.f).val(checkOut.getDate());
  },

  resetHotel : function(el) {
    el.empty();
  },

  setHotelNone : function(el) {
    el.append('<option value="none">ホテルを選択してください</option>');
  },

  setHotels : function(hotels)  {
    for(var i in hotels) {
      if (this.hotels[hotels[i].country] == undefined) this.hotels[hotels[i].country] = [];
      this.hotels[hotels[i].country][i] = hotels[i].code;
      this.hotels[hotels[i].code] = hotels[i];
    }
  },

  setHotel : function(hotel) {
    $('input[name=hotel]', this.f).val(hotel.code);
  },

  addCountryListener : function() {
    var self = this;
    $('select[name=country]', self.f).change(function(e) {
      self.resetHotel($('select[name=hotel]', self.f));
      if ($(this).val() != 'none') {
        var hotelsByCountry = self.hotels[$(this).val()];
        
        for(var i in hotelsByCountry) {
          var hotel = self.hotels[hotelsByCountry[i]];
          var option = '<option value="' + hotel.code + '">' + hotel.name + '</option>';
          $('select[name=hotel]', self.f).append(option);
        }
      }
      else {
        self.setHotelNone($('select[name=hotel]', this.f));
      }
    });
  },

  setDayOptions : function(el) {
    var self = this;
    var prefix = $(el).attr('name').replace('Date', '');
    var firstDayOfNextMonth = new Date(
      $('input[name=' + prefix + 'Year]', self.f).val(),
      $('select[name=' + prefix + 'Month]', self.f).val(), // next month
      1);
    var lastDay = (new Date(firstDayOfNextMonth -1)).getDate();
    var option = '';
    $('select[name=' + prefix + 'Date]', self.f).empty();
    for(i = 1; i <= lastDay; i ++) {
      option += '<option value="' + i + '">' + i + '</option>';
    }
    $('select[name=' + prefix + 'Date]', self.f).append(option);
  },

  addDateListener : function() {
    var self = this;

    // when check-in month changes, update date select otions
    $('select[name=arriveMonth]', self.f).change(function(e) {
      var ciDateD = $('select[name=arriveDate]', self.f).val();
      self.setDayOptions($('select[name=arriveDate]', self.f));
      $('select[name=arriveDate]', self.f).val(ciDateD);
    });

    // when check-out month changes, update date select options
    $('select[name=departMonth]', self.f).change(function(e) {
      var coDateD = $('select[name=departDate]', self.f).val();
      self.setDayOptions($('select[name=departDate]', self.f));
      $('select[name=departDate]', self.f).val(coDateD);
    });

    $('select[name=arriveMonth],select[name=arriveDate],select[name=departMonth],select[name=departDate]', self.f).change(function(e) {
      var depDate = new Date(
        self.now.getFullYear(),
        $('select[name=arriveMonth]', self.f).val() - 1,
        $('select[name=arriveDate]', self.f).val(),23,59,59);
      var retDate = new Date(
        self.now.getFullYear(),
        $('select[name=departMonth]',self.f).val() - 1,
        $('select[name=departDate]', self.f).val(),23,59,59);
      if (depDate.getTime() < self.now.getTime()) {
        depDate.setFullYear(self.now.getFullYear() + 1);
      }
      if (retDate.getTime() < self.now.getTime()) {
        retDate.setFullYear(self.now.getFullYear() + 1);
      }
      $('input[name=arriveYear]', this.f).val(depDate.getFullYear());
      $('input[name=departYear]', this.f).val(retDate.getFullYear());
    });

    // whenever the check-in date changes, update the check-out date select fields
    $('select[name=arriveMonth],select[name=arriveDate]', self.f).change(function(e) {

      var firstDayOfNextMonth = new Date(
        $('input[name=arriveYear]', self.f).val(),
        $('select[name=arriveMonth]', self.f).val(), // next month
        1);
      var lastDay = (new Date(firstDayOfNextMonth -1)).getDate();
      if (Number($('select[name=arriveDate]', self.f).val()) <= lastDay) {
        var nextDay = new Date(
          $('input[name=arriveYear]', self.f).val(),
          Number($('select[name=arriveMonth]', self.f).val()) - 1,
          Number($('select[name=arriveDate]', self.f).val()) + 1);
      }
      else {
        // add logic to set check-in to the 1st of the month
        var nextDay = new Date(
          $('input[name=arriveYear]', self.f).val(),
          Number($('select[name=arriveMonth]', self.f).val()) - 1,
          2);
      }
      $('input[name=departYear]', self.f).val(nextDay.getFullYear());
      $('select[name=departMonth]', self.f).val(nextDay.getMonth() + 1);
      self.setDayOptions($('select[name=departDate]', self.f));
      $('select[name=departDate]', self.f).val(nextDay.getDate());
    });
  },

  addSubmitListener : function() {
    var self = this;
    $(self.f).submit(function(e) {
      e.preventDefault();
      var uri = self.f.attr('action');
      // check if hotel is a SELECT elem or an INPUT elem
      var hotelCode = 'none';
      if ($('select[name=hotel]', self.f).length > 0) {
        hotelCode = $('select[name=hotel]', self.f).val();
      }
      else if ($('input[name=hotel]', self.f).length > 0) {
        hotelCode = $('input[name=hotel]', self.f).val();
      }
      if (hotelCode == 'none') return false;
      if (isNaN(hotelCode)) {
        // if the hotel.code is not a number, set dest parameter
        uri += '?' + 'dest=' + hotelCode;
      }
      else {
        uri += '?' + 'Hotel=' + hotelCode;
      }

      // additional hotel parameters
      if ((self.hotels[hotelCode]) &&
          (self.hotels[hotelCode].urlParameters)) {
        uri += self.hotels[hotelCode].urlParameters;
      }
      else if (self.hotel.urlParameters) {
        uri += self.hotel.urlParameters;
      }

      // chain, language, and start page parameters
      uri += '&Chain=9542&locale=ja-JP&start=1';

      // page specific parameters
      if (self.pageParameters) {
        if (self.pageParameters.src) {
          uri += '&src=' + self.pageParameters.src;
        }
        if (self.pageParameters.shell) {
          uri += '&shell=' + self.pageParameters.shell;
        }
      }

      // arrival and departure paramters
      var arrive = $('select[name=arriveMonth]', self.f).val() + '/' +
                   $('select[name=arriveDate]', self.f).val() + '/' +
                   $('input[name=arriveYear]', self.f).val();
      var depart = $('select[name=departMonth]', self.f).val() + '/' +
                   $('select[name=departDate]', self.f).val() + '/' +
                   $('input[name=departYear]', self.f).val();
      uri += '&arrive=' + arrive;
      uri += '&depart=' + depart;

      // number of adults, and number of rooms
      uri += '&adult=' + $('select[name=adult]', self.f).val();
      uri += '&rooms=' + $('select[name=rooms]', self.f).val();
      
      // promotion code
      var promo = 'none';
      if ($('select[name=promo]', self.f).length > 0) {
        promo = $('select[name=promo]', self.f).val();
      }
      else if ($('input[name=promo]', self.f).length > 0) {
        promo = $('input[name=promo]', self.f).val();
      }
      if (promo != 'none') {
        uri += '&promo=' + promo;
      }

      if (self.f.attr('target') != '') {
        var w = window.open(uri);
      }
      else {
        location = uri;
      }
    });
  }
}
