// Ajax Javascript Class.
//var Ajax3 = new Object()

// Our main object constructor
Ajax3 = function(_script) {
        var doc;
        var script = _script;
        var _this = this;
        
        var callback;

		this.async = false;		// 101228 DEFAULT TO SYNCHRONOUS CALLS 
		        
        this.I = function()
        {
                this.G("rq=Init");
        }

        // Request data from server
        this.G = function(str, _cb)
        {
                callback = _cb;
                
                try {
                	if (typeof window.ActiveXObject != 'undefined' ) 
                    { 
                    	doc = new ActiveXObject("Microsoft.XMLHTTP"); 
                        if (_this.async) doc.onreadystatechange = _this.ResponseHandler;
                    } 
                    else 
                    { 
                        doc = new XMLHttpRequest(); 
                        if (_this.async) doc.onload = _this.ResponseHandler;
                    }
    
    					                      
	                doc.open( "POST", script, _this.async);
    	            doc.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); 
        	        str += "&rh=" + script;
					//alert(str);
                	doc.send(str); 
                	if (!_this.async) _this.ResponseHandler();
                }
                catch(e)
                {
                        //window.location = 'noactivex.php';
                }
        }


        // The Ajax response callback
		// 20110103 Really obscure bug, which took ages to suss, but found that ResponseHandler needed to
		// be a privileged function, ie. prefixed with 'this.', rather than a private function.
        this.ResponseHandler = function()
        {
                //document.body.style.cursor = 'default';       // 0909091114
            if(doc.readyState == 4 || doc.readyState == 0)
                {
                // only if "OK"
                if(doc.status == 200)
                        {
                                // What is being sent back - XML or JSON?
                                var reply = doc.responseXML;
                                if (reply)
                                        var xml = reply.getElementsByTagName('reply');
                                        
                                if(xml && xml.length)   // must check that xml is an object AND that it has a length
                                {
                                        var msg = reply.getElementsByTagName('message');
                                        if (msg[0])
                                                 if (msg[0].firstChild)
                                                                alert(msg[0].firstChild.nodeValue);
                                        ProcessJavascript(reply);
                                }
                                else
                                {
                                        // it's JSON...
                                        //alert(doc.responseText);
                                        var x = eval('(' + doc.responseText + ')');
                                        
                                        if (x.data.dbgmsg)
                                                alert(x.data.dbgmsg);
                                                
                                        if (x.data.unknown)
                                        {
	                                        alert("Unknown Server Request");
	                                        return;
	                                    }
	                                    
										callback(x.data);
                                }
                }
                else
                        {
                    alert("There was a problem retrieving the data:\n" +
                                doc.statusText);
                }
            }
            
        }
        
        
        ProcessJavascript = function(reply)
        {
                var js = reply.getElementsByTagName('Javascript');
                if (js.length)
                {
                         //alert(js[0].firstChild.nodeValue);
                         //alert(js[0].childNodes.length);
                         
                         // FireFox limits its nodeValue size to 4096 bytes, and splits large
                         // amounts of data into multiple nodes, hence we have to loop thru
                         // childNodes here even though we supplied just a single 'Javascript'
                         // xml node.
                         
                         var ss = '';
                         for (i=0; i<js[0].childNodes.length; i++)
                         {
                                 var s = js[0].childNodes[i].nodeValue;
                         
                                 // (If js not wrapped in CDATA, it should be, but never mind!)
                                 if (s.substring(0, 9) == '<![CDATA[')
                                 {
                                         s = s.substring(9);
                                         //alert("1: " + s);
                                 }
                                 
                                 var s_len = s.length;
                                 if (s.substring(s_len-3) == ']]>')
                                 {
                                         s = s.substring(0, s_len-3);
                                         //alert("2: " + s);
                                 }
                                 
                                 //alert(s);
                                 ss += s;
                         }
                         
                         ss = _this.RawUrlDecode(ss);
                         eval(ss);   // execute string.
                }
        }

}



Ajax3.prototype.RawUrlDecode = function(str) {
    var hash_map = {}, ret = str.toString(), unicodeStr='', hexEscStr='';
 
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
 
    // The hash_map is identical to the one in urlencode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
 
 
    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }
 
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = ret.replace(/%([a-fA-F][0-9a-fA-F])/g, function (all, hex) {return String.fromCharCode('0x'+hex);}); // These Latin-B have the same values in Unicode, so we can convert them like this
    ret = decodeURIComponent(ret);
 
    return ret;
}






