page = {

	// Globals
	chr_mode: 1,

	isRefresh: false,
	rowA: null,
	rowR: null,

	history : null,
	historyHnd: function(e) {
		var pos = this.history.get();
		if ( pos === null ) {
			return false;
		} else {
			pos = parseInt(pos, 10);
		}

		if ( pos < this.history.lastPos ) {
			switch ( pos ) {
			case 1:
				this.stepA();
				break;
			case 2:
				this.stepB();
				break;
			}
		}
	},

	init: function() {
		this.history = new History(this.historyHnd, "start");

		var frm = document.forms.avlFrm;

		if ( isset(app.uri.queryKey.quick) ) {	// if quick search, init form and run avl
			setTimeout(this.avl.delegate(this, [frm]), 100);
		}

		// Setup Charter
		if ( parseInt(getValue(frm.chr_type), 10) != 1 ) {
			dom.show('chr-rnd');
		}
		Event.on(cssQuery('input[name=chr_type]', frm), 'click', this.dirChange);
		frm=null;
	},
	dirChange: function(e) {
		dom[parseInt(this.value, 10) == 1 ? "hide" : "show"]('chr-rnd');
	},

	// AVAILABILITY
	avl: function(frm, refreshing) {
		if ( !app.loaded ) {
			return false;
		}

		this.chr_mode = parseInt(getValue(frm.chr_type), 10);

		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 ( this.chr_mode != 1 && !hasValue(frm.tod) ) { return form.error("Please input a valid return date.", frm.tod); }

		if ( !hasValue(frm.dep_cnt_code) && !hasValue(frm.arr_cnt_code) ) { return form.error("Please select at least the country of departure or destination to continue."); }
		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); }

		dom[this.chr_mode==2 ? "show" : "hide"]('sum-rnd');

		this.rowA = null;
		this.rowR = null;
		this.isRefresh = refreshing;

		ajax.post(frm);
		ajax.queue(this.avlHnd, "avl");
		ajax.loadXML(frm.action, "avl", "Searching for available charter flights.");
		return false;
	},

	avlHnd: function() {
		var xmlObj = ajax.getXML("avl");

		// handle errors or empty result sets
		if ( parseInt(xmlObj.getAttribute('flag'), 10) > 1 ) {
			ajax.checkXMLResult("avl");
			return;
		}

		this.history.set(2);
		$('avl-tbl').addRowsXML(xmlObj);		// append results
		if ( !this.isRefresh ) {
			dom.toggle('step-1','step-2');		// toggle sections
		}
	},

	stepA: function() {
		dom.toggle('step-1','step-2');

		$('avl-tbl').clear();
		this.rowA = null;
		this.rowR = null;
		$('sum_flight').innerHTML = lang.common.na;
		$('sum_flight_rnd').innerHTML = lang.common.na;

		this.history.set(1);
	},

	// SERVICE SELECTION
	sel: function() {
		$$('input', app.row)[0].checked = true;
		this.rSel();
	},

	rSel: function() {
		var desc = new StringBuilder();
		desc.append(app.row.cells[1].innerHTML);
		desc.append(', ');
		desc.append(app.row.cells[2].innerHTML.split('<br/>')[0]);
		desc.append('<br/>');
		desc.append(app.row.cells[5].innerHTML);
		desc.append('<br/>');
		desc.append(app.row.cells[3].innerHTML);

		// handle selections
		if ( app.row.getAttribute('direction') == 'A' ) {
			this.rowA = app.row;
			$('sum_flight').innerHTML = desc.toString('');
		} else {
			this.rowR = app.row;
			$('sum_flight_rnd').innerHTML = desc.toString('');
		}

		// make sure dates are at least a day apart
		if ( this.rowA !== null && this.rowR !== null ) {
			if ( this.rowA.getAttribute('chr_date').toDate().dayDiff(this.rowR.getAttribute('chr_date').toDate()) <= 0 ) {
				// cancel selection
				if ( app.row.getAttribute('direction') == 'A' ) {
					$$('input', this.rowR)[0].checked = false;
					this.rowR = null;
					$('sum_flight_rnd').innerHTML = "N/A";
				} else {
					$$('input', this.rowA)[0].checked = false;
					this.rowA = null;
					$('sum_flight').innerHTML = "N/A";
				}
			}
		}

		if ( this.chr_mode == 1 || app.row.getAttribute('direction') == 'R' ) {
			return;
		}

		// if ROUNDTRIP, search for pairs
		var bonds = app.row.getAttribute('bond').split('^');
		var rows = $('avl-tbl').getRows();

		for ( var r=0; r<rows.length; r++ ) {	// search in table for [bond]
			var found = false;
			if ( rows[r] == app.row && bonds.length ) {	// skip if same row
				found = true;
			} else if ( rows[r].getAttribute('direction') == 'R' && bonds.length ){
				var rBonds = rows[r].getAttribute('bond').split('^');
				for ( var b=0, bnd=null; bnd=rBonds[b]; b++ ) {
					if ( Array.exists(bonds, bnd) ) {
						found = true;
						break;
					}
				}
			}

			if ( found ) {
				dom.classAdd(rows[r], "rnd");
			} else {
				dom.classRemove(rows[r], "rnd");
			}
		}
	},

	interceptEnter: function(frm) {
		switch ( this.history.lastPos ) {
		case 2:
			this.prebook(frm);
			break;
		case 3:
			this.bkService(frm);
			break;
		}
		return false;
	},

	// PRE-BOOKING
	prebook: function(frm) {
		// make sure everything is selected
		if ( this.chr_mode != 1 ) {
			if ( !this.rowA || !this.rowR ) {
				return form.error("Please select both outbound and inbound flights to continue.", null);
			}
		} else {
			if ( !this.rowA ) {
				return form.error("Please select a flight to continue.", null);
			}
		}
		
		var srcFrm = document.forms.avlFrm;
		frm.chr_type.value = this.chr_mode;

		// fill form data
		frm.pax_adult.value = srcFrm.pax_adult.value;
		frm.pax_child.value = srcFrm.pax_child.value;
		frm.pax_infant.value = srcFrm.pax_infant.value;

		// outbound flight
		frm.chr_no.value = this.rowA.getAttribute('chr_no');
		frm.chr_class.value = this.rowA.getAttribute('chr_class');
		frm.bk_date.value = this.rowA.getAttribute('chr_date');
		frm.bk_time.value = this.rowA.getAttribute('time_dep');
		frm.units_info.value = this.rowA.getAttribute('units');
		frm.sales_info.value = this.rowA.getAttribute('sales');

		if ( this.chr_mode == 2 ) {
			frm.chr_no_rnd.value = this.rowR.getAttribute('chr_no');
			frm.chr_class_rnd.value = this.rowR.getAttribute('chr_class');
			frm.bk_date_rnd.value = this.rowR.getAttribute('chr_date');
			frm.bk_time_rnd.value = this.rowR.getAttribute('time_dep');
			frm.units_info_rnd.value = this.rowR.getAttribute('units');
			frm.sales_info_rnd.value = this.rowR.getAttribute('sales');
		} else {
			frm.chr_no_rnd.value = "";
			frm.chr_class_rnd.value = "";
			frm.bk_date_rnd.value = "";
			frm.bk_time_rnd.value = "";
			frm.units_info_rnd.value = "";
			frm.sales_info_rnd.value = "";
		}

		// pax_types_str > ADULT:2^CHILD:1^INFANT:0^
		paxTypesStr = new StringBuilder();
		paxTypesStr.append('ADULT:'+frm.pax_adult.value);
		paxTypesStr.append('CHILD:'+frm.pax_child.value);
		paxTypesStr.append('INFANT:'+frm.pax_infant.value);
		frm.pax_types_str.value = paxTypesStr.toString('^') + '^';

		// run pre-booking
		ajax.post(frm);
		ajax.queue(this.prebookHnd, "prebooking");
		ajax.loadXML("?action=prebook", "prebooking", "Calculating total price for confirmation.");
		return false;
	},

	prebookHnd: function() {
		if ( !ajax.checkXMLResult("prebooking", true) ) {
			dom.show('step-2');
			ajax.checkXMLResult("prebooking");
			return;
		}
		dom.hide('step-2');
		this.history.set(3);

		// display pre-booking result
		var xmlResponse = ajax.getXML("prebooking");
		var node = xmlResponse.selectSingleNode('book');

		var sum = $('summary');
		Behavior.remove(sum);
		Event.purgeElement(sum);
		sum.innerHTML = dom.xmlText(node);
		sum = null;

		var frm = document.forms.chrFrm;
		frm.price.value = xmlResponse.getAttribute('price');
		frm.status.value = xmlResponse.getAttribute('status');
		frm.pax_types_str.value = xmlResponse.getAttribute('pax_types_str');
		frm.pax_names_str.value = xmlResponse.getAttribute('pax_names_str');
		frm.pax_refs_str.value = xmlResponse.getAttribute('pax_refs_str');
		frm.chr_type_out.value = xmlResponse.getAttribute('chr_type_out');
		if ( this.chr_mode != 1 ) {
			frm.pax_refs_str_rnd.value = xmlResponse.getAttribute('pax_refs_str_rnd');
			frm.chr_type_rnd.value = xmlResponse.getAttribute('chr_type_rnd');
		} else {
			frm.pax_refs_str_rnd.value = "";
			frm.chr_type_rnd.value = "";
		}

		passengers.list(frm,0);

		dom.show('step-3');
		$('step-3').scrollIntoView(true);
	},

	stepB: function() {
		dom.hide('step-3');
		dom.show('step-2');
		this.history.set(2);
	},

	// book service
	bkService: function(frm) {
		if ( !passengers.validate(frm, false) ) {
			return false;
		}

		ajax.queue(this.bkHnd, "chr_bk");
		ajax.post(frm);
		ajax.loadXML("bookings/add_service.cfm", "chr_bk", "Adding service to shopping cart.");
		return false;
	},

	bkHnd: function() {
		if ( !ajax.checkXMLResult( "chr_bk", true ) ) {
			ajax.checkXMLResult("chr_bk");
			return;
		}

		nav("cart.cfm");
	}

};