// JavaScript Document

var _activeHotel = null;
var _arrHotels = new Array();
var _ctrlHotelCountry = null;
var _ctrlHotelState = null;
var _ctrlHotelCity = null;
var _ctrlHotel = null;
var _mask = null;

function enable()
{
		var _i = 0;
		
		// 4: clear 'hotel' form control
		_ctrlHotel.options.length = 0;
		_ctrlHotel.options[0] = new Option("--", "");
		
		// if a hotel has been selected, back-build the form
		if ( _activeHotel != null ) {
			
			for ( _i = 0; _i < _ctrlHotelCountry.length; _i++ )
			{
				if ( _ctrlHotelCountry[_i].value == _activeHotel.country ) { _ctrlHotelCountry[_i].selected = true; }
				handleClick_hotelCountry();
			}
			
			for ( _i = 0; _i < _ctrlHotelState.length; _i++ )
			{
				if ( _ctrlHotelState[_i].value == _activeHotel.administrativeArea ) { _ctrlHotelState[_i].selected = true; }
				handleClick_hotelState();
			}
			
			for ( _i = 0; _i < _ctrlHotelCity.length; _i++ )
			{
				if ( _ctrlHotelCity[_i].value == _activeHotel.locality ) { _ctrlHotelCity[_i].selected = true; }
				handleClick_hotelCity();
			}
			
			for ( _i = 0; _i < _ctrlHotel.length; _i++ )
			{
				if ( _ctrlHotel[_i].value == _activeHotel.id ) { _ctrlHotel[_i].selected = true; }
			}
			
		}
		
		// 5: show and enable 'hotelState' and 'hotelCity' form controls
		_mask.style.display = 'block';
	try
	{
	}
	catch(err) { _status = false; }
}

function handleClick_hotelCity()
{
	_hotel = null;
	_hotels = new Array();
	
	for ( _i = 0; _i < _arrHotels.length; _i++ )
	{
		if (
			_arrHotels[_i].locality == _ctrlHotelCity.value &&
			_arrHotels[_i].administrativeArea == _ctrlHotelState.value &&
			_arrHotels[_i].country == _ctrlHotelCountry.value &&
			jQuery.inArray(_arrHotels[_i].name, _hotels) == -1
		) {
			_hotel = {
				id:_arrHotels[_i].id,
				name:_arrHotels[_i].name
			}
			
			_hotels.push(_hotel);
		}
	}
	
	_hotels.sort();
	
	_ctrlHotel.options.length = 0;
	_ctrlHotel.options[0] = new Option("--", "");
	$.validationEngine.closePrompt("#hotel");
	
	for ( _i = 0; _i < _hotels.length; _i++ )
	{
		_ctrlHotel.options[_i + 1] = new Option(_hotels[_i].name, _hotels[_i].id);
	}
}

function handleClick_hotelCountry()
{
	_administrativeAreas = new Array();
	
	for ( _i = 0; _i < _arrHotels.length; _i++ )
	{
		if ( _arrHotels[_i].country == _ctrlHotelCountry.value && jQuery.inArray(_arrHotels[_i].administrativeArea, _administrativeAreas) == -1 ) {
			_administrativeAreas.push(_arrHotels[_i].administrativeArea);
		}
	}
	
	_administrativeAreas.sort();
	
	_ctrlHotelState.options.length = 0;
	_ctrlHotelState.options[0] = new Option("--", "");
	_ctrlHotelCity.options.length = 0;
	_ctrlHotelCity.options[0] = new Option("--", "");
	_ctrlHotel.options.length = 0;
	_ctrlHotel.options[0] = new Option("--", "");
	$.validationEngine.closePrompt("#hotel");
	
	for ( _i = 0; _i < _administrativeAreas.length; _i++ )
	{
		_ctrlHotelState.options[_i + 1] = new Option(_administrativeAreas[_i], _administrativeAreas[_i]);
	}
}

function handleClick_hotelState()
{
	_localities = new Array();
	
	for ( _i = 0; _i < _arrHotels.length; _i++ )
	{
		if (
			_arrHotels[_i].administrativeArea == _ctrlHotelState.value &&
			_arrHotels[_i].country == _ctrlHotelCountry.value &&
			jQuery.inArray(_arrHotels[_i].locality, _localities) == -1
		) {
			_localities.push(_arrHotels[_i].locality);
		}
	}
	
	_localities.sort();
	
	_ctrlHotelCity.options.length = 0;
	_ctrlHotelCity.options[0] = new Option("--", "");
	_ctrlHotel.options.length = 0;
	_ctrlHotel.options[0] = new Option("--", "");
	$.validationEngine.closePrompt("#hotel");
	
	for ( _i = 0; _i < _localities.length; _i++ )
	{
		_ctrlHotelCity.options[_i + 1] = new Option(_localities[_i], _localities[_i]);
	}
}

function init()
{	
	/*
	STEPS:
	1: locate form controls in DOM
	2: attach handlers to 'hotelCountry', 'hotelState' and 'hotelCity' form controls
	3: read contents of 'hotel' form control as written to page and parse form data into array
	-- if status is 'true'
	4: clear 'hotel' form control
	5: show and enable 'hotelState' and 'hotelCity' form controls
	*/
	
	var _hotel = null;
	var _i = 0;
	var _j = 0;
	var _status = true;
	var _temp = null;
	
	// 1: locate form controls in DOM
	try
	{
		_ctrlHotelCountry = document.getElementById("hotelCountry");
		_ctrlHotelState = document.getElementById("hotelState");
		_ctrlHotelCity = document.getElementById("hotelCity");
		_ctrlHotel = document.getElementById("hotel");
		_mask = document.getElementById("hideMe");
	}
	catch(err) { _status = false; }
	
	// 2: attach handlers to 'hotelCountry', 'hotelState' and 'hotelCity' form controls
	try
	{
		$('#hotelCountry').change(function() { handleClick_hotelCountry(); });
		$('#hotelState').change(function() { handleClick_hotelState(); });
		$('#hotelCity').change(function() { handleClick_hotelCity(); });
	}
	catch(err) { _status = false; }
	
	//3: read contents of 'hotel' form control as written to page and parse form data into array
	try
	{
		for ( _i = 1; _i < _ctrlHotel.length; _i++ )
		{
			_temp = _ctrlHotel[_i].text.split(',');
			
			_hotel = {
				id: _ctrlHotel[_i].value,
				country: _temp[0],
				administrativeArea: _temp[1],
				locality: _temp[2],
				name: ""
			};
			
			for ( _j = 3; _j < _temp.length; _j++ )
			{
				if ( _hotel.name == "" ) { _hotel.name = _temp[_j]; } else { _hotel.name = _hotel.name + "," + _temp[_j]; }
			}
			
			_arrHotels.push(_hotel);
			
			// look for an active selection
			if (_i != 0 && _ctrlHotel.selectedIndex == _i) { _activeHotel = _hotel; }
		}
	}
	catch(err) { _status = false; }
	
	// initialization successful; enable the added controls
	if ( _status == true ) { enable(); }
}

$(document).ready(function() {
	// hide the submit btn for language switching
	$("#lang-switch-btn").hide();
	// initialize in-line form validation
	// http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/
	$("#theForm").validationEngine({
		failure			: function() {},
		promptPosition	: "bottomLeft",
		success			: false
	})
	
	// trigger progressive enhancement of hotel/country/state selection
	init();
	
});