﻿/******************************************************
XmlDoc类xeyin.com
******************************************************/
function XmlDoc() {
    this.doc;
    this.isIE;
    this.onreadystatechange;
    if (typeof (ActiveXObject) != "undefined") {
        var e;
        try {
            this.doc = new ActiveXObject("Msxml2.DOMDocument");
        }
        catch (e) {
            try {
                this.doc = new ActiveXObject("Msxml.DOMDocument");
            }
            catch (e) { }
        }
        isIE = true;
    }
    else
        isIE = false;
}

XmlDoc.prototype =
    {
        loadXml: function(xmlString) {
            if (isIE) {
                var docObject = this;
                this.doc.onreadystatechange = function() { docObject.loadXmlComplete() };
                this.doc.loadXML(xmlString);
            }
            else {
                Element.prototype.__defineGetter__("text", function() { if (this.firstChild == null) return ""; return this.firstChild.nodeValue; })
                var parser = new DOMParser();
                this.doc = parser.parseFromString(xmlString, "text/xml");
                this.returnDoc();
                delete parser;
                this.parser = null;
            }
        },

        loadXmlComplete: function() {
            if (this.doc.readyState == 4)
                this.returnDoc();
        },

        returnDoc: function() {
            if (typeof (this.onreadystatechange) == "function") {
                this.onreadystatechange(this.doc);
                delete this.doc;
                this.doc = null;
            }
        }

    }

/************************************************
XmlRequest类
**************************************************/
function XmlRequest() {
    this.request;
    this.action;
    this.returnContent;
    this.onCallbackComplete;
    var e;
    try {
        this.request = new XMLHttpRequest();
    }
    catch (e) {
        try {
            this.request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) { }
    }
}

XmlRequest.prototype = {
    send: function(data) {
        var thisObject = this;
        this.request.onreadystatechange = function() { thisObject.getData(); };
        this.request.open("post", this.action == "" ? document.forms[0].action : this.action, true);
        this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        this.request.send(data == null ? "" : data);
    },

    getData: function() {
        if (this.request.readyState == 4) {
            this.returnContent = this.request.responseText;
            if (typeof (this.onCallbackComplete) == "function") {
                this.onCallbackComplete(this.returnContent);
                delete this.request;
                this.request = null;
            }
        }
    }
}
/***********************************************/
function requestPage(url, callfunction, sendData) {
    var xmlRequest = new XmlRequest();
    xmlRequest.action = url;
    //xmlRequest.onCallbackComplete = function(callbackContent){callbackComplete(callbackContent,callfunction);};
    xmlRequest.onCallbackComplete = callfunction;
    xmlRequest.send(sendData);
}

function requestDom(url, callfunction, sendData) {
    requestPage(url, function(callbackContent) { callbackComplete(callbackContent, callfunction); }, sendData);
}

function callbackComplete(callbackContent, docOnReadyFunction) {
    var doc = new XmlDoc();
    doc.onreadystatechange = docOnReadyFunction;
    doc.loadXml(callbackContent);
}

/***********************************************/

function ajaxRequest(action, params, callbackFun, sendData) {
    requestPage("/AjaxEngine.aspx?action=" + action + "&" + params, callbackFun, sendData);
}


function ajax_UpdatePlace(place, action, params, showLoading, callbackFun, timeout) {
    var e = typeof (place) == "string" ? $(place) : place;
    //var e1 = typeof (disablePlace) == "string" ? $(disablePlace) : disablePlace;
    showPlaceCover(place, true);
    window.setTimeout(function() {
        requestPage("/AjaxEngine.aspx?action=" + action + "&" + params, function(content) { ajax_UpdatePlace_Callback(e, content, callbackFun); }, null);
    }, timeout ? timeout : 1000);

}

function ajax_UpdatePlace_Callback(place, content, callbackFun/*,loadingUI*/) {

    setInnerHTML(place, content);
    if (typeof (callbackFun) == "function")
        callbackFun();
    showPlaceCover(place, false);
}

function setInnerHTML(el, htmlCode) {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('msie') >= 0 && ua.indexOf('opera') < 0) {
        htmlCode = '<div style="display:none">for IE</div>' + htmlCode;
        htmlCode = htmlCode.replace(/<script([^>]*)>/gi, '<script$1 defer>');
        el.innerHTML = htmlCode;
        el.removeChild(el.firstChild);
    } else {
        var el_next = el.nextSibling;
        var el_parent = el.parentNode;
        el_parent.removeChild(el);
        el.innerHTML = htmlCode;
        if (el_next) {
            el_parent.insertBefore(el, el_next)
        } else {
            el_parent.appendChild(el);
        }
    }
}

/******************* 2011-01-18 xiaoby ****************************/
function ajaxRequestByUrl(urlStr, action, params, callbackFun, sendData) {
    requestPage(urlStr + "?action=" + action + "&" + params, callbackFun, sendData);
}

function ajax_UpdatePlaceByUrl(place, urlStr, action, params, showLoading, callbackFun, timeout) {
    var e = typeof (place) == "string" ? $(place) : place;
    showPlaceCover(place, true);
    window.setTimeout(function() {
        requestPage(urlStr + "?action=" + action + "&" + params, function(content) { ajax_UpdatePlace_Callback(e, content, callbackFun); }, null);
    }, timeout ? timeout : 1000);

}

