if (!Array.indexOf){
  Array.prototype.indexOf = function(obj) {
    for(var i=0; i<this.length; i++) {
      if(this[i]===obj) {
        return i;
      }
    }

    return -1;
  }
}

function jsEnabled() {
  $j("body").addClass("js-enabled");
}

function initAuxNav() {
  $j('#login').click( function() {
    if(!$j('#login-panel').is(':visible')) {
      if($j('#flags').is(':visible'))
        $j('#flags').slideUp('fast');

          $j('#login-panel').slideDown('fast');
          return false;
        } else {
      $j('#login-panel').slideUp('fast');
      return false;
    }

  } );

  $j('#choose-country').click( function() {
    if(!$j('#flags').is(':visible')) {
      if($j('#login-panel').is(':visible'))
        $j('#login-panel').slideUp('fast');

          $j('#flags').slideDown('fast');
          return false;
        } else {
      $j('#flags').slideUp('fast');
      return false;
    }

  } );

  $j('#forgot-pwd').click( function() {
    $j('#pwd-form').show();
    $j('#login-form').hide();
    return false;
  } );

  $j('#return-to-login').click( function() {
    $j('#login-form').show();
    $j('#pwd-form').hide();
    return false;
  } );
};

function initAuxAction() {
  $j('#aux-action li').hover(function() {
    $j(this).addClass('aux-action-hover');
  }, function() {
    $j(this).removeClass('aux-action-hover');
   });

   $j('#aux-action li').bind('click', function() {
    var links = $j(this).find('a');
    if (links[0].href) {
      window.location = links[0].href;
    }
  });
}

// creates the curved corners on the popular places tags
function initCorners() {
  $j("#popular-places li").wrapInner('<span class="TR"><span class="BL"><span class="BR"></span></span></span>');
};

function initDestination() {
  // set initial page state
  $j('#specific').addClass('inactive-field');
  
  $j('#FABCountryChoice').click( function(){ 
    $j(this).removeClass('inactive-field'); 
    $j('#FABChoice').removeClass('inactive-field'); 
    $j('#specific').addClass('inactive-field');
   } );
  
  $j('#specific').click( function(){ 
      $j(this).removeClass('inactive-field'); 
    $j('#FABCountryChoice').addClass('inactive-field');
    $j('#FABChoice').addClass('inactive-field'); 
   } );
  
   $j('#specific').keydown( function(){ } );
};

