function submitTwice()
{
	document.intForm.submit();
}

/* FlightSearchValidator object */
function FlightSearchValidator(earliestBookingDate,formId,departCalendar,returnCalendar,dataContainer, region, intShowHideReturn){
	this.earliestBookingDate=earliestBookingDate;
	this.formId=formId;
	this.departCalendar=departCalendar;
	this.returnCalendar=returnCalendar;
	this.region=region;
	this.dataContainer = dataContainer;
	this.invalidLocationOptions=['','select a city','Select a City','selectacity','separator'];
	this.intShowHideReturn = intShowHideReturn;
}

FlightSearchValidator.prototype.isProduction=function(){
	return (window.location.host.indexOf('www.qantas')==0||window.location.host.indexOf('qantas')==0);
}
FlightSearchValidator.prototype.bookingEngineUrl=function(){
	return (this.isProduction()?'http://wftc3.e-travel.com':'http://siteacceptance.wftc3.e-travel.com');
}
FlightSearchValidator.prototype.isReturn=function(){
	for(var i=0;i<this.tripType.length;++i){
		if((this.tripType[i].value=='R'||this.tripType[i].value=='return')&&this.tripType[i].checked){
			return true;
		}
	}
	return false;
}
FlightSearchValidator.prototype.validateLocations=function(){
	for(var i=0;i<this.invalidLocationOptions.length;++i){
		if(this.from.value==this.invalidLocationOptions[i]){
			alert(this.dataContainer.errorMsgs['bookingForm.error.departurelocation']);
			this.from.focus();
			return false;
		}
	}
	for(var i=0;i<this.invalidLocationOptions.length;++i){
		if(this.to.value==this.invalidLocationOptions[i]||this.toBox.value==this.invalidLocationOptions[i]){
			alert(this.dataContainer.errorMsgs['bookingForm.error.arrivallocation']);
			this.toBox.focus();
			return false;
		}
	}
	if(this.from.value==this.to.value){
		alert(this.dataContainer.errorMsgs['bookingForm.error.departurearrivallocation']);
		this.from.focus();
		return false;
	}
	return true
}

FlightSearchValidator.prototype.validateDates=function(){
	if(this.departCalendar.getDate().getTime()<this.earliestBookingDate.getTime()){
		alert(this.dataContainer.errorMsgs['bookingForm.error.startdate']);
		this.departCalendar.monthYearField.focus();
		return false;
	}
	if(this.isReturn()&&this.returnCalendar.getDate().getTime()<this.departCalendar.getDate().getTime()){
		alert(this.dataContainer.errorMsgs['bookingForm.error.startenddate']);
		this.returnCalendar.monthYearField.focus();
		return false;
	}
	return true;
}

