function launchBBSearch(form)
{
    params = '?r='+form.r.value;
    params += '&search_type='+form.search_type.value;
    params += '&departure_city='+form.departure_city.value;
    params += '&arrival_city='+form.arrival_city.value;
    params += '&departure_month='+getSelectionValue(form.departure_month);
    params += '&departure_day='+getSelectionValue(form.departure_day);
    params += '&return_month='+getSelectionValue(form.return_month);
    params += '&return_day='+getSelectionValue(form.return_day);
    params += '&submit='+form.submitButton.value;
    params += '&num_travelers='+getSelectionValue(form.num_travelers);
    
    var w = window.open('http://rd.bookingbuddy.com'+params, 'BBWin');
    w.focus();
    return true;
}
function getSelectionValue(list)
{
    index = list.selectedIndex;
    return list.options[index].value;
}
function displayCompareForm(mode) {
    var dbbw = document.getElementById('bb_widget');
    if (dbbw) dbbw.className = mode;
}
function changeDisabled(element, state) {
    var el = document.getElementById(element);
    if(el) el.disabled=state;
}
var months = new Array("Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec");
//writeMonthOptions only gets executed once on page load to populate the month dropdowns and set the initial dates
function writeMonthOptions() {
   var today = new Date();
   var future = new Date(today);
   future.setFullYear(future.getFullYear() + 1);
   var dropDownMonths = new Array('air_departure_month', 'air_return_month');
   var dropDownDays = new Array('air_departure_day', 'air_return_day');
   var i = 0;
   var ddM = dropDownMonths.length;
   var ddD = dropDownDays.length;
   var optionValue;
   var optionText;
   var dropDownDay;

   //Write days for the current month to all day dropdowns
   for(n=0;n<ddD;n++) {
    dropDownDay = document.getElementById(dropDownDays[n]);
    writeDayOptions(today, dropDownDay);
    dropDownDay.selectedIndex = today.getDate() - 1;
   }
   
   //Write months for the next year, starting with the current month, to all month dropdowns
   while (today.getTime() < future.getTime()) {
      optionText = months[today.getMonth()];
      optionValue = '0' + (today.getMonth() + 1) + ' ' + today.getFullYear();
      
      for(n=0;n<ddM;n++) {
  		    dropDownMonth = document.getElementById(dropDownMonths[n]);
  		    dropDownMonth.options[i] = new Option(optionText, optionValue);
  	        dropDownMonth.selectedIndex = 0;
      }
      i++;
      today.setMonth(today.getMonth() + 1);
   }

   //The date of the depart dropdowns should be +7 days
   var dropDownDaysDepart = new Array('air_departure_day');
   var dropDownMonthsDepart = new Array('air_departure_month');
   var dDDR = dropDownDaysDepart.length;
   var dropDownMonth;
   var days;
   var newDay;
   for(n=0;n<dDDR;n++) {
    dropDownDay = document.getElementById(dropDownDaysDepart[n]);
    days = dropDownDay.options.length;
    newDay = dropDownDay.selectedIndex + 7;
    if(newDay >= days) {
	    dropDownMonth = document.getElementById(dropDownMonthsDepart[n]);
	    dropDownMonth.selectedIndex++;
	    dropDownDay.selectedIndex = newDay - days;
    } else {
	    dropDownDay.selectedIndex = newDay;
    }
   }

   //The date of the return dropdowns should be +14 days
   var dropDownDaysReturn = new Array('air_return_day');
   var dropDownMonthsReturn = new Array('air_return_month');
   var dDDR = dropDownDaysReturn.length;
   var dropDownMonth;
   var days;
   var newDay;
   for(n=0;n<dDDR;n++) {
    dropDownDay = document.getElementById(dropDownDaysReturn[n]);
    days = dropDownDay.options.length;
    newDay = dropDownDay.selectedIndex + 14;
    if(newDay >= days) {
	    dropDownMonth = document.getElementById(dropDownMonthsReturn[n]);
	    dropDownMonth.selectedIndex++;
	    dropDownDay.selectedIndex = newDay - days;
    } else {
	    dropDownDay.selectedIndex = newDay;
    }
   }
}

function writeDayOptions(date, dropdown) {
      var days = 32 - new Date(date.getFullYear(), date.getMonth(), 32).getDate();
      dropdown.options.length = 0;
      for(x=0;x<days;x++) {
          dropdown.options[x] = new Option(x+1, x+1);
      }
}

function changeMonth(dropDownMonth, dropDownDay) {
    var value = dropDownMonth.options[dropDownMonth.selectedIndex].value;
    var month = value.substring(1,2) - 1;
    var year = value.substring(3,7);
    var theDate = new Date(year, month);
    writeDayOptions(theDate, document.getElementById(dropDownDay));
}

function setBBInput(set_field, fieldvalue) {
    var g = document.getElementById(set_field);
    if(g!==null) { g.value = fieldvalue; }
}

window.onload = function() {
    writeMonthOptions();
}