function initDropDown() {  
  
/*  $j('#d-nav li span').bind('click', function() {
    if($j(this).hasClass('down')) {
      $j(this).removeClass('down');
      $j(this).addClass('up');
      $j('#d-sub-nav').slideDown('fast').show();
      //Note the iframe is required to solve a rendering issue
      //http://weblogs.asp.net/bleroy/archive/2005/08/09/how-to-put-a-div-over-a-select-in-ie.aspx
      if($j.browser.msie && $j.browser.version < 7 ) {
        $j('#d-sub-nav').append('<iframe></iframe>');
      }
    } else {
      $j(this).removeClass('up');
      $j(this).addClass('down');
      $j('#d-sub-nav').slideUp('slow');
      
      if($j.browser.msie && $j.browser.version < 7 ) {
        $j('#d-sub-nav iframe').remove();
      }
    }
  });*/
  $j('#d-btn').bind('click', function() {
    if($j(this).hasClass('down')) {
      $j(this).removeClass('down');
      $j(this).addClass('up');
      $j('#d-sub-nav').slideDown('fast').show();
      //Note the iframe is required to solve a rendering issue
      //http://weblogs.asp.net/bleroy/archive/2005/08/09/how-to-put-a-div-over-a-select-in-ie.aspx
      if($j.browser.msie && $j.browser.version < 7 ) {
        $j('#d-sub-nav').append('<iframe></iframe>');
      }
    } else {
      $j(this).removeClass('up');
      $j(this).addClass('down');
      $j('#d-sub-nav').slideUp('slow');

      if($j.browser.msie && $j.browser.version < 7 ) {
        $j('#d-sub-nav iframe').remove();
      }
    }
  });
  $j('#gu-btn').bind('click', function() {
    if($j(this).hasClass('down')) {
      $j(this).removeClass('down');
      $j(this).addClass('up');
      $j('#gu-sub-nav').slideDown('fast').show();
      //Note the iframe is required to solve a rendering issue
      //http://weblogs.asp.net/bleroy/archive/2005/08/09/how-to-put-a-div-over-a-select-in-ie.aspx
      if($j.browser.msie && $j.browser.version < 7 ) {
        $j('#gu-sub-nav').append('<iframe></iframe>');
      }
    } else {
      $j(this).removeClass('up');
      $j(this).addClass('down');
      $j('#gu-sub-nav').slideUp('slow');

      if($j.browser.msie && $j.browser.version < 7 ) {
        $j('#gu-sub-nav iframe').remove();
      }
    }
  });
  
  $j('#ts').hover(function(){
	  if($j('#d-btn').hasClass('down')) {
      $j('#d-btn').removeClass('down');
      $j('#d-btn').addClass('up');
      $j('#d-sub-nav').slideDown('fast').show();
      //remove gu
      $j('#gu-btn').removeClass('up');
            $j('#gu-btn').addClass('down');
            $j('#gu-sub-nav').slideUp('slow');
      //Note the iframe is required to solve a rendering issue
      //http://weblogs.asp.net/bleroy/archive/2005/08/09/how-to-put-a-div-over-a-select-in-ie.aspx
      if($j.browser.msie && $j.browser.version < 7 ) {
        $j('#d-sub-nav').append('<iframe></iframe>');
        $j('#gu-sub-nav iframe').remove();
      }
	  }
	
	}, function(){
  });

  $j('#gu').hover(function(){
    if($j('#gu-btn').hasClass('down')) {
      $j('#gu-btn').removeClass('down');
      $j('#gu-btn').addClass('up');
      $j('#gu-sub-nav').slideDown('fast').show();
      //remove ts
      $j('#d-btn').removeClass('up');
      $j('#d-btn').addClass('down');
      $j('#d-sub-nav').slideUp('slow');
      //Note the iframe is required to solve a rendering issue
      //http://weblogs.asp.net/bleroy/archive/2005/08/09/how-to-put-a-div-over-a-select-in-ie.aspx
      if($j.browser.msie && $j.browser.version < 7 ) {
        $j('#gu-sub-nav').append('<iframe></iframe>');
        $j('#d-sub-nav iframe').remove();
      }
    }

  }, function(){
  });
  
  $j('#d-sub-nav').hover(
			function() { $j('#d-sub-nav').show(); },
			function() { 
				$j('#d-nav li span').removeClass('up');
     		$j('#d-nav li span').addClass('down');
     		$j('#d-sub-nav').slideUp('slow');
      
     		if($j.browser.msie && $j.browser.version < 7 ) {
      		$j('#d-sub-nav iframe').remove();
     		}
   });
  $j('#gu-sub-nav').hover(
      function() { $j('#gu-sub-nav').show(); },
      function() {
        $j('#d-nav li span').removeClass('up');
        $j('#d-nav li span').addClass('down');
        $j('#gu-sub-nav').slideUp('slow');

        if($j.browser.msie && $j.browser.version < 7 ) {
          $j('#gu-sub-nav iframe').remove();
        }
   });
  
  $j('.d-nav-item').hover(function(){
    if($j('#d-nav li span').hasClass('up')) {
      $j('#d-nav li span').removeClass('up');
      $j('#d-nav li span').addClass('down');
      $j('#d-sub-nav').slideUp('slow');
      $j('#gu-sub-nav').slideUp('slow');
      //Note the iframe is required to solve a rendering issue
      //http://weblogs.asp.net/bleroy/archive/2005/08/09/how-to-put-a-div-over-a-select-in-ie.aspx
      if($j.browser.msie && $j.browser.version < 7 ) {
        $j('#d-sub-nav iframe').remove();
        $j('#gu-sub-nav iframe').remove();
      }
	  }
	
	},function(){});

}

