﻿
    //-----------------------------------------------------------------------------------------
    //Ajax
    //Author: 蒋懂礼
    //-----------------------------------------------------------------------------------------    
        
    var Ajax = new Ajax();
    
    function Ajax()
    {
        this.StartRequest = Ajax_StartRequest;  //(rqPage, params, callBack, buttonID, ybRequest, escapeParam)
    }    
        
    //-----------------------------------------------------------------------------------------
        
    var Ajax_xmlHttp;
    var Ajax_button;
    var Ajax_callBackFunction;
    var Ajax_tempCursor;

    //rqPage            : 请求的页面
    //params            : 传到请求页面的参数    
    //callBack          : 回调方法
    //buttonID          : 发生请求的按钮ID
    //ybRequest         : 是否异步请求
    //noEscape          : 是否不用加密
    function Ajax_StartRequest(rqPage, params, callBack, buttonID, ybRequest, escapeParam)
    {
        if(ybRequest == null)
            ybRequest = true;

        Ajax_callBackFunction = callBack;
        Ajax_button = document.getElementById(buttonID);
        if(Ajax_button != null)
        {
            Ajax_tempCursor = Ajax_button.style.cursor;
            Ajax_button.disabled = "disabled";
            Ajax_button.style.cursor = "wait";
        }
        Ajax_xmlHttp = createXmlHttpRequest();
        Ajax_xmlHttp.onreadystatechange = Ajax_HandleResult;
        Ajax_xmlHttp.open("POST", rqPage, ybRequest);
       Ajax_xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
       // Ajax_xmlHttp.setRequestHeader("Content-Type","text/html;charset=UTF-8;");

        if(escapeParam == null || escapeParam)
            Ajax_xmlHttp.send(escape(params));
        else
            Ajax_xmlHttp.send(params);
    }
            
 
    function createXmlHttpRequest()
    {
        if(window.XMLHttpRequest)
        {
            Ajax_xmlHttp=new XMLHttpRequest();
        
            if(Ajax_xmlHttp.overrideMimeType)
                {
                    Ajax_xmlHttp.overrideMimeType("text/xml");
                }
        }
        else if(window.ActiveXObject)
        {
            try
            {
                Ajax_xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
            }
            catch(e)
            {
                Ajax_xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");   
            }
        }
        if(!Ajax_xmlHttp)
        {
            window.alert("你的浏览器不支持创建XMLhttpRequest对象");
        }
        return Ajax_xmlHttp;
    }


    function Ajax_HandleResult()
    {
        if(Ajax_xmlHttp.readyState == 4)
        {
            if(Ajax_xmlHttp.status == 200)
            {
                if(Ajax_callBackFunction != null)
                    Ajax_callBackFunction(Ajax_xmlHttp);

                if(Ajax_button != null)
                {
                    Ajax_button.disabled = "";
                    Ajax_button.style.cursor = Ajax_tempCursor;
                }
            }
            else
                alert("ErrorCode: " + Ajax_xmlHttp.status + "\nErrorText: " +  Ajax_xmlHttp.statusText);
        }
    }
    