//var loadingText = "<div id=temp style=\"width:100%; height:100%;\" align=\"center\"><img src=\"/images/loadingAnimation.gif\" /></div>";

var g_loadingImage = '/images/loadingAnimation.gif';
var hasLoadingText = true;
function fnGetDefaultLoadingText(){
    if(hasLoadingText)
        return '<div id=temp style="width:100%; height:100%;" align="center"><img src="'+g_loadingImage+'" /></div>';
    return '';
}
function fnGetDefaultLoadingTextCenter(mLeft,mTop){
    if(hasLoadingText)
        return '<div id=temp style="width:100%; height:100%;margin-top:' + mTop + 'px; margin-left:'+mLeft+'px;" align="center"><img src="'+g_loadingImage+'" /></div>';
    return '';
}


// Mozilla only implementation!

// Constructor for generic HTTP client
function HTTPClient() {};


// Add methods and properties as array
HTTPClient.prototype = {
    url: null,

    // Instance of XMLHttpRequest
    xmlhttp: null,

    // Used to make sure multiple calls are not placed
    // with the same client object while another in progress
    callinprogress: false,

    // The user defined handler - see MyHandler below
    userhandler: null,

    onException : function(str){
        
    },

    init: function(url) {
        this.url = url;
//        this.xmlhttp = new XMLHttpRequest();

		try{
			this.xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch(oc){
				try{
					this.xmlhttp = new XMLHttpRequest();
				}catch(e3){
					alert(e3.description);
				}
			}
		}
    },

    // handler argument is a user defined object to be called
    asyncGET: function (handler) {

        // Prevent multiple calls
        if (this.callinprogress) {
            throw "Call in progress";
        };

        this.userhandler = handler;

        // Open an async request - third argument makes it async
        this.xmlhttp.open('GET',this.url,true);

        // Have to assign "this" to a variable - not sure why can't use directly
        var self = this;

        // Assign a closure to the onreadystatechange callback
        this.xmlhttp.onreadystatechange = function() {
            self.stateChangeCallback(self);
        }

        // Send the request
        this.xmlhttp.send(null);
    },

    stateChangeCallback: function(client) {

        switch (client.xmlhttp.readyState) {

            // Request not yet made
            case 1:
                try {
                    client.userhandler.onInit();
                } catch (e) { client.onException(e.description); }
            break;

            // Contact established with server but nothing downloaded yet
            case 2:
                try {
                    // Check for HTTP status 200
                    if ( client.xmlhttp.status != 200 ) {
                        client.userhandler.onError(
                            client.xmlhttp.status,
                            client.xmlhttp.statusText
                            );

                        // Abort the request
                        client.xmlhttp.abort();

                        // Call no longer in progress
                        client.callinprogress = false;
                    }
                } catch (e) { client.onException(e.description ); }
            break;

            // Called multiple while downloading in progress
            case 3:
                // Notify user handler of download progress
                try {
                    // Get the total content length
                    // -useful to work out how much has been downloaded
                    try {
                        var contentLength = 
                            client.xmlhttp.getResponseHeader("Content-Length");
                    } catch (e) {
                        var contentLength = null;
                        client.onException(e.description);
                    } 

                    // Call the progress handler with what we've got
                    client.userhandler.onProgress(
                        client.xmlhttp.responseText,
                        contentLength
                    );

                } catch (e) { client.onException(e.description ); }
            break;

            // Download complete
            case 4:
                try {
                    client.userhandler.onLoad(client.xmlhttp.responseText);
                } catch (e) {
                    client.onException(e.description);
                } finally {
                    // Call no longer in progress
					//alert(client.toString());
                    client.callinprogress = false;
                }
            break;
        }
    }
}

// A user defined handler to response to the XMLHTTPRequest
function HTMLOutputHandler(){};

HTMLOutputHandler.prototype = {

    xmlHttpRequestFunction : undefined,
    
    xmlHttpRequestTargetId : undefined,

    xmlHttpRequestLoadingText : undefined,
    
    callFunctionAfter : undefined,


    onInit: function() {
        
		/* New Code Added : start ============================================================ */
/*		
		if(xmlHttpRequestTargetId == "data") {
			loadingText = "<center><div id=temp style=\"width:;\" align=\"center\"><img src=\"/images/loading.gif\" /></div></center>";
		}
*/		
		/* New Code Added : end ============================================================== */
        progress(this,this.xmlHttpRequestLoadingText);
    },
    onError: function(status,statusText) {
        progress(this,"Error: "+status+": "+statusText+"<br>");
    },
    onProgress: function(responseText,length) {
        /*
    	if(length != null)
	        progress(this,"Downloaded "+responseText.length+" of "+length+"<br>");
	    else
	    	progress(this,responseText.length+" bytes have been downloaded<br>");
	    */
    },
    onLoad: function(result) {
	    success(this,result);
    }
}


