/** * {url} is the base URL with which this * AMAX object will communicate * * {isXML} is true|false depending on * if this object will return its response * in proper xml format (t) * or simple text (f) * * * 2007 Supernova Media Group Inc. All rights reserved. * Amir Sobhi. (amir [ at ] supernova [ dot ] com) */ function AMAX(url, isXML) { this.url = url; this.hp = getRO(); hp = this.hp; var ev = new Object(); var me = this; ev.onLoad = function(){}; /** * returns a new HTTP Request Object * depending on browser */ function getRO() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if (window.ActiveXObject) { var r = new ActiveXObject("Msxml2.XMLHTTP"); if (!r) return new ActiveXObject("Microsoft.XMLHTTP"); return r; } } /** * sends the query string {q} using * method {m}. {o} is the event whose onLoad * is called upon successfull loading */ this.send = function(q,m) { ev.onLoad = this.onLoad; ev.success = false; q = q.replace(/\s+/i,"_"); hp.open(m, url + (m.toLowerCase()=="get" ? q.charAt(0) == '?' ? q : '?' + q : "")); hp.onreadystatechange = handle; hp.send(m.toLowerCase()=="post" ? q : null); } /** * private method for handling the * request */ function handle(e) { try { if(hp.readyState == 4) { if(hp.status == 200) { if (isXML) setRspn(hp.responseXML); else setRspn(hp.responseText); } } } catch (e) { // do nothing // or // alert('An Error Occurred while parsing "'+ url + '"'); } } /** * private method for setting the response and * calling the events .onLoad property */ function setRspn(e) { ev.success = true; ev.data = e; ev.onLoad(); document.toolkit.events.onAMAXLoad(); } /** * * URL encode / decode functionality added to Amax * Extracted from : http://www.webtoolkit.info/ * **/ // public method for url encoding this.encode = function (string) { return escape(_utf8_encode(string)); } // public method for url decoding this.decode = function (string) { return _utf8_decode(unescape(string)); } // private method for UTF-8 encoding function _utf8_encode (string) { string = string.replace(/\r\n/g,"\n"); var utftext = ""; for (var n = 0; n < string.length; n++) { var c = string.charCodeAt(n); if (c < 128) utftext += String.fromCharCode(c); else if((c > 127) && (c < 2048)) { utftext += String.fromCharCode((c >> 6) | 192); utftext += String.fromCharCode((c & 63) | 128); } else { utftext += String.fromCharCode((c >> 12) | 224); utftext += String.fromCharCode(((c >> 6) & 63) | 128); utftext += String.fromCharCode((c & 63) | 128); } } return utftext; } // private method for UTF-8 decoding function _utf8_decode (utftext) { var string = ""; var i = 0; var c = c1 = c2 = 0; while ( i < utftext.length ) { c = utftext.charCodeAt(i); if (c < 128) { string += String.fromCharCode(c); i++; } else if((c > 191) && (c < 224)) { c2 = utftext.charCodeAt(i+1); string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); i += 2; } else { c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2); string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); i += 3; } } return string; } }