function isLeapYear(nYear)
{
  if ((nYear % 4) == 0)
  {
    if ((nYear % 100) == 0)
    {
      if ((nYear % 400) == 0)
      {
        return true;
      }
      else
      {
        return false;
      }
    }
    else
    {
      return true;
    }
  }
  else
  {
    return false;
  }
}

/*function setDate(day_id, month_year_id, oDate)
{
  var nDay   = oDate.getDate();
  var nMonth = oDate.getMonth()+1;
  if (nMonth < 10)
  {
    nMonth = '0'+nMonth;
  }
  var nYear  = oDate.getFullYear();
  var sMonthYear = nMonth+'.'+nYear;
  var oDaySel       = document.getElementById(day_id);
  var oMonthYearSel = document.getElementById(month_year_id);

  oDaySel.options.selectedIndex = nDay-1;
  for (var i = 0; i < oMonthYearSel.options.length; i++)
  {
    if (oMonthYearSel.options[i].value == sMonthYear)
    {
      oMonthYearSel.options.selectedIndex = i;
      break;
    }
  }
}

function getDate(day_id, month_year_id)
{
  var oDaySel = document.getElementById(day_id);
  var oMonthYearSel = document.getElementById(month_year_id);
  var nDay = oDaySel.options[oDaySel.options.selectedIndex].value;
  var sMonthYear = oMonthYearSel.options[oMonthYearSel.options.selectedIndex].value;
  var aMonthYear = sMonthYear.split('.');
  var nMonth = aMonthYear[0]-1;
  var nYear = aMonthYear[1];
  var oDate = new Date(nYear, nMonth, nDay);
  return oDate;
}

function checkMaxDay(day_id, month_year_id)
{
  var aMontsMaxDays = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
  var aMontsNames = new Array('January','February','March','April','May','June',
                              'July','August','September','October','November','December');
  var oDaySel = document.getElementById(day_id);
  var oMonthYearSel = document.getElementById(month_year_id);
  var nDay = oDaySel.options[oDaySel.options.selectedIndex].value;
  var sMonthYear = oMonthYearSel.options[oMonthYearSel.options.selectedIndex].value;
  var aMonthYear = sMonthYear.split('.');
  var nMonth = aMonthYear[0]-1;
  var nYear = aMonthYear[1];

  // check date against max day
  var nMaxDay = aMontsMaxDays[nMonth];
  if (nMonth == 1) // February
  {
    if (!isLeapYear(nYear))
    {
      nMaxDay = 28;
    }
  }
  if (nDay > nMaxDay) // selected day is greater than max day
  {
    alert(aMontsNames[nMonth]+' in '+nYear+' has '+nMaxDay+' days.');
    oDaySel.options.selectedIndex = nMaxDay-1;
  }
}

function checkCidMaxDay()
{
  return  checkMaxDay('cid_day', 'cid_month_year');
}

function checkCodMaxDay()
{
  return  checkMaxDay('cod_day', 'cod_month_year');
}

function setCid(oDate)
{
  return setDate('cid_day', 'cid_month_year', oDate);
}

function setCod(oDate)
{
  return setDate('cod_day', 'cod_month_year', oDate);
}

function getCid()
{
  return getDate('cid_day', 'cid_month_year');
}

function getCod()
{
  return getDate('cod_day', 'cod_month_year');
}

function validateDates()
{
  var isValid = true;
  var n24Hours = 1000*60*60*24;
  var n30Days  = n24Hours*30;
  var aMontsNames = new Array('January','February','March','April','May','June',
                              'July','August','September','October','November','December');

  var oCurrDate = new Date();

  checkCidMaxDay();
  checkCodMaxDay();

  var oCid  = getCid();
  var oCod  = getCod();
  var nDiff = (oCod.getTime() - oCid.getTime()) / n24Hours;

  if (oCid < oCurrDate) // check-in date is in the past
  {
    var oFirstPossibleCid = new Date(oCurrDate.getTime() + n24Hours);
    alert('The first possible check-in date is '+oFirstPossibleCid.getDate()+' '
                                                +aMontsNames[oFirstPossibleCid.getMonth()]+' '
                                                +oFirstPossibleCid.getFullYear()+'.');
    setCid(oFirstPossibleCid);
    isValid = false;
    validateDates();
  }


  if (nDiff < 1)
  {
    var oFirstPossibleCod = new Date(oCid.getTime() + n24Hours);
    alert('The first possible check-out date is '+oFirstPossibleCod.getDate()+' '
                                                 +aMontsNames[oFirstPossibleCod.getMonth()]+' '
                                                 +oFirstPossibleCod.getFullYear()+'.');
    isValid = false;
    setCod(oFirstPossibleCod);
  }
  else if (nDiff > 30)
  {
    var oLastPossibleCod = new Date(oCid.getTime() + n30Days);
    alert('The last possible check-out date is '+oLastPossibleCod.getDate()+' '
                                                 +aMontsNames[oLastPossibleCod.getMonth()]+' '
                                                 +oLastPossibleCod.getFullYear()+'.');
    isValid = false;
    setCod(oLastPossibleCod);
  }

  return isValid;
}
*/
function displayChildAges()
{
  var oParentSel = document.getElementById('room_type_sel');
  var oChildSel  = document.getElementById('room_count_sel');

  var room_type  = oParentSel.options[oParentSel.options.selectedIndex].value;
  var room_count = oChildSel.options[oChildSel.options.selectedIndex].value;

  //alert(room_type+' - '+room_count);

  if ((room_type != 'DB') && (room_type != 'TB'))
  {
    // only double & twin rooms may have a child ages
    for (var i = 1; i <= 4; i++)
    {
      var sAgeSelId = 'child_age_'+i;
      var oAgeSel = document.getElementById(sAgeSelId);
      if (oAgeSel)
      {
        //oAgeSel.setAttribute('disabled', 'disabled');
        oAgeSel.options.selectedIndex = 0;
		oAgeSel.style.visibility = "hidden";
      }
    }
    return;
  }

  for (var i = 1; i <= 4; i++)
  {
    var sAgeSelId = 'child_age_'+i;
    var oAgeSel = document.getElementById(sAgeSelId);
    if (oAgeSel)
    {
      if (i > room_count)
      {
        //oAgeSel.setAttribute('disabled', 'disabled');
        oAgeSel.options.selectedIndex = 0;
		oAgeSel.style.visibility = "hidden";
      }
      else
      {
        //oAgeSel.removeAttribute('disabled');
		oAgeSel.style.visibility = "visible";
      }
    }
  }

}

