/* Script for AA $Revision: 1.28 $ */
// Form handler generic (static)
dojo.declare("mam.app.AirportAtlasHandler", null, {
	fields:null,
	isAward:null,
	aaUrl:null,
	linkId:null,
	fieldNames:null,
	constructor: function(params) {
		dojo.mixin(this, params);
		for (field in this.fields) {
			dojo.connect(dojo.byId(field), "onclick", this, "_openAA");
			// problem with competing events form airport typeahead - therefore comment out:
			//dojo.connect(dojo.byId(this.fields[field].fieldId), "onchange", this, "_duplicateAirport");
			dojo.removeClass(dojo.byId(field), mam.INVISCLASS);
		}
		if (this.fieldNames != null) {
			this.fields["oo"] = this.fields[this.fieldNames[0]];
			this.fields["od"] = this.fields[this.fieldNames[1]];
		}
		dojo.subscribe(mam.topics.RECEIVEAIRPORT, this, "_receiveAirport");
	},
	/* Gets the airport and airport code from airport atlas and splits them up to separate strings 
	 * If requester is a selectlist, it is replaced with inputfield  
	 */
	_receiveAirport: function(pMessage) {
		if (this.linkId != null) {
			var oldAirportEle = dojo.byId(pMessage.tAirportField);
			// if element is selectlist, replace it with inputfield
			if(oldAirportEle.type.indexOf("select")>=0)
			{
				// the formelement is a selectlist, so a new html-element of typ 'input' is generated.
				var newAirportEle = document.createElement("input");
			 	// get 'name' and 'id' attribute for the new element form the old element.
			 	// important for the form-communication with the backend.
				newAirportEle.setAttribute("name",oldAirportEle.getAttribute("id")); // we use allways name=id in inputfields, except the selectlist in AD!
				newAirportEle.setAttribute("id",oldAirportEle.getAttribute("id"));
				newAirportEle.setAttribute("class", "mam-field-ab");
				var oParentEle = oldAirportEle.parentNode; // get the parent Node of the formelement (a selectlist).
				oParentEle.replaceChild(newAirportEle,oldAirportEle); // replace the selectlist with the new input field in the DOM-tree.
			}
			var airport = pMessage.airport.split("|");	
			this.setAirport(pMessage.tAirportField, airport[0]);
			this.setIata(pMessage.tIataField, airport[1]);
			if (this.fields[this.fields[this.linkId].depFieldId] != null)
			{
				this.setDepAirport(airport);
			}
		}
	},
	/* Writes the airport to the requesting field */
	setAirport: function(pFieldId, pAirport) {
		dojo.byId(pFieldId).value=pAirport;
	},
	/* Writes the airport code to the requesting field */
	setIata: function(pIataFieldId, pIata) {
		dojo.byId(pIataFieldId).value=pIata;
	},
	/* Writes the airport and the airport code to the requesting fields */
	setDepAirport: function(airport) {
		var depFieldId = this.fields[this.fields[this.linkId].depFieldId];
		if (dojo.byId(depFieldId.fieldId).value == "") {
			this.setAirport(depFieldId.fieldId, airport[0]);
			this.setIata(depFieldId.iataFieldId, airport[1]);
		}
	},
	/* Opens the airport atlas with the given parameters */
	_openAA: function(object) {
		this.linkId = object.target.id;
		var fieldId = this.fields[this.linkId].fieldId;
		var iataFieldId = this.fields[this.linkId].iataFieldId;
		mam.popupMgr.popupWin({url:this.aaUrl + mam.QueryChecker._getSepForQuery(this.aaUrl) + 
			'requester=' + this.linkId + '&fid=' + fieldId + '&hfid=' + iataFieldId + '&pAirportName=' + iataFieldId +	'&isAward=' + this.isAward +	 
			'&oo=' + this.fields["oo"].fieldId + 
			'&od=' + this.fields["od"].fieldId + 
			'&ooCode=' + this.fields["oo"].iataFieldId + 
			'&odCode=' + this.fields["od"].iataFieldId + 
			'&' + this.fields["oo"].fieldId + '=' + dojo.byId(this.fields["oo"].fieldId).value + 
			'&' + this.fields["od"].fieldId + '=' + dojo.byId(this.fields["od"].fieldId).value + 
			'&' + this.fields["oo"].iataFieldId + '=' + dojo.byId(this.fields["oo"].iataFieldId).value + 
			'&' + this.fields["od"].iataFieldId + '=' + dojo.byId(this.fields["od"].iataFieldId).value +
			this._ifAWB() +
			'',width:464,height:322,scrollbars:'no',resizable:'yes',anchorId:fieldId});
		return false;
	},
	/* Checks whether the object was initialized from award booking or from flight award teaser 
	 * --> Returns additional parameters for adding to url if object was initialized in award booking
	 */
	_ifAWB: function() {
		if (this.fields["io"] != null) {
			return '&io=' + this.fields["io"].fieldId + 
			'&id=' + this.fields["id"].fieldId +
			'&ioCode=' + this.fields["io"].iataFieldId + 
			'&idCode=' + this.fields["id"].iataFieldId +
			'&' + this.fields["io"].fieldId + '=' + dojo.byId(this.fields["io"].fieldId).value + 
			'&' + this.fields["id"].fieldId + '=' + dojo.byId(this.fields["id"].fieldId).value + 
			'&' + this.fields["io"].iataFieldId + '=' + dojo.byId(this.fields["io"].iataFieldId).value + 
			'&' + this.fields["id"].iataFieldId + '='  + dojo.byId(this.fields["id"].iataFieldId).value + '';
		}
		else {
			return "";
		}	 
	},
	/*	Duplicates the input of an outbound airport field with the return field, if the return field is empty.
	 *  fieldId is the ID of the outbound input field. Array of fields is searched for the corresponding depFieldId 
	 *	and the fieldId value is copied to it. 
	 */	
	_duplicateAirport: function(object) {
		var fieldId = object.target.id;
		for (field in this.fields) {
			if (fieldId == this.fields[field].fieldId) {
				dojo.byId(this.fields[field].iataFieldId).value = ""; // Resets the Iata-Code, if airport is filled in manually
				var depFieldId = this.fields[field].depFieldId;
				if (depFieldId != null) {
					var tFieldId = this.fields[depFieldId].fieldId;
					if (dojo.byId(tFieldId).value == "") {
						dojo.byId(tFieldId).value = dojo.byId(fieldId).value;
					}
				}
				break;
			}
		}
	}
});