﻿function HttpReq()
{
	this.action = null;
	this.XML = null;
	this.httpInterface = null;

	// Initialise XMLHttpRequest object
	this.resetInterface();

	return true;
}

/* Check if the HttpReq object is available */
HttpReq.prototype.isAvailable = function()
{
	if (this.httpInterface == null)
	{
		return false;
	}
	
	return true;
}

/* Execute the action which has been associated with the completion of this object */
HttpReq.prototype.executeAction = function()
{
	// If XMLHR object has finished retrieving the data
	if (this.httpInterface.readyState == 4)
	{
		// If the data was retrieved successfully
		try
		{
			if (this.httpInterface.status == 200)
			{
				this.action();
			}
			// IE returns status = 0 on some occasions, so ignore
			else if (this.httpInterface.status != 0)
			{
				//alert("There was an error while retrieving the URL: " + this.httpInterface.statusText);
			}
		}
		catch (error)
		{
		}
	}

	return true;
}

/* Return responseText */
HttpReq.prototype.getText = function()
{
	return this.httpInterface.responseText;
}

/* Return responseXML */
HttpReq.prototype.getXML = function()
{
	return this.httpInterface.responseXML;
}

/* Initialise object and load URL */
HttpReq.prototype.loadURL = function(url, cgi)
{
	this.resetInterface();
	var url2 ;
	
	if (cgi != null)
	{
		url2 = url + "?" + cgi;
	}
	else
	{
	    url2 = url;
	}
	
	//alert(url);
	this.httpInterface.open("GET", url2);
	this.httpInterface.send(null);

	return true;
}

/* Turn off existing connections and create a new XMLHR object */
HttpReq.prototype.resetInterface = function()
{
	var self = this;

	if (this.httpInterface != null && this.httpInterface.readyState != 0 && this.httpInterface.readyState != 4)
	{
		this.httpInterface.abort();
	}

    this.httpInterface = getHttpObj();
    
	this.httpInterface.onreadystatechange = function()
		{
			self.executeAction();

			return true;
		};

	return true;
}

/* Assign the function which will be executed once the XMLHR object finishes retrieving data */
HttpReq.prototype.setAction = function(actionFunction)
{
	this.action = actionFunction;

	return true;
}

function getHttpObj()
{
    var httpObj = null;
    try
	{
	    httpObj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (error)
	{
		try
		{
		    httpObj = new XMLHttpRequest();	
		}
		catch (error)
		{
		    httpObj = null;
		}
	}
	
	return httpObj;
}

function loadSyncHttp(url, cgi)
{
	var httpReq = getHttpObj();
	var response = null;
	var url = url + "?" + cgi;
	
	if (null != httpReq)
	{
	    //alert("Calling url:" + url);
	    httpReq.open("GET", url, false); 
	    httpReq.send(null);
	
	    if (httpReq.status == 200)
		    response =  httpReq.responseText;
	}
	
	return response;
}

function loadSyncHttpXML(url, cgi)
{
	var httpReq = getHttpObj();
	var response = null;
	
	if (null != httpReq)
	{
	    httpReq.open("GET", url + "?" + cgi, false); 
	    httpReq.send(null);
	}
	 if (httpReq.status == 200)
		    response =  httpReq.responseXML;
	
	return response;
}

function htmlEncode(s) {
        var str = new String(s);
        str = str.replace(/&/g, "&#38;");
        str = str.replace(/</g, "&#60;");
        str = str.replace(/>/g, "&#62;");
        str = str.replace(/"/g, "&#34;");
        str = str.replace(/'/g, "&#39;");
        
        return str;
} 

function evaluate(res)
{
    if (res.length > 0)
    {
        try
	    {
	        return eval(res);
	    }
	    catch (error)
	    {
	        //alert(error);
	    }
	}
}

function evaluateObjectEqual(res)
{
    var obj = null;
    if (res.length > 0)
    {
        try
	    {
	        res = "obj = " + res;
	        eval(res);
	    }
	    catch (error)
	    {
	        obj = null;
	       // alert(error.message);
	    }
	}
	
	return obj;
}

 function parseJson (text, filter) 
 {
    var j;

    function walk(k, v) 
    {
        var i;
        if (v && typeof v === 'object') 
        {
            for (i in v) 
            {
                if (Object.prototype.hasOwnProperty.apply(v, [i])) 
                {
                    v[i] = walk(i, v[i]);
                }
            }
        }
        return filter(k, v);
    }
    
    if (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/.test(text.
            replace(/\\./g, '@').
            replace(/"[^"\\\n\r]*"/g, ''))) 
    {
        writeLine("start eval: " + (new Date()));
        j = eval('(' + text + ')');
        writeLine("end eval: " + (new Date()));
        return typeof filter === 'function' ? walk('', j) : j;
    }
    
    throw new SyntaxError('parseJSON');
}

function addOnLoad(newOnLoad)
{
    var curOnLoad = window.onload;
    if (typeof window.onload != "function")
	{
		window.onload = newOnLoad;
	}
	else
	{
		window.onload = function()
		{
			curOnLoad();
			newOnLoad();
		}
	}
}

function addOnUnload(newOnUnload)
{
    var curOnUnload = window.onunload;
    if (typeof window.onunload != "function")
	{
		window.onunload = newOnUnload;
	}
	else
	{
		window.onunload = function()
		{
			curOnUnload();
			newOnUnload();
		}
	}
}

function StringBuilder(value)
{
    this.strings = new Array("");
    this.append(value);
}

StringBuilder.prototype.append = function (value)
{
    if (value)
    {
        this.strings.push(value);
    }
}

StringBuilder.prototype.clear = function ()
{
    this.strings.length = 1;
}

StringBuilder.prototype.toString = function ()
{
    return this.strings.join("");
}