
// *** Global vars ***//
var gAirtradeLastLocatorId = null;
var gAirtradeCompleterResults = {};	
var gAirtradeCompleterObjs = {};
var inputId;
var inputTxt;
var tvalue;	//text value

/*
* findCityDWR() send the input text to GeocoderServiceDwr.listcities() method
*/
function findCityDWR(text, id)
{	
	inputId = id;
	inputTxt = text;
	if(text.length == 0 ) DWREngine._execute(dwrURL , 'GeocoderServiceDwr', 'listcities', inputTxt, fillCListDWRCallClistback);
	if(text.length <3 ) return;
	tvalue = text.split(","); 
	inputTxt = tvalue[0];
	DWREngine._execute(dwrURL , 'GeocoderServiceDwr', 'listcities', inputTxt, fillCListDWRCallClistback);

}

/*
* fillCListDWRCallClistback() call back method gives the list of cities
*/
function fillCListDWRCallClistback(list) 
{	city=list;
	AirtradeBuildOptions(inputTxt, list, inputId)
}


/*
* completerPopupWrap() to show the cities list
*/
function completerPopupWrap(inputEl)
{
	popupff(inputEl, "locator",0,1);

}

/*
* completerPopdownWrap() to close the cities list
*/
function completerPopdownWrap()
{
	popdown(true);
}


// *** The Completer Obj *** This is method is called from init() method 