function progress(handler,string) {
    document.getElementById(handler.xmlHttpRequestTargetId).style.display='block';
    document.getElementById(handler.xmlHttpRequestTargetId).innerHTML = string;
}


// Just a function to help display results
function success(handler,string) {

    /* New Code Added Here : start ========================================= */
    if(handler.xmlHttpRequestFunction != undefined){
        if(!handler.callFunctionAfter){
            if(handler.xmlHttpRequestFunction(string)){
                document.getElementById(handler.xmlHttpRequestTargetId).style.display='none';
                return;             
            }
        }
    }/* New Code Added Here : end ========================================== */

    document.getElementById(handler.xmlHttpRequestTargetId).style.display='block';
    document.getElementById(handler.xmlHttpRequestTargetId).innerHTML = string;
    
    /* New Code Added Here : start ========================================= */
    if(handler.xmlHttpRequestFunction != undefined){
        if(handler.callFunctionAfter){
            if(handler.xmlHttpRequestFunction(string)){
                document.getElementById(handler.xmlHttpRequestTargetId).style.display='none';
                return;             
            }
        }
    }/* New Code Added Here : end ========================================== */
} 


function xmlHttpRequestCall(url, targetId){
	try{
    	var responseHandler = new HTMLOutputHandler();
        responseHandler.xmlHttpRequestLoadingText = fnGetDefaultLoadingText();
	    responseHandler.xmlHttpRequestTargetId = targetId;
        responseHandler.xmlHttpRequestFunction = undefined;
	    var client = new HTTPClient();
	    url +='&random='+Math.round(Math.random()*10000);
	    client.init(url);
        client.asyncGET(responseHandler);
	}catch(e){
		alert(e);
	}
}


function xmlHttpRequestCallCenter(url, targetId, w , h){
	
    mLeft = w/2 - 208/2;
    mTop = h/2 - 13/3;
	
	try{
    	var responseHandler = new HTMLOutputHandler();
        responseHandler.xmlHttpRequestLoadingText = fnGetDefaultLoadingTextCenter(mLeft,mTop);
	    responseHandler.xmlHttpRequestTargetId = targetId;
        responseHandler.xmlHttpRequestFunction = undefined;

	    var client = new HTTPClient();
	    url +='&random='+Math.round(Math.random()*10000);
	    client.init(url);
        client.asyncGET(responseHandler);
	}catch(e){
		alert(e);
	}
}
	
function xmlHttpRequestCallFunction(url,targetId,functionID){
    
	var responseHandler = new HTMLOutputHandler();
    responseHandler.xmlHttpRequestLoadingText = fnGetDefaultLoadingText();
	responseHandler.xmlHttpRequestTargetId = targetId;
    responseHandler.xmlHttpRequestFunction = functionID;
    responseHandler.callFunctionAfter = false;
	var client = new HTTPClient();
	url +='&random='+Math.round(Math.random()*10000);
	client.init(url);
    client.asyncGET(responseHandler);
}

function xmlHttpRequestCallFunctionCenter(url,targetId,functionID,w,h){

	mLeft = w/2 - 208/2;
    mTop = h/2 - 13/3;

	//try{
		var responseHandler = new HTMLOutputHandler();
        responseHandler.xmlHttpRequestLoadingText = fnGetDefaultLoadingTextCenter(mLeft,mTop);
		responseHandler.xmlHttpRequestTargetId = targetId;
		responseHandler.xmlHttpRequestFunction = functionID;
        responseHandler.callFunctionAfter = false;
		var client = new HTTPClient();
		url +='&random='+Math.round(Math.random()*10000);
		client.init(url);
		client.asyncGET(responseHandler);
	//}catch(e) {
		//alert(e);
	//}
}

function xmlHttpRequestCallFunctionAfterCenter(url,targetId,functionID,w,h){

	mLeft = w/2 - 208/2;
    mTop = h/2 - 13/3;

	//try{
		var responseHandler = new HTMLOutputHandler();
        responseHandler.xmlHttpRequestLoadingText = fnGetDefaultLoadingTextCenter(mLeft,mTop);
		responseHandler.xmlHttpRequestTargetId = targetId;
		responseHandler.xmlHttpRequestFunction = functionID;
        responseHandler.callFunctionAfter = true;
		var client = new HTTPClient();
		url +='&random='+Math.round(Math.random()*10000);
		client.init(url);
		client.asyncGET(responseHandler);
	//}catch(e) {
		//alert(e);
	//}
}


