﻿$(function () {
  var dates = $("#Arrival, #Departure").datepicker({
    minDate: 0,
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 2,
    onSelect: function (selectedDate) {
      var option = this.id == "Arrival" ? "minDate" : "maxDate",
					instance = $(this).data("datepicker"),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings);
      dates.not(this).datepicker("option", option, date);

    }
  });
});


$().ready(function () {
  $('#divRoom2').hide(); $('#divRoom3').hide(); $('#divRoom4').hide(); $('#divAge11').hide(); $('#divAge12').hide(); $('#divAge13').hide(); $('#divAge21').hide(); $('#divAge22').hide(); $('#divAge23').hide(); $('#divAge31').hide(); $('#divAge32').hide(); $('#divAge33').hide(); $('#divAge41').hide(); $('#divAge42').hide(); $('#divAge43').hide();    
    showRooms($('#NumberOfRooms').val());
});

$.widget("custom.catcomplete", $.ui.autocomplete, {
  _renderMenu: function (ul, items) {
    var self = this,
				currentCategory = "";
    $.each(items, function (index, item) {
          if (item.category != currentCategory) {
        ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
        currentCategory = item.category;
      }
      self._renderItem(ul, item);
    });
  }
});


if (typeof String.prototype.trim !== 'function') { String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g, ''); } } 


$(function () {
  $("#Destination").catcomplete({
    source: function (request, response) {
      $.ajax({
        url: "/hotel/locations",
        dataType: "json",
        delay: 100,
        data: {
          maxRows: 10,
          Location: request.term
        },
        success: function (data) {
          response($.map(data.Locations, function (item) {
            return {
              label: item.LocationName + (item.State ? ", " + item.State : "") + (item.Country && item.LocationType != "LANDMARK" ? ", " + item.Country : ""),
              value: item.LocationType == "Airport" ? item.State : item.LocationName + (item.State ? ", " + item.State : ", " + item.Country),
              location: item.LocationName,
              internalid: item.InternalId,
              state: item.State,
              country: (item.Country ? item.Country.trim() : ""),
              type: item.LocationType,
              category: item.LocationType
            }
          }));
        }
      });
    },
    minLength: 2,
    select: function (event, ui) {
      if (ui.item != null) {
        $('#InternalId').val(ui.item.internalid);
        $('#State').val(ui.item.state);
        $('#Country').val(ui.item.Country ? ui.item.Country.trim() : "");
        $('#SearchType').val(ui.item.type);
        if (ui.item.type == "Airport") {
          $('#Location').val(ui.item.location.substring(0, 3));
        } else {
          $('#Location').val(ui.item.location);
        }
      }
    },

    open: function () {
    },
    close: function () {
    }
  });
});









function showRooms(count) {
    if (count > 4) {        
		document.location = "http://groups.hotelsavings.com/GroupForm.cfm";        
    }
    for (var i = 4; i > count; i--) {
        var row = $('#divRoom' + i.toString());
        $('#divRoom' + i.toString()).fadeOut('normal');
    }

    for (var i = 1; i <= count; i++) {
        $('#divRoom' + i.toString()).fadeIn('slow');
        showAges(i);
    }
}

function showAges(roomNumber) {
    var childCount = parseInt($('#NumberOfChildren\\[' + (roomNumber - 1).toString() + '\\]').val());
    for (var j = (childCount + 1); j <= 3; j++) {
        $('#divAge' + roomNumber.toString() + j.toString()).fadeOut('normal');
    }
    for (var j = 1; j <= childCount; j++) {
        $('#divAge' + roomNumber.toString() + j.toString()).fadeIn('slow');
    }
    }


    



   
