(function(){

	Queue.add(function(){
	
	
	
	var frm = document.forms.crrFrm;
	if(frm){
		frm.pickups.value="";
		frm.returns.value="";
		var depComplete=null,
			colComplete=null;
		depComplete = new v8.AutoComplete(frm.pickups,
							JS.apply(
									 
								new v8.AutoComplete.Ajax('crr/cartrawler.list.cfm'),	// we don't pass a url here @see: page.topologyChange()
								{
									format: function(data) {
										//frm.pickups.value ='';
									//	frm.pickup_code.value='';
										//frm.pickups.style='background:#fff url(../theme/loading.gif) no-repeat right center !important;';
										return Array.map(data.output, function(output, i) {
											return output ;
										});
										
									},
									handle: function(data, i, input) {
										input.value = data.output[i] ;
										frm.pickup_code.value= data.codes[i];
										frm.p_cty_code.value= data.city[i];
										frm.p_cnt_rg_code.value= data.region[i];
										frm.p_cnt_code.value= data.country[i];
										frm.p_loctype.value= data.loctype[i];									
									}
								}
							), {delay:0.3, max:20, width: 300});
					colComplete = new v8.AutoComplete(frm.returns,
							JS.apply(
									 
								new v8.AutoComplete.Ajax('crr/cartrawler.list.cfm'),	// we don't pass a url here @see: page.topologyChange()
								{
									format: function(data) {
										//frm.returns.value ='';
								//		frm.return_code.value='';;
										
										return Array.map(data.output, function(output, i) {
											return output ;
										});
										
									},
									handle: function(data, i, input) {
										input.value = data.output[i] ;
										frm.return_code.value= data.codes[i];
										frm.r_cty_code.value= data.city[i];
										frm.r_cnt_rg_code.value= data.region[i];
										frm.r_cnt_code.value= data.country[i];
										frm.r_loctype.value= data.loctype[i];									
									}
								}
							), {delay:0.3, max:20, width: 300});
					
					frm.pickUp_flag.checked=true;
					
					Event.on(frm.pickUp_flag,'click',page.toggleReturn);
	}
				
});
	
// flight auto-complete
var autoCompleteUrl = "flt/autocomplete.cfc?method=airport",
	autoCompleteSource = new v8.AutoComplete.Ajax(autoCompleteUrl),
	autoCompleteFrom = null,
	autoCompleteFromVal = null,
	autoCompleteTo = null,
	autoCompleteToVal = null;

autoCompleteSource.format = function(data) {
	return Array.map(data, function(el){
		return el.display;
	});
};

autoCompleteSource.handle = function(data, i, trg) {
	var lbl = trg.name == 'locFrom' ? 'startPt' : 'endPt';

	trg.form[lbl].value = data[i].code;
	trg.form[lbl+'Ind'].value = data[i].indicator;

	//trg.value = data[i].name + ", " + data[i].city;
	trg.value = data[i].code + ', ' + data[i].city;
	autoCompleteSource.config(autoCompleteUrl + "&domestic=" + data[i].cnt_code);

	// Use this to clear selected airport when value changes
	if ( trg.name == 'locFrom' ) {
		autoCompleteFromVal = trg.value;
	} else {
		autoCompleteToVal = trg.value;
	}
	Event.on(trg, "keyup", airUnlockIndicators);
	dom.classAdd(trg, "completed");

};

function airUnlockIndicators(e) {
	var cVal = this.name == 'locFrom' ? autoCompleteFromVal : autoCompleteToVal;

	if ( this.value != cVal ) { 
		var lbl = ( this.name == 'locFrom' ? 'startPt' : 'endPt' );

		this.value = this.form[lbl].value = this.form[lbl+"Ind"].value = "";
		if ( String.fromCharCode(Event.getCharCode(e)).match(/[a-z]/i) ) {
			this.value = String.fromCharCode(Event.getCharCode(e));
		}

		cVal = null;
		dom.classRemove(this, "completed");
		Event.removeListener(this, "keyup", airUnlockIndicators);
	}
}

//flight direction
function dirChange(e, obj) {
	dom[parseInt(getValue(obj), 10) == 1 ? "hide" : "show"]('flt-rnd');
}

Queue.add(function(){
	//===[ Flights ]===
	var frm = document.forms.fltFrm;
	if(frm){
		Event.on(cssQuery('input[name=flt_type]', frm), 'click', dirChange, frm.flt_type);
		if ( parseInt(getValue(frm.flt_type), 10) == 1 ) {
			dom.hide('flt-rnd');
		}
	
		autoCompleteFrom = new v8.AutoComplete(frm.locFrom, autoCompleteSource, {width: 300, threshold:3, delay: 0.3 });
		autoCompleteTo = new v8.AutoComplete(frm.locTo, autoCompleteSource, {width: 300, threshold:3, delay: 0.3 });
		
		frm=null;
	}
									
});
	
})();

page = {
	show:false,
	init:function(){
			if ( isset(app.uri.queryKey.quick) ) {
				page.show=true;
			}
		},
	htl_avl: function(frm) {
		if ( !app.loaded ) {
			return false;
		}

		if ( !hasValue(frm.htl_cnt_code) ) { return form.error(lang.htl_cnt_code_error, frm.destination); }
		if ( frm.checkin && !hasValue(frm.checkin) ) { return form.error(lang.checkin_error, frm.checkin); }
		if ( frm.checkin && getValue(frm.checkin).toDate().dayDiff(getValue(frm.checkout).toDate()) <= 0 ) { return form.error(lang.daydiff_error + getValue(frm.checkin), frm.checkout); }
		if ( frm.checkout && !hasValue(frm.checkout) ) { return form.error(lang.checkout_error, frm.checkout); }
		//checkout > checkin
		if ( frm.rooms && !hasValue(frm.rooms) ) { return form.error(lang.rm_error, frm.rooms); }
		if( frm.rooms ){
			var total_rooms = parseInt(getValue(frm.rooms), 10), rm = 1,
			chld = 0, c=0, age=0, maxYear = (new Date().getFullYear()), minYear = maxYear-120, total_pax = 0,
			ageObj, yearObj;
		}else{
			var total_rooms = 1, rm = 1,
			chld = 0, c=0, age=0, maxYear = (new Date().getFullYear()), minYear = maxYear-120, total_pax = 0,
			ageObj, yearObj;
		}

		for ( ; rm <= total_rooms; rm++ ) {
			total_pax = 0;
			if( frm.rooms ){
				chld = parseInt(getValue(frm['children_'+rm]), 10);
			}
			if ( chld > 0 ) {
				for ( c=1; c <= chld; c++ ) {
					total_pax++;
					if( frm.rooms ){
						ageObj = frm['childage_'+rm+'_'+c];
						yearObj = frm['child_birth_'+rm+'_'+c];
					}
					if ( hasValue(ageObj) ) {
						age = parseInt(getValue(ageObj), 10);
						if ( age > 17 ) {
							return form.error(lang.childage_error, ageObj);
						}
						yearObj.value = "";
					} else if ( !hasValue(yearObj) || yearObj.value == "dd/mm/yyyy" ) {
						return form.error(lang.childbirth_missing, yearObj);
					} else if ( Date.compare(yearObj.value.toDate(), new Date()) > 0 ) {
						return form.error(lang.childbirth_error, yearObj);
					}
				}
			}
			if( frm.rooms ){
				total_pax += getValue(frm['adults_' + rm]);
			}else{
				total_pax =1;
				}
			if ( total_pax == 0 ) { return form.error(lang.pax_error, frm['adults_' + rm]); }
		}
		
		return true;
	},
	crrAvl: function(frm) {
			if(show){
					if(!hasValue(frm.country))
			 				 return form.error("Please select a country",frm.country);
			 		if(!hasValue(frm.pickup_code))
							return form.error("Please select a pick up location",frm.pickups);
					if(!frm.pickUp_flag.checked &&!hasValue(frm.return_code))
								return form.error("Please select a drop off location",frm.returns);
					else{
							frm.return_code.value=frm.pickup_code.value;
						}			
					 if(!validateDate(frm.pickupdate))
							return form.error("Please select a pick up date",frm.pickupdate);
					 if(!validateDate(frm.returndate))
							return form.error("Please select a return  date",frm.returndate);
					 if(!validateTime(frm.pickuptime))
							return form.error("Please select a pick up time",frm.pickuptime);
					 if(!validateTime(frm.returntime))
							return form.error("Please select a return time",frm.returntime);	
					 if((frm.pickupdate.value.toDate().compareTo(frm.returndate.value.toDate())>=0))	
							return form.error("Drop off date must be greater than pick up date",frm.returndate);	
					 if(!hasValue(frm.drAge, 1) || ! parseInt(frm.drAge.value))
							  return form.error("Please select driver age",frm.drAge);
					 if(parseInt(frm.drAge.value)<=18)
							  return form.error("Driver age should be greater than or equal to 18",frm.drAge); 
					 if(!hasValue(frm.country_res))
							  return form.error("Please select a country ",frm.country_res) ;
					 if(frm.p_loctype.value=='C'){
						 	var act='avail';
					}else{
						var act='Eavail';
					 }	
					
			}
		return true;
	},
	
	chrAvl: function(frm) {
		if(show){
			chrMode = parseInt(getValue(frm.chr_type), 10);
	
			if ( !hasValue(frm.fromd) ) { return form.error("Please input departure date.", frm.fromd); }
			if ( !hasValue(frm.dep_cnt_code) ) { 
				return form.error("Please select at least the country of departure.", frm.dep_cty_air_code);			
			}
			if ( !hasValue(frm.arr_cnt_code) ) { 
				return form.error("Please select at least the country of destination.", frm.arr_cty_air_code);			
			}
			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;
	},
	
	fltAvl: function(frm) {
		if(show){
			var fltMode = parseInt(getValue(frm.flt_type), 10);
	
			if ( !hasValue(frm.locFrom) || frm.locFrom.value.length < 3 ) {
				return form.error("Please type your departure's location ( at least 3 characters ).", frm.locFrom);
			}
			if ( !hasValue(frm.locTo) || frm.locTo.value.length < 3 ) {
				return form.error("Please type your destination's location ( at least 3 characters ).", frm.locTo);
			}
	
			if ( !validateDate(frm.fromd) || app.today.dayDiff(frm.fromd.value.toDate()) < 1 ) {
				return form.error("Please input a valid departure date.", frm.fromd);
			}
			if ( fltMode != 1 && !dateValidatePeriod(frm.fromd, frm.tod, 1) ) {
				return form.error("Please input a valid return date.", frm.tod);
			}
		
			if ( !validateInt(frm.pax_adult, 0) ) {return form.error("Please input a valid number of adults.", frm.pax_adult);}
			if ( !validateInt(frm.pax_child, 0) ) {return form.error("Please input a valid number of children.", frm.pax_child);}
			if ( !validateInt(frm.pax_infant, 0) ) {return form.error("Please input a valid number of infants.", frm.pax_infant);}
			
			var pax = parseInt(frm.pax_adult.value, 10) + parseInt(frm.pax_child.value, 10) + parseInt(frm.pax_infant.value, 10);
			if ( pax < 1 ) {
				return form.error("At least one person should exist for availability to continue.", frm.pax_adult);
			} else if ( 10 <= pax ) {
				return form.error("Too many passengers specified. A maximum of 9 passengers is allowed.", frm.pax_adult);
			}
		}
		return true;
	},
	
	toggleReturn:function(){
			if(show){
				    var frm=document.forms['crrFrm'];
					if(frm.pickUp_flag.checked){
							dom.hide('return');
							frm.returns.value= "";
							frm.return_code.value= "";
							frm.r_cty_code.value="";
							frm.r_cnt_rg_code.value="";
							frm.r_cnt_code.value= "";
							frm.r_loctype.value= "";
						}else{
							dom.show('return');
							}
			}
	},

	amadeus_avl:function(frm){
		if(show){
			if( !hasValue(frm.External_FlightFareSearch_SearchType) ){ 
				return form.error("Please select a flight direction ",frm.External_FlightFareSearch_SearchType) ;
			}
			ajax.post(frm,"External_FlightSearchTypeByProvider,"
							+ "Culture,"
							+ "IsExternalAccess,"
							+ "External_FlightFareSearch_From,"
							+ "External_FlightFareSearch_To,"
							+ "External_FlightFareSearch_DepartureDate,"
							+ "External_FlightFareSearch_DepartureTime,"
							+ "External_FlightFareSearch_ReturnDate,"
							+ "External_FlightFareSearch_ArrivalTime,"
							+ "External_FlightFareSearch_NumberOfAdults,"
							+ "External_FlightFareSearch_NumberOfChilds,"
							+ "External_FlightFareSearch_NumberOfInfants,"
							+ "External_FlightFareSearch_FlightClass,"
							+ "External_FlightFareSearch_SearchType" );
			frm.action = "http://staging.amadeus.com.tr/lord/portals/lord/flightfaresearch.aspx";
			frm.submit();
		}
	}	

};
