<!--
/*
    json.js
    2006-04-28

    This file adds these methods to JavaScript:

        object.toJSONString()

            This method produces a JSON text from an object. The
            object must not contain any cyclical references.

        array.toJSONString()

            This method produces a JSON text from an array. The
            array must not contain any cyclical references.

        string.parseJSON()

            This method parses a JSON text to produce an object or
            array. It will return false if there is an error.
*/
(function () {
    var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        s = {
            array: function (x) {
                var a = ['['], b, f, i, l = x.length, v;
                for (i = 0; i < l; i += 1) {
                    v = x[i];
                    f = s[typeof v];
                    if (f) {
                        v = f(v);
                        if (typeof v == 'string') {
                            if (b) {
                                a[a.length] = ',';
                            }
                            a[a.length] = v;
                            b = true;
                        }
                    }
                }
                a[a.length] = ']';
                return a.join('');
            },
            'boolean': function (x) {
                return String(x);
            },
            'null': function (x) {
                return "null";
            },
            number: function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            object: function (x) {
                if (x) {
                    if (x instanceof Array) {
                        return s.array(x);
                    }
                    var a = ['{'], b, f, i, v;
                    for (i in x) {
                        v = x[i];
                        f = s[typeof v];
                        if (f) {
                            v = f(v);
                            if (typeof v == 'string') {
                                if (b) {
                                    a[a.length] = ',';
                                }
                                a.push(s.string(i), ':', v);
                                b = true;
                            }
                        }
                    }
                    a[a.length] = '}';
                    return a.join('');
                }
                return 'null';
            },
            string: function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            }
        };

    Object.prototype.toJSONString = function () {
        return s.object(this);
    };

    Array.prototype.toJSONString = function () {
        return s.array(this);
    };
})();

String.prototype.parseJSON = function () {
    try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                this.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + this + ')');
    } catch (e) {
        return false;
    }
};

function dAX()
{
  	this.debug = false;
  	this.call = function () 
  	{
		var dax_sfunc = "";
		var dax_cfunc = "";
		var result = "";
		var xmlHttpObject = false;
		var iv;
		
		if(arguments.length<3) 
		{
			alert("Incorrect number of parameters. Please check your function call.");
			return;
		} 
		dax_url = arguments[0];
		dax_sfunc = arguments[1];
		dax_cfunc = arguments[2];
	
		dax_url = _checkUrl(dax_url);
		if(!dax_url)
		{
			alert("No server URL given. Please provide the URL with the first parameter or set global var php_url = 'URL_of_your_server_script'.");
			return;
		}

		var dax_poststr = "dax_afunc=call&dax_sfunc=" + encodeURI(dax_sfunc) + "&dax_cfunc=" + encodeURI(dax_cfunc);
		for(var i = 3; i < arguments.length; i++)
		{
			dax_poststr += _addPostArg(arguments[i]);
		}
		
		xmlHttpObject = _getHTTPReqObj();		
		if (!xmlHttpObject) 
		{
			alert('unable to establish communication  :( ');
			return false;
		}
		
		if((dax_sfunc == null) || (dax_sfunc == "")) 
		{
			if(arguments[3])
				dax_poststr = arguments[3];
		}
		
		if((dax_cfunc == null) || (dax_cfunc == ""))
		{
			xmlHttpObject.open('POST', dax_url, false);
			xmlHttpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttpObject.setRequestHeader("Content-length", arguments.length+1);
			//xmlHttpObject.setRequestHeader("Content-length", 8096);
			xmlHttpObject.setRequestHeader("Connection", "close");
			xmlHttpObject.send(dax_poststr);
			return xmlHttpObject.responseText;
		} 
		else 
		{
			xmlHttpObject.onreadystatechange = function () 
			{
				if (xmlHttpObject.readyState == 4) 
				{
					try
					{
						if(xmlHttpObject.status == 200) 
						{
							result = xmlHttpObject.responseText;
							if (result.parseJSON())
								eval(dax_cfunc+'(result.parseJSON());');
							else 
								eval(dax_cfunc+"(result);");
						} 
						else 
						{
							if(xmlHttpObject.status != 0) 
							{
								alert('There was a problem with the request. '+xmlHttpObject.status);
							}
						}
					}
					catch(e){;}
				}
			}
			xmlHttpObject.open('POST', dax_url, true);
			xmlHttpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttpObject.setRequestHeader("Content-length", arguments.length+1);
			//xmlHttpObject.setRequestHeader("Connection", "close");
			xmlHttpObject.send(dax_poststr);
			return xmlHttpObject;
		}
  	}

	/**************** private ****************/
	
	function _getHTTPReqObj()
	{
		var obj = false;
		if(window.XMLHttpRequest) // mozi, konqui ...
		{
			obj = new XMLHttpRequest();
			if (obj.overrideMimeType) 
			{
				obj.overrideMimeType('text/plain');
			}
		} 
		else if (window.ActiveXObject) // ie
		{
			try 
			{
				obj = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch (e) 
			{
				try 
				{
					obj = new ActiveXObject("Microsoft.XMLHTTP");
				} 
				catch (e) {;}
			}
		}
		return obj;
	}
	function _checkUrl(dax_url)
	{
		if((dax_url == null) || (dax_url == ""))
		{
			if(typeof(php_url) != "string")
			{
				return false;
			}
			dax_url = php_url;
		}
		return dax_url;
	}
	function _addPostArg(a)
	{
		var ret = "";
		if(typeof(a) == "object")
		{
			ret = "&dax_sfunc_args[]="+encodeURI(a.toJSONString());
		}
		else
		{
			ret = "&dax_sfunc_args[]="+encodeURI(a);
		}
		return ret;
	}
}
var dax = new dAX();
//-->
