var Transport = {
    filename: "format.js",
    debugging: {
        isDebugging: 0,
        debuggingMode: 0,
        linefeed: "",
        containerId: 0
    },
    debug: function(b, a) {
        this.debugging = {
            isDebugging: b,
            debuggingMode: a,
            linefeed: a ? "<br />": "\n",
            containerId: "dubugging-container" + new Date().getTime()
        }
    },
    onComplete: function() {},
    onRunning: function() {},
    run: function(b, c, i, a, e, h) {
        c = this.parseParams(c);
        a = typeof(a) === "string" && a.toUpperCase() === "GET" ? "GET": "POST";
        if (a === "GET") {
            var g = new Date();
            b += c ? (b.indexOf("?") === -1 ? "?": "&") + c: "";
            b = encodeURI(b) + "&" + g.getTime() + g.getMilliseconds();
            c = null
        } else {
            c = encodeURI(c).replace(/\+/g, "%2b")
        }
        e = typeof(e) === "string" && ((e = e.toUpperCase()) === "JSON" || e === "XML") ? e: "TEXT";
        h = h === false ? false: true;
        var k = this.createXMLHttpRequest();
        try {
            var j = this;
            if (typeof(j.onRunning) === "function") {
                j.onRunning()
            }
            k.open(a, b, h);
            if (a === "POST") {
                k.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
            }
            if (h) {
                k.onreadystatechange = function() {
                    if (k.readyState === 4) {
                        switch (k.status) {
                        case 0:
                        case 200:
                            if (typeof(j.onComplete) === "function") {
                                j.onComplete()
                            }
                            if (typeof(i) === "function") {
                                i.call(j, j.parseResult(e, k), k.responseText)
                            }
                            break;
                        case 304:
                            break;
                        case 400:
                            alert("XmlHttpRequest status: [400] Bad Request");
                            break;
                        case 404:
                            alert("XmlHttpRequest status: [404] \nThe requested URL " + b + " was not found on this server.");
                            break;
                        case 409:
                            break;
                        case 503:
                            alert("XmlHttpRequest status: [503] Service Unavailable");
                            break;
                        default:
                        }
                        k = null
                    }
                };
                k.send(c)
            } else {
                if (typeof(j.onRunning) === "function") {
                    j.onRunning()
                }
                k.send(c);
                var l = j.parseResult(e, k);
                k = null;
                if (typeof(j.onComplete) === "function") {
                    j.onComplete()
                }
                if (typeof(i) === "function") {
                    i.call(j, l, k.responseText)
                }
                return l
            }
        } catch(f) {
            if (typeof(j.onComplete) === "function") {
                j.onComplete()
            }
            throw this.filename + "/run() error:" + f
        }
    },
    displayDebuggingInfo: function(c, a) {
        if (!this.debugging.debuggingMode) {
            alert(c)
        } else {
            var e = this.debugging.containerId;
            if (!document.getElementById(e)) {
                div = document.createElement("DIV");
                div.id = e;
                div.style.position = "absolute";
                div.style.width = "98%";
                div.style.border = "1px solid #f00";
                div.style.backgroundColor = "#eef";
                var b = document.body.scrollTop || window.pageYOffset || 0;
                div.style.top = document.body.clientHeight * 0.6 + b + "px";
                document.body.appendChild(div);
                div.innerHTML = "<div></div><hr style='height:1px;border:1px dashed red;'><div></div>"
            }
            var d = div.getElementsByTagName("DIV");
            if (a === "param") {
                d[0].innerHTML = c
            } else {
                d[1].innerHTML = c
            }
        }
    },
    createXMLHttpRequest: function() {
        var d = null;
        if (window.ActiveXObject) {
            var a = ["Microsoft.XMLHTTP", "MSXML6.XMLHTTP", "MSXML5.XMLHTTP", "MSXML4.XMLHTTP", "MSXML3.XMLHTTP", "MSXML2.XMLHTTP", "MSXML.XMLHTTP"];
            for (var c = 0; c < a.length; c++) {
                try {
                    d = new ActiveXObject(a[c]);
                    break
                } catch(b) {
                    continue
                }
            }
        } else {
            d = new XMLHttpRequest()
        }
        return d
    },
    onXMLHttpRequestError: function(b, a) {
        throw "URL: " + a + "\nreadyState: " + b.readyState + "\nstate: " + b.status + "\nheaders: " + b.getAllResponseHeaders()
    },
    parseParams: function(d) {
        var e = "";
        d = d ? d: "";
        if (typeof(d) === "string") {
            e = d
        } else {
            if (typeof(d) === "object") {
                try {
                    e = "JSON=" + JSON.stringify(d)
                } catch(b) {
                    alert("Can't stringify JSON!");
                    return false
                }
            } else {
                alert("Invalid parameters!");
                return false
            }
        }
        if (this.debugging.isDebugging) {
            var a = this.debugging.linefeed,
            c = "[Original Parameters]" + a + d + a + a + "[Parsed Parameters]" + a + e;
            this.displayDebuggingInfo(c, "param")
        }
        return e
    },
    preFilter: function(a) {
        return a.replace(/\xEF\xBB\xBF/g, "")
    },
    parseResult: function(c, f) {
        var a = null;
        switch (c) {
        case "JSON":
            a = this.preFilter(f.responseText);
            try {
                a = JSON.parse(a)
            } catch(d) {
                throw this.filename + "/parseResult() error: can't parse to JSON.\n\n" + f.responseText
            }
            break;
        case "XML":
            a = f.responseXML;
            break;
        case "TEXT":
            a = this.preFilter(f.responseText);
            break;
        default:
            throw this.filename + "/parseResult() error: unknown response type:" + c
        }
        if (this.debugging.isDebugging) {
            var b = this.debugging.linefeed,
            e = "[Response Result of " + c + " Format]" + b + a;
            if (c === "JSON") {
                e = "[Response Result of TEXT Format]" + b + f.responseText + b + b + e
            }
            this.displayDebuggingInfo(e, "result")
        }
        return a
    }
};
var Ajax = Transport;
Ajax.call = Transport.run;
