﻿// Default text to display in an element while it is loading
var DEFAULT_LOADING_TEXT="<table width=\"100%\" height=\"100%\"><tr><td align=\"center\" valign=\"middle\"><img src=\"/Directory/images/loading.gif\" width=\"169\" height=\"39\" border=\"0\" alt=\"Loading\" /></td></tr></table>";
// The current element waiting to be populated with a response
var PopulatingElement=null;
// The code to execute immediately following a successful population
var CodeAfterPopulation=null;

// Init the object used for conenctions
function GetXmlHttpObject(){
    var xmlHttp=null;
    try {
        xmlHttp=new XMLHttpRequest();
    } catch(e) {
        try {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}


// Populate the element with the given 'DivID' with the response from calling 'LoadURL'
// You can optionally pass a third parameter with the text to display during load (defaults to DEFAULT_LOADING_TEXT)
function PopulateElement(DivID, LoadURL, LoadingText, CodeToFollow){
	// See if LoadingText was passed.  If not, go with the default
	if (LoadingText==null){
        LoadingText=DEFAULT_LOADING_TEXT;
	}
	if (PopulatingElement!=null){
		document.getElementById(DivID).innerHTML=LoadingText;
	    // If trying to populate an element while it is awaiting a response, abort the previous call
	    // Otherwise, wait until the current operation is complete by retrying this function every 10ms
	    if (PopulatingElement==document.getElementById(DivID)){
            xmlHttp.abort();
        } else {
		    setTimeout("PopulateElement('"+DivID+"', '"+LoadURL+"', "+(LoadingText==null?"null":"'"+LoadingText+"'")+", "+(CodeToFollow==null?"null":"'"+CodeToFollow+"'")+")", 10);
		    return false;
        }
	}
	PopulatingElement=document.getElementById(DivID);
    PopulatingElement.innerHTML=LoadingText;
	CodeAfterPopulation=CodeToFollow;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		return false;
	}
	PopulatingElement.innerHTML=LoadingText;
	xmlHttp.onreadystatechange=PopulateElementResponse;
	xmlHttp.open("GET", LoadURL, true);
	xmlHttp.send(null);
	return false;
}


// Populate the element with the response upon completion
function PopulateElementResponse(){
	if (xmlHttp.readyState==4){
		PopulatingElement.innerHTML=xmlHttp.responseText;
		if (CodeAfterPopulation!=null){
		    eval(CodeAfterPopulation);
		}
		PopulatingElement=null;
		CodeAfterPopulation=null;
	}
}


// Pass true or false to either show or hide the search results area
function ShowElement(ElementID, State){
    document.getElementById(ElementID).style.visibility=(State?"visible":"hidden");
}