﻿(function($) {
	$.ibeWidget = function(el, options)
	{
		var base = this;
		
		// Access to jQuery and DOM versions of element
		base.$el = $(el);
		base.el = el;
		
		base.today = new Date();
		
		base.init = function()
		{
			base.options = $.extend({},$.ibeWidget.defaultOptions, options);
			
			//Calculate some of the other options
			base.advancedReservationDate = new Date(base.today.getTime() + (base.options.advancedReservationTime * Date.DAY));
			base.advancedReservationDate.setHours(0);
			base.advancedReservationDate.setMinutes(0);
			base.advancedReservationDate.setSeconds(0);
			base.advancedReservationDate.setMilliseconds(0);
			switch (base.options.formLanguage)
			{
			  case 1 :
			    //English
			    base.options.dateFormat = '%m/%d/%Y';
			    base.options.requiredDatesMessage = "You must choose dates.";
			    base.options.checkinBeforeCheckoutMessage = "Check In must be before Check Out.";
			    break;
			  case 2 :
			    //French
			    base.options.dateFormat = '%d/%m/%Y';
			    base.options.requiredDatesMessage = "Vous devez choisir des dates.";
			    base.options.checkinBeforeCheckoutMessage = "Check In must be before Check Out.";
			    break;
			  case 3 :
			    //German
			    base.options.dateFormat = '%d.%m.%Y';
			    base.options.requiredDatesMessage = "Bitte wählen Sie Termine.";
			    base.options.checkinBeforeCheckoutMessage = "Check In must be before Check Out.";
			    break;
			  case 4 :
			    //Spanish
			    base.options.dateFormat = '%d/%m/%Y';
			    base.options.requiredDatesMessage = "Usted debe seleccionar fechas.";
			    base.options.checkinBeforeCheckoutMessage = "Check In must be before Check Out.";
			    break;
			  case 5 :
			    //Japanese
			    base.options.dateFormat = '%Y/%m/%d';
			    base.options.requiredDatesMessage = "選択してください日付.";
			    base.options.checkinBeforeCheckoutMessage = "Check In must be before Check Out.";
			    break;
			}

      //Setup the input fields and the calendar icons as buttons for the calendars
      Calendar.setup({
        inputField	  : base.options.checkinFieldId,
        button			  : base.options.checkinFieldId,
        onUpdate		  : base.UpdateCheckoutDate,
        ifFormat		  : base.options.dateFormat,
        align				  : 'Br',
        singleClick	  : true,
        weekNumbers	  : false,
        electric      : false,
        cache 			  : true,
        date          : base.advancedReservationDate,
        dateStatusFunc: function(date, y, m , d){
          return base.DateStatusHandler(date,y,m,d);
        }
      });

      Calendar.setup({
        inputField	  : base.options.checkinFieldId,
        button			  : 'checkinCalendarIcon',
        onUpdate		  : base.UpdateCheckoutDate,
        ifFormat		  : base.options.dateFormat,
        align				  : 'Br',
        singleClick	  : true,
        weekNumbers	  : false,
        electric      : false,
        cache 			  : true,
        date          : base.advancedReservationDate,
        dateStatusFunc: function(date, y, m , d){
          return base.DateStatusHandler(date,y,m,d);
        }
      });

      Calendar.setup({
        inputField	  : base.options.checkoutFieldId,
        button			  : base.options.checkoutFieldId,
        onUpdate		  : null,
        ifFormat		  : base.options.dateFormat,
        align				  : 'Br',
        singleClick	  : true,
        weekNumbers	  : false,
        electric      : false,
        cache 			  : true,
        date          : base.advancedReservationDate,
        dateStatusFunc: function(date, y, m , d){
          return base.DateStatusHandler(date,y,m,d);
        }
      });

      Calendar.setup({
        inputField	  : base.options.checkoutFieldId,
        button			  : 'checkoutCalendarIcon',
        onUpdate		  : null,
        ifFormat		  : base.options.dateFormat,
        align				  : 'Br',
        singleClick	  : true,
        weekNumbers	  : false,
        electric      : false,
        cache 			  : true,
        date          : base.advancedReservationDate,
        dateStatusFunc: function(date, y, m , d){
          return base.DateStatusHandler(date,y,m,d);
        }
      });
      
      //Process the general form fields and then pass off the settings and values to the
      //engines custom submit handler
      $("form", base.el).submit(function()
      {
        if ($("#"+base.options.languagesFieldId).length == 1)
          base.options.languagesFieldValue = $("#"+base.options.languagesFieldId).val();
        else
          base.options.languagesFieldValue = "";
        base.options.checkinFieldValue = $("#"+base.options.checkinFieldId).val();
        base.options.checkinDate = Date.parseDate(base.options.checkinFieldValue, base.options.dateFormat);
        base.options.checkoutFieldValue = $("#"+base.options.checkoutFieldId).val();
        base.options.checkoutDate = Date.parseDate(base.options.checkoutFieldValue, base.options.dateFormat);
        base.options.nights = (base.options.checkoutDate.getTime() - base.options.checkinDate.getTime()) / Date.DAY;
        base.options.roomsFieldValue = $("#"+base.options.roomsFieldId).val();
        base.options.adultsFieldValue = $("#"+base.options.adultsFieldId).val();
        base.options.childrenFieldValue = $("#"+base.options.childrenFieldId).val();
        for (var i = 0; i < base.options.customFieldIds.length; i++)
        {
          var sValue = $("#"+base.options.customFieldIds[i]).val();
          base.options.customFieldValues.push(sValue);
        }
        
        if ($.trim(base.options.checkinFieldValue) == "" && $.trim(base.options.checkoutFieldValue) == "")
        {
          alert(base.options.requiredDatesMessage);
          return false;
        }
        else if (base.options.checkoutDate <= base.options.checkinDate)
        {
          alert(base.options.checkinBeforeCheckoutMessage);
          return false;
        }
        else
        {
          if (typeof window.ibeWidgetSubmitFunction == 'function')
          {          
            return window.ibeWidgetSubmitFunction(base.options);
          }
        }
      });
      
		}
		
		base.UpdateCheckoutDate = function(cal)
		{
	    var date = cal.date;
	    var time = date.getTime()
	    var field = document.getElementById(base.options.checkoutFieldId);
	    if (field.value == "")
	    {
		    time += Date.DAY;
		    var date2 = new Date(time);
		    field.value = date2.print(base.options.dateFormat);
	    }
    }
    
    base.DateStatusHandler = function (date, y, m, d)
    {
      var calDate = date.getTime();
      var limitDate = base.advancedReservationDate.getTime();
      
      if (calDate < limitDate)
        return true;  //disable date
      else
        return false;
    }
		
		base.init();
	};
	
	$.ibeWidget.defaultOptions = {
    dateFormat: "%m/%d/%Y",
    requiredDatesMessage: "You must choose dates.",
    checkinBeforeCheckoutMessage: "Check In must be before Check Out.",
    languagesFieldValue: "",
    checkinFieldValue: "",
    checkinDate: null,
    checkoutFieldValue: "",
    checkoutDate: null,
    nights: 0,
    roomsFieldValue: "",
    adultsFieldValue: "",
    childrenFieldValue: "",
    customFieldValues: []
	};
	
	$.fn.ibeWidget = function(options)
	{
		return this.each(function()
		{
			(new $.ibeWidget(this, options));
		});
	}

})(jQuery);

function distributeGuestsInRooms(iRooms, iAdults, iChildren)
{
  var iAdultsLeft = iAdults;
  var iChildrenLeft = iChildren;
  var oRoomAdults = new Array();
  var oRoomChildren = new Array();
  var iRoomIndex = 0;
  
  for (var i = 0; i < iRooms; i++)
  {
    oRoomAdults[i] = 0;
    oRoomChildren[i] = 0;
  }
  
  iRoomIndex = 0;
  while (iAdultsLeft > 0)
  {
    oRoomAdults[iRoomIndex]++;
    iAdultsLeft--;
    
    iRoomIndex++;
    if (iRoomIndex == iRooms)
    {
      iRoomIndex = 0;
    }
  }
  
  iRoomIndex = 0;
  while (iChildrenLeft > 0)
  {
    oRoomChildren[iRoomIndex]++;
    iChildrenLeft--;
    
    iRoomIndex++;
    if (iRoomIndex == iRooms)
    {
      iRoomIndex = 0;
    }
  }
  
  var oRoomDistribution = {
    roomAdults: oRoomAdults,
    roomChildren: oRoomChildren
  };
  
  return oRoomDistribution;
}