﻿ 
  var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  
  function populateCitySelect( country ) {
    if ( country.length == 0 ) {
      if(document.theForm.Choice.options) {
        document.theForm.Choice.length = 1;
        document.theForm.Choice.options[0] = new Option("Choose a Country");
        document.theForm.Choice.options[0].value = '';
      }
      return;
    }
    if ( country == 'Holland' ) newcountry = 'Netherlands';
    if ( country == 'Britain' ) newcountry = 'England';
    var newOptions = Cities[country];
    if(document.theForm.Choice.options) {
      document.theForm.Choice.length = 1;
      document.theForm.Choice.options[0] = new Option("Choose a city or property:");
      document.theForm.Choice.options[0].value = '';
      document.theForm.Choice.options[0].selected = true;
      for ( i in newOptions ) {
        var thisEntry = newOptions[i];
        var newOption = new Option( thisEntry );
        newOption.value = 'C'+thisEntry+';'+country;
        document.theForm.Choice.options[document.theForm.Choice.length++] = newOption;
      }
    }
  }
  
  function makeValidDate() {
    var year = document.theForm.selYear.options[ document.theForm.selYear.selectedIndex ].value;
    var month = document.theForm.selMonth.options[ document.theForm.selMonth.selectedIndex ].value;
    var day = document.theForm.selDay.options[ document.theForm.selDay.selectedIndex ].value;
    var maxDay = 31;
    if ( month == 4 || month == 6 || month == 9 || month == 11 ) {
      maxDay = 30;
    } else if ( month == 2 ) {
      if ( year%100 != 0 && year%4 == 0 ) {
        maxDay = 29;
      } else {
        maxDay = 28;
      }
    }
    document.theForm.selDay.selectedIndex = Math.min(day, maxDay)-2;
  }
  
  function CheckSearchPanel() {
    var formObj = document.theForm;
    var now = new Date();
    var todaydate = (now.getDate() < 10 )?'0' + now.getDate():now.getDate();
    var todaymonth = (now.getMonth() < 9 )?'0' + (now.getMonth()+1):(now.getMonth()+1);
    var chosendate = formObj.selDay.options[formObj.selDay.selectedIndex].value;
    var chosenmonth = formObj.selMonth.options[formObj.selMonth.selectedIndex].value;
    var chosen = formObj.selYear.options[formObj.selYear.selectedIndex].value + '' + chosenmonth + '' + '' + chosendate;
    var today = now.getFullYear() + '' + todaymonth + '' + '' + todaydate;
    if ( chosen < today ) {
      alert("Please enter dates ahead of today's date.  Thank you.");
      return false;
    } else if( formObj.Choice.length && !formObj.Choice.options.selectedIndex ) {
      alert("You haven't chosen a city or hostel.");
      return false;
    } 
    
    groupPanel = document.getElementById('BE_SP_GroupTypes');
    if(null !== groupPanel) {
      if(groupPanel.style.display != 'none') {
        if(!formObj.GroupType.selectedIndex) {
          alert("Please specify a type of group..");
          return false;
        }
        if(!formObj.AgeRanges012.checked && !formObj.AgeRanges1218.checked && !formObj.AgeRanges1821.checked && !formObj.AgeRanges2135.checked && !formObj.AgeRanges3550.checked && !formObj.AgeRanges50.checked) {
          alert("Please specify an age range for your group..");
          return false;
        }
      }
    }
    return true;
  }
  
  function toggleGroupPanel(state) {
    var groupPanelTypes = document.getElementById('BE_SP_GroupTypes');
    var groupPanelAges = document.getElementById('BE_SP_AgeRanges');
    var sel1 = document.getElementById('PersonsSelect1');
    var sel2 = document.getElementById('PersonsSelect2');
    if(state) {
      groupPanelTypes.style.display = '';
      groupPanelAges.style.display = '';
      if(sel1 && sel2) {
        sel1.disabled=true;
        sel2.disabled=false;
        sel1.style.display = 'none';
        sel2.style.display = '';
        if(sel2.options[sel2.selectedIndex].value=='NOGROUP'){
          sel2.selectedIndex=1;
        }
      }
    } else {
      groupPanelTypes.style.display = 'none';
      groupPanelAges.style.display = 'none';
      if(sel1 && sel2) {
        sel1.disabled=false;
        sel2.disabled=true;
        sel1.style.display = '';
        sel2.style.display = 'none';
        if(sel1.options[sel1.selectedIndex].value=='GROUP'){
          sel1.selectedIndex=1;
        }
      }
    }
  }
  
  function SwitchTypes(element) {
    if (element.checked == true) {
      if(document.theForm.SelPropTypes_HOSTEL) document.theForm.SelPropTypes_HOSTEL.checked = true;
      if(document.theForm.SelPropTypes_HOTEL) document.theForm.SelPropTypes_HOTEL.checked = true;
      if(document.theForm.SelPropTypes_GUESTHOUSE) document.theForm.SelPropTypes_GUESTHOUSE.checked = true;
      if(document.theForm.SelPropTypes_APARTMENT) document.theForm.SelPropTypes_APARTMENT.checked = true;
      if(document.theForm.SelPropTypes_CAMPSITE) document.theForm.SelPropTypes_CAMPSITE.checked = true;
    } else {
      if(document.theForm.SelPropTypes_HOSTEL) document.theForm.SelPropTypes_HOSTEL.checked = false;
      if(document.theForm.SelPropTypes_HOTEL) document.theForm.SelPropTypes_HOTEL.checked = false;
      if(document.theForm.SelPropTypes_GUESTHOUSE) document.theForm.SelPropTypes_GUESTHOUSE.checked = false;
      if(document.theForm.SelPropTypes_APARTMENT) document.theForm.SelPropTypes_APARTMENT.checked = false;
      if(document.theForm.SelPropTypes_CAMPSITE) document.theForm.SelPropTypes_CAMPSITE.checked = false;
    }
  }
  
  function SwitchAllTypes(element) {
    if (element.checked == false) {
      document.theForm.ShowAll.checked = false;
    } else {
      var selectAll = true;
      if(document.theForm.SelPropTypes_HOSTEL && !document.theForm.SelPropTypes_HOSTEL.checked) selectAll = false;
      if(document.theForm.SelPropTypes_HOTEL && !document.theForm.SelPropTypes_HOTEL.checked) selectAll = false;
      if(document.theForm.SelPropTypes_GUESTHOUSE && !document.theForm.SelPropTypes_GUESTHOUSE.checked) selectAll = false;
      if(document.theForm.SelPropTypes_APARTMENT && !document.theForm.SelPropTypes_APARTMENT.checked) selectAll = false;
      if(document.theForm.SelPropTypes_CAMPSITE && !document.theForm.SelPropTypes_CAMPSITE.checked) selectAll = false;
      document.theForm.ShowAll.checked = selectAll;
    }
  }
populateCitySelect( "Netherlands" );
  
    function checkGroup(menu) {
      if(menu.options[menu.selectedIndex].value == 'GROUP')
        toggleGroupPanel(1);
      if(menu.options[menu.selectedIndex].value == 'NOGROUP')
        toggleGroupPanel(0);
    }
    
    function ShowFancyResult(form) {
      if (CheckSearchPanel()) {
        var formObj = document.theForm;
        var data_post = $(formObj).serialize();
        var url = $(formObj).attr('action');
	      $.fancybox({
		      type		: 'iframe',
		      href		: url+'?'+data_post,
		      width: 720,
		      height: 445,
		      onStart: function() {
		        $('iframe').hide(); // fix displaying youtube flash-player over popup window
		      },
		      onClosed: function() {
		        $('iframe').show();
		      }
	      });
      }
    }

