<!----------------------------------------------------------------->
<!-- Copyright Kayak.com 2005                                    -->
<!-- $Id: kayak-affiliate.js,v 1.1 2005/03/18 18:28:32 pme Exp $ -->
<!-- See http://developer.kayak.com for details.		 -->
<!----------------------------------------------------------------->

// Show-Hide functions
function selectFlight()
{
  showDiv("flight");
  hideDiv("hotel");
  hideDiv("car");
}

function selectHotel()
{
  hideDiv("flight");
  showDiv("hotel");
  hideDiv("car");
}

function selectCar()
{
  hideDiv("flight");
  hideDiv("hotel");
  showDiv("car");
}

function getObj(divId)
{
  var obj;
  if (document.getElementById)    // DOM3 = IE5, NS6
  { 
    obj = document.getElementById(divId).style;
  }
  else if (document.layers) // Netscape 4
  { 
    obj = document.divId;
  }
  else // IE 4
  { 
    obj = document.all.divId.style; 
  }
  return obj;
}

function showDiv(divId)
{
  var obj = getObj(divId);
  obj.visibility = 'visible';
  obj.display = 'inline';
}

function hideDiv(divId)
{
  var obj = getObj(divId);
  obj.visibility = 'hidden';
  obj.display = 'none';
}

function dateFormat(month, day, year)
{
  return year + "." + month + "." + day;
}

var maxDay = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

// Init kayak widget fields
function initKayak()
{
  var today = new Date();
  var day = today.getDate();
  var month = today.getMonth();
  var baseYear = today.getFullYear();
  for (var i = 0; i < 3; i++)
  {
    document.flightForm.depart_year[i] = new Option(baseYear+i, baseYear+i);
    document.flightForm.return_year[i] = new Option(baseYear+i, baseYear+i);
    document.hotelForm.checkin_year[i] = new Option(baseYear+i, baseYear+i);
    document.hotelForm.checkout_year[i] = new Option(baseYear+i, baseYear+i);
	document.carForm.pickup_year[i] = new Option(baseYear+i, baseYear+i);
    document.carForm.dropoff_year[i] = new Option(baseYear+i, baseYear+i);
  }

  var departDay = day;
  var departMonth = month;
  var departYear = 0;

  if (departDay >= maxDay[departMonth])
  {
    departDay = 0;
    departMonth += 1;
    if (departMonth > 11)
    {
      departMonth = 0;
      departYear += 1;
    }
  }

  var returnDay = departDay + 1;
  var returnMonth = departMonth;
  var returnYear = departYear;

  if (returnDay >= maxDay[returnMonth])
  {
    returnDay = 1;
    returnMonth += 1;
    if (returnMonth > 11)
    {
      returnMonth = 0;
      returnYear += 1;
    }
  }
  
  var pickupDay = day;
  var pickupMonth = month;
  var pickupYear = 0;

  if (pickupDay >= maxDay[pickupMonth])
  {
    pickupDay = 0;
    pickupMonth += 1;
    if (pickupMonth > 11)
    {
      pickupMonth = 0;
      pickupYear += 1;
    }
  }

  var dropoffDay = pickupDay + 1;
  var dropoffMonth = pickupMonth;
  var dropoffYear = pickupYear;

  if (dropoffDay >= maxDay[dropoffMonth])
  {
    dropoffDay = 1;
    dropoffMonth += 1;
    if (dropoffMonth > 11)
    {
      dropoffMonth = 0;
      dropoffYear += 1;
    }
  }

  document.flightForm.depart_month[departMonth].selected = true;
  document.flightForm.depart_day[departDay].selected = true;
  document.flightForm.depart_year[departYear].selected = true;

  document.flightForm.return_month[returnMonth].selected = true;
  document.flightForm.return_day[returnDay].selected = true;
  document.flightForm.return_year[returnYear].selected = true;

  document.hotelForm.checkin_month[departMonth].selected = true;
  document.hotelForm.checkin_day[departDay].selected = true;
  document.hotelForm.checkin_year[departYear].selected = true;

  document.hotelForm.checkout_month[returnMonth].selected = true;
  document.hotelForm.checkout_day[returnDay].selected = true;
  document.hotelForm.checkout_year[returnYear].selected = true;
  
  document.carForm.pickup_month[departMonth].selected = true;
  document.carForm.pickup_day[departDay].selected = true;
  document.carForm.pickup_year[departYear].selected = true;

  document.carForm.dropoff_month[returnMonth].selected = true;
  document.carForm.dropoff_day[returnDay].selected = true;
  document.carForm.dropoff_year[returnYear].selected = true;
  
}

