
    //counter to help create unique ID's
    var idCounter = 0;
    var http_request = false;


function AutoSuggest(elem, suggestions)
{
	var inText = "";
	var show_div = 0;


        //The 'me' variable allow you to access the AutoSuggest object from the elem's event handlers defined below.
        var me = this;

        //A reference to the element we're binding the list to.
        this.elem = elem;

        this.suggestions = suggestions;

        //Arrow to store a subset of eligible suggestions that match the user's input
        this.eligible = new Array();

        //The text input by the user.
        this.inputText = null;

        //A pointer to the index of the highlighted eligible item. -1 means nothing highlighted.
        this.highlighted = -1;

        //A div to use to create the dropdown.
        this.div = document.getElementById("autosuggest");


        //Do you want to remember what keycode means what? Me neither.
        var TAB = 9;
        var ESC = 27;
        var KEYUP = 38;
        var KEYDN = 40;
	var SPACE = 32;

        //The browsers' own autocomplete feature can be problematic, since it will
        //be making suggestions from the users' past input.
        //Setting this attribute should turn it off.
        elem.setAttribute("autocomplete","off");

        //We need to be able to reference the elem by id. If it doesn't have an id, set one.
        if(!elem.id)
        {
                var id = "autosuggest" + idCounter;
                idCounter++;

                elem.id = id;
        }


        /********************************************************
        onkeydown event handler for the input elem.
		Tab key = use the highlighted suggestion, if there is one.
        	Esc key = get rid of the autosuggest dropdown
        	Up/down arrows = Move the highlight up and down in the suggestions.
        ********************************************************/
        elem.onkeydown = function(ev)
        {
                var key = me.getKeyCode(ev);

                switch(key)
                {
                        case TAB:
                        	me.useSuggestion();
                        break;

                        case SPACE:
                        	me.useSuggestion();
                        break;


                        case ESC:
                        	me.hideDiv();
                        break;

                        case KEYUP:
                        	if (me.highlighted > 0)
                        	{
                                	me.highlighted--;
                        	}

                        	me.changeHighlight(key);
                        break;

                        case KEYDN:

				if (me.highlighted < (me.eligible.length - 1))
                        	{
                                	me.highlighted++;
                        	}
                        	me.changeHighlight(key);

                        break;
                }
        };

        /********************************************************
        onkeyup handler for the elem
	If the text is of sufficient length, and has been changed,
        then display a list of eligible suggestions.
        ********************************************************/
        elem.onkeyup = function(ev)
        {

		inText = this.value;

		comma_pos = this.value.lastIndexOf(",");
		inTextlen = this.value.length;

		if(comma_pos != -1)
		{
			if(inTextlen > comma_pos + 1 )
			{
				//user inserted comma.
				inText = this.value.substr(comma_pos + 1);
			}
		}


                var key = me.getKeyCode(ev);
                switch(key)
                {
                //The control keys were already handled by onkeydown, so do nothing.
                case TAB:
                case ESC:
                case KEYUP:
                case KEYDN:

                        return;
                default:

                        //if (this.value != me.inputText && this.value.length > 0)
			if ( this.value.length > 0)
                        {
                                me.inputText = this.value;
				show_div = 0;
                                me.getEligible();

				if(show_div)
				{
	                            me.createDiv();
    	                            me.positionDiv();
        	                    me.showDiv();
				}
				else {
					me.hideDiv();
				}
                        }
                        else
                        {
                                me.hideDiv();
                        }
                }
        };

        /********************************************************
        Insert the highlighted suggestion into the input box, and remove the suggestion dropdown.
        ********************************************************/

        this.useSuggestion = function()
        {

		comma_pos = me.inputText.lastIndexOf(",");
		var inputString = me.inputText.substring(0,comma_pos);
                if (this.highlighted > -1)
                {
			if(inputString.length == 0) {
        	                this.elem.value = this.eligible[this.highlighted];
			}
			else {
				this.elem.value = inputString + "," + this.eligible[this.highlighted];
			}

			var str=this.elem.value;
			this.elem.value = str.replace(/&lt;/,"<");
			var str=this.elem.value;
			this.elem.value = str.replace(/&gt;/,">");
			var str=this.elem.value;
			this.elem.value = str.replace(/<b>/,'');
                        var str=this.elem.value;
                        this.elem.value = str.replace(/<\/b>/,'');


                        this.hideDiv();
                        //It's impossible to cancel the Tab key's default behavior.
                        //So this undoes it by moving the focus back to our field right after
                        //the event completes.
                        setTimeout("document.getElementById('" + this.elem.id + "').focus()",0);
                }
        };

        /********************************************************
        Display the dropdown. Pretty straightforward.
        ********************************************************/
        this.showDiv = function()
        {
                this.div.style.display = 'block';
        };


       /********************************************************
        Hide the dropdown and clear any highlight.
        ********************************************************/
        this.hideDiv = function()
        {
                this.div.style.display = 'none';
                this.highlighted = -1;
        };

        /********************************************************
        Modify the HTML in the dropdown to move the highlight.
        ********************************************************/
        this.changeHighlight = function()
        {
                var lis = this.div.getElementsByTagName('LI');
                for (i in lis)
                {
                        var li = lis[i];

                        if (this.highlighted == i)
                        {
                                li.className = "selected";
                        }
                        else
                        {
                                li.className = "";
                        }
                }
        };

        /********************************************************
        Position the dropdown div below the input text field.
        ********************************************************/

        this.positionDiv = function()
        {
                var el = this.elem;
                var x = 0;
                var y = el.offsetHeight;

                //Walk up the DOM and add up all of the offset positions.
                while (el.offsetParent && el.tagName.toUpperCase() != 'BODY')
                {
                        x += el.offsetLeft;
                        y += el.offsetTop;
                        el = el.offsetParent;
                }

                x += el.offsetLeft;
                y += el.offsetTop;

                this.div.style.left = x + 'px';
                this.div.style.top = y + 'px';
        };

        /********************************************************
        Build the HTML for the dropdown div
        ********************************************************/

        this.createDiv = function()
        {
                var ul = document.createElement('ul');

                //Create an array of LI's for the words.
                for (i in this.eligible)
                {
                        var word = this.eligible[i];

                        var li = document.createElement('li');
                        var a = document.createElement('a');
                        a.href="javascript:false";
                        a.innerHTML = word;
                        li.appendChild(a);

                        if (me.highlighted == i)
                        {
                                li.className = "selected";
                        }

                        ul.appendChild(li);
                }

                this.div.replaceChild(ul,this.div.childNodes[0]);

                /********************************************************
                mouseover handler for the dropdown ul move the highlighted suggestion with the mouse
                ********************************************************/

                ul.onmouseover = function(ev)
                {
                        //Walk up from target until you find the LI.

                        var target = me.getEventSource(ev);

                        while (target.parentNode && target.tagName.toUpperCase() != 'LI')
                        {
                                target = target.parentNode;
                        }

                        var lis = me.div.getElementsByTagName('LI');


                        for (i in lis)
                        {
                                var li = lis[i];
                                if(li == target)
                                {
                                        me.highlighted = i;
                                        break;
                                }
                        }
                        me.changeHighlight();
                };

                /********************************************************
                click handler for the dropdown ul
                insert the clicked suggestion into the input
                ********************************************************/
                ul.onclick = function(ev)
                {
                        me.useSuggestion();
                        me.hideDiv();
                        me.cancelEvent(ev);
                        return false;
                };

                this.div.className="suggestion_list";
                this.div.style.position = 'absolute';
		this.div.style.width = "30%";

        };

        /********************************************************
        determine which of the suggestions matches the input  Match##
        ********************************************************/
        this.getEligible = function()
        {
		show_div = 0;
                this.eligible = new Array();

                for (i in this.suggestions)
                {
                        var suggestion = this.suggestions[i];

                        if(suggestion.toLowerCase().match(inText.toLowerCase()))
                        {
				var str = suggestion;
				suggestion = str.replace(inText,'<b>'+inText+'</b>');
                                this.eligible[this.eligible.length]=suggestion;
				show_div = 1;
                        }
                }
        };

        /********************************************************
        Helper function to determine the keycode pressed in a browser-independent manner.
        ********************************************************/

        this.getKeyCode = function(ev)
        {
                if(ev)                  //Moz
                {
                        return ev.keyCode;
                }
                if(window.event)        //IE
                {
                        return window.event.keyCode;
                }
        };

        /********************************************************
        Helper function to determine the event source element in a
        browser-independent manner.
        ********************************************************/
        this.getEventSource = function(ev)
        {
                if(ev)     //Moz
                {
                        return ev.target;
                }

                if(window.event)        //IE
                {
                        return window.event.srcElement;
                }
        };

        /********************************************************
        Helper function to cancel an event in a
        browser-independent manner.
        (Returning false helps too).
        ********************************************************/
        this.cancelEvent = function(ev)
        {
                if(ev)                  //Moz
                {
                        ev.preventDefault();
                        ev.stopPropagation();
                }
                if(window.event)        //IE
                {
                        window.event.returnValue = false;
                }
        }
}


    function get_email_addresses(url)
    {
        http_request = false;
        if (window.XMLHttpRequest)
	{
		// Mozilla, Safari,...
                http_request = new XMLHttpRequest();

                	if (http_request.overrideMimeType)
			{
                            http_request.overrideMimeType('text/xml');
                            // See note below about this line
                        }
   	}
	else if (window.ActiveXObject)
	{ // IE
                    try
		    {
                            http_request = new ActiveXObject("Msxml2.XMLHTTP");
                    }
	            catch (e)
		    {
                     	try {
                             http_request = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {}
                    }
      	}

        if (!http_request)
	{
               alert('Giving up :( Cannot create an XMLHTTP instance');
               return false;
        }

        http_request.onreadystatechange = alertContents;

        var col_value = document.getElementById('name').value;
        url = url + '?char=' + col_value;
        http_request.open('GET', url, true);
        http_request.send(null);
    }


    function alertContents()
    {
                if (http_request.readyState == 4)
		{
                        if (http_request.status == 200)
			{
				show_popup(http_request.responseText);

                        } else {
                        	alert('There was a problem with the request.');
                        }
                }
    }

    function AddressPopUp()
    {
	document.search.name.focus();
	get_email_addresses('getdate.php');

    }


	function show_popup(address_list_string)
	{
		if(address_list_string != "0")
		{
		var addresses = address_list_string.split(",");

	        //Find all of the INPUT tags
    	    		var tags = document.getElementsByTagName('input');
	        	for (i in tags)
    	    		{
        	        	var tag = tags[i];
            	    		//If it's a text tag, attach an AutoSuggest object.
	                	if(tag.type && tag.type.toLowerCase() == "text" && tag.name == "name")
    	            		{

    	                    		new AutoSuggest(tag,addresses);
        	        	}
	        	}
		}
	}

	window.onload = AddressPopUp;









