Object.prototype.extendTrueLoreAJAX = function(o, override) { for (var prop in o) { if (override == false && this[prop] != null) { continue; } this[prop] = o[prop]; } return this; }; String.prototype.extendTrueLoreAJAX({ trimLeft: function() { return this.replace(/^\s+/, ""); }, trimRight: function() { return this.replace(/\s+$/, ""); }, trim: function() { return this.replace(/(^\s+)|(\s+$)/g, ""); } }, false); String.extendTrueLoreAJAX({ format: function(s) { for (var i = 1; i < arguments.length; i++) { var reg = new RegExp("\\{" + (i - 1) + "\\}", "g"); s = s.replace(reg, arguments[i]); } return s; } }, false); var Class = { create: function() { return function() { if (typeof (this.initialize) == "function") { this.initialize.apply(this, arguments); } } } }; if (!window.addNamespace) { window.addNamespace = function(ns) { var nsParts = ns.split("."); var root = window; for (var i = 0; i < nsParts.length; i++) { if (typeof (root[nsParts[i]]) == "undefined") { root[nsParts[i]] = {}; } root = root[nsParts[i]]; } }; } if (!window.XMLHttpRequest) { window.XMLHttpRequest = function() { var xmlHttp = null; var clsids = ["Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]; for (var i = 0; i < clsids.length && xmlHttp == null; i++) { try { xmlHttp = new ActiveXObject(clsids[i]); } catch (e) { } } return xmlHttp; }; } var TrueLoreAjax = {}; TrueLoreAjax.toJSON = function(o) { if (o == null) { return "null"; } var v, a = []; switch (o.constructor) { case Number: return isFinite(o) ? o.toString() : "null"; case Boolean: return o.toString(); case String: o = o.replace(/\\/g, '\\\\').replace(/\t/g, '\\t').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\"/g, '\\"'); return '"' + o + '"'; case Array: for (var i = 0; i < o.length; i++) { v = TrueLoreAjax.toJSON(o[i]); a.push(v); } return "[" + a.join(",") + "]"; case Date: var d = {}; d.Year = o.getFullYear(); d.Month = o.getMonth() + 1; d.Day = o.getDate(); d.Hour = o.getHours(); d.Minute = o.getMinutes(); d.Second = o.getSeconds(); d.Millisecond = o.getMilliseconds(); return TrueLoreAjax.toJSON(d); default: if (typeof (o) == "object") { for (var p in o) { if (typeof (o[p]) != "function") { v = '"' + p + '":' + TrueLoreAjax.toJSON(o[p]); a.push(v); } } return "{" + a.join(",") + "}"; } break; } return o.toString(); }; TrueLoreAjax.Request = Class.create(); TrueLoreAjax.Request.prototype = { url: null, method: null, args: null, context: null, callback: null, xmlHttp: null, timer: null, timeout: 30 * 1000, initialize: function(url) { if (url != null) { this.url = url; } this.xmlHttp = new XMLHttpRequest(); }, getEmptyResponse: function() { var r = { error: null, value: null, context: this.context }; r.request = { method: this.method, args: this.args }; return r; }, getResponse: function() { var r = this.getEmptyResponse(); if (this.xmlHttp.getResponseHeader("Content-Type").indexOf("text/xml") > -1) { r.value = this.xmlHttp.responseXML; } else { var txt = this.xmlHttp.responseText; if (txt != null && txt.length > 0) { eval("r.value = " + txt + ";"); } } return r; }, doStateChange: function() { if (this.xmlHttp.readyState == 4) { if (this.timer != null) { window.clearTimeout(this.timer); } var r = null; if (this.xmlHttp.status == 200) { r = this.getResponse(); } else { r = this.getEmptyResponse(); r.error = { message: this.xmlHttp.statusText, name: "TrueLoreAjaxxRequestError" }; } if (typeof (this.callback) == "function") { this.callback(r); } } }, doTimeout: function() { this.xmlHttp.onreadystatechange = function() { }; this.xmlHttp.abort(); var r = this.getEmptyResponse(); r.error = { message: "Ajax reuqest timeout!", name: "TrueLoreAjaxTimeoutError" }; this.callback(r); }, invoke: function(method, args, callback, context) { if (this.xmlHttp == null) { var r = this.getEmptyResponse(); r.error = { message: "Sorry, initialize TrueLoreAjax failed! please check if has your browser been forbidden the ActiveXObject.", name: "TrueLoreAjaxInitializeError" }; return r; } this.method = method; this.args = args; this.context = context; this.callback = callback; if (context && context.timeout) { this.timeout = context.timeout; } var async = (typeof (callback) == "function"); var me = this; var json = String((new Date()).valueOf()); if (args.length > 0) { json = TrueLoreAjax.toJSON(args) + json; } this.xmlHttp.open("POST", this.url, async); this.xmlHttp.setRequestHeader("Content-Type", "text/plain; charset=utf-8"); this.xmlHttp.setRequestHeader("Ajax-method", method); if (async) { this.timer = window.setTimeout(function() { me.doTimeout.call(me); }, this.timeout); this.xmlHttp.onreadystatechange = function() { me.doStateChange.call(me); } } this.xmlHttp.send(json); if (!async) { while (this.xmlHttp.readyState != 4) { } return this.getResponse(); } } };