intrasitePath = "website_search.php";
submitButton = "search_submit";
searchBox = "search_box";

function AJAXSearch ( searchPath, submitButton, textField )
{
    this.executeSearch = function ( )
    {
        if( thisObj.textField )
        {
            thisObj.searchTerm = thisObj.textField.value;
            thisObj.textField.className = thisObj.textFieldClass + " busy";
            if( document.getElementById("ajaxsearch_container") )
            {
                oldSearch = document.getElementById("ajaxsearch_container");
                oldSearch.parentNode.removeChild( oldSearch );
            }
            if( window.XMLHttpRequest )
            {
                thisObj.xmlhttp = new XMLHttpRequest();
            }
            // IE
            else if ( window.ActiveXObject )
            {
                thisObj.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
			
            // now if we have a valid XMLHttp object we send the request.
            if( thisObj.xmlhttp != null )
            {
                thisObj.xmlhttp.onreadystatechange = thisObj.displayResults;
                thisObj.xmlhttp.open( "GET", thisObj.searchPath + "?searchterm=" + thisObj.searchTerm + "&ajax=true" , true );
                thisObj.xmlhttp.send( null );
            }
            else
            {
                alert( "Sorry your browser does not support some functionality necessary for this search engine to work, please upgrade your browser to a newer version");
            }
            return( false );
        }
    }
		
    this.displayResults = function ()
    {
        if( thisObj.xmlhttp.readyState == 4 )
        {
            if( thisObj.xmlhttp.status == 200 )
            {
                thisObj.textField.className = thisObj.textFieldClass;
                var results = thisObj.xmlhttp.responseXML.getElementsByTagName("result");
                var resultsContainer = document.createElement("div");
                resultsContainer.id = "ajaxsearch_container";
                resultsContainer.className = "ajaxsearch_container";
		
                var resultsHeader = document.createElement( "div");
                resultsHeader.className = "ajaxsearch_results_header";
                resultsContainer.appendChild( resultsHeader );
		
                var resultTerm = document.createElement("div");
                resultTerm.className = "ajaxsearch_searchtext";
                resultTerm.appendChild( document.createTextNode( "Search for: " + thisObj.searchTerm.substr(0, 50) ));
                resultsHeader.appendChild( resultTerm );
		
                var numberResults = document.createElement("div");
                numberResults.className = "ajaxsearch_number_results";
                numberResults.appendChild( document.createTextNode( results.length + " results found." ));
                resultsHeader.appendChild( numberResults );
		
                var closeButton = document.createElement("div");
                closeButton.className = "ajaxsearch_close_button";
                closeButton.onclick = function ()
                {
                    var container = document.getElementById("ajaxsearch_container");
                    container.parentNode.removeChild( container );
                }
                resultsHeader.appendChild( closeButton );
		
                var resultsBox = document.createElement("div");
                resultsBox.className = "ajaxsearch_results";
                resultsContainer.appendChild( resultsBox );
				
                var odd = false;
                for( x = 0, arrayLength = results.length; x < arrayLength; x++)
                {
                    tempDiv = document.createElement("div");
                    if( !odd )
                    {
                        tempDiv.className = "ajaxsearch_result_element even";
                    }
                    else
                    {
                        tempDiv.className = "ajaxsearch_result_element odd";
                    }
                    odd = !odd;
					
                    if( results[x].getAttribute("class") == "highlight" )
                    {
                        tempDiv.className += " highlight";
                    }
			
                    tempLink = document.createElement("a");
                    tempLink.className = "ajaxsearch_result_title";
                    tempLink.href = results[x].getElementsByTagName("url")[0].firstChild.nodeValue;
                    tempSpan = document.createElement("span");
                    tempSpan.innerHTML = results[x].getElementsByTagName("title")[0].firstChild.nodeValue;
                    tempLink.appendChild( tempSpan );
                    tempDiv.appendChild( tempLink );
			
                    if (results[x].getElementsByTagName("description")[0].firstChild)
                    {
                        var description = document.createElement("div");
                        description.innerHTML = results[x].getElementsByTagName("description")[0].firstChild.nodeValue
                        tempDiv.appendChild( description );
                    }
		
                    resultsBox.appendChild( tempDiv );
                }
					
                thisObj.textField.parentNode.appendChild( resultsContainer );
            }
        }
    }
		
    var thisObj = this;
    this.xmlhttp = null
    this.searchPath = searchPath;
    if( typeof( submitButton ) == "string" )
    {
        submitButton = document.getElementById( submitButton );
    }
    if( typeof( submitButton ) == "object" )
    {
        this.submitButton = submitButton;
    }
    if( typeof( textField ) == "string" )
    {
        textField = document.getElementById( textField );
    }
    if( typeof( textField ) == "object" )
    {
        this.textField = textField;
    }
    if( this.submitButton )
    {
        this.submitButton.onclick = this.executeSearch;
    }
    this.textField.onsubmit = this.executeSearch;
    this.textField.onkeydown = function (e)
    {
        if (!e)
        {
            e = window.event;
        }
        if( e.keyCode == 13 )
        {
            this.onsubmit();
            return false;
        }
    }
    this.textFieldClass = textField.className;
    this.textField.value = "Search";
    this.textField.onfocus = function ()
    {
        if( this.value == "Search" )
        {
            this.value = "";
        }
    }
    this.textField.onblur = function ()
    {
        if( this.value == "" )
        {
            this.value = "Search";
        }
    }
}
	
function website_search_init ()
{
    testSearch = new AJAXSearch ( intrasitePath, submitButton, searchBox);
}
	
if( window.onload )
{
    var oldOnLoad = window.onload;
    window.onload = function ()
    {
        website_search_init();
        oldOnLoad();
    }
}
else
{
    window.onload = website_search_init;
}