 //============================
//AutoSuggestBox version 1.5.4
//============================


//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 msResourceDir;
	var mbHasFocus;


	//Internal attributes
	var mnSelMenuItem = 0;	
	var mbCancelSubmit;
	var msOldTextBoxValue="";
	

	//Class methods
	this.GetKey				=GetKey;
	this.GetTextBoxCtrl		=GetTextBoxCtrl;
	this.GetMenuDiv			=GetMenuDiv;
	
	this.GetXmlHttp			=GetXmlHttp;
	this.GetDataFromServer	=GetDataFromServer;
	
	this.SetSelectedValue	=SetSelectedValue;
	this.SetTextBoxValue	=SetTextBoxValue;
	this.GetTextBoxValue	=GetTextBoxValue;
	
    this.OnMouseClick		=OnMouseClick;
	this.OnMouseOver		=OnMouseOver;
 
 	this.OnKeyDown			=OnKeyDown;
	this.OnKeyPress			=OnKeyPress;
	this.OnKeyUp			=OnKeyUp;
    
    this.OnBlur				=OnBlur;
    
    this.GetSelMenuItemDiv	=GetSelMenuItemDiv;
    this.GetMenuItemDivID	=GetMenuItemDivID;
    this.GetMenuItemDiv		=GetMenuItemDiv;
    
    this.MoveUp				=MoveUp;
    this.MoveDown			=MoveDown;
    
    this.SelectMenuItem		=SelectMenuItem;
    this.UnselectMenuItem	=UnselectMenuItem;
    
    this.IsVisibleMenuDiv	=IsVisibleMenuDiv;
	this.MoveMenuDivIfAbsolutePos	=MoveMenuDivIfAbsolutePos;
	this.ShowMenuDiv		=ShowMenuDiv;
	this.HideMenuDiv		=HideMenuDiv;
		

	//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; 
		}
	}
	
	
	function GetTextBoxCtrl()
	{
		return document.getElementById(this.msTextBoxID);
	}
	
	
	function GetMenuDiv()
	{
		return document.getElementById(this.msMenuDivID);
	}
	
	
	//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="/community/app/admin/callback/GetAutoSuggestData.aspx?TextBoxID=" + encodeURIComponent(this.msTextBoxID) + 
										"&MenuDivID=" + encodeURIComponent(this.msMenuDivID) + 
										"&DataType=" + encodeURIComponent(this.msDataType) + 
										"&NumMenuItems=" + this.mnNumMenuItems + 
										"&IncludeMoreMenuItem=" + this.mbIncludeMoreMenuItem + 
										"&MoreMenuItemLabel=" + encodeURIComponent(this.msMoreMenuItemLabel) + 
										"&MenuItemCSSClass=" + encodeURIComponent(this.msMenuItemCSSClass) + 
										"&Keyword=" + encodeURIComponent(sValue) +
										"&DataSource=" + encodeURIComponent(this.msDataSource)
				
		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=document.getElementById(this.msTextBoxID + "_SelectedValue");
		hdnSelectedValue.value=sValue;
	}



	function SetTextBoxValue()
	{
		var divMenuItem=this.GetSelMenuItemDiv();
		//alert(divMenuItem.getAttribute('value'));
		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);
			txtCtrl.setAttribute('GUID',sValue);
		}
	}


	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 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())
			{
				this.HideMenuDiv();
				
				evt.cancelBubble = true;
				
				if (evt.returnValue) evt.returnValue = false;
				if (evt.stopPropagation) evt.stopPropagation();
				
				this.mbCancelSubmit=true;
     		}
     		else
     		{
     			this.mbCancelSubmit=false;
     		}
		}
		else
		{
			this.HideMenuDiv();
		}
				
		return true;
	}
	
	
	function OnKeyPress(evt)
	{
		TRACE("OnKeyPress : " + this.GetKey(evt));
		if ((this.GetKey(evt)==13) && (this.mbCancelSubmit)) 
		{
			return false;
		}
			
		return true;
	}	
	function OnKeyUp(evt)
	{
		var nKey;
		nKey=this.GetKey(evt);
				
		//Skip up/down/enter
		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))
			{
				var divMenu = this.GetMenuDiv();
				if (divMenu.timer) window.clearTimeout(divMenu.timer);
				
				//replace double quotes with empty space
				sNewValue=sNewValue.replace(/'/g,"");
				//Add escape char to single quote
				sNewValue=sNewValue.replace(/\'/, "\\\'");				
				//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()
	{
		this.HideMenuDiv();
		this.mbHasFocus=false;
	}		
	function GetSelMenuItemDiv()
	{
		return this.GetMenuItemDiv(this.mnSelMenuItem);
	}			
	function GetMenuItemDivID(nMenuItem)
	{
		return (this.msTextBoxID + "_mi_" + nMenuItem);
	}	
	function GetMenuItemDiv(nMenuItem)
	{
		var sDivMenuItemID=this.GetMenuItemDivID(nMenuItem);
		return document.getElementById(sDivMenuItemID)
	}
	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=document.getElementById(this.msMenuDivID + "_content");
			
			var divIframe;
			divIframe=document.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()
	{
		this.GetMenuDiv().style.visibility = 'hidden';
		this.mnSelMenuItem=0;
	}
	
	
	//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)
	{
		var txtTrace=document.getElementById("txtASBTrace");
		if (txtTrace!=null)
			txtTrace.value = txtTrace.value + sText + "\n";
	}
}

 
 
 
 function tp_addLI_PLT(id, textboxID, hiddenInputID, removeImage, removeText, removeTextCSS, MaxWords){
			
			
            var Parent = document.getElementById(id);
            var NewLI = document.createElement("LI");	
            var TextBoxVal = document.getElementById(textboxID).value;	  
            var ElementID = TextBoxVal;                   
            var removeLink = "";
            var doContinue = "true";
                        
            if (MaxWords != "-1")
			{			
				//var tempArr = trim(TextBoxVal.split(' '))
				var tempArr = TextBoxVal.split(' ');
				if (tempArr.length > MaxWords)
				{
					alert("The maximum number of words for this entry is " + MaxWords);
					doContinue = "false";
				}
			} 
			var uID = Math.floor(Math.random()*10001)     
			ElementID = ElementID+"||"+uID;       
			if (removeImage != "")
			{				
				removeLink = " <img src=\"" + removeImage + "\" onclick=\"tp_removeLI_PLT('" + ElementID + "','" + hiddenInputID + "');\">"				
			}
			else
			{
				removeLink = "<a href=\"javascript:void(0)\" class=\"" + removeTextCSS + "\" onClick=\"tp_removeLI_PLT('" + ElementID +"','" + hiddenInputID + "');\">" + removeText + "</a>"
			}        
			if (TextBoxVal == "")
				doContinue = "false"
				  
			if (doContinue == "true")
			{
				NewLI.innerHTML = TextBoxVal + " " + removeLink;
				NewLI.id = ElementID;
				Parent.appendChild(NewLI);
	                        
				//adding to the hidden input here
				var OldHiddenInputVal = document.getElementById(hiddenInputID).value;
				document.getElementById(hiddenInputID).value = OldHiddenInputVal + ',' + TextBoxVal;            
            }
    }
    function tp_removeLI_PLT(id, hiddenInputID){	
		var sElVal = id.split('||')[0]		
		var Parent = document.getElementById(id).parentNode;				
		var Child = document.getElementById(id);				
        Parent.removeChild(Child);                 
        var OldHiddenInputVal = document.getElementById(hiddenInputID).value;
        var NewHiddenInputVal = OldHiddenInputVal.replace(sElVal, '');        
        document.getElementById(hiddenInputID).value = NewHiddenInputVal;        
    }
    function tp_addLI_PL(id, textboxID, hiddenInputID, removeImage, removeText, removeTextCSS, MaxWords, SpecialHiddenID){
			//alert(document.getElementById(textboxID).getAttribute('GUID'));
			var TextBoxVal = document.getElementById(textboxID).value;				
			var TextBoxGUID = document.getElementById(textboxID).getAttribute('GUID');
			var HiddenInputToFind = hiddenInputID + "_" + TextBoxGUID
			
			var bShowError = false
			var bIsAllowed = false
			try
			{		     	
				if  (TextBoxGUID == null)
				{
					//alert('guid is null');
					bIsAllowed = false
					bShowError = true
				}
				else
				{
					if 	(document.getElementById(HiddenInputToFind) != null)
					{			
						//alert('already exists');			
						var temp = document.getElementById(HiddenInputToFind).value;
						bIsAllowed = false;
						bShowError = false;
					}
					else
					{				
						//alert('here');		
						//doesn't exist. create and proceed
						var currentElement = document.createElement("input");
						currentElement.setAttribute("type", "hidden");
						currentElement.setAttribute("name", HiddenInputToFind);
						currentElement.setAttribute("id", HiddenInputToFind);
						currentElement.setAttribute("value", TextBoxGUID);
						tp_insertAfter(currentElement, hiddenInputID + "_id");
						//document.appendChild(currentElement);	
						//alert(document.getElementById(HiddenInputToFind).id);				
						bIsAllowed = true
					}		
				}		
			}
			catch(e)
			{
				//alert(e);
				bIsAllowed = false
				bShowError = true
			}
			
			if ( bIsAllowed == false && bShowError == true)
			{
				alert('Sorry, the item is not in the accepted list');
			}
			else if ( bIsAllowed == false && bShowError == false)
			{				
				//do nothing
				//alert('far');
			}
			else
			{
				var Parent = document.getElementById(id);
				var NewLI = document.createElement("LI");	             
				var ElementID = TextBoxVal                   
				var removeLink = "";
				var doContinue = "true";
				if (MaxWords != "-1")
				{			
					//var tempArr = trim(TextBoxVal.split(' '))
					var tempArr = TextBoxVal.split(' ')
					if (tempArr.length > MaxWords)
					{
						alert("The maximum number of words for this entry is " + MaxWords);
						doContinue = "false";
					}
				}				
					            
				if (removeImage != "")
				{				
					removeLink = " <img src=\"" + removeImage + "\" onclick=\"tp_removeLI_PL('" + ElementID + "','" + HiddenInputToFind + "');\">"				
				}
				else
				{
					removeLink = "<a href=\"javascript:void(0)\" class=\"" + removeTextCSS + "\" onClick=\"tp_removeLI_PL('" + ElementID + "','" + HiddenInputToFind + "');\">" + removeText + "</a>"
				}          
				if (doContinue == "true")
				{
					//alert('continue');
					NewLI.innerHTML = TextBoxVal + " " + removeLink;
					NewLI.id = ElementID;
					Parent.appendChild(NewLI);		            		                     
				}
            }
    }
    function tp_insertAfter(newElement,targetElement) 
    {	
		var parent = document.getElementById(targetElement).parentNode;
		//alert(parent);
		if(parent.lastchild == document.getElementById(targetElement)) 
		{
			parent.appendChild(newElement);
		} 
		else 
		{
			parent.insertBefore(newElement, document.getElementById(targetElement).nextSibling);
		}		
	}
    function tp_removeLI_PL(id, hiddenInputID){				
		//remove from list
		//alert(hiddenInputID);		
		var Parent = document.getElementById(id).parentNode;				
		var Child = document.getElementById(id);	
		//alert(Parent);			
        Parent.removeChild(Child);            
        //removing hidden input
        var Parent2 = document.getElementById(hiddenInputID).parentNode;
        var Child2 = document.getElementById(hiddenInputID);
        //alert(Parent2);
        Parent2.removeChild(Child2);        
    }
    function tp_doWildcardSearch(id)
    {
		var v = document.getElementById(id).value;		
		window.location = '/community/app/nf/search/tptresults.aspx?wc=' + v;
    }
    function tp_doNewsItemSearch(id)
    {
		var v = document.getElementById(id).value;
		v = v.trim();		
		var fi = 'http://'+location.host+'/blogapp/'+v+'.atom';
		window.location = '/community/app/templates/tptNewsPreview2.aspx?f='+fi;
    }
    function tp_ViewPhotos2(id)
    {		
		var v = document.getElementById(id).value;
		//v = v.trim();	
		window.location = '/community/app/templates/tptblogtypeentries.aspx?k=' + v;
    }
    function tp_ViewPhotos(tag)
    {		
		window.location = '/community/app/templates/tptblogtypeentries.aspx?k='+tag;
    }
    function tp_ViewVideos(id)
    {		
		// commented out by TJM because this eval was breaking on the ATLFAL video tag site
		//var v = document.getElementById(id).value;
		//v = v.trim();		
		window.location = '/community/app/templates/tptblogtypeentries.aspx?tpt=tp_videogallery&k=' + id;
    }