//============================
//AutoSuggestBox version 1.5.3
//============================


//Global variable and methods that keeps track of all ASB 
//javascript objects on the page
var g_ASBObjects;

function asbAddObj(sTextBoxID, oJSAutoSuggestBox)
{
	if (typeof(g_ASBObjects) == "undefined")
		g_ASBObjects=new Array();
			
	g_ASBObjects[sTextBoxID]=oJSAutoSuggestBox;
}


function asbGetObj(sTextBoxID)
{
	//JSAutoSuggestBox.//TRACE("asbGetObj : " + g_ASBObjects[sTextBoxID].msTextBoxID);
	return g_ASBObjects[sTextBoxID];
}


///////////////////////////////////////////////////
//Class that stores all auto suggest box properties
///////////////////////////////////////////////////

function JSAutoSuggestBox()
{	
	//Class properties
	var msTextBoxID;
	var msMenuDivID;
	var msDataType;
	var mnMaxSuggestChars;
	var mnKeyPressDelay;
	var mnNumMenuItems;
	var mbIncludeMoreMenuItem;
	var msMoreMenuItemLabel;
	var msMenuCSSClass;
	var msMenuItemCSSClass;
	var msSelMenuItemCSSClass;
	var mbUseIFrame;
	var mbLimitToList;
	
	var msResourceDir;
	var mbHasFocus;

	var msExtraParamsEval;
	var msDataSourceURL;

	//Internal attributes
	var mbSelectionChanged = false;
	var mnSelMenuItem = 0;	
	var mbCancelSubmit;
	var msOldTextBoxValue="";
	var mbAlreadyBlurred = false;

	//Class methods
	this.GetKey				=GetKey;
	this.GetTextBoxCtrl		=GetTextBoxCtrlFirstTime;
	this.GetHiddenValueCtrl	=GetHiddenValueCtrlFirstTime;
	this.GetMenuDiv			=GetMenuDivFirstTime;
	
	this.GetXmlHttp			=GetXmlHttp;
	this.GetDataFromServer	=GetDataFromServer;
	
	this.SetSelectedValue	=SetSelectedValue;
	this.GetSelectedValue	=GetSelectedValue;
	this.SetTextBoxValue	=SetTextBoxValue;
	this.GetTextBoxValue	=GetTextBoxValue;
	
    this.OnMouseClick		=OnMouseClick;
	this.OnMouseOver		=OnMouseOver;
 
 	this.OnKeyDown			=OnKeyDown;
	this.OnKeyPress			=OnKeyPress;
	this.OnKeyUp			=OnKeyUp;
	this.OnFocus			=OnFocus;    
    this.OnBlur				=OnBlur;
    
    this.GetSelMenuItemDiv	=GetSelMenuItemDiv;
    this.GetMenuItemDivID	=GetMenuItemDivID;
    this.GetMenuItemDiv		=IsIE()?GetMenuItemDiv_IE:GetMenuItemDiv_NonIE;
    
    this.MoveUp				=MoveUp;
    this.MoveDown			=MoveDown;
    
    this.SelectMenuItem		=SelectMenuItem;
    this.UnselectMenuItem	=UnselectMenuItem;
    
    this.IsVisibleMenuDiv	=IsVisibleMenuDiv;
	this.MoveMenuDivIfAbsolutePos	=MoveMenuDivIfAbsolutePos;
	this.ShowMenuDiv		=ShowMenuDiv;
	this.HideMenuDiv		=HideMenuDiv;
		
	this.LimitToList		=LimitToList;
	
	// optional callback function to be called when selection changes
	this.OnSelectionChanged = null;
	this.FireSelectionChanged = FireSelectionChanged;

	// single-cast events:
	this.OnAsbBlurred = function(){};
	this.OnAsbFocused = function(){};
	

	// GetElementById browser-specific strategy:
	this.GetElementById		=IsIE()?GetElementById_IE:GetElementById_NonIE;
	
	function GetElementById_IE(id)
	{
		return document.all[id];
	}
	
	function GetElementById_NonIE(id)
	{
		return document.getElementById(id);
	}
	
	//Detects what key was pressed
	function GetKey(evt)
	{
		evt = (evt) ? evt : (window.event) ? event : null;
		if (evt)
		{
			var cCode = (evt.charCode) ? evt.charCode :
					((evt.keyCode) ? evt.keyCode :
					((evt.which) ? evt.which : 0));
			return cCode; 
		}
	}
	

	var moHiddenValueCtrl = null;
	function GetHiddenValueCtrlFirstTime()
	{
		this.moHiddenValueCtrl = this.GetElementById(this.msTextBoxID + "_SelectedValue");
		this.GetHiddenValueCtrl = GetHiddenValueCtrlSecondTime;
		return this.moHiddenValueCtrl;
	}
	function GetHiddenValueCtrlSecondTime()
	{
		return this.moHiddenValueCtrl;
	}
	
	
	var moTextBoxCtrl = null;
	function GetTextBoxCtrlFirstTime()
	{
		this.moTextBoxCtrl = this.GetElementById(this.msTextBoxID);
		this.GetTextBoxCtrl = GetTextBoxCtrlSecondTime;
		return this.moTextBoxCtrl;
	}
	function GetTextBoxCtrlSecondTime()
	{
		return this.moTextBoxCtrl;
	}
	
	
	
	
	var moMenuDiv = null;
	function GetMenuDivFirstTime()
	{
		this.moMenuDiv = this.GetElementById(this.msMenuDivID);
		this.GetMenuDiv = GetMenuDivSecondTime;
		return this.moMenuDiv;
	}
	function GetMenuDivSecondTime()
	{
		return this.moMenuDiv;
	}
	
	
	//Create and return XmlHttp object
	function GetXmlHttp()
	{
		var oXmlHttp=false;
		
		// -----> This method was provided from Jim Ley's website 
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
		// JScript gives us Conditional compilation, we can cope with old IE versions.
		// and security blocked creation of the objects.
		try {
			oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		try {
			oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			oXmlHttp = false;
		}
		}
		/*@end @*/
	
		if (!oXmlHttp && typeof XMLHttpRequest!='undefined') 
		{
			oXmlHttp = new XMLHttpRequest();
		}
		
		return oXmlHttp;
	}	
	


	
	

  	// Make a call back to our server side page and return the results from our query 
	// to a DIV tag sitting under the text box
	function GetDataFromServer(sValue)
	{
		var sUrl;
		sUrl=this.msDataSourceURL + 
				"?TextBoxID=" + escape(this.msTextBoxID) + 
				"&MenuDivID=" + escape(this.msMenuDivID) + 
				"&DataType=" + escape(this.msDataType) + 
				"&NumMenuItems=" + this.mnNumMenuItems + 
				"&IncludeMoreMenuItem=" + this.mbIncludeMoreMenuItem + 
				"&MoreMenuItemLabel=" + escape(this.msMoreMenuItemLabel) + 
				"&MenuItemCSSClass=" + escape(this.msMenuItemCSSClass) + 
				"&Keyword=" + escape(sValue);

		 
		// add extra params:
		var extraParams;
		if (this.msExtraParamsEval=='')
		{
			extraParams = '';
		}
		else
		{
			extraParams = eval(this.msExtraParamsEval);
		}
		sUrl+=	"&ExtraParams=" + escape(extraParams);
		
		
				
		//TRACE("GetDataFromServer: " + sUrl);
	
		var oXmlHttp=GetXmlHttp();
		oXmlHttp.open("GET", sUrl, true);
		
		var me=this;	//Use it to be able to access ShowMenuDiv and HideMenuDiv in the function()
		oXmlHttp.onreadystatechange=function()
		{
			if (oXmlHttp.readyState==4) 
			{
				if (oXmlHttp.responseText!="")
				{
					if (me.mbHasFocus)
					{
						me.ShowMenuDiv(oXmlHttp.responseText);
					}
				}				
				else
				{	
					me.HideMenuDiv()
				}
			}
		}
		
		oXmlHttp.send(null)
	}


	
	function SetSelectedValue(sValue)
	{
		//TRACE("SetSelectedValue: " + sValue);
	
		var hdnSelectedValue=this.GetHiddenValueCtrl();

		// if selection changed, mark it for the OnSelectionChanged event:
		if (hdnSelectedValue.value!=sValue) 
		{
			////TRACE('selection changed. Old:' + hdnSelectedValue.value + ', new:' + sValue);
			this.mbSelectionChanged = true;
		}
		
		// and store new value:
		hdnSelectedValue.value=sValue;
		
	}

	function GetSelectedValue()
	{
		//TRACE("GetSelectedValue");
	
		// 2do: cache reference to hidden
		return this.GetHiddenValueCtrl().value;
	}


	function SetTextBoxValue()
	{
		var divMenuItem=this.GetSelMenuItemDiv();
			
		if(divMenuItem)
		{
			var sValue=divMenuItem.getAttribute('value');
			//TRACE("SetTextBoxValue : Set selected item to " + sValue);
		
			//Set selected value of control to the value of selected menu item
			this.SetSelectedValue(sValue);
				
			var txtCtrl=this.GetTextBoxCtrl();
			txtCtrl.value = GetInnerHtml(divMenuItem);
		}
	}


	function GetTextBoxValue()
	{
		var txtCtrl=this.GetTextBoxCtrl();
		return(txtCtrl.value);
	}
	
	
				
	function OnMouseClick(nMenuIndex)
	{
		this.mnSelMenuItem=nMenuIndex;
	
		this.SetTextBoxValue();
		this.HideMenuDiv();
	}
	


	function OnMouseOver(nMenuIndex)
	{
		this.SelectMenuItem(nMenuIndex);
	}
	
	
		
	function OnFocus(evt)
	{
		// mark that we havn't yet been blurred:
		this.mbAlreadyBlurred = false;
		
		// Save current text box value before key press takes affect
		// this is so we don't clear value when tab-focused for the first time
		this.msOldTextBoxValue=this.GetTextBoxValue();
		
		// fire ASB events:
		this.OnAsbFocused();
		
		return true;
	}


	function OnKeyDown(evt)
	{
		//TRACE("OnKeyDown : " + this.GetKey(evt) + ", " + this.msTextBoxID);
	
		//Indicate that control has focus
		this.mbHasFocus=true;
		
		//Save current text box value before key press takes affect
		this.msOldTextBoxValue=this.GetTextBoxValue();
		//TRACE("OnKeyDown : old text box value='" + this.msOldTextBoxValue + "'");
		
		var nKey;
		nKey=this.GetKey(evt);
				
		//TRACE("OnKeyDown : Key is " + nKey);
				
		//Detect if the user is using the down button
		if(nKey==38) //Up arrow
		{
			this.MoveDown()
		}
		else if(nKey==40) //Down arrow
		{
			this.MoveUp()
		}
		else if(nKey==13) //Enter
		{
			//TRACE("OnKeyDown : IsVisibleMenuDiv - " + this.IsVisibleMenuDiv());
			if (this.IsVisibleMenuDiv())
			{
				// enforce LimitToList and fire SelectionChanged:
				this.LimitToList();
				this.FireSelectionChanged();								
				
				this.HideMenuDiv();
				
				evt.cancelBubble = true;
				
				if (evt.returnValue) evt.returnValue = false;
				if (evt.stopPropagation) evt.stopPropagation();
				
				this.mbCancelSubmit=true;
     		}
     		else
     		{
     			this.mbCancelSubmit=false;
     		}
		}
		else if (nKey==9) //Tab
		{
			// act as though we're blurred *BEFORE* actually getting blurred:
			this.OnBlur();
		}
		else
		{
			// hide the menu, assuming it will be re-displayed on keyup if necessary
			this.HideMenuDiv();
		}
				
		
		return true;
	}
	
	
	function OnKeyPress(evt)
	{
		//TRACE("OnKeyPress : " + this.GetKey(evt));
		var nKey = this.GetKey(evt);
		if ((nKey==13) && (this.mbCancelSubmit)) 
		{
			return false;
		}
			
		return true;
	}
	
	
	
	function OnKeyUp(evt)
	{
		var nKey;
		nKey=this.GetKey(evt);
		
		//TRACE("OnKeyUp : " + nKey);
		
		
		//Skip up/down/enter 
//		if (((nKey!=38) && (nKey!=40) && (nKey!=13)) || (this.GetTextBoxValue()=='')) //unless text is empty
		if ((nKey!=38) && (nKey!=40) && (nKey!=13))
		{
			var sNewValue;
			sNewValue=this.GetTextBoxValue();
			
			//Limit num of characters to display suggestions	
			if ((sNewValue.length <= this.mnMaxSuggestChars) && (sNewValue.length > 0))
//			if (sNewValue.length <= this.mnMaxSuggestChars) // allow getting empty keyword
			{
				//TRACE("OnKeyUp : Getting data for '" + sNewValue + "'");
				
				var divMenu = this.GetMenuDiv();
				if (divMenu.timer) window.clearTimeout(divMenu.timer);
				
				//Add escape char to single quote
				// NOTICE: added g for global-search (so all occurrences are replaced and not only the first occurrance)
				sNewValue=sNewValue.replace(/\'/g, "\\\'");
				
				//Set timer to update div.  If user types quickly return suggestions when he stops.  
				var sFunc="asbGetObj('" + this.msTextBoxID + "').GetDataFromServer('" + sNewValue + "')";
				//TRACE("OnKeyUp : " + sFunc);
							
				divMenu.timer = window.setTimeout(sFunc, this.mnKeyPressDelay);
			}
		
		
			if (this.msOldTextBoxValue!=sNewValue)
			{
				this.SetSelectedValue("");
			}
		}
	}
		
	
	function OnBlur()
	{
		//TRACE("OnBlur");
		if (this.mbAlreadyBlurred==true) return;
		
		// enforce limitTolist:
		this.LimitToList();

		this.HideMenuDiv();
		this.mbHasFocus=false;
		
		// fire optional onselectionchanged event if selection has changed:
		this.FireSelectionChanged();
		
		this.mbAlreadyBlurred = true;
		
		// fire the ASB events:
		this.OnAsbBlurred();
	}

	function LimitToList()
	{
		// limit to list if necessary:
		if (!this.mbLimitToList) return;
		
		// auto-select first item if no selection:
		if (this.GetSelectedValue()=='')
		{
			this.mnSelMenuItem=1;
			this.SetTextBoxValue();
		}
		
		// clear selection if we still don't have any text:
		var txtbox = this.GetTextBoxCtrl();
		if (txtbox.value == '') this.SetSelectedValue('');
		
		// and clear textValue if no selectedValue:
		if (this.GetSelectedValue()=='') txtbox.value='';
	}
		
	function FireSelectionChanged()
	{
		if (this.mbSelectionChanged)
		{
			this.mbSelectionChanged = false;
			if (this.OnSelectionChanged!=null) this.OnSelectionChanged(this);
		}
	}		

	function GetSelMenuItemDiv()
	{
		return this.GetMenuItemDiv(this.mnSelMenuItem);
	}
			
			
	function GetMenuItemDivID(nMenuItem)
	{
		return (this.msTextBoxID + "_mi_" + nMenuItem);
	}
	
		
	function GetMenuItemDiv_NonIE(nMenuItem)
	{
		//2do: use menueDiv[n] instead of getElementById
		var sDivMenuItemID=this.GetMenuItemDivID(nMenuItem);
		return document.getElementById(sDivMenuItemID);
	}

	function GetMenuItemDiv_IE(nMenuItem)
	{
		var sDivMenuItemID=this.GetMenuItemDivID(nMenuItem);
		return document.all[sDivMenuItemID];


//		var menuDiv = this.GetMenuDiv();
//		if (menuDiv.children.length<1) return null;
//		if (menuDiv.children[0].tagName!='DIV') return null;
//		
//		var menuDivInnerDiv = menuDiv.children[0];
//		if (menuDivInnerDiv.children.length<nMenuItem) return null;
//		return menuDivInnerDiv.children[nMenuItem-1];
	}
		

	function MoveUp()
	{
		var nMenuItem;
		nMenuItem=this.mnSelMenuItem+1;
		
		//Check if menu item exists
		if(this.GetMenuItemDiv(nMenuItem))
		{
			this.SelectMenuItem(nMenuItem)
		}
	}


	function MoveDown()
	{
		var nMenuItem;
		nMenuItem=this.mnSelMenuItem-1;
		
		if(nMenuItem!=0)
		{
			this.SelectMenuItem(nMenuItem)
		}
	}


	//Highlights a div
	function SelectMenuItem(nMenuItem)
	{
		var divMenuItem=this.GetMenuItemDiv(nMenuItem)
					
		if(divMenuItem)
		{
			if (nMenuItem!=this.mnSelMenuItem)
			{
				this.UnselectMenuItem();
				
				this.mnSelMenuItem=nMenuItem;
				this.SetTextBoxValue();
						
				divMenuItem.className=this.msSelMenuItemCSSClass;
			}
		}
	}

	//unhighlights a div
	function UnselectMenuItem()
	{
		var divMenuItem=this.GetSelMenuItemDiv()
	
		if(divMenuItem)
		{
			divMenuItem.className=this.msMenuItemCSSClass;
		}
	}


	function IsVisibleMenuDiv()
	{
		if (this.GetMenuDiv().style.visibility == 'hidden')
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	
	
	function MoveMenuDivIfAbsolutePos()
	{
		var txtCtrl=this.GetTextBoxCtrl();
		var divMenu=this.GetMenuDiv();
				
		if (txtCtrl.style.position!="absolute")
			return;
			
		//TRACE("MoveMenuDivIfAbsolutePos Moving absolute");
		
		
		//Move menu right under text box
		divMenu.style.left	=txtCtrl.offsetLeft;
		divMenu.style.top	=txtCtrl.offsetTop + txtCtrl.offsetHeight;
	}
	
	
	
	function ShowMenuDiv(sDivContent)
	{
		this.MoveMenuDivIfAbsolutePos();
		
		//TRACE("ShowMenuDiv : " + this.msTextBoxID);
	
		var divMenu=this.GetMenuDiv();
		var sInnerHtml;

		//Use IFrame of the same size as div		
		if (IsIE() && this.mbUseIFrame) 
		{
			sInnerHtml = "<div id='" + this.msMenuDivID + "_content'>";
			sInnerHtml += sDivContent;
			sInnerHtml += "</div>";
			
			var sBlankPage=this.msResourcesDir + "/Blank.html";	//Use blank page to hide 'nonsecure items' message in IE when using HTTPS
			sInnerHtml += "<iframe id='" + this.msMenuDivID + "_iframe' src='" + sBlankPage  + "' frameborder='1' scrolling='no'></iframe>";
		}
		else
		{
			sInnerHtml=sDivContent;
		}
		
		
		divMenu.innerHTML = sInnerHtml;
		
		
		if (IsIE() && this.mbUseIFrame) 
		{
			var divContent;
			divContent=this.GetElementById(this.msMenuDivID + "_content");
			
			var divIframe;
			divIframe=this.GetElementById(this.msMenuDivID + "_iframe");
					
			//Remember display type
			divContent.className=this.msMenuCSSClass;
			divMenu.className="asbMenuBase";
				
			divIframe.style.width = divContent.offsetWidth + 'px';
			divIframe.style.height = divContent.offsetHeight + 'px';
			divIframe.marginTop = "-" + divContent.offsetHeight + 'px';

		}	
		
		divMenu.style.visibility = 'visible';
	}
	

				
	function HideMenuDiv()
	{
		var menuDiv = this.GetMenuDiv();
		menuDiv.style.visibility = 'hidden';
		this.mnSelMenuItem=0;
		
		// clear menuDiv so it's hidden content isn't selectable:
		menuDiv.innerHTML = '';
	}
	
	
	//Utility functions	(don't need this.)
	function IsIE()
	{
		return ( navigator.appName=="Microsoft Internet Explorer" ); 
	}
	

	function IsNav()
	{
		return ( navigator.appName=="Netscape" );
	}
	
	
	function GetInnerHtml(oItem)
	{
		var sOut;
		if (oItem.innerText)
		{
			sOut=oItem.innerText;   // IE
		}
		else if (oItem.textContent)
		{
			sOut=oItem.textContent; // Mozilla
		}
		return (sOut);
	} 


//	function TRACE(sText)
//	{
//		//return;
//		var txtTrace=this.GetElementById("txtASBTrace");
//		if (txtTrace!=null)
//			txtTrace.value = txtTrace.value + sText + "\n";
//	}
}
