var xmlDoc;
var temp;
var dropdown = document.getElementById('smart');
var testPattern;
var hotelStartText = 'Hotel or Resort Name';

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function searchWebService(){
  $.ajax({
    type: "POST",
    data: {"brandId": -1},
    url: "/searchData/searchData.asmx/getData",
    datatype: "xml",
    success: loadData
  });
}

function loadData(xml){
  xmlDoc = xml;
}

function bindCalendars(){
  jQuery("#ctl00_ContentPlaceHolder1_PHGreservations1_txbxArive").datepicker({
        buttonImageOnly: true,
        showButtonPanel: true,
        numberOfMonths: 2
    });
  jQuery("#ctl00_ContentPlaceHolder1_PHGreservations1_txbxDepart").datepicker({
        buttonImageOnly: true,
        showButtonPanel: true,
        numberOfMonths: 2
    });
  jQuery('body').click(function(){
    jQuery('#smart').removeClass('show');
  });
  jQuery('#smart').click(function(event){event.stopPropagation();});
  jQuery('#cal_for_txbxArive').click(function(){
    jQuery('#ctl00_ContentPlaceHolder1_PHGreservations1_txbxArive').focus();
  });
  jQuery('#cal_for_txbxDepart').click(function(){
    jQuery('#ctl00_ContentPlaceHolder1_PHGreservations1_txbxDepart').focus();
  });
  $('#ctl00_ContentPlaceHolder1_PHGreservations1_txbxHotel')
       .focusin(function(){
         if($(this).val() == hotelStartText){
           $(this).val('');
         }
       })
       .focusout(function(){
         if($(this).val() == ''){
           $(this).val(hotelStartText);
         }
       });
}

function searchHotel(obj){
  var city = document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlCity').options[document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlCity').selectedIndex].value;
  var state = document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlState').options[document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlState').selectedIndex].value;
  var country = document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlCountry').options[document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlCountry').selectedIndex].value;
  var hotels;
  var hotelsExist = false;
  if(city != ''){
    hotels = $(xmlDoc).find("City:contains('" + city + "')").parent();
  }else if(state != ''){
    hotels = $(xmlDoc).find("State:contains('" + state + "')").parent();
  }else if(country != ''){
    hotels = $(xmlDoc).find("Country:contains('" + country + "')").parent();
  }else{
    hotels = $(xmlDoc).find('full');
  }
  testPattern = new RegExp('(' + obj.value.toLowerCase() + ')');
  $('#smart').html('');
  if (obj.value.length > 0){
    $(hotels).each(function(){
      temp = jQuery(this).find('Name').text();
      if (testPattern.exec(temp.toLowerCase())){
        $('#smart').append('<li><a href="#" onclick="return pickHotel(\'' + temp.replace("'","\\'") + '\', \'' + jQuery(this).find('City').text() + '\', \'' + jQuery(this).find('State').text() + '\', \'' + jQuery(this).find('Country').text() + '\', \'' + jQuery(this).find('ID').text() + '\')">' + temp + '</a></li>');
        hotelsExist = true;
      }
    });
    if(hotelsExist){
      jQuery('#smart').addClass('show');
    }else{
      $('#ctl00_ContentPlaceHolder1_PHGreservations1_propertyCode').val('');
    }
  }
}

function searchCity(city){
  $('#smart').html('');
  getDetails('', '', city.options[city.selectedIndex].value);
}

function searchState(state){
  $('#smart').html('');
  getDetails('', state.options[state.selectedIndex].value, '');
}

function searchCountry(country){
  $('#smart').html('');
  getDetails(country.options[country.selectedIndex].value, '', '');

}
function getDetails(country, state, city){
  var hotel;
  if(city != ''){
    hotel = $(xmlDoc).find("City:contains('" + city + "')").first().parent();
    setDropDowns($(hotel).find('Country').text(), $(hotel).find('State').text(), city);
  }else if(state != ''){
    hotel = $(xmlDoc).find("State:contains('" + state + "')").first().parent();
    setDropDowns($(hotel).find('Country').text(), state, '');
  }else{
    setDropDowns(country, '', '');
  }
}

function setDropDowns(country, state, city){
  var atState = 0;
  var atCity = 0;
  var stateArray = new Array();
  var cityArray = new Array();

  $(xmlDoc).find('full').each(function(){
    if((country == '')||(country == jQuery(this).find('Country').text())){
      if(jQuery(this).find('State').text() != ''){
        stateArray[atState] = jQuery(this).find('State').text();
        atState++;
      }
      if(((state == '')||(state == jQuery(this).find('State').text()))&&(cityArray[atCity] = jQuery(this).find('City').text() != '')){
        cityArray[atCity] = jQuery(this).find('City').text();
        atCity++;
      }
    }
  });
  // state
  $('#ctl00_ContentPlaceHolder1_PHGreservations1_ddlState').html('');
  $('#ctl00_ContentPlaceHolder1_PHGreservations1_ddlState').prepend('<option value="">All States / Provinces</option>');
  stateArray.sort();
  temp='';
  for(x=0;x<atState;x++){
    if(stateArray[x] != temp){
      $('#ctl00_ContentPlaceHolder1_PHGreservations1_ddlState').append('<option value="' + stateArray[x] + '">' + stateArray[x] + '</option>');
      temp = stateArray[x];
    }
  }
  //  city
  $('#ctl00_ContentPlaceHolder1_PHGreservations1_ddlCity').html('');
  $('#ctl00_ContentPlaceHolder1_PHGreservations1_ddlCity').prepend('<option value="">All Cities</option>');
  cityArray.sort();
  temp='';
  for(x=0;x<atCity;x++){
    if(cityArray[x] != temp){
      $('#ctl00_ContentPlaceHolder1_PHGreservations1_ddlCity').append('<option value="' + cityArray[x] + '">' + cityArray[x] + '</option>');
      temp = cityArray[x];
    }
  }
  setSelect(document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlCity'), city);
  setSelect(document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlState'), state);
  setSelect(document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlCountry'), country);
}

function pickHotel(hotelname, city, state, country, id){
  jQuery('#smart').html('');
  jQuery('#ctl00_ContentPlaceHolder1_PHGreservations1_txbxHotel').val(hotelname);
  jQuery('#ctl00_ContentPlaceHolder1_PHGreservations1_propertyCode').val(id);
  jQuery('#smart').removeClass('show');
//  jQuery('#clearAll').addClass('show');
  setSelect(document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlCity'), city);
  setSelect(document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlState'), state);
  setSelect(document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlCountry'), country);
  return false;
}

function setSelect(ddl, selectedValue){
  for(x=0; x<ddl.options.length; x++){
    if(ddl.options[x].value == selectedValue){
      ddl.selectedIndex = x;
      break;
    }
  }
}

function clearAll(){
//  jQuery('#clearAll').removeClass('show');
  $('#smart').html('');
  setDropDowns('','','');
  $('#ctl00_ContentPlaceHolder1_PHGreservations1_txbxHotel').val('');
  $('#ctl00_ContentPlaceHolder1_PHGreservations1_propertyCode').val('');
  document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlCity').selectedIndex = 0;
  document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlState').selectedIndex = 0;
  document.getElementById('ctl00_ContentPlaceHolder1_PHGreservations1_ddlCountry').selectedIndex = 0;
  return false;
}

function clearHotelName(hotelName){
  if(hotelName.value == 'Hotel or Resort Name'){
    hotelName.value = '';
  }
}

addLoadEvent(bindCalendars);
addLoadEvent(searchWebService);