(function(){

var activeService = "htl";

function switchService(e) {
	var srvType = this.id.replace(/srv[-]/, '');

	$('srv-'+activeService).className = "";
	dom.hide(activeService+'-quick');
	dom.show(srvType+'-quick');
	$('srv-'+srvType).className = "selected";
	activeService = srvType;
}

function dstChange(e) {
	ajax.queue(dstSet, "dstChg");
	ajax.loadXML("htl/dst-tbl.cfm?rooms="+this.value, "dstChg", "Updating persons table.");
}
function dstSet() {
	var response = ajax.getXML('dstChg');
	$('dst-tbl').innerHTML = dom.xmlText(response);
}
function dirChange(e) {
	dom[parseInt(this.value, 10) == 1 ? "hide" : "show"]('chr-rnd');
}
function trfToggle(e) {
	dom[this.checked ? "show" : "hide"]('trf-rnd');
}

Queue.add(function(){
	Event.on($$('li', 'tabs'), 'click', switchService);
	dom.show(activeService+'-quick');
	$('srv-'+activeService).className = "selected";

	var frm = document.forms.htlFrm;
	Event.on(frm.rooms, 'change', dstChange);
	if ( $$('select', 'dst-tbl').length/2 != frm.rooms.value ) {
		frm.rooms.value = $$('select', 'dst-tbl').length/2;
	}

	// Setup Charter
	frm = document.forms.chrFrm;
	if ( parseInt(getValue(frm.chr_type), 10) != 1 ) {
		dom.show('chr-rnd');
	}
	Event.on(cssQuery('input[name=chr_type]', frm), 'click', dirChange);

	// Setup Transfers
	frm = document.forms.trfFrm;
	if ( frm.rnd.checked ) {
		dom.show('trf-rnd');
	}
	Event.on(frm.rnd, 'click', trfToggle);

	frm=null;
	
	if ( $('index-flash') ) {
		var flaSrc = "index.swf",
			flaArr = lang.flash.split(',');
		if ( flaSrc.length ) {
			flaSrc = flaArr[Math.floor( Math.random() * flaArr.length )]; 
		}

		flash.embed({
			movie	: "content/flash/" + flaSrc,
			width	: 766,
			height	: 236,
			options : {background : "#ffffff"},
			version	: "6",
			target	: 'index-flash'
		});
	}
});

page = {

	bind: {
		htlFrm: {
			fromd: { bind:"tod" },
			tod: { bind:"fromd" },
			rooms: { min:1 },
			cty_code: { filter:"htl" }
		},
		chrFrm: {
			fromd: { bind:"tod" },
			tod: { bind:"fromd" },
			dep_cty_air_code: { filter:"chr" },
			arr_cty_air_code: { filter:"chr" },
			ret_cty_air_code: { filter:"chr", empty:"- "+lang.chr.same_as_departure+" -" }
		},
		crrFrm: {
			cty_code: { filter:"crrplace" },
			cars: { min:1 },
			fromd: { bind:"tod" },
			tod: { bind:"fromd" }
		},
		othFrm: {
			fromd_st: { bind:"tod_st" },
			tod_st: { bind:"fromd_st" },
			cty_code: { filter:"oth" }
		},
		trfFrm: {
			trf_date: { bind:"ret_date" },
			ret_date: { bind:"trf_date" }
		}
	},

	// Accommodations
	htlAvl: function(frm) {
		if ( !hasValue(frm.cnt_code) ) { return form.error("Please select at least a country.", frm.cty_label); }
		if ( !hasValue(frm.fromd) ) { return form.error("Please input check-in date.", frm.fromd); }
		if ( app.today.dayDiff(getValue(frm.fromd).toDate()) < 1 ) { return form.error("Please select a date after " + app.today.format()); }
		if ( !hasValue(frm.tod) ) { return form.error("Please input check-out date.", frm.tod); }
		if ( !hasValue(frm.rooms) ) { return form.error("Please input number of rooms.", frm.rooms); }
		return true;
	},
	htlList:function() {
		var listUrl = app.uri.protocol + '://' + app.uri.host + app.uri.directory + 'htl_search_results.cfm';
		if ( Env.isHost ) {
			nav(listUrl);
		} else {
			window.open(listUrl, Env.legacy ? "_blank" : "_top");
		}
	},

	chrAvl: function(frm) {
		if ( !hasValue(frm.fromd) ) { return form.error("Please input departure date.", frm.fromd); }
		if ( app.today.dayDiff(getValue(frm.fromd).toDate()) < 1 ) { return form.error("Please select a date after " + app.today.format()); }
		if ( parseInt(getValue(frm.chr_type), 10) != 1 && !hasValue(frm.tod) ) { return form.error("Please input return date.", frm.tod); }
		if ( !hasValue(frm.dep_cnt_code) ) { return form.error("Please select at least the country of departure and destination to continue."); }
		if ( !hasValue(frm.arr_cnt_code) ) { return form.error("Please select at least the country of destination."); }
		if ( frm.dep_cnt_code.value == frm.arr_cnt_code.value ) {
			if( !hasValue(frm.dep_cty_code) && !hasValue(frm.arr_cty_code) ) {
				return form.error("Please select at least one city ( destination or departure )");
			}
		}
		if ( parseInt(frm.pax_adult.value, 10) + parseInt(frm.pax_child.value, 10) + parseInt(frm.pax_infant.value, 10) < 1 ) { return form.error("At least one person should exist for availability to continue.", frm.pax_adult); }
		return true;
	},

	othAvl: function(frm) {
		if ( !hasValue(frm.fromd_st) ) { return form.error("Please input a date.", frm.fromd_st); }
		if ( app.today.dayDiff(getValue(frm.fromd_st).toDate()) < 1 ) { return form.error("Please select a date after " + app.today.format()); }
		return true;
	},

	crrAvl: function(frm) {
		if ( !hasValue(frm.cnt_code) ) { return form.error("Please select at least a country ( Location field )."); }
		if ( !hasValue(frm.fromd) ) { return form.error("Please input delivery date.", frm.fromd); }
		if ( app.today.dayDiff(getValue(frm.fromd).toDate()) < 1 ) { return form.error("Please select a date after " + app.today.format()); }
		if ( !hasValue(frm.tod) ) { return form.error("Please input collection date.", frm.tod); }
		if ( !hasValue(frm.cars) ) { return form.error("Please input number of cars.", frm.cars); }
		return true;
	},

	trfAvl: function(frm) {
		if ( !hasValue(frm.trf_date) ) { return form.error("Please select a date.", frm.trf_date); }
		if ( app.today.dayDiff(getValue(frm.trf_date).toDate()) < 1 ) { return form.error("Please select a date after " + app.today.format()); }
		if ( frm.rnd.checked ) {
			if ( !hasValue(frm.ret_date) ) { return form.error("Please type the departure date.", frm.ret_date); }
		}
		if ( !hasValue(frm.fzone_id) ) { return form.error("Please select a departure zone."); }
		if ( !hasValue(frm.tzone_id) ) { return form.error("Please select a destination zone."); }
		if ( getValue(frm.fzone_id) == getValue(frm.tzone_id) ) { return form.error("Destination zone cannot be the same as departure zone."); }
		if ( parseInt(frm.adults.value, 10) + parseInt(frm.children.value, 10) + parseInt(frm.infants.value, 10) === 0 ) {
			return form.error("At least one passenger must be specified.", frm.adults);
		}
		return true;
	}

};

})();