﻿(function(){

	Queue.add(function(){
	var frm = document.forms.crrFrm;
	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);
				
});
Queue.add(function(){
	/*
	var frm = document.forms.tab_fltFrm;
	frm.dep_air.value="";
	frm.arr_air.value="";
	
	depCompleteflt = new v8.AutoComplete(frm.dep_air,
						JS.apply(
								 
							new v8.AutoComplete.Ajax('common/airport.list.cfm'),	// we don't pass a url here @see: page.topologyChange()
							{
								format: function(data) {
									
									return Array.map(data.output, function(output, i) {
										return data.city[i] + ', ' +output;
									});
									
								},
								handle: function(data, i, input) {
									input.value =  data.city[i];
									frm.External_FlightFareSearch_From.value= data.codes[i]+';C';
									
								}
							}
						), {delay:0.1, max:20, threshold:3, width: 300});
	colCompleteflt = new v8.AutoComplete(frm.arr_air,
						JS.apply(
							new v8.AutoComplete.Ajax('common/airport.list.cfm'),	// we don't pass a url here @see: page.topologyChange()
							{
								
								format: function(data) {
									
									return Array.map(data.output, function(output, i) {
										return data.city[i] + ', ' +output;
									});
									
								},
								handle: function(data, i, input) {
									input.value =  data.city[i];
									frm.External_FlightFareSearch_To.value= data.codes[i]+';C';							
								}
							}
						), {delay:0.1, max:20, threshold:3, width: 300});
	var depCompleteflt=null,
		colCompleteflt=null;
					*/				
});
	
})();

page = {
	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 ( !hasValue(frm.checkin) ) { return form.error(lang.checkin_error, frm.checkin); }
		if ( getValue(frm.checkin).toDate().dayDiff(getValue(frm.checkout).toDate()) <= 0 ) { return form.error(lang.daydiff_error + getValue(frm.checkin), frm.checkout); }
		if ( !hasValue(frm.checkout) ) { return form.error(lang.checkout_error, frm.checkout); }
		//checkout > checkin
		if ( !hasValue(frm.rooms) ) { return form.error(lang.rm_error, 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;

		for ( ; rm <= total_rooms; rm++ ) {
			total_pax = 0;
			chld = parseInt(getValue(frm['children_'+rm]), 10);

			if ( chld > 0 ) {
				for ( c=1; c <= chld; c++ ) {
					total_pax++;
					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);
					}
				}
			}
			total_pax += getValue(frm['adults_' + rm]);
			if ( total_pax == 0 ) { return form.error(lang.pax_error, frm['adults_' + rm]); }
		}
		
		return true;
	},
	crrAvl: function(frm) {
					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) {
		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;
	},
	
	toggleReturn:function(){
				    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( !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";
		alert('Under Construction');
		//frm.submit();
	}			

};
