﻿var xmlHttp;
var backText;
var backXml;

function createXMLHttpRequest() {
    try {
        xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
        } catch (e) {
            try {
                xmlHttp = new XMLHttpRequest();
            } catch (e) { }
        }
    }

}

function ajaxshow(url) {


    //创建浏览器兼容的XMLHttpRequest对象


    var xmlhttp1;
    try {
        xmlhttp1 = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
        try {
            xmlhttp1 = new ActiveXObject('Microsoft.XMLHTTP');
        } catch (e) {
            try {
                xmlhttp1 = new XMLHttpRequest();
            } catch (e) { }
        }
    }
    // alert(xmlhttp1);
    //定义XMLHttpRequest对象的事件处理程序
    xmlhttp1.onreadystatechange = function() {
        if (xmlhttp1.readyState == 4) {
            //关闭显示条

            if (xmlhttp1.status == 200) {
                //当加载成功时将内容显示于页面
                backText = xmlhttp1.responseText;
                //alert(backText);
            } else {
                //否则弹出错误信息

            }
        }
    }
    //创建一个连接
    xmlhttp1.open("get", url);
    //发送请求
    xmlhttp1.send(null);

}

function showresult() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            backText = xmlHttp.responseText;


        }
    }
}

function showxml() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            backXml = xmlHttp.responseXML;

        }
    }
}

function ajaxGet(url) {
    createXMLHttpRequest();

    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = showresult;
    xmlHttp.send(true);
}

function ajaxPost(url) {
    createXMLHttpRequest();
    xmlHttp.open("post", url, true);
    xmlHttp.onreadystatechange = showresult;
    xmlHttp.send(true);
}
function ajaxPosts(url, callback) {
    createXMLHttpRequest();
    showloading(true);
    if (createXMLHttpRequest.os() == "MSIE") {
        xmlHttp.open("post", url, true);
        xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            showloading(false);
                if (xmlHttp.status == 200) {
                    backText = xmlHttp.responseText;
                    callback(backText);
                }
            }
        }
        xmlHttp.send(true);
    }
    else {
        xmlHttp.open("post", url, true);
        xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            showloading(false);
                if (xmlHttp.status == 200) {
                    backText = xmlHttp.responseText;
                    callback(backText);
                }
            }
        }
        xmlHttp.send(true);
    }
}
//遮罩
function showloading(isdisplay) {
    if (isdisplay) {
        var xScroll, yScroll;

        if (window.innerHeight && window.scrollMaxY) {
            xScroll = window.innerWidth + window.scrollMaxX;
            yScroll = window.innerHeight + window.scrollMaxY - 300;
        } else if (document.body.scrollHeight > document.body.offsetHeight) {
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else {
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }

        var windowWidth, windowHeight;

        if (self.innerHeight) {
            if (document.documentElement.clientWidth) {
                windowWidth = document.documentElement.clientWidth;
            } else {
                windowWidth = self.innerWidth;
            }
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) {
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) {
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }
        if (yScroll < windowHeight) {
            pageHeight = windowHeight;
        } else {
            pageHeight = yScroll;
        }
        if (xScroll < windowWidth) {
            pageWidth = xScroll;
        } else {
            pageWidth = windowWidth;
        }

        document.getElementById("loadinglayer").style.display = "block";
        document.getElementById("loadinglayer").style.width = pageWidth + "px";
        document.getElementById("loadinglayer").style.height = pageHeight + "px";
        document.getElementById("loadingdiv").style.display = "block";
        document.getElementById("loadingdiv").style.top = (yScroll - windowHeight + windowHeight / 2 + 16) + "px";
        document.getElementById("loadingdiv").style.left = (pageWidth / 2 - 50) + "px";
    } else {
        document.getElementById("loadinglayer").style.display = "none";
        document.getElementById("loadingdiv").style.display = "none";
    }
}

function ajaxXml(url) {
    createXMLHttpRequest();
    xmlHttp.open("get", url, true);
    xmlHttp.onreadystatechange = showxml;
    xmlHttp.send(true);

}

createXMLHttpRequest.os = function() {
    if (navigator.userAgent.indexOf("MSIE") > 0) {
        return "MSIE";
    }
    else if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) {
        return "Firefox";
    }
    else {
        return "其他浏览器";
    }
}
