﻿    /************************************
     * 创建日期：2008-08-29
     * 作    者：Boron
     * 功    能：Ajax处理
     * 联系方式：13720896576
    ************************************/
var DataValue="";
function Encode(data){return data.split("%").join("%25").split("=").join("%3d").split("&").join("%26").split("+").join("%2b");};//编码数据
function Trim(s)
{
   s = s.replace(/^\s*/,'').replace(/\s*$/, '');
   return s;
}

//取得表单中的数据
function GetData(FormID)
{
    var DataValue="";
    var thisForm=document.getElementById(FormID);
    var elemCount = thisForm.elements.length;
	for( var i=0; i<elemCount; i++ )
	{
		curElem = thisForm.elements[i];
		ControlId = curElem.id;
		ControlType=curElem.type;
		ControlValue=curElem.value;
		if ( ControlType== "submit" || ControlType== "button" || ControlId=="__VIEWSTATE" || ControlId=="__EVENTVALIDATION"){ continue;};
        if ( ControlType== "select-multiple" || ControlType== "select-one" )
        { 
		    for (var x=0;x< curElem.options.length; x++) //取得列表框
			{
				if (curElem.options[x].selected) { DataValue+=ControlId+"="+curElem.options[x].value+"&"; }
			};
        }
        else if ( (ControlType!= "checkbox" && ControlType!= "radio") || curElem.checked ) { DataValue+=ControlId+"="+Encode(ControlValue)+"&"; };//checkbox/radio
	};
};
//RequestTyp:请求对象 Get/poft、RequestUrl:请求地址、RequestData：当RequestTyp为post时使用的数据,可为空、HandleObj:反回数据处理函数(自定议)HandleObj(obj)、MessageSticky:提示信息框显示位置，默认国屏幕右上角。
function IdealAjax(RequestTyp,RequestUrl,RequestData,HandleObj,MessageSticky)
{
    var ThisAjax = new Object();
    ThisAjax.SetTimeOut=null;//定时执行
    ThisAjax.DivObjID=Math.random(); //提示操作信息 
    ThisAjax.elem = document.getElementById(ThisAjax.DivObjID);
    ThisAjax.waitElement=null;
    ThisAjax.CreateWaitElement=function()//创建提示信息
    {
        if (!ThisAjax.elem)
        {
            ThisAjax.elem = document.createElement("div");
            ThisAjax.elem.id = ThisAjax.DivObjID;
            ThisAjax.elem.className="Loading";
            ThisAjax.elem.innerHTML = "Loading ...";
            ThisAjax.elem.style.visibility = "hidden";
            document.body.insertBefore(ThisAjax.elem,document.body.firstChild);
        };
        ThisAjax.waitElement = ThisAjax.elem;
    };
    ThisAjax.ClearWaitElement=function()//消毁提示框
    {
        if(String(ThisAjax.waitElement)!='undefined' && ThisAjax.waitElement!='')
        {
            ThisAjax.waitElement.parentNode.removeChild(ThisAjax.waitElement);
        }
        clearTimeout(ThisAjax.SetTimeOut);
    };
    ThisAjax.RequetTimeOut=function()//请求超时处理
    {
        alert("请求超时，请稍后再试!"); ThisAjax.ClearWaitElement(); ThisAjax.XmlHttpObj.abort();
    };
    ThisAjax.CreateHttpObject=function()
    {
        var xmlhttp;
        /*@cc_on
        @if (@_jscript_version >= 5)
           try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");} catch (E){xmlhttp = false;}}
        @else
           xmlhttp = false;
        @end @*/
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){try {xmlhttp = new XMLHttpRequest();}catch (e){xmlhttp = false;}}; return xmlhttp;
    };
    ThisAjax.XmlHttpObj=ThisAjax.CreateHttpObject();//创建XmlHttpRequest对象
    if (RequestUrl.indexOf("?") > 0) { RequestUrl += "&randnum=" + Math.random();}else{RequestUrl += "?randnum=" + Math.random();};
    ThisAjax.XmlHttpObj.open(RequestTyp,RequestUrl,true);//打开
    var This=ThisAjax.XmlHttpObj; //取得当前XmlHttpObj对象
    This.onreadystatechange = function()
    {
        if(This.readyState == 1)
        {
            ThisAjax.CreateWaitElement();ThisAjax.waitElement.style.visibility = 'visible';//初始化提示信息框
            ThisAjax.SetTimeOut=setTimeout(function(){ThisAjax.RequetTimeOut();},15000);//超时处理(15秒)
        };
        if (This.readyState == 4)
        {
            if(This.status == 200 || This.status == 304)
            {
                if(String(HandleObj)!='undefined' && HandleObj!=''){  HandleObj(This.responseText); }; 
                ThisAjax.ClearWaitElement();
            }
            else{  HandleObj(This.responseText);  ThisAjax.ClearWaitElement(); };//如果请求失败返回失败信息。
        };
    };
    This.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    if(String(RequestData)!='undefined' && RequestData!='') { This.send(RequestData); } else { This.send(null); };
};