// What we really need is a map of countries to cities and vice versa, what
// we're going to use is an array!

var gecko = navigator.userAgent.toLowerCase().indexOf("gecko") != -1;

days = new Array(7);
days[0] = 'Sunday';
days[1] = 'Monday';
days[2] = 'Tuesday';
days[3] = 'Wednesday';
days[4] = 'Thursday';
days[5] = 'Friday';
days[6] = 'Saturday';

var allCountries = true;

function clearSelect(select) {
    for (i = select.options.length ; i >= 0 ; --i) {
        select.options[i] = null;
    }
}

    function updateState(location, state) {
      var selected = state.options[state.selectedIndex].value;
      clearSelect(state);
      var value = location.options[location.selectedIndex].value;
      var foundSelected = false;
      if (location.selectedIndex == 0) {
        for (i = 0 ; i < stateCountry.length ; i++) {
          addEntry(state, stateCountry[i][2], stateCountry[i][1]);
          if (stateCountry[i][1] == selected) {
            state.selectedIndex = state.options.length - 1;
            foundSelected = true;
          }
        }
      } else {
        for (i = 0 ; i < stateCountry.length ; i++) {
          if (stateCountry[i][0] != value) continue;
          addEntry(state, stateCountry[i][2], stateCountry[i][1]);
          if (stateCountry[i][1] == selected) {
            state.selectedIndex = state.options.length - 1;
            foundSelected = true;
          }
        }
        if (state.options.length == 0) 
          addNoValueEntry(state, '-- Not Applicable --');
      }
      if (!foundSelected) state.selectedIndex = 0;
    }

function updateCountry(location, hotel) {
    if (allCountries) {
	var selected = hotel.options[hotel.selectedIndex].text;
	var selectedCountry = null;
	for (i = 0 ; i < cityHotel.length ; i++) {
	    if (cityHotel[i][0] == selected) {
		selectedCountry = cityHotel[i][1];
		break;
	    }
	}
	if (selectedCountry) {
	    for (i = 0 ; i < location.options.length ; i++) {
		if (location.options[i].text == selectedCountry) {
		    location.selectedIndex = i;
		    break;
		}
	    }
	}
    }
}

function updateCity(location, hotel) {
    var selected = hotel.options[hotel.selectedIndex].text;
    // first we'll clear the select box!
    clearSelect(hotel);
    // Find which value we're looking for, insert them as we go!
    var value = location.options[location.selectedIndex].text;
    var foundSelected = false;

    if (location.selectedIndex == 0) {
        var initial = null;
        for (i = 0 ; i < cityHotel.length ; i++) {
	    cityName = cityHotel[i][0];
            if (cityName == initial) continue;
            addEntry(hotel, cityName);
	    if (cityName == selected) {
		    hotel.selectedIndex = hotel.options.length - 1;
		    foundSelected = true;
		  }
            initial = cityName;
        }
	allCountries = true;
    } else {
        var initial = null;
        for (i = 0 ; i < cityHotel.length ; i++) {
	    cityName = cityHotel[i][0];
            if (cityHotel[i][1] != value) continue;
            if (cityName == initial) continue;
            addEntry(hotel, cityName);
	    if (cityName == selected) {
		hotel.selectedIndex = hotel.options.length - 1;
		foundSelected = true;
            }
            initial = cityName;
        }
	allCountries = false;
    }
	if (!foundSelected) {
	    if (hotel.options.length > 1) {
		insertPleaseSelect(hotel);
	    }
	    hotel.selectedIndex = 0;
	}
}

function insertPleaseSelect(select) {
    // Move each option forward
    select.length += 1;
    for (i = (select.length - 1); i > 0 ; i--) {
	select.options[i].value = select.options[i - 1].value;
	select.options[i].text = select.options[i - 1].text;
    }
    if (!gecko) { 
        select.options[0].value = "Select a Hotel"; 
	select.options[0].text = "Select a Hotel";
    } else { 
        var option = new Option("Select a Hotel", "Select a Hotel", false, false); 
        select.options[0] = option; 
    } 
}