function populateFindABedPanel( country ) {
  newcountry = country.replace(/[^0-9a-zA-Z]/g,'');
  newcountry = newcountry.replace(/\'/g,'');

  if ( country.length == 0 ) {
    document.theForm.FABChoice.length = 1;
    document.theForm.FABChoice.options[0] = new Option("");
    document.theForm.FABChoice.options[0].value = '';

    document.theForm.FABChoice.options[1] = new Option("Choose a Country First");
    document.theForm.FABChoice.options[1].value = '';

    return;
  }

  if ( country == 'Holland' ) {
    newcountry = 'Netherlands';
  }

  if ( country == 'Britain' ) {
    newcountry = 'UK';
  }

  newOptions = eval(newcountry+'Array');

  document.theForm.FABChoice.length = 1;
  document.theForm.FABChoice.options[0] = new Option("Choose a city");
  document.theForm.FABChoice.options[0].value = '';

  document.theForm.FABChoice.options[0].selected = true;

  for ( i = 0; i < newOptions.length; i++ ) {
    document.theForm.FABChoice.length = (i + 1);

    thisEntry = newOptions[i];
    newOption = new Option( thisEntry );
    newOption.value = thisEntry + ';' + country;
    document.theForm.FABChoice.options[i+1] = newOption;
  }
}

function showHostelCountries( chosenCountry ) {
  document.theForm.FABCountryChoice.length = 1;
  document.theForm.FABCountryChoice.options[0] = new Option("Choose country");
  document.theForm.FABCountryChoice.options[0].value = '';
  var count = 167;
  for(var i=0; i < count; i++ ) {
    document.theForm.FABCountryChoice.length = i+1;
    newOption = new Option( CountryList[i] );
    newOption.value = CountryList[i];
    document.theForm.FABCountryChoice.options[i+1] = newOption;
  }
  for(var j=0; j < document.theForm.FABCountryChoice.length; j++ ) {
    if(document.theForm.FABCountryChoice.options[j].value == chosenCountry) {
      document.theForm.FABCountryChoice.options[j].selected = true;
    }
  }
  return;
}
    
function checkFindBedPanel() {
  var pattern = new RegExp("(^| )inactive-field( |$)");
  if (!pattern.test(document.theForm.FABCountryChoice.className)) {
    if ( document.theForm.FABCountryChoice.options[document.theForm.FABCountryChoice.selectedIndex].value == '' ) {
      alert("Please select a country.");
      return false;
    }
  }

  var selectedDate = document.theForm.DateStart.value;

  var nowDate = new Date();
  todayDate  = ( nowDate.getDate() < 10 )  ? '0' + (nowDate.getDate())    : (nowDate.getDate());
  todayMonth = ( nowDate.getMonth() < 10 ) ? '0' + (nowDate.getMonth()+1) : (nowDate.getMonth()+1);

  var currentDate = nowDate.getFullYear() + '-' + todayMonth + '-' + todayDate;

  if (selectedDate < currentDate) {
    alert("Please enter a date ahead of today's date, thank you.");
    return false;
  }

  return true;
}

function checkGroupPanel() {
  var frm = document.theForm;

  if (frm.FABChoice.options[frm.FABChoice.selectedIndex].value == "") {
    alert("Please choose a Country and City.");
    return false;
  }

  if (frm.GroupType.selectedIndex == 0) {
    alert("Please specify a type of group.");
    return false;
  }

  if (!frm.AgeRanges012.checked && !frm.AgeRanges1218.checked && !frm.AgeRanges1821.checked &&
     !frm.AgeRanges2135.checked && !frm.AgeRanges3550.checked && !frm.AgeRanges50.checked) {
    alert("Please specify an age range for your group.");
    return false;
  }

  if (frm.Persons.selectedIndex == 0) {
    alert("Please specify a number of people greater than 8.");
    return false;
  }
}

function checkKeyPress(e) {
  if (window.event) keycode = window.event.keyCode;
  else if (e) keycode = e.which;

  if (keycode == 13) {
    populateFindABedPanel(document.getElementById('FABCountryChoice').options[document.getElementById('FABCountryChoice').selectedIndex].value);
    return false;
  } else {
    return true;
  }
}

function checkGroup(menu) { 
  if (menu.options[menu.selectedIndex].value == 'GROUP') {
    document.theForm.submit();
  }
}

function calendarClose(id) {
  var monthNames = new Array ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

  calendarResult = id;

  if (id) {
    document.theForm.DateStart.value = id;

    var parsedDate = id.split ("-");
    document.theForm.DisplayDateStart.value = parsedDate[2]+' '+monthNames[(parsedDate[1]-1)]+' '+parsedDate[0];
  }

  calendarRemove();
}

function updateDateStart (item) {
  var monthNames = new Array ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

  var parsedDate = item.value.split(" ");
  if (parsedDate.length == 3) {
    newMonth = ( (monthNames.indexOf(parsedDate[1])+1) < 10 )  ? '0' + (monthNames.indexOf(parsedDate[1])+1) : (monthNames.indexOf(parsedDate[1])+1);

    if ((monthNames.indexOf(parsedDate[1])+1)) {
      document.theForm.DateStart.value = parsedDate[2] + '-' + newMonth + '-' + parsedDate[0];
    }
  }

  if (!item.value) {
    document.theForm.DateStart.value = '';
  }
}

function calendarRemove() {
  var div = document.getElementById('datesearchcalendar');
  if (div) { 
    div.style.display = 'none';
  }

  targetId = '';
}

var calendarResult = '';
var targetId = '';

function calendar() {
  date = document.theForm.DateStart.value;
  inputBoxId = 'CalendarDate';
  // someones clicked a second time on the spawning link
  var quickRunAway = (targetId == inputBoxId);

  // calendarRemove resets the targetId, so can't be called before the check
  calendarRemove();

  if (quickRunAway) {
    return;
  }

  targetId = inputBoxId;

  var div = document.getElementById('datesearchcalendar');
  var textBox = document.getElementById(targetId);

  calendarPopulate(date);
}

function calendarPopulate(date) {
  var dayNames   = new Array ('Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su');
  var monthNames = new Array ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  var id;
  var styleClass = '';
  var table = '';

  var nowDate = new Date();
  todayDate  = ( nowDate.getDate() < 10 )  ? '0' + (nowDate.getDate())    : (nowDate.getDate());
  todayMonth = ( nowDate.getMonth() < 10 ) ? '0' + (nowDate.getMonth()+1) : (nowDate.getMonth()+1);

  var currentDate = nowDate.getFullYear() + '-' + todayMonth + '-' + todayDate;

  var today = new Date();
  var blah = new Date();

  var parsedDate = date.split ("-");
  if (parsedDate.length != 3) {
    blah.setDate (1);
    blah.setYear (today.getFullYear());
    blah.setMonth (today.getMonth());
  } else {
    blah.setDate(1);
    blah.setYear(parsedDate[0]);
    blah.setMonth((parsedDate[1]-1));
  }

  /* set up the prev and next month links */
  zarg = new Date();
  zarg.setYear(blah.getFullYear());
  zarg.setDate(1);
  zarg.setMonth(blah.getMonth()-1);

  if (zarg.getMonth()+1 == today.getMonth() && (today.getFullYear() == zarg.getFullYear())) {
    zarg.setYear(today.getFullYear()+2);
  }

  prevMonth = "&laquo;&nbsp;<a href='javascript:calendarPopulate(\""+zarg.getFullYear()+"-"+(zarg.getMonth()+1)+"-"+zarg.getDate()+"\")'>"+monthNames[zarg.getMonth()]+"</a>";

  zarg.setYear(blah.getFullYear());
  zarg.setDate(1);
  zarg.setMonth(blah.getMonth()+1);

  if (zarg.getFullYear() > (today.getFullYear()+2) ) {
    zarg.setMonth(today.getMonth());
    zarg.setYear(today.getFullYear());
  }

  nextMonth = "<a href='javascript:calendarPopulate(\""+zarg.getFullYear()+"-"+(zarg.getMonth()+1)+"-"+zarg.getDate()+"\")'>"+monthNames[zarg.getMonth()]+"</a>&nbsp;&raquo;";

  /* links for next and prev year */
  nextYear = "<a href=\"javascript:calendarPopulate(\""+(blah.getFullYear()+1)+"-"+(blah.getMonth()+1)+"-01\")\">&laquo;"+(blah.getFullYear()+1)+"</a>";
  prevYear = "<a href=\"javascript:calendarPopulate(\""+(blah.getFullYear()-1)+"-"+(blah.getMonth()+1)+"-01\")\">"+(blah.getFullYear()-1)+"&raquo;</a>";


  table += '<table class="calendar">\n';
  table += '<tr>';
  table += '<td colspan="7" align="center">';
  table += prevMonth+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  table += monthNames[blah.getMonth()]+'&nbsp;'+blah.getFullYear()+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  table += nextMonth + '</td>';
  table += '</tr>\n';
  table += '<tr>\n';

  for (i=0;i<5;i++) table+='<td align="center">'+dayNames[i]+'</td>';
  for (;i<7;i++) table+='<td align="center">'+dayNames[i]+'</td>';


  table+='</tr>\n';
  table+='<tr>\n';

  // we want to start on a Monday
  if (blah.getDay() != 1) {
    var i=-(blah.getDay()+5)%7;
 
    for (; i < 1; i++) {
      workingDate = new Date(blah);
      workingDate.setDate(i);

      if ((month=(workingDate.getMonth()+1))<10) {
        month='0'+month;
      }

      if ((day=(workingDate.getDate()))<10) {
        day='0'+day;
      }

      id = workingDate.getFullYear() + '-' + month + '-' + day;

      if (id < currentDate) {
        styleClass='calendarInvalidDate';
      } else if (date == id) {
        styleClass='calendarHighlight';
      } else if (today.getDate() == workingDate.getDate() && today.getMonth() == workingDate.getMonth()
         && today.getFullYear() == workingDate.getFullYear()) {
        styleClass='calendarWrongToday';
      } else if ((workingDate.getDay==0)||(workingDate.getDay() == 6)) {
        styleClass='calendarWrongMonthWeekend';
      } else  {
        styleClass='calendarWrongMonthWeekday';
      }

      table+='<td class="'+styleClass+'">';
      if (id < currentDate) {
        table+='x';
      } else {
        table+='<a id="'+id+'" href="javascript:void(0);" onclick="calendarClose(this.id);">'+workingDate.getDate()+'</a>';
      }
      table+='</td>\n';
    }
  } else {
    var i = 1;
  }

  for (;;i++)  {
    workingDate = new Date(blah);
    workingDate.setDate(i);

    if ((month=(workingDate.getMonth()+1))<10) month='0'+month;
    if ((day=(workingDate.getDate()))<10) day='0'+day;
    id = workingDate.getFullYear()+'-'+month+'-'+day;

    // keep going until it's the next month
    if (workingDate.getMonth()!=blah.getMonth()) {
      break;
    }

    if (workingDate.getDay()==1) {
      table+='</tr>\n<tr>\n';
    }

    if (id < currentDate) {
      styleClass='calendarInvalidDate';
    } else if (date == id) {
      styleClass='calendarHighlight';
    } else if ((workingDate.getDay()==0)||(workingDate.getDay()==6)) {
      styleClass='calendarWeekend';
    } else {
      styleClass='calendarWeekday';
    }

    table+='<td class="'+styleClass+'">';
    if (id < currentDate) {
      table+='x';
    } else {
      table+='<a id="'+id+'" href="javascript:void(0);" onclick="calendarClose(this.id);">'+workingDate.getDate()+'</a>';
    }
    table+='</td>\n';
  }

  if (workingDate.getDay()!=1)  {
    for (;;i++) {
      workingDate = new Date(blah);
      workingDate.setDate(i);

      if ((month=(workingDate.getMonth()+1))<10) month='0'+month;
      if ((day=(workingDate.getDate()))<10) day='0'+day;
      id = workingDate.getFullYear()+'-'+month+'-'+day;

      if (id < currentDate) {
        styleClass='calendarInvalidDate';
      } else if (date == id) {
        styleClass='calendarHighlight';
      } else if(today.getDate() == workingDate.getDate() && today.getMonth() == workingDate.getMonth()
         && today.getFullYear() == workingDate.getFullYear()) {
        styleClass='calendarWrongToday';
      } else if ((workingDate.getDay()==0)||(workingDate.getDay()==6)) {
        styleClass='calendarWrongMonthWeekend';
      } else {
        styleClass='calendarWrongMonthWeekday';
      }

      table+='<td class="'+styleClass+'">';
      if (id < currentDate) {
        table+='x';
      } else {
        table+='<a id="'+id+'" href="javascript:void(0);" onclick="calendarClose(this.id);">'+workingDate.getDate()+'</a>';
      }
      table+='</td>\n';
      if (workingDate.getDay()==0) break;
    }
  }

  table+='</tr>\n'

  table+='<tr><td colspan="7" align="center"><a href="javascript:calendarClose(0)">Close</a></td></tr>';
  table+='</table>';

  var div = document.getElementById('datesearchcalendar');
  div.innerHTML = table;
  div.style.display = 'block';
}

function popup(url) {
  window.open(url,'','height=550,width=500,scrollbars=yes,resizable=no')
}

