function toggleSubmitButton(f) {
  f.submit.disabled = (parseInt(f.country.value, 10) <= 0) || (parseInt(f.city.value, 10) <= 0);
}

function refreshRooms(room_number) {
  var roomNumberOfChildren = document.getElementById("r" + room_number + "_nc").value;

  for (var i = 2; i <= 3; i++) {
    document.getElementById("room" + i).style.display = (i > room_number ? "none" : "");
    document.getElementById("room_b" + i).style.display = (i > room_number ? "none" : "");
  }
}

function refreshChildrenAges(room_number) {
  var roomNumberOfChildren = document.getElementById("r" + room_number + "_nc").value;

  switch (roomNumberOfChildren) {
    case "0":
      document.getElementById("r" + room_number + "_child_ages").style.display = 'none';
      document.getElementById("r" + room_number + "_child_ages_1_td").style.display = 'none';
      document.getElementById("r" + room_number + "_child_ages_2_td").style.display = 'none';
      break;

    case "1":
      document.getElementById("r" + room_number + "_child_ages").style.display = '';
      document.getElementById("r" + room_number + "_child_ages_1_td").style.display = '';
      document.getElementById("r" + room_number + "_child_ages_2_td").style.display = 'none';
      break;

    case "2":
      document.getElementById("r" + room_number + "_child_ages").style.display = '';
      document.getElementById("r" + room_number + "_child_ages_1_td").style.display = '';
      document.getElementById("r" + room_number + "_child_ages_2_td").style.display = '';
      break;
  }
}

function validateSearchForm(f, msgPleaseSelectCountryAndCity, msgPleaseSelectCity) {

  if (f.country.value < 0) {
    alert(msgPleaseSelectCountryAndCity);
    f.country.focus();
    return;
  }

  //if (f.city.value) {
  //  alert(msgPleaseSelectCity);
  //  f.city.focus();
  //  return;
  //}

  //RegionId=18789&ArrivalDay=14&ArrivalMonthYear=200612&Nights=1&Rooms=1&Adults=2&AffiliateId=58

  //var url = "http://www.e-planettravel.com/Customer/Availability/HotelSearchResults.aspx?";
  //var url = "http://213.219.52.226/Customer/Availability/HotelSearchResults.aspx?";
  var url = "http://www.now-searching.com/Customer/Availability/HotelSearchResults.aspx?";


  // Affiliate ID
  url += "AffiliateId=" + f.affiliate_id.value;

  // Site visitor language of choice
  url += "&lang=" + f.lang.value;

  // Region ID
  var regId = "";
  if(f.district.selectedIndex >= 0) {
    if (f.district.options[f.district.selectedIndex].value == 0) {
      regId = f.city.options[f.city.selectedIndex].value;
    } else {
      regId = f.district.options[f.district.selectedIndex].value;
    }
  } else {
    regId = f.city.options[f.city.selectedIndex].value;
  }
  url += "&RegionId=" + regId;


  // Hotel name
  url += f.hotelname.value != "" ? "&HotelName=" + f.hotelname.value : "";


  // Arrival date and stay duration

  var arrival_day   = f.ad.value;
  var arrival_month = f.am.value;
  if (arrival_month < 10) arrival_month = "0" + arrival_month;
  var arrival_year  = f.yr.value;
  var nights        = f.txt_num_nights.value;
  var noOfRooms     = f.rrr.value;

  url += "&ArrivalDay=" + arrival_day;
  url += "&ArrivalMonthYear=" + arrival_year + arrival_month;
  url += "&Nights=" + nights;
  url += "&Rooms=" + noOfRooms;


  // Transform number of stars to MinRating and MaxRating

  var ratingOptions = "";
  switch (f.category.value) {
    case "1": ratingOptions = "&MinRating=10&MaxRating=20"; break;
    case "2": ratingOptions = "&MinRating=20&MaxRating=30"; break;
    case "3": ratingOptions = "&MinRating=30&MaxRating=40"; break;
    case "4": ratingOptions = "&MinRating=40&MaxRating=50"; break;
    case "5": ratingOptions = "&MinRating=50"; break;
  }
  url += ratingOptions;


  // Get number of rooms and total number of adults
  var room1ch = 0;
  var room2ch = 0;
  var room3ch = 0;
  var noofad1 = 0;
  var noofad2 = 0;
  var noofad3 = 0;
  if (noOfRooms == 1) {
    room1ch = parseInt(f.r1_nc.value);
    noofad1  = parseInt(f.r1_na.value);
  } else if (noOfRooms == 2) {
    room1ch = parseInt(f.r1_nc.value);
    room2ch = parseInt(f.r2_nc.value);
    noofad1  = parseInt(f.r1_na.value);
    noofad2  = parseInt(f.r2_na.value);
  } else if (noOfRooms == 3) {
    room1ch = parseInt(f.r1_nc.value);
    room2ch = parseInt(f.r2_nc.value);
    room3ch = parseInt(f.r3_nc.value);
    noofad1  = parseInt(f.r1_na.value);
    noofad2  = parseInt(f.r2_na.value);
    noofad3  = parseInt(f.r3_na.value);
  }


  // Number of adults
  url += (noofad1 > 0 ? "&Adults=" + noofad1 : "") +
         (noofad2 > 0 ? "&Adults2=" + noofad2 : "") +
         (noofad3 > 0 ? "&Adults3=" + noofad3 : "");

  // Number of children and their ages
  if ((room1ch + room2ch + room3ch) > 0) {
    var childAgeStr = "";
    var age = 0;
    var childcounter = 0;
    var childstr = "";

    // Building ChildAge string for room 1
    if (room1ch > 0) {
      childstr += "&Children=" + room1ch;
      for (var ch = 1; ch <= room1ch; ch++) {
        age = window.document.getElementById("r1_child_ages_" + ch);
        if (age != null) {
          childcounter++;
          childAgeStr += "&Child" + childcounter  + "Age=" + age.value;
        }
      }
    }

    // Building ChildAge string for room 2
    childcounter = 0;
    if (room2ch > 0) {
      childstr += "&Children2=" + room2ch;
      for (var ch = 1; ch <= room2ch; ch++) {
        age = window.document.getElementById("r2_child_ages_" + ch);
        if (age != null) {
          childcounter++;
          childAgeStr += "&Child" + childcounter  + "Age2=" + age.value;
        }
      }
    }

    // Building ChildAge string for room 3
    childcounter = 0;
    if (room3ch > 0) {
      childstr += "&Children3=" + room3ch;
      for (var ch = 1; ch <= room3ch; ch++) {
        age = window.document.getElementById("r3_child_ages_" + ch);
        if (age != null) {
          childcounter++;
          childAgeStr += "&Child" + childcounter  + "Age3=" + age.value;
        }
      }
    }

    url += childstr + childAgeStr;
  }


  //alert(url.replace(/&/g, "\n"));
  //f.search_url.value = url;
  //return true;
  document.location = url;
}

