SearchForm = function(outboundAirportInput,inboundAirportInput,outboundJawAirportInput,inboundJawAirportInput,outboundDateInput,inboundDateInput,defaultDateFormat,outboundFlexibility,inboundFlexibility) {
	this.outboundAirportInput = outboundAirportInput;
	this.inboundAirportInput = inboundAirportInput;
	this.outboundJawAirportInput = outboundJawAirportInput;
	this.inboundJawAirportInput = inboundJawAirportInput;
	this.outboundDateInput = outboundDateInput;
	this.outboundFlexibility = outboundFlexibility;
	this.inboundFlexibility = inboundFlexibility;
	this.inboundDateInput = inboundDateInput;
	this.defaultDateFormat = defaultDateFormat;
	this.outboundDate = this.getDefaultOutboundDate();
	this.inboundDate = this.getDefaultInboundDate();
	this.apChoices = new Array();
	this.apChoiceIndex = 0;
}

SearchForm.prototype = {
    outboundAirport: null,
	inboundAirport: null,
	outboundJawAirport: null,
	inboundJawAirport: null,
	outboundDate: null,
	inboundDate: null,
	tripType:null, 
	outboundAirportInputField:null,
	inboundAirportInputField:null,
	outboundJawAirportInputField:null,
	inboundJawAirportInputField:null,
	outboundDateInputField:null,
	outboundFlexibility:null,
	inboundDateInputField:null,
	inboundFlexibility:null,
	apChoices:null,
	searching:false,
	
	valid: function() {
		var validForm = true;
		var errorMsgs = "";
		
		if ( trim(this.outboundAirport).length < 3 ) {
			flagError( get(this.outboundAirportInput) );
			errorMsgs += "Please enter a valid departure airport\n";
			validForm = false;
		}
		if ( trim(this.inboundAirport).length < 3 ) {
			flagError( get(this.inboundAirportInput) );
			errorMsgs += "Please enter a valid arrival airport\n";
			validForm = false;
		}
		if ( this.tripType == 3 && trim(this.outboundJawAirport).length < 3 ) {
			flagError( get(this.outboundJawAirportInput) );
			errorMsgs += "Please enter a valid return airport\n";
			validForm = false;
		}
		if ( this.outboundDate == null ) {
			flagError( get(this.outboundDateInput) );
			errorMsgs += "Invalid depart date\n";
			validForm = false;
		}
		if ( this.tripType > 1 && this.inboundDate == null ) {
			flagError( get(this.inboundDateInput) );
			errorMsgs += "Invalid return date\n";
			validForm = false;
		}
		if ( this.inboundDate != null && this.inboundDate < this.outboundDate ) {
			flagError( get(this.inboundDateInput) );
			errorMsgs += "Return date must be later than departure date\n";
			validForm = false;
		}
		
		if ( !validForm ) {
			alert(errorMsgs);
			hideWaitMsg();
		}
		return validForm;
	},
	
	startSearch: function() {
		var returnFunction = function(jsonReturnMsg) {
			var json = eval("new Object(" + jsonReturnMsg + ")");
			var errOutput = "";
			for ( var key in json.errors ) {
				flagError( get(key) );
				errOutput += json.errors[key] + "\n";
			}
			if ( errOutput != "" ) {
				alert(errOutput);//get("searchError").innerHTML = errOutput;//showElement("searchError");
			}
			else if ( defined(json.returnView) ) {
				if ( window.location.href.indexOf(views["results"]) >= 0 ) {
				
					replaceURL( views[json.returnView] );
				}
				else {
					goToURL( views[json.returnView] );
				}
			}
			this.searching = false;
		}
		if ( this.valid() && !this.searching ) {
			this.searching = true;
			$.get("/nospring/ajax/setSearch?" + this.getQueryString(),returnFunction);
		}
	},
	
	search: function() {
		var returnFunction = function(jsonReturnMsg) {
			var json = eval("new Object(" + jsonReturnMsg + ")");
			var errOutput = "<ul>";
			for ( var key in json.errors ) {
				hideElement("waitPage");
				flagError( get(key) );
				errOutput += "<li>" + json.errors[key] + "</li>";
			}
			errOutput += "</ul>";
			if ( errOutput != "<ul></ul>" ) {
				get("searchError").innerHTML = errOutput;
				showElement("searchError");
			}
			else if ( defined(json.returnView) ) {
				replaceURL( views[json.returnView] );
			}
		}
		if ( this.valid() ) {
			submitAjax("search",returnFunction,"POST");
		}
	},
	
	initField: function(fieldID,apDesc) {
		if ( fieldID == this.outboundDateInput || fieldID == this.inboundDateInput ) {
			this.initDates();
		}
		else {
			var valueToShow = ( !defined(apDesc) ) ? SearchForm.getAPChoice().apDesc: apDesc;
			if ( fieldID == this.outboundAirportInput ) {
				get(this.outboundAirportInput).value = valueToShow;
				get(this.inboundJawAirportInput).value = valueToShow;
			}
			else if ( fieldID == this.inboundAirportInput ) {
				get(this.inboundAirportInput).value = valueToShow;
			}	
			else if ( fieldID == this.outboundJawAirportInput ) {
				get(this.outboundJawAirportInput).value = valueToShow;
			}
		}
	},
	
	initCodes: function() {
		get(this.outboundAirportInput).value = this.outboundAirport;
		get(this.inboundAirportInput).value = this.inboundAirport;
		get(this.outboundJawAirportInput).value = this.outboundJawAirport;
		get(this.inboundJawAirportInput).value = this.outboundAirport;
	},
	
	// if each date is valid update the display versions (input box and day of week text)
	initDates: function() {
		if ( this.outboundDate != null ) {
			get(this.outboundDateInput).value = this.outboundDate.format(this.defaultDateFormat);
			get("outboundDateDayOfWeek").innerHTML = this.outboundDate.format("DD");
		}
		if ( this.inboundDate != null ) {
			get(this.inboundDateInput).value = this.inboundDate.format(this.defaultDateFormat);
			get("inboundDateDayOfWeek").innerHTML = this.inboundDate.format("DD");
		}
	},
	initFlexibility: function() {
		if ( get("outboundFlexibility") != null ) {
			get("outboundFlexibility").value = this.outboundFlexibility;
		}
		if ( get("inboundFlexibility") != null ) {
			get("inboundFlexibility").value = this.inboundFlexibility;
		}
	},
	
	getQueryString: function() {
		if(this.outboundAirport.indexOf("(")>=0) this.outboundAirport = this.outboundAirport.split("(")[1].split(")")[0];
    if(this.inboundAirport.indexOf("(")>=0) this.inboundAirport = this.inboundAirport.split("(")[1].split(")")[0];
		
		output = "outboundAirport=" + this.outboundAirport;
		output += "&inboundAirport=" + this.inboundAirport;
		output += "&outboundDate=" + this.outboundDate.format("mm/dd/yyyy");
		output += "&outboundFlexibility=" + this.outboundFlexibility;
		output += "&tripType=" + this.tripType;
		if ( this.tripType > 1 ) {
			output += "&inboundDate=" + this.inboundDate.format("mm/dd/yyyy");
			output += "&inboundFlexibility=" + this.inboundFlexibility;
		}
		if ( this.tripType == 3 ) {
			if(this.outboundJawAirport.indexOf("(")>=0) this.outboundJawAirport = this.outboundJawAirport.split("(")[1].split(")")[0];
      if(this.outboundAirport.indexOf("(")>=0) this.outboundAirport = this.outboundAirport.split("(")[1].split(")")[0];

			output += "&outboundJawAirport=" + this.outboundJawAirport;
			output += "&inboundJawAirport=" + this.outboundAirport;
		}		
		return output;
	},
	
	getOutboundAirport: function() {
		return this.outboundAirport;
	},
	
	getInboundAirport: function() {
		return this.inboundAirport;
	},
	
	getOutboundJawAirport: function() {
		return this.outboundJawAirport;
	},
	
	getInboundJawAirport: function() {
		return this.inboundJawAirport;
	},
	
	getOutboundDate: function() {
		return this.outboundDate;
	},
	
	getOutboundFlexibility: function() {
		return this.outboundFlexibility;
	},
	
	getInboundDate: function() {
		return this.inboundDate;
	},
	
	getInboundFlexibility: function() {
		return this.inboundFlexibility;
	},
	
	getTripType: function() {
		return this.tripType;
	},
	
	setOutboundAirport: function(apCode) {
		this.outboundAirport = apCode;
		this.inboundJawAirport = apCode;
	},
	
	setInboundAirport: function(apCode) {
		this.inboundAirport = apCode;
	},
	
	setOutboundJawAirport: function(apCode) {
		this.outboundJawAirport = apCode;
	},
	
	setInboundJawAirport: function(apCode) {
		this.inboundJawAirport = apCode;
	},
	
	setOutboundDate: function(date,updateDisplay) {
		this.outboundDate = date;
		try {
			this.outboundDate.setHours(12);
			//alert(this.inboundDate.getTime() + " - " + this.outboundDate.getTime());
			if ( this.inboundDate.getTime() < this.outboundDate.getTime() ) {
				this.inboundDate.setTime(this.outboundDate.getTime());
			}
			if ( updateDisplay) {
				this.initDates();
			}
		}
		catch(e) {
			//alert("Invalid depart date");
			//get(this.outboundDateInput).focus();
		}
	},
	
	setOutboundFlexibility: function(outboundFlexibility) {
		this.outboundFlexibility = outboundFlexibility;
	},
	
	setInboundDate: function(date,updateDisplay) {
		this.inboundDate = date;
		try {
			this.inboundDate.setHours(12);
			if ( updateDisplay) this.initDates();
		}
		catch(e) {
			//alert("Invalid return date");
			//get(this.inboundDateInput).focus();
		}
		
	},
	
	setInboundFlexibility: function(inboundFlexibility) {
		this.inboundFlexibility = inboundFlexibility;
	},
	
	setTripType: function(tripType) {
		this.tripType = tripType;
	},
	
	setByFieldId: function(fieldID,value,updateDisplay,apDesc) {

		if ( fieldID == this.outboundAirportInput ) this.setOutboundAirport(value);
		else if ( fieldID == this.inboundAirportInput ) SearchForm.setInboundAirport(value);
		else if ( fieldID == this.outboundJawAirportInput ) SearchForm.setOutboundJawAirport(value);
		else if ( fieldID == this.outboundDateInput ) this.setOutboundDate(value,updateDisplay);
		else if ( fieldID == this.inboundDateInput ) SearchForm.setInboundDate(value,updateDisplay);
		else if ( fieldID == "outboundFlexibility" ) SearchForm.setOutboundFlexibility(value);
		else if ( fieldID == "inboundFlexibility" ) SearchForm.setInboundFlexibility(value);

		if ( updateDisplay ) this.initField(fieldID,apDesc);
	},
	
	getDefaultOutboundDate: function() {
		var date = getCurrentDateRef();
		date.addDay(15);
		return date;
	},

	getDefaultInboundDate: function() {
		var date = getCurrentDateRef();
		date.addDay(29);
		return date;
	},
	
	getAPChoiceIndex: function() {
		return this.apChoiceIndex;
	},
	
	getAPChoice: function() {
		return this.apChoices[this.apChoiceIndex];
	},
	
	setAPChoice: function(index) {
		this.apChoiceIndex = index;
	},
	
	prevAPChoice: function() {
		this.apChoiceIndex = Math.max(this.apChoiceIndex-1,0);
	},
	
	nextAPChoice: function() {
		this.apChoiceIndex = Math.min(this.apChoiceIndex+1,this.apChoices.length - 1);
	},
	
	addAPChoice: function(airport) {
		this.apChoices.push(airport);
	},
	
	resetAPChoices: function() {
		this.apChoices = new Array();
		this.apChoiceIndex = 0;
	},
	
	hasChoices: function() {
		return ( this.apChoices.length > 0 ) ? true:false;
	}
	
}