function AirtradeCompleter(inputId, popupFunc, popdownFunc, popupid)
{	
	// internal vars
	this.locCode = null;
	this.locDesc = null;
	this.inputId = inputId;
	this.currentLocator = null;
	this.completerTimeout = 0;
	this.currentOption = -1;
	this.nrOfOptions = 0;
	this.popupFunc = popupFunc;
	this.popdownFunc = popdownFunc;
	this.popupid = popupid;
	this.locked = false;
	this.OPTION_FILLER = "&nbsp;&nbsp;";
	this.LOCATORID = "Airtradelocator";

	gAirtradeCompleterObjs[inputId] = this;
   
	this.setCode = function(code) 
	{
		this.locCode = code;
	}

	this.setDesc = function(desc) 
	{  
		var inp = document.getElementById(this.inputId);
		inp.value = desc;
		this.locDesc = new Object();
		this.locDesc.y = desc;
		this.locDesc.c = '';
		this.locDesc.cc = '';
	}

	this.removeOptionFiller = function(option)
	{
		return option.replace(RegExp("^\&nbsp;\&nbsp;(.*)","ig"),"$1");
	}

	this.selectLocatorOption = function(nr, locator)
	{
		if (locator == null)
			locator = document.getElementById(this.LOCATORID);

		var inp = document.getElementById(this.inputId);

		this.selectOption(city[nr], inp);
	}
	this.selectOption = function(option, inp)
	{
		
		inp.value = option;
		this.locDesc = option;
		
	}


	this.lowlight = function()
	{
		var popupDiv = document.getElementById(this.LOCATORID);
		if(this.currentOption >= 0 && popupDiv.childNodes[this.currentOption] != "undefined")
			popupDiv.childNodes[this.currentOption].className = "locatoroption";
	}
	this.highlight = function(dir, update, scrollIntoView)
	{
		var popupDiv = document.getElementById(this.LOCATORID);
		if(this.currentOption >= 0 && this.nrOfOptions > 0)
			popupDiv.childNodes[this.currentOption].className = "locatoroption";
		this.currentOption = this.currentOption + dir;
		if(this.currentOption < 0)
			this.currentOption = 0; 
		else if(this.currentOption >= popupDiv.childNodes.length)
			this.currentOption = popupDiv.childNodes.length-1; 
		popupDiv.childNodes[this.currentOption].className = "locatoroptionmo";
		if (scrollIntoView)
		{
			if (scrollIntoView == 1) // down
				popupDiv.childNodes[this.currentOption].scrollIntoView(false);
			else if (scrollIntoView == -1) // up
				popupDiv.childNodes[this.currentOption].scrollIntoView(true);
		}
		if(update &&  this.nrOfOptions > 0) {
			this.selectLocatorOption(this.currentOption, popupDiv);
		}
	}

	// used to adjust the height and width of list
	this.adjustLocatorHeight = function(locator,extra)
	{
		// check the locator height
		var nr = (this.nrOfOptions) ? this.nrOfOptions+(extra?extra:0) : 1;
		nr = (nr > this.MAXOPTIONS) ? this.MAXOPTIONS : nr;
		var h = locator.firstChild.offsetHeight;
		var ieExtraSpace = 3;
		var pop = document.getElementById(this.popupid);

			if (nr < 10)
			{
				pop.style.height = h*nr+ "px";
			}
			else{
				pop.style.height = h*10+ "px";
			}	
		horzPosPopup();
		setPopupCover();
	}
	
	// this method select first optin when the user not selected any option
	this.selectDefaultAirport = function(locator, shouldTab)
	{
		var tabToEl = null;
		if (this.nrOfOptions && locator.childNodes.length > 0)
		{
			if (this.currentOption != -1)
				this.selectLocatorOption(this.currentOption, locator);
			else
				this.selectLocatorOption(0, locator);

		}

		this.removeAllOptions();
		this.popdownFunc();

		if (tabToEl && tabToEl.select && getMouseOverCtrl() == '')  
			tabToEl.select();
	}


	/*
	* Events handlers for the input text box
	*/
	this.locatorkd = function(me, ev)
	{
		var locator = null;
		var popupDiv = document.getElementById(this.popupid);
		if(popupDiv.calledby != me)
		{
			this.popupFunc(me);
			locator = document.getElementById(this.LOCATORID);
			if (locator.firstChild == null)
			{
				nothing =  document.createElement('DIV');
				locator.appendChild(nothing);
				nothing.innerHTML = "<span class=\"noresults\">" + rm.nowSearching + "</span>";

				this.adjustLocatorHeight(locator);
			}
		}
		
		ev=ev||event||null;
		if (ev) 
		{
			var cc=ev.charCode||ev.keyCode||ev.which;

			if(cc == 9 || cc == 13) //tab, enter
			{
				if (locator == null)
					locator = document.getElementById(this.LOCATORID);
				this.selectDefaultAirport(locator, false);
				return false;
			}
			else if(cc==38||cc==57385)
			{
				this.highlight(-1, true, -1);
				return false;
			}
			else if(cc==40||cc==57386)
			{
				this.highlight(1, true, 1);
				return false;
			}
		}
		return true;
	}

	this.locatorkp = function(me, ev)
	{
		ev=ev||event||null;
		if (ev) 
		{  
			var cc=ev.charCode||ev.keyCode||ev.which;
		    if (cc==38||cc==57385||cc==40||cc==57386) return false; //up or down
          
			if (cc==13 && this.currentOption > -1)
			{   
				this.selectLocatorOption(this.currentOption);
				this.lowlight();
			
				this.removeAllOptions();
				popdown(true);
			}
		}
		return true;
	}
	this.locatorku = function(me, ev)
	{   
		var popupDiv = document.getElementById(this.popupid); 
		if(popupDiv.calledby == me)
		{
			ev=ev||event||null;
			if (ev) 
			{   
				 var cc=ev.charCode||ev.keyCode||ev.which;
				 if (cc==38||cc==40||cc==57385||cc==57386||cc==13 || cc==27) 				 
					return false;				 				
				 gAirtradeLastLocatorId = me.id;
				 window.clearTimeout(this.completerTimeout);
				 //Set a new one			
				 this.completerTimeout = window.setTimeout(AirtradeCompleterGo, 250);
			}
		}
		return true;
	}

	this.locatormc = function(me, ev)
	{
		me.select();
	}

	this.locatorblur = function(me)
	{
		if (me.id != this.inputId)
			return;

		var locator = document.getElementById(this.LOCATORID);
		if (locator == null)
		{
			return;
		}

		this.selectDefaultAirport(locator, true);
	}

	// Events handlers for the city options

	this.Airtradeoptionmc = function(me, ev)
	{
	}

	this.Airtradeoptionmout = function()
	{
		this.lowlight();
		this.className = "locatoroption";
		this.currentOption = -1;
	}

	this.Airtradeoptionmover = function(option)
	{
		this.lowlight();
		this.currentOption = option.index;
		this.highlight(0);
	}

	this.removeAllOptions = function()
	{
		var popupDiv = document.getElementById(this.LOCATORID);
		if (popupDiv && popupDiv.childNodes)
		{
			while (popupDiv.childNodes.length)
			{
				popupDiv.removeChild(popupDiv.firstChild);
			}
		}
		
		this.nrOfOptions = 0;
		this.currentOption = -1;
	}
	this.loadLocked = function(text)
	{    
		var matches = [];
		var one;
		var utxt = text.toUpperCase();
		for(var i = 0; i < this.locked.length-1;i++)
		{
			one = this.locked[i];
			if(one.m.toUpperCase().indexOf(utxt)==0 || one.y.toUpperCase().indexOf(utxt)==0)
				matches[matches.length] = one;
		}
		if(matches.length>0)
			AirtradeShowOptions(text,matches,this.inputId);
		else
		{
			AirtradeShowOptions(text,this.locked,this.inputId);					
			var input =  document.getElementById(this.inputId);
			input.value = rm.lockedpicker;
			input.select();

		}
	}
}


// *** Timer method calls findCityDWR() method ***//

function AirtradeCompleterGo()
{		
	var obj = gAirtradeCompleterObjs[gAirtradeLastLocatorId];
	var popupDiv = document.getElementById(gAirtradeLastLocatorId);
	if(popupDiv)
	{
		 var text = popupDiv.value;

			if(obj.locked)
				obj.loadLocked(text);
			else
			{
				findCityDWR(text, gAirtradeLastLocatorId);
			}

	}
	
}

// *** Event handlers ***//

function Airtradelocatorkd(me,ev)
{
	var obj = gAirtradeCompleterObjs[me.id];
	obj.locatorkd(me, ev);
}

function Airtradelocatorkp(me, ev)
{   
	var obj = gAirtradeCompleterObjs[me.id];
    obj.locatorkp(me, ev);
}

function Airtradelocatorku(me, ev)
{
	var obj = gAirtradeCompleterObjs[me.id];
	obj.locatorku(me, ev);
}

function Airtradelocatormc(me, ev)
{
	var obj = gAirtradeCompleterObjs[me.id];
	obj.locatormc(me, ev);
}

function Airtradelocatorblur(me)
{
	var obj = gAirtradeCompleterObjs[me.id];
	obj.locatorblur(me);
}

function Airtradeoptionmc(ev)
{	
}

function Airtradeoptionmout(ev)
{
	ev=ev||window.event||null;
	var targ = getEvTarget(ev);
	var option = (targ.inputId) ? targ : targ.parentNode;
	var obj = gAirtradeCompleterObjs[option.inputId];
	obj.Airtradeoptionmout();
}

function Airtradeoptionmover(ev)
{
	ev=ev||window.event||null;
	var targ = getEvTarget(ev);
	var option = (targ.inputId) ? targ : targ.parentNode;
	var obj = gAirtradeCompleterObjs[option.inputId];
	obj.Airtradeoptionmover(option);
}
// end of listeners

/*
* AirtradeBuildOptions() method calls AirtradeShowOptions() 
*/
function AirtradeBuildOptions(input, matches, inputId)
{
	gAirtradeCompleterResults[input] = matches;
	AirtradeShowOptions(input,matches,inputId);
}

/*
*  AirtradeShowOptions() method to construct the dropdown list
*/
function AirtradeShowOptions(input, list, inputId)
{   
		cityList = list;
		var obj = gAirtradeCompleterObjs[inputId];
		obj.removeAllOptions();
		var popupDiv = document.getElementById(obj.LOCATORID);	//
		var option,match,name;
		var home = false;
		if(popupDiv) 
		{   
			if(  (cityList != null) && (cityList.length > 0 ) )
			{								
				var r1 = new RegExp("([(, ]" + input+")","ig");	
				var r2 = new RegExp("^(" + input+")","i");				
				for (var i=0; i < cityList.length; i++ )
				{				
					match = cityList[i];				
					option =  document.createElement('DIV');
					popupDiv.appendChild(option);
					option.onmouseover = Airtradeoptionmover; 
					option.onmouseout = Airtradeoptionmout;
					option.onclick = Airtradeoptionmc;
					option.className = "locatoroption";
					option.index = i;
					option.inputId = inputId;
					name =cityList[i].replace(r1,"<b>$1</b>");
					option.innerHTML = obj.OPTION_FILLER+cityList[i]+obj.OPTION_FILLER;
					option.option = match;	
				}
				
				obj.nrOfOptions = cityList.length + (home?1:0);
			}	
			else   //no results or default results
			{
				nothing =  document.createElement('DIV');
				popupDiv.appendChild(nothing);
				if(input.length == 0) 
				{
					nothing.innerHTML = "<span class=\"noresults\">" + rm.nowSearching + "</span>";
				}
				else
				{
					nothing.innerHTML = "<span class=\"noresults\">" + rm.noResults + "</span>";
				}
				obj.nrOfOptions = 0;
			}
			obj.adjustLocatorHeight(popupDiv);
			if(input.length == 0) return false;
 		 }
}


