﻿// Array to handle connections
var SS_XML_HTTP=new Array();

// Array to previous element content
var SS_PreviousElementContent=new Array();




// Return an object used for connections
function SS_Get_XML_HTTP_Object(){
    var SS_XML_HTTP=null;
    try {
        SS_XML_HTTP=new XMLHttpRequest();
    } catch(e) {
        try {
            SS_XML_HTTP=new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            SS_XML_HTTP=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return SS_XML_HTTP;
}



// Populate the given element with the response from calling 'LoadURL'
// You can optionally pass a third parameter with the text to display during load
// Passing null as LoadingText leaves the element unchanged during load
// Passing JS code as 'CodeAfter' will be executed following the population
// The element must have an ID
function SS_PopulateElement(Element, LoadURL, LoadingText, CodeAfter){
	Element=SS_GetElement(Element);
	if (Element!=null){
		if (Element.id!=null){

			// See if the Element is already being changed
			if (SS_XML_HTTP[Element.id]!=null){
				SS_XML_HTTP[Element.id].abort();
				SS_RevertElement(Element.id);
			}

			// Remember the previous Element content and display LoadingText
			SS_PreviousElementContent[Element.id]=Element.innerHTML;
			if (LoadingText!=null){
				Element.innerHTML=LoadingText;
			}

			// Init the connection
			SS_XML_HTTP[Element.id]=SS_Get_XML_HTTP_Object();
			if (SS_XML_HTTP[Element.id]==null){
				SS_RevertElement(Element.id);
				return false;
			}

			// To prevent script caching in some browsers
			LoadURL=LoadURL+(LoadURL.indexOf("?")==-1?"?":"&")+"cacheid="+Math.random();

			// Prepare the call
			SS_XML_HTTP[Element.id].onreadystatechange=function(){
				if (SS_XML_HTTP[Element.id].readyState==4){
					Element.innerHTML=SS_XML_HTTP[Element.id].responseText;
					SS_XML_HTTP[Element.id]=null;
					if (CodeAfter!=null){
						eval(CodeAfter);
					}
				}
			}

			// Execute the call
			SS_XML_HTTP[Element.id].open("GET", LoadURL, true);
			SS_XML_HTTP[Element.id].send(null);
		}
	}

	return false;
}



// Return the given element.
// Passing the Element's ID as a string will return the element itself.
// Passin the Element itself will return itself.
function SS_GetElement(Element){
	if (typeof(Element)=="string"){
		Element=document.getElementById(Element);
	}
	return Element;
}



// Set the given element with the given content
// If the element has an ID, its previous content will be remembered
function SS_SetElement(Element, Content){
	Element=SS_GetElement(Element);
	if (Element!=null){
		if (Element.id!=null){
			SS_PreviousElementContent[Element.id]=Element.innerHTML;
		}
		Element.innerHTML=Content;
	}

	return false;
}



// Revert an element's content to its previous state
// The element must have an ID
function SS_RevertElement(Element){
	Element=SS_GetElement(Element);
	if (Element!=null){
		if (Element.id!=null){
			if (SS_PreviousElementContent[Element.id]!=null){
				Element.innerHTML=SS_PreviousElementContent[Element.id];
			}
		}
	}

	return false;
}
