﻿function FindUsers(url) {

    this.url = "";
    this.tempUrl = url;
    this.idDropDownList = "";
    this.idtextBoxEnterUser = "";
	
    this.TextBoxObject = null;
	this.UserIDObject = null;
	
	this.Changed = false;
	this.isAllowSelectMultiUser = false;
	
	this.Trim = function(stringToTrim) {
		return stringToTrim.replace(/^\s+|\s+$/g,"");
	}
	
	this.LeftTrim = function(stringToTrim) {
		return stringToTrim.replace(/^\s+/,"");
	}
	
	this.RightTrim = function(stringToTrim) {
		return stringToTrim.replace(/\s+$/,"");
	}
	
	function TrimAll(sString) 
	{ 
		while (sString.substring(0,1) == ' ') 
		{ 
			sString = sString.substring(1, sString.length); 
		} 
		while (sString.substring(sString.length-1, sString.length) == ' ') 
		{ 
			sString = sString.substring(0,sString.length-1); 
		} 
		return sString; 
	} 
	
	
    this.Find = function(obj, selectionMode, listGuild, viewGuild) 
    {	
        this.SetWait(obj);
        this.isAllowSelectMultiUser = false;
        this.tempUrl = url + "?selectionMode=" + selectionMode + "&ListGuild=" + listGuild + "&ViewGuild=" + viewGuild +"&keySearch=";
	    this.GetTextBoxObject(obj);
        if(this.TextBoxObject == null || this.TextBoxObject.value == '')
		 return this.AlertMessage();
		 var tmpObject = this;
		window.setTimeout(function()
		 {		
	        if (tmpObject.CreateURL())
	        {
	            tmpObject.MakeRequest();
	            this.Changed = true;
	        }
		}, 500);
        return false;
    } 
	
	this.GetTextBoxObject = function(btnSearch)
	{	 		
		try
		{
			var tmpObj = btnSearch.parentNode.parentNode.parentNode;
			if( tmpObj != null && tmpObj.nodeName.toLowerCase() == "tr") //hidden
			{
				 if(tmpObj.firstChild.firstChild.nodeName.toLowerCase() == "input" )				
				 {
					 if( tmpObj.firstChild.firstChild.getAttribute('type').toLowerCase() == "text")
					 {
					   this.TextBoxObject = tmpObj.firstChild.firstChild;
					   this.TextBoxObject.focus();
					 }
					 if( tmpObj.firstChild.firstChild.nextSibling.getAttribute('type').toLowerCase() == "hidden")
					 {
						this.UserIDObject = tmpObj.firstChild.firstChild.nextSibling;
					 }
				   return true;
				 }		 
			}		
		}catch(e)
		{
		  return false;
		}
	}
	
	this.GetObjectTextBox = function(obj)
	{	
	    var tmpObj = obj.parentNode;
	    if( tmpObj.firstChild.nextSibling.getAttribute('type').toLowerCase() == "hidden")
		{
		    this.UserIDObject = tmpObj.firstChild.nextSibling;
		}
							 
	    if (this.TextBoxObject != null)
        {
            if (this.TextBoxObject != obj)
	        {
    		    this.TextBoxObject.value = this.itemSelected.substring(0, this.itemSelected.length - 1);
    		    this.itemSelected = obj.value + ";";
    	        this.idUserSelected = this.UserIDObject.value + ";";    		    
		    }
        }
        else
        {
            this.itemSelected = obj.value + ";";
    	    this.idUserSelected = this.UserIDObject.value + ";";
    	}
		this.TextBoxObject = obj;
	}

	this.PanelLoaded = function(objPanel){
		alert(objPanel.id);
	}	
	
    this.CreateURL = function() {    
            var keyEntered = this.TextBoxObject.value;
		    var tmpValue = keyEntered.split(';');
		
			if(tmpValue.length == 1)	
			{			
			  this.itemSelected = "";	
			  this.idUserSelected = "";
			} 
			
			if(this.isAllowSelectMultiUser == "false")
			{
				if(tmpValue.length >= 2)
				{
					alert("You are only allowed to enter one item.");
					this.RemoveWait();
					return false;
				}
			}
			
		    keyEntered = tmpValue[tmpValue.length - 1];
			if (this.Trim(keyEntered) == '')
			{
			    this.AlertMessage();
			    return false;
			}
        var urlTemp = this.tempUrl + keyEntered;        
        this.url = urlTemp;
        return true;
    }

    this.MakeRequest = function() {
    try {
                var objTemp = this;                        
                var xmlHttp = new XMLHttpRequest();               
                xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
                    objTemp.GetValueFromServer(xmlHttp.responseText);
                }
            }
            xmlHttp.open("GET", this.url, true);
            xmlHttp.send(null);
        } 
        catch (e) {
            alert(e.message);
        }
    }

	
    this.GetValueFromServer = function(value) {
	
		var arrObjs = eval(value);	
		if(this.TempObjPanel != null)
		   this.TempObjPanel.innerHTML = "";
			
        this.ClearTimeOut();
        this.HiddendropDownList(false, 0);		
		var isNodata = false;
        var tempElement = "<table cellpadding=0 cellspacing=0 border=0px>";
        for (var i = 0; i < (arrObjs.length - 1); i++) {
		
			if(arrObjs[i].value == "No Data"){			
				tempElement = this.EntryNotFound();
				isNodata = true;
				break;
		    } //ms-formbody style=\"Width:223px;cursor:Hand\" class=ms-formbody
            tempElement += "<tr><td class=ms-formbody ><DIV><DIV style=\"Width:211px;cursor:Hand\" onmouseout=bambooFindUser.MouseOut(); onmouseover=bambooFindUser.MouseOver(this); onclick=bambooFindUser.SelectedItem(this,'"+arrObjs[i].value+"');> " + arrObjs[i].text + " </DIV></DIV></td></tr>";
        }
		tempElement +="</table>";
		
		if(this.TempObjPanel != null)
			    this.TempObjPanel.innerHTML = tempElement;  
			
			
		if(!isNodata)			
			this.HiddendropDownList(true, 2000);
			
			this.RemoveWait();
    }
	// style=\"font-style:italic; color:Red; background-color:Transparent\"
	this.EntryNotFound = function()	{	
			this.TextBoxObject.title = "This entry is not found.";	
			if(this.isAllowSelectMultiUser == "false")
			{
			    return "<SPAN class=ms-error>No exact match was found.</SPAN>";
			}
			return "<SPAN class=ms-error></br>No exact match was found.</SPAN>";   
	   //This entry is not found.
	}

    this.itemSelected = "";
	this.idUserSelected = "";
    this.SelectedItem = function(objdiv, idUser) { 
		this.itemSelected  += this.RightTrim(objdiv.innerText) + ";";		
		this.TextBoxObject.value = this.itemSelected.substring(0, this.itemSelected.length - 1);		
		
		this.idUserSelected += this.RightTrim(idUser) + ";";		
		this.UserIDObject.value = this.idUserSelected.substring(0, this.idUserSelected.length - 1);				
		
        this.HiddendropDownList(true, 200);
    }

    this.clearTimeOut;
    this.HiddendropDownList = function(isHidden, timeDelay) {
    var tempObj = this;
    tempObj.clearTimeOut = window.setTimeout(
        function() {
            if (isHidden)
				tempObj.TempObjPanel.style.display = "none";              				
            else
				tempObj.TempObjPanel.style.display = "block";             
        }, timeDelay)
    }

    this.ClearTimeOut = function() {
     window.clearTimeout(this.clearTimeOut);
    }

    this.MouseOver = function(obj) {
        this.ClearTimeOut();
    }

    this.MouseOut = function() {
        this.HiddendropDownList(true, 1000);
    }
    
    this.Test = function(string) {
        alert("Testing: " + string);
    }
	
	this.TempObjPanel = null;
	this.GetObjectTable = function(obj)
	{	
		//alert("hello");
		if ( obj.firstChild.firstChild.tagName.toLowerCase() == "tr" )
		{
			var	object = obj.firstChild.firstChild.nextSibling;
			if(object.firstChild.firstChild.tagName.toLowerCase() == "div")
			{
				this.TempObjPanel = object.firstChild.firstChild;
			}
		}
	}
	
	this.AlertMessage = function()
	{
	  alert("Please, Enter key word to search");
	  this.RemoveWait();
	  return false;
	}
	
	this.tmpObjToSetCursor = null;
	this.SetWait = function(obj)
	{
		this.tmpObjToSetCursor = obj;
		obj.style.cursor="wait";
		document.body.style.cursor="wait";	
	}
	
	this.RemoveWait = function()
	{
		if(this.tmpObjToSetCursor != null)
		{
			this.tmpObjToSetCursor.style.cursor = "auto";
			this.tmpObjToSetCursor = null;
		}
		document.body.style.cursor = "auto";	
	}
}

var bambooFindUser = new FindUsers("/_Layouts/Bamboo.SharePointFilters/BambooSearchValueKey.ashx");

