			/*
				Flight Mechanism Module
				
				2006-04-19 : - Requries that Ajax.js is included as well as Flight.js when FMM is utillized
							 - Requries that Common.js to be able to use the addEvent function
				hej
			*/
		
			function FMM(FromIATABox, textBox, VisibilityList,CallbackURL,countrycode, requestFunction){
				// If no request override function is passed then use default.
				if(requestFunction!=null) this.RequestData = requestFunction;
				else this.RequestData=this.requestFlightData;
					
				this.SuggestionList = null;				//	Result-DIV as an object
				this.delayTime=1400;					//	1.4 sec before showing suggestion box 
				this.selected=-1;						//	Keeps track of selected item in the result div
				this.textBox=textBox;					//  Name of the textbox object
				this.VisibleObjects = VisibilityList;	//  Array of names for the objects (dropdownlists) that should be hidden		
				this.suggestionTime = 0 ;				//  Keeps track of time
				this.FromIATA=getObj(FromIATABox);		//  Dropdown which hold departure IATA code
				this.CallbackURL = CallbackURL;			//  URL to Ajax callback functions ie. www.sembo.se/common/callback.aspx
				this.Language = countrycode;			//  From which site the call comes
				this.active = true;					//  
				window.onresize = function(){this.PositionElement(this);};	//	Add handler for window resize events
				Mycontext=this;
				addEvent(getObj(Mycontext.textBox),'keyup',function (e) {Mycontext.PerformFlightCallback(e);});
			}
			
			FMM.prototype.GetSuggestionList = function(){
				if (this.SuggestionList == null){
					this.SuggestionList=document.createElement("DIV");
					document.body.appendChild(this.SuggestionList);
					this.SuggestionList.className='suggestions';
					this.SuggestionList.id='suggestions';
				}
				return this.SuggestionList
			}

			FMM.prototype.Timer = function(l){ // time lenght in ms
				this.Invalidate();
				ref=this;
				var s = setTimeout(function(){ref.Validate(ref)}, l);			
			}

			FMM.prototype.Validate = function(caller){	// have to pass over the reference of the calling object or else it will get lost in the context switch occuring by the spawned process execution (this)
				if(caller!=null) caller=this;
				caller.StartAnimation();
				caller.RequestData();
			}
	
			FMM.prototype.Invalidate = function(){
				this.active=false;	
			}

			FMM.prototype.DelayHide = function(time){
				this.suggestionTime+=1;
				this.DelayTimer(time);	// Time from last event in textbox to when to hide the suggestionbox
			}
			
			FMM.prototype.DelayTimer = function(l){	// time lenght in ms
				myRef=this;
				var s = setTimeout(function(){myRef.Timeout()}, l);	
			}
			
			FMM.prototype.Timeout = function(){
				this.suggestionTime-=1;	
				if (this.suggestionTime==0){
					this.HideSuggestionList();	// delay finnished 
				}
			}
			
			FMM.prototype.StartAnimation = function(){
			//	getObj(this.textBox).className = 'FlightSelectLoadShort';
			}
			
			FMM.prototype.StopAnimation = function(){
			//	getObj(this.textBox).className = 'FlightSelectShort';
			}

			FMM.prototype.HideSuggestionList = function(){
				// var obj = document.getElementById("suggestions");
				var obj = this.GetSuggestionList(); 
				if( obj != null )
					obj.style.visibility = 'hidden';
				//Show the hidden ones
				for(ctrl in this.VisibleObjects){
					obj=getObj(this.VisibleObjects[ctrl]);
					if(obj != null)
						obj.style.visibility = 'visible' ;
				}	
				this.active=true;						
			}

			
			FMM.prototype.KeyUpHandler = function(code){
				var suggestionList = this.GetSuggestionList(); 
				if( suggestionList.style.visibility == 'visible' ){
					if( code == 38 ){
						if( selected > 0 )
						{
							var selectedObjA = 
								this.SuggestionList.childNodes[selected];
							selectedObjA.className = 'normal';
							
							selected -= 1;
							
							var selectedObjB = 
								this.SuggestionList.childNodes[selected];	

							selectedObjB.className = 'selected';
							this.DelayHide(this.delayTime);		// Delay hide of suggestionbox with two more seconds
						}
					}
					
					else if( code == 40 ){	
						if( selected < maximum )
						{

							if (selected>=0){
								var selectedObjA = 
									this.SuggestionList.childNodes[selected];	
								selectedObjA.className = 'normal';
							}
							selected += 1;
							
							var selectedObjB = 
								this.SuggestionList.childNodes[selected];	

							selectedObjB.className = 'selected';
							this.DelayHide(this.delayTime);		// Delay hide of suggestionbox with two more seconds
							
						}
					}
					
					else if(code == 13 || code==27){
						var selectedObj = 
							this.SuggestionList.childNodes[selected];	
						if( selectedObj != null ){
							var textObj=selectedObj.childNodes[0];
							
							var textField = getObj(this.textBox);

							if( textField != null ){
								var cityName;
			
								if (textObj.innerText != undefined){ 
									cityName = textObj.innerText;
									textField.value=cityName;
									textField.focus();						
								}
								else{
									cityName = textObj.innerHTML;//textContent;
									textField.value=cityName;
								}
								this.HideSuggestionList();
							}
							
						}
					}
				}
				return false;
			}

			FMM.prototype.GetLeftPosition = function(nameId){
				var leftPos = 0;
				var currentObj = getObj(nameId);
				
				if( currentObj != null ){
					while( currentObj.tagName != "BODY" ){
						leftPos += currentObj.offsetLeft;
						currentObj = currentObj.offsetParent;        
					}
				}
				return leftPos;
			}

			FMM.prototype.GetTopPosition = function(nameId){
				var topPos = 0;
				var currentObj = getObj(nameId);
				
				if( currentObj != null ){
				while( currentObj.tagName != "BODY" )					{
						topPos += currentObj.offsetTop;
						currentObj = currentObj.offsetParent;        
					}
				}
				return topPos;
			}
			
			FMM.prototype.PositionElement = function(responseContext){
				var suggestionList = responseContext.GetSuggestionList(); 

				if( suggestionList != null ){
					var yposition=responseContext.GetTopPosition(responseContext.textBox)+21;
					suggestionList.style.left = responseContext.GetLeftPosition(responseContext.textBox);
//					if (browserType==3) 
//						suggestionList.style.top=yposition+25;
//					else 
						suggestionList.style.top = yposition;
				}
			}

			FMM.prototype.onCityClick = function(e, responseContext){
				if (!e) var e = window.event;

				var text;

					
				if (e.srcElement != undefined){

					obj=responseContext.getParentByName(e.srcElement,'Container');
//					obj=e.srcElement.parentElement;					
					text=obj.childNodes[0].innerText;
				}
				else if(e.target != undefined){
					obj=responseContext.getParentByName(e.target,'Container');
//					obj=e.target.parentNode;
					text=obj.childNodes[0].textContent;
				}

				var textField = getObj(responseContext.textBox);
				if( textField != null ){
//					if (browserType==3) textField.value=text.substring(0,text.length);	//Opera fix
//					else 
						textField.value=text;
					if(textField.focus != undefined)
						textField.focus();
					responseContext.HideSuggestionList();
				}
			}

			FMM.prototype.getParentByName = function (obj,name){
				while(obj.id.indexOf(name)==-1){
					if(obj.parentNode != undefined)
						obj=obj.parentNode;
					else if (obj.parentElement)
						obj=obj.parentElement
				}
				return obj;
			}

			FMM.prototype.CallbackHandlerFlight = function(response, responseContext){
				responseContext.StopAnimation();	

				var suggestionList = responseContext.GetSuggestionList(); 
				if( suggestionList != null )
				{
					suggestionList.innerHTML = '';
					
					var items = response.getElementsByTagName("Location");
					if( items.length > 0 )
					{
						selected = -1;
						maximum = (items.length - 1);								
						for (var index = 0; index < items.length; index++)
						{
							var itemObj = document.createElement("span");
							if( itemObj != null )
							{

								var locationName = 
									GetElementValue(items[index], 'Name')
								var locationPrice = 
									GetElementValue(items[index], 'Price')

								if(itemObj.attachEvent != undefined)
									itemObj.attachEvent('onclick', function (e) {responseContext.onCityClick(e,responseContext);});
								else if (itemObj.addEventListener != undefined)
									itemObj.addEventListener('click',function (e) {responseContext.onCityClick(e,responseContext);} ,false);								
								
								itemObj.className = 'normal';
								itemObj.name= ("Container_" + index);
								itemObj.id= ("Container_" + index);
								itemObj.tabIndex = index;
									
								itemObj.innerHTML = ('<div style="float:left;" name="Location_' + index + '">' + locationName + '</div>');
								itemObj.innerHTML += ('<div style="float:right;color: red;" id="Price_' + index + '">' + locationPrice + '</div><br>');

								suggestionList.appendChild(itemObj);
							}
						}
						
						suggestionList.style.height = '';
						suggestionList.style.overflow = 'visible';
						
						if( items.length > 10 )
						{
							suggestionList.style.height = '60';
							suggestionList.style.overflow = 'auto';
						}
						
						if( suggestionList.style.visibility != 'visible' )
						{
							responseContext.PositionElement(responseContext)
							suggestionList.style.visibility = 'visible';
							for(ctrl in responseContext.VisibleObjects){
								obj=getObj(responseContext.VisibleObjects[ctrl]);
								if(obj != null)
									obj.style.visibility = 'hidden' ;
							}							
						}

						responseContext.DelayHide(4000);

					}
					else
						responseContext.HideSuggestionList();
					
					this.active=true;	
				}
			}

			FMM.prototype.PerformFlightCallback = function(e){
				var code;
				if (!e) var e = window.event;
				if (e.keyCode) code = e.keyCode;
				else if (e.which) code = e.which;
				
				if( code != 38 && code != 40 && code != 13 ){		
					if (this.active==true){
						this.Timer(this.delayTime);	
					}
				}
				else{
					this.KeyUpHandler(code);
				}
			}	

			FMM.prototype.requestFlightData = function (){
				var params = getObj(this.textBox);
				var FromIATA = this.FromIATA;
				if(this.CallbackURL==null) alert('Reference URL for Ajax callback is missing.');
				if( params != null ){			
					if(params.value.length!=0){

						var Proxy = 
							new AjaxProxy(this.CallbackURL,'getFlightDestination2','post',this);
						Proxy.parameters.set("CountryCode", this.Language);
						Proxy.parameters.set("Word", params.value);
						Proxy.parameters.set("FromIATA", FromIATA.value);

						Proxy.format = "xml";
						Proxy.responseCallback =this.CallbackHandlerFlight;
						Proxy.execute();
					}
				}
			}