FlightSearchValidator.prototype.validatePax=function(){
	var numAdults=parseInt(this.adults.value);
	var numChild=parseInt(this.children.value);
	var numInfant=parseInt(this.infants.value);
	var totalPax=numAdults+numChild+numInfant;
	var maxPax=this.dataContainer.maxPax;
	if(totalPax>maxPax){
		alert(this.dataContainer.errorMsgs['bookingForm.error.maxpaxs']);
		this.adults.focus();
		return false;
	}
	if(numInfant>numAdults){
		alert(this.dataContainer.errorMsgs['bookingForm.error.infant']);
		this.infants.focus();
		return false;
	}
	return true;
}
FlightSearchValidator.prototype.isSearchByBestPrice=function(){
	return true;
}
FlightSearchValidator.prototype.isFlexible=function(){	
		if( this.flexiDates !=null  && this.flexiDates.value !=null )
		{
			if(this.flexiDates.value.indexOf('true') >-1 )
			{				
				return true;
			}
		}		
		
	return false;
}
FlightSearchValidator.prototype.createCommonQueryString=function(){
	var s=null;
	var officeId='';
	var s='';
	s+='LANGUAGE=GB';
	s+='&TRIP_TYPE='+(this.isReturn()?'R':'O');
	s+='&B_LOCATION_1='+this.from.value;
	s+='&E_LOCATION_1='+this.to.value;
	s+='&B_DATE_1='+this.departCalendar.getDate().format('YYYYmmDDHH00');
	s+='&B_DATE_2='+this.returnCalendar.getDate().format('YYYYmmDDHH00');
	var numAdults=parseInt(this.adults.value);
	var numChild=parseInt(this.children.value);
	var numInfant=parseInt(this.infants.value);
	var paxCount=1;
	for(var i=0;i<numAdults;++i){
		s+='&TRAVELLER_TYPE_'+paxCount+'=ADT';
		if(i<numInfant){
			s+='&HAS_INFANT_'+paxCount+'=TRUE'; //Infants get same paxCount as accompanying adult.
		}
		++paxCount;
	}
	for(var i=0;i<numChild;++i){
		s+='&TRAVELLER_TYPE_'+paxCount+'=CHD';
		++paxCount;
	}
	s+='&SEVEN_DAY_SEARCH=TRUE';
	s+='&CUSTOMER_TYPE=P';
	s+='&ARRANGE_BY=N';
	if(this.region=='sp'){
		s+='&SO_SITE_OFFICE_ID=AKLQF08AA';
	}
	s+='&PAGE_FROM='+"/regions/dyn/quickSearch";
	return s;
}
FlightSearchValidator.prototype.createDomesticQueryString=function(isFFLoggedIn){
	var s=this.createCommonQueryString();
	if(this.region=='au'){
		s+='&SITE=QFD';
		var bookingServlet='/pl/QFdomestic/en/FamilyPricerAvailabilityServlet';
	}else{
		s+='&SITE=QFN';
		var bookingServlet='/pl/QFdomestic/en/OverrideServlet';
		s+='&EMBEDDED_TRANSACTION=FamilyPricerAvailabilityServlet';
		s+='&SO_SITE_FD_INTERNA=FALSE';
	}
//	s+='&FARE_FAMILY='+this.fareFamily.value;
	
	// if date searched is after 24th May - redirect through Route Filter Server
	var cutoverDate=new Date(2004,4,25,0,0,0);

	if(isFFLoggedIn){
		var ffParam ="RF_FFCONLY=Y";		
		var servletAddress = "RF_SERVLET="+this.bookingEngineUrl()+bookingServlet;
		s = 'https://'+location.host+'/regions/dyns/routeFilter?'+s+'&'+servletAddress+'&'+ffParam;
	}
	else {
		s=this.bookingEngineUrl()+bookingServlet+'?'+s;
	}
	return s;
}
FlightSearchValidator.prototype.createInternationalQueryString=function(isFFLoggedIn){
	var s=this.createCommonQueryString();
	var tt=false;
	var flightDataItem = FlightDataCache.getFlightDataItem(this.dataContainer, this.from.value,this.to.value);
	if(this.region=='au'||this.region=='sp'){
		tt=(flightDataItem.isTransTasman());
	}
	if(tt){
		s+='&SITE=QFN';
//		s+='&FARE_FAMILY='+this.fareFamily.value;
		if(this.region=='au'){
			if(!isFFLoggedIn){
				s='FamilyPricerAvailabilityServlet?'+s;
			}
		}else{
			s+='&EMBEDDED_TRANSACTION=FamilyPricerAvailabilityServlet';
		}
	}else{
		s+='&SITE=QFI';
		if(this.region=='au'||this.region=='sp'){
//			s+='&CABIN='+this.fareFamily.value.substr(0,1);
		}else{
//			s+='&CABIN='+this.fareClass.value;
		}
		if(this.isSearchByBestPrice()){
			s+='&EMBEDDED_TRANSACTION=ValuePricerServlet';
		}else{
			s+='&EMBEDDED_TRANSACTION=AirAvailabilityServlet';
		}
		if(this.region=='au'||this.region=='sp'){
			if(this.region=='au'){
				s+='&SO_SITE_PREFERRED_CARRIER=QFAO';
				s+='&SO_SITE_FC_INCLUDE_AIRLINES=QFAO';
			}
		}else{
			if(this.region=='uk'){
				s+='&SO_SITE_OFFICE_ID=LONQF18AA';
				s+='&SO_SITE_PREFERRED_CARRIER=QFBA';
				s+='&SO_SITE_FC_INCLUDE_AIRLINES=QFBA';
			}else if(this.region=='sg'){
				s+='&SO_SITE_OFFICE_ID=SINQF18AA';
				s+='&SO_SITE_PREFERRED_CARRIER=QFBA';
				s+='&SO_SITE_FC_INCLUDE_AIRLINES=QFBA';
			}else if(this.region=='as'){
				s+='&SO_SITE_OFFICE_ID=SINQF18AA';
				s+='&SO_SITE_PREFERRED_CARRIER=QFBA';
				s+='&SO_SITE_FC_INCLUDE_AIRLINES=QFBA';
			}else if(this.region=='us'){
				s+='&SO_SITE_OFFICE_ID=LAXQF18AA';
			}
			s+='&SO_QUEUE_OFFICE_ID=SYDQF2RES';
			s+='&SO_SITE_FP_CC_CARDHOLDERNAME=';
			s+='&SO_GL=%3C%3Fxml+version%3D%271.0%27+encoding%3D%27iso-8859-1%27%3F%3E%3CSO_GL%3E%3CGLOBAL_LIST%3E%3CNAME%3ESITE_CREDIT_CARD%3C%2FNAME%3E%3CLIST_ELEMENT%3E%3CCODE%3EXX%3C%2FCODE%3E%3CLIST_VALUE%3E%3C%2FLIST_VALUE%3E%3C%2FLIST_ELEMENT%3E%3C%2FGLOBAL_LIST%3E%3C%2FSO_GL%3E';
		}
	}
	if(isFFLoggedIn){
		var ffParam ="RF_FFCONLY=Y";
		if(this.region=='au'&&tt){
			var servletAddress = "RF_SERVLET="+this.bookingEngineUrl()+"/pl/QFinternational/en/FamilyPricerAvailabilityServlet";
		}else{
			var servletAddress = "RF_SERVLET="+this.bookingEngineUrl()+"/pl/QFinternational/en/OverrideServlet";
		}
		s = 'https://'+location.host+'/regions/dyns/routeFilter?'+s+'&'+servletAddress+'&'+ffParam;
	}
	else {
		if(this.region=='au'&&tt){
			s=this.bookingEngineUrl()+'/pl/QFinternational/en/'+s;
		}else{
			s=this.bookingEngineUrl()+'/pl/QFinternational/en/OverrideServlet?'+s;
		}
	}
	return s;
}
/**
 * Creates a cookie to store the input of a flight search and sets it in the document
 */
FlightSearchValidator.prototype.setSelectionDetailsCke=function(type){
	var sortBy = "";
    if(this.region=='au'||this.region=='sp'){
//		var fare=this.fareFamily.value;
		var srchby="";
	}else{
		var fare=this.travelClass.value;
		var srchby="|SRCHBY:"+(this.isSearchByBestPrice()?'B':'S');
	}
	if ((this.region=='au' && type=='int') 
		|| (this.region=='sg') 
		|| (this.region=='uk') 
		|| (this.region=='eu') 
		|| (this.region=='us') 
		|| (this.region=='as') 
		|| (this.region=='sp' && type=='int')) {
		fare=this.travelClass.value;
		srchby="|SRCHBY:"+(this.isFlexible()?'T':'F');
        if(document.getElementById("intArrangeBy")!=null)
        {
            sortBy = document.getElementById("intArrangeBy").selectedIndex;
		    sortBy="|SORTBY:"+sortBy;
        }
	}
	if(type=='dom'){
		srchby="|SRCHBY:"+(this.isFlexible()?'T':'F');
		sortBy = document.getElementById("domArrangeBy").selectedIndex;
		sortBy="|SORTBY:"+sortBy;
	}
    
	var z="selections=DATES:"+this.departCalendar.serialize()+
			","+this.returnCalendar.serialize()+
			"|PORTS:"+this.from.value+
			","+this.to.value+
			"|PSGRS:"+this.adults.value+
			","+this.children.value+
			","+this.infants.value+
			"|TYPE:"+type+
			"|TRIP:"+(this.isReturn()?'R':'O')+srchby+
			"|REGION:"+this.region+
			"|FARE:"+fare+
            sortBy;
    document.cookie=z+";path=/";
    /* 
    	note that the path to the cookie has been set to /regions
    	this means that all requests to pages in the regions app
    	will have this cookie passed to them.  This is an overhead.
    	The reason for this change was that this cookie is used by the 
    	homepage ( non-struts, path regions/dyn) and the new quickSearch page
    	(struts, path regions/do/dyn).
    */
}


FlightSearchValidator.prototype.domesticInitialise=function(isFFLoggedIn){
	var form=document.getElementById(this.formId);
	this.tripType=form.domTripType;
	this.from=form.domFrom;
	this.to=form.domTo;
	this.adults=form.domAdults;
	this.children=form.domChildren;
	this.infants=form.domInfants;
//	this.fareFamily=form.domFareFamily;
	if(this.region=='au' || this.region=='sp')
	{
		this.flexiDates=form.domFlexiDates;
		this.sortFlightsBy=form.domArrangeBy;
	}

}

FlightSearchValidator.prototype.domesticValidateAndSubmit=function(isFFLoggedIn,submitForm){
	this.domesticInitialise();
	
	// The order these validate methods are called in is important.
	if(!this.validateLocations()){return false;}
	if(!this.validateDates()){return false;}
	if(!this.validatePax()){return false;}
	this.setSelectionDetailsCke('dom');
	if(!submitForm){
		var queryString=this.createDomesticQueryString(isFFLoggedIn);
		if(!this.isProduction()){
			alert(queryString);
		}
		document.location=queryString;
	}else{
		document.domForm.submit();
	}
	return true;
}


FlightSearchValidator.prototype.internationalInitialise=function(){
	var form=document.getElementById(this.formId);
	this.tripType=form.intTripType;
	this.from=form.intFrom;
	this.to=form.intTo;
	this.toBox=form.intToTextField;
	this.adults=form.intAdults;
	this.children=form.intChildren;
	this.infants=form.intInfants;
//	this.fareFamily=form.intFareFamily;
//	this.fareClass=form.intFareClass;
	this.travelClass=form.intTravelClass;
	this.flexiDates=form.intFlexiDatesCheckBox;
    this.sortFlightsBy=form.intArrangeBy;
}
FlightSearchValidator.prototype.internationalValidateAndSubmit=function(isFFLoggedIn,submitForm){
	this.internationalInitialise();
	
	// The order these validate methods are called in is important.
	if(!this.validateLocations()){return false;}
	if(!this.validateDates()){return false;}
	if(!this.validatePax()){return false;}
	this.setSelectionDetailsCke('int');
	if(this.isFlexible())
	{
			document.getElementById("intFlexiDates").value="true"
	}
	else
	{
			document.getElementById("intFlexiDates").value="false"
	}
	if(!submitForm){
		var queryString=this.createInternationalQueryString(isFFLoggedIn);
		if(!this.isProduction()){
			alert(queryString);
		}
		document.location=queryString;
	}else{
		this.setIntFormProperties();
		if ($j.browser.safari)
		{
			setTimeout('submitTwice()',10);
		}else if ( navigator.userAgent.toLowerCase().indexOf('chrome') > -1 )
		{
			setTimeout('submitTwice()',10);
		}
		document.getElementById(this.formId).submit();
	}
	addPastSearch();
	return true;
}


FlightSearchValidator.prototype.setIntFormProperties=function(){
	document.getElementById(this.formId).intTransTasman.value = "false";
	if(this.region == 'au' || this.region =='sp'){
		var flightDataItem = FlightDataCache.getFlightDataItem(this.dataContainer, this.from.value,this.to.value);
		if(flightDataItem.isTransTasman())
			document.getElementById(this.formId).intTransTasman.value = "true";
	}
}
FlightSearchValidator.prototype.domesticSetSelections=function(){
	this.setSelections(true);
}
FlightSearchValidator.prototype.internationalSetSelections=function(){
	this.setSelections(false);
}
/**
 * Parses the cookie string and populates corresponding fields in the flight search form
 * NB: the order in which form fields are set is important. 
 */
