// Two arrays used to index the raterange selector.
// The first two elements are zero, which means restrictions will not apply
// See checkrates
var arFromRate = [0, 0,250, 400, 600, 1000];
var arToRate = [0, 250,400,600, 1000, 0];

function redrawScreen(bc) {
  document.form.boxclicked.value = bc;
  document.form.action="TravelBook?action=s";
  document.form.submit();
}

function submitForm() {
  var f = document.form;

  if (!checkdates()) {
    return false;
  }

  var box;
  if (f.elements["box" + f.boxcount.value].value != "0") {
    box = f.elements["box" + f.boxcount.value];
  } else {
    box = f.elements["box" + (Number(f.boxcount.value)-1)];
  }

  if (box.value == "179") {
    alert("Your search is a bit wide. Please choose a sub-location within South Africa.");
    return false;
  }

  f.locationid.value = box.value;
  f.locationtext.value= box.options[box.selectedIndex].text;
  // Set the rate range
  if (f["fromrate"]) {
    f.fromrate.value = arFromRate[f.raterange.selectedIndex];
    f.torate.value = arToRate[f.raterange.selectedIndex];
  }

  return true;
}

function checkdates() {

  df = document.form;

  day = df.nbday.value;
  month = df.nbmonth.value;
  year = df.nbyear.value;

  if (!ValidDate(day, month, year)) {
     alert("The date you have chosen looks incorrect.");
     return false;
   }

  userDate = new Date(year, month-1, day);
  if (!checkDate(userDate)) {
    return false;
  }

  return true;

}

function ValidDate(day, month, year) {

  if ((month == 4)||(month==6)||(month==9)||(month==11)) {
    if (day > 30) {
      return false;
    } 
  } else if (month == 2) {
    if ((year - 1960) % 4 != 0) {
      if ( day > 28 ) {
        return false;
      }
    } else if ( day > 29 ) {
      return false;
    }
  }

  return true;
}

function checkDate(userdt) {
  dt = new Date();
  dt.setHours(0);
  dt.setMinutes(0);
  dt.setSeconds(0);
  dt.setMilliseconds(0);

  if (dt.getTime() > userdt.getTime()) {
    alert("You cannot choose a date earlier than today.");
    return false;
  } else {
    return true;
  }
}