function setRoomCount(nSelected)
{
  var oParentSel = document.getElementById('room_type_sel');
  var oChildSel  = document.getElementById('room_count_sel');

  var room_type  = oParentSel.options[oParentSel.options.selectedIndex].value;

  switch (room_type)
  {
    case 'DB': // double
      var max_users = 4;
      break;
    case 'Q':  // quad
      var max_users = 2;
      break;
    case 'SB': // single
      var max_users = 9;
      break;
    case 'TR': // triple
      var max_users = 3;
      break;
    case 'TB': // twin
      var max_users = 4;
      break;
    case 'TS': // twin for sole use
      var max_users = 9;
      break;
    default:
      alert('Invalid room type.');
      return;
  }

  for (var i = oChildSel.options.length - 1; i >= 0; i--)
  {
    var oOption = oChildSel.options.item(i);
    oChildSel.removeChild(oOption);
  }

  for (var i = 1; i <= max_users; i++)
  {
    var oOption = document.createElement('OPTION');
    oOption.setAttribute('value', i);

    if (i == nSelected)
    {
      oOption.setAttribute('selected', 'selected');
    }

    var oText = document.createTextNode(i);
    oOption.appendChild(oText);
    oChildSel.appendChild(oOption);
  }
  displayChildAges();
}

function submitSearchForm()
{
  var oForm = document.getElementById('search');
  var oHid = document.getElementById('submit1');

  //if (validateDates())
  //{
    oHid.value = 1;
    oForm.submit();
  //}
}