function submitHotel()
{
  if (document.hotelForm.ct.value.length == 0)
  {
    alert("Please enter a city.");
    return false;
  }

  var month = document.hotelForm.checkin_month.value;
  var day = document.hotelForm.checkin_day.value;
  var year = document.hotelForm.checkin_year.value;
  document.hotelForm.d1.value = dateFormat(month, day, year);

  if (day > maxDay[month-1])
  {
    alert("Please enter a valid check-in date.");
    return false;
  }

  month = document.hotelForm.checkout_month.value;
  day = document.hotelForm.checkout_day.value;
  year = document.hotelForm.checkout_year.value;
  document.hotelForm.d2.value = dateFormat(month, day, year);

  if (day > maxDay[month-1])
  {
    alert("Please enter a valid check-out date.");
    return false;
  }

  return true;
}

function submitFlight()
{
  if (document.flightForm.a1.value.length == 0)
  {
    alert("Please enter a departure city or airport.");
    return false;
  }

  if (document.flightForm.a2.value.length == 0)
  {
    alert("Please enter an arrival city or airport.");
    return false;
  }

  var month = document.flightForm.depart_month.value;
  var day = document.flightForm.depart_day.value;
  var year = document.flightForm.depart_year.value;
  document.flightForm.d1.value = dateFormat(month, day, year);

  if (day > maxDay[month-1])
  {
    alert("Please enter a valid departure date.");
    return false;
  }

  month = document.flightForm.return_month.value;
  day = document.flightForm.return_day.value;
  year = document.flightForm.return_year.value;
  document.flightForm.d2.value = dateFormat(month, day, year);

  if (day > maxDay[month-1])
  {
    alert("Please enter a valid return date.");
    return false;
  }

  return true;
}

function submitCar()
{
  if (document.carForm.a1.value.length == 0)
  {
    alert("Please enter a pick-up city.");
    return false;
  }

  var month = document.carForm.pickup_month.value;
  var day = document.carForm.pickup_day.value;
  var year = document.carForm.pickup_year.value;
  document.carForm.d1.value = dateFormat(month, day, year);

  if (day > maxDay[month-1])
  {
    alert("Please enter a valid pick-up date.");
    return false;
  }

  month = document.carForm.return_month.value;
  day = document.carForm.return_day.value;
  year = document.carForm.return_year.value;
  document.carForm.d2.value = dateFormat(month, day, year);

  if (day > maxDay[month-1])
  {
    alert("Please enter a valid return date.");
    return false;
  }

  return true;
}


//Used to clear default text in input boxes.
//  Use the || operator to add other default
//  text to the conditional statement

function clickClear(inputObj) {
  if(inputObj.value == "City, STATE") {
    inputObj.value = "";
  }
}

// Init kayak widget fields
function initKayakOldStyle()
{
  var today = new Date();
  var day = today.getDate();
  var month = today.getMonth();
  var baseYear = today.getFullYear();
  for (var i = 0; i < 3; i++)
  {
    document.flightForm.depart_year[i] = new Option(baseYear+i, baseYear+i);
    document.flightForm.return_year[i] = new Option(baseYear+i, baseYear+i);
    document.hotelForm.checkin_year[i] = new Option(baseYear+i, baseYear+i);
    document.hotelForm.checkout_year[i] = new Option(baseYear+i, baseYear+i);
  }

  var departDay = day;
  var departMonth = month;
  var departYear = 0;

  if (departDay >= maxDay[departMonth])
  {
    departDay = 0;
    departMonth += 1;
    if (departMonth > 11)
    {
      departMonth = 0;
      departYear += 1;
    }
  }

  var returnDay = departDay + 1;
  var returnMonth = departMonth;
  var returnYear = departYear;

  if (returnDay >= maxDay[returnMonth])
  {
    returnDay = 1;
    returnMonth += 1;
    if (returnMonth > 11)
    {
      returnMonth = 0;
      returnYear += 1;
    }
  }

  document.flightForm.depart_month[departMonth].selected = true;
  document.flightForm.depart_day[departDay].selected = true;
  document.flightForm.depart_year[departYear].selected = true;

  document.flightForm.return_month[returnMonth].selected = true;
  document.flightForm.return_day[returnDay].selected = true;
  document.flightForm.return_year[returnYear].selected = true;

  document.hotelForm.checkin_month[departMonth].selected = true;
  document.hotelForm.checkin_day[departDay].selected = true;
  document.hotelForm.checkin_year[departYear].selected = true;

  document.hotelForm.checkout_month[returnMonth].selected = true;
  document.hotelForm.checkout_day[returnDay].selected = true;
  document.hotelForm.checkout_year[returnYear].selected = true;
}