FlightSearchValidator.prototype.setSelections=function(isDomestic){
	
	//	departure and arrival airports are now stored in the UserContext
	//	so we must retrieve from there, not from the 'selections' cookie for these values.
	var depCity;
	var arrCity;
	
	if((document.cookie.indexOf("usercontext="))!=-1){
		var userContextCookie=getCookieValue("usercontext");	
		var depAirportInd = userContextCookie.indexOf("dep#");
		var arrAirportInd = userContextCookie.indexOf("arr#");
		depCity = userContextCookie.substring(depAirportInd+4,depAirportInd+7);
		arrCity = userContextCookie.substring(arrAirportInd+4,arrAirportInd+7);	
	}
	//	if we don't have either the departure or arrival airport, pre-populate nothing on the form
	
	if(depCity!=null || arrCity!=null){
		if((document.cookie.indexOf("selections="))!=-1){
			var val=readCookieValue("selections");
			var regionInd=val.indexOf("REGION:");
			var reg=val.substring(regionInd+7,regionInd+9);
			if(this.region==reg)
			{
				var datesInd=val.indexOf("DATES:");
				var portsInd=val.indexOf("PORTS:");
				var typeInd=val.indexOf("TYPE:");
				var fareInd=val.indexOf("FARE:");
				var psgrsInd=val.indexOf("PSGRS:");
				var tripInd=val.indexOf("TRIP:");
				var srchByInd=val.indexOf("SRCHBY:");
				var sortByInd=val.indexOf("SORTBY:");
				var dates=val.substring(datesInd+6,portsInd-1);
				var z=dates.split(",");
				var depDate=z[0];
				var retDate=z[1];			
				var type=val.substring(typeInd+5,typeInd+8);
				var fare;
				if (sortByInd != -1) {
					fare = val.substring(fareInd+5, sortByInd-1);
				} else {
					fare = val.substring(fareInd+5);
				}
				var fares=fare.split(",");
				var adults=val.substring(psgrsInd+6,psgrsInd+7);
				var children=val.substring(psgrsInd+8,psgrsInd+9);
				var infants=val.substring(psgrsInd+10,psgrsInd+11);
				var trip=val.substring(tripInd+5,tripInd+6);
				var srchBy=val.substring(srchByInd+7,srchByInd+8);
				var sortBy;
				if (sortByInd != -1) {				
					sortBy=val.substring(sortByInd+7,sortByInd+8);
				}
				if (type=="dom" && isDomestic) {
					this.domesticInitialise();
				
					this.departCalendar.deserialize(depDate);
					this.returnCalendar.deserialize(retDate);
					
//					this.set(this.fareFamily,fares[0]);
					this.setCommonFields(depCity, arrCity, adults, children, infants, srchBy, trip);
					if(!browser.isMSIE5x)
						this.intShowHideReturn();
					if(this.region=='au' || this.region=='sp'){
						this.sortFlightsBy.selectedIndex=sortBy;
					}
					this.intShowHideReturn();
					if (window.navigator.userAgent.toLowerCase().indexOf('msie 5.5') == -1)
						this.intShowHideReturn();
				} else if (type=="int" && !isDomestic) {
					this.internationalInitialise();
					this.departCalendar.deserialize(depDate);
					this.returnCalendar.deserialize(retDate);						
					/*
					if(this.region!='us'){
						changePanelTab('flightSearchTabbedPanels','internationalTab','internationalPanel');
					}
					*/
					
					if (this.region=='au' || this.region=='sp') {
						if (sortBy != null && sortBy != "")
							this.sortFlightsBy.selectedIndex=sortBy;
					}
					if (fares.length>1){
						this.set(this.travelClass, fares[1]);
					} else {
						this.set(this.travelClass, fares);
					}
					this.setCommonFields(depCity, arrCity, adults, children, infants, srchBy, trip);
					this.intShowHideReturn();
					if (window.navigator.userAgent.toLowerCase().indexOf('msie 5.5') == -1){
					this.intShowHideReturn();
				}
			}
		}
	}
	}
    // Start bodgy code - remove when NFS Trans Tasman return has cutover for good.
    // Fix for IE5.5 - on hitting back button the oneway radial was not cached but the sortby (NFS) is shown for return
    // This is a bodge for NFS issues where trans tasman one way and return cutover dates differ.
	if(document.getElementById('divSortBy')!=null && (window.navigator.userAgent.toLowerCase().indexOf('msie 5.5') != -1) && !isAfterTTReturnNfsCutover())
	{
		var flightDataItem = FlightDataCache.getFlightDataItem(this.dataContainer, document.getElementById('intFrom').value,document.getElementById('intTo').value);
		if(flightDataItem.isTransTasman())
		{
			document.getElementById('intTripTypeReturn').checked = true;
			if (this.sortFlightsBy != null)
				this.sortFlightsBy.selectedIndex = 0;
			checkT_Tasman();
		}
    }
    // End bodgy code
}
/**
 * Sets fields common to all regions in the flight search form
 */
FlightSearchValidator.prototype.setCommonFields=function(depCity, arrCity, adults, children, infants, srchBy, tripType){
	this.set(this.from, depCity);
	this.set(this.to, arrCity);
	this.set(this.adults, adults);
	this.set(this.children, children);
	this.set(this.infants, infants);
	if (this.tripType) {
		if (tripType == 'R') {
			this.tripType[0].checked=true;
		} else {
			this.tripType[1].checked=true;
		}
	}
//	if (this.searchType) {
//		if (srchBy == 'B') {
//			this.searchType[0].checked=true;
//		} else {
//			this.searchType[1].checked=true;
//		}
//	}
	if (this.flexiDates) {
		if (srchBy == 'T') {
			if(document.getElementById('transTasmanPostThrottleDate')!=null)
				document.getElementById('transTasmanPostThrottleDate').style.display="block";
			this.flexiDates.value=true;
			this.flexiDates.checked=true;
		} else {
			this.flexiDates.value=false;
			this.flexiDates.checked=false;
		}
	}
}
FlightSearchValidator.prototype.set=function(frmelem, value){
	if(value) {
		for(var i=0;i<frmelem.length;i++){
			if(frmelem.options[i].value==value) {
				frmelem.options[i].selected = true;
				break;
			}
		}
	}
}
FlightSearchValidator.prototype.preSelectSingleOpt=function(frmelem){
	if(frmelem != null && frmelem.options != null){
			if(frmelem.options.length == 3)
				frmelem.selectedIndex=2;
	}
}
/*
Look, this change is done by Zhan Zhang on 14/05/2007
What does this method do?
It validate the form and submit the request, no matter it is a domestic form or international form
*/
FlightSearchValidator.prototype.flightValidateAndSubmit=function(isFFLoggedIn,submitForm,isDomestic){
	this.flightInitialise();
	// The order these validate methods are called in is important.
	if(!this.validateLocations()){return false;}
	if(!this.validateDates()){return false;}
	if(!this.validatePax()){return false;}
	if (isDomestic)
		this.setSelectionDetailsCke('dom');
	else
		this.setSelectionDetailsCke('int');
	if(!submitForm){
		var queryString=this.createFlightQueryString(isFFLoggedIn,isDomestic);
		if(!this.isProduction()){
			alert(queryString);
		}
		document.location=queryString;
	}else{
		document.domForm.submit();
	}
	return true;
}

FlightSearchValidator.prototype.flightInitialise=function(isFFLoggedIn){
	var form=document.getElementById(this.formId);
	this.tripType=form.domTripType;
	this.from=form.domFrom;
	this.to=form.domTo;
	this.adults=form.domAdults;
	this.children=form.domChildren;
	this.infants=form.domInfants;
//	this.fareFamily=form.domFareFamily;
	this.flexiDates=form.domFlexiDates;
	this.sortFlightsBy=form.domArrangeBy;
	this.travelClass=form.intTravelClass;
}


FlightSearchValidator.prototype.createFlightQueryString=function(isFFLoggedIn,isDomestic){
	if (isDomestic) {
		return createDomesticQueryString(isFFLoggedIn);
	} else {
		return createInternationalQueryString(isFFLoggedIn);
	}
	return s;
}

//Zhan's code finish here