function addNoValueEntry(select, value) {
  if (!gecko) { 
    var cLength = select.length; 
    select.length = cLength + 1; 
    select.options[cLength].text = value; 
    select.options[cLength].value = ""; 
  } else { 
    var option = new Option(value, "", false, false); 
    select.options[select.length] = option; 
  } 
}


function addEntry(select, value) {
    addEntry(select, value, value);
}

function addEntry(select, value, code) {
 	if (!code) code = value;
  if (!gecko) {
	  var cLength = select.length;
    select.length = cLength + 1;       
    select.options[cLength].text = value; 
    select.options[cLength].value = code;       
  } else { 
    var option = new Option(value, code, false, false);
    select.options[select.length] = option;
  }
}

function populate() {
    clearSelect(document.form.location);
    var initial = null;
    for (i = 0 ; i < cityHotel.length ; i++) {
        if (cityHotel[i][1] == initial) continue;
        addEntry(document.form.location, cityHotel[i][1]);
        initial = cityHotel[i][1];
    }
    updateCity(document.form.location, document.form.hotel);
}

///------------------------------------------------------------------------------------------------------------
function changeDate(day, month, box) {
    // Need to calculate the year..
    var today = new Date();
    var todayMonth = today.getMonth();
    var todayDay = today.getDay();

    var yearVal = today.getYear();
    if (today.getYear() < 1900) yearVal += 1900;
    var monthVal = Number(month.options[month.selectedIndex].value - 1); 
    var dayVal = Number(day.selectedIndex) + 1;	

    if (monthVal < todayMonth) {
        yearVal += 1;	
    }
    var date = new Date(yearVal, monthVal, dayVal);
    // Check it's a real date
    if (monthVal == date.getMonth() && dayVal == date.getDate()) {
        box.value = days[date.getDay()];
	return true;
    } else {
        box.value = '';
	return false;
    }
}

function ensureBefore(inDay, inMonth, outDay, outMonth, dayOfWeek) {
    var inMonthValue = Number(inMonth.options[inMonth.selectedIndex].value - 1);
    var outMonthValue = Number
	(outMonth.options[outMonth.selectedIndex].value - 1);
    var minMonth = Number(inMonth.options[0].value - 1);
    var realInMonthValue = inMonthValue;
    if (inMonthValue < minMonth) {
        realInMonthValue += 12;
    }
    var realOutMonthValue = outMonthValue;
    if (outMonthValue < minMonth) {
        realOutMonthValue += 12;
    }
    if (realInMonthValue > realOutMonthValue || 
        (inMonthValue == outMonthValue && 
         inDay.selectedIndex >= outDay.selectedIndex)) {
        // We'll make a date add a day and read it back out.
        var today = new Date();
        var year = (inMonthValue > today.getMonth() ? today.getYear() 
                    : today.getYear() + 1);
        var date = new Date(year, inMonthValue, inDay.selectedIndex + 1);
        // The day after Feb 30th is meaningless
        if (date.getMonth() != inMonthValue || 
            date.getDate() != (inDay.selectedIndex + 1)) {
            return;
        }
        // Add a day (not as easy as you'd hope)
        if (date.getDate != 31) {
            date.setDate(date.getDate() + 1);
        } else {
            date.setDate(1);
            if (date.getMonth() != 11) {
                date.setMonth(date.getMonth() + 1);
            } else {
                date.setMonth(0);
                date.setYear(date.getYear() + 1);
            }
        }
        outDay.selectedIndex = date.getDate() - 1;
        // This is a bit of a cheat but easier
        if (date.getMonth() == inMonthValue) {
            outMonth.selectedIndex = inMonth.selectedIndex;
        } else {
            // Our only worry is that we won't have enough space in the
            // select box
            var desiredIndex = inMonth.selectedIndex + 1;
            if (desiredIndex == outMonth.options.length) {
                outMonth.selectedIndex = inMonth.selectedIndex;
                outDay.selectedIndex = inDay.selectedIndex;
            } else {
                outMonth.selectedIndex = desiredIndex;
            }
        }
        changeDate(outDay, outMonth, dayOfWeek);
    }
}
