﻿//ajax操作类

//创建ZAJAX对象
function ZAJAX(url)
{    
    //创建xmlhttp对象
    this.create = function()
    {
    if(window.XMLHttpRequest) return new XMLHttpRequest();
    try{
    return new ActiveXObject('MSXML2.XMLHTTP.4.0');
    }catch(e){try{
    return new ActiveXObject('MSXML2.XMLHTTP');
    }catch(e){try{
    return new ActiveXObject('Microsoft.XMLHTTP');
    }catch(e){return null;}}}
    }
    //xmlhttp对象
    var ZAJAXHttpRequest = this.create();
    //loading事件
    this.onloading;
    var temponloading;
    //同步
    this.sync = function (arg)
    {
        if(this.onloading!=undefined)
        {
            this.onloading(true);
        }
        ZAJAXHttpRequest.open('POST', url, false);
        ZAJAXHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        ZAJAXHttpRequest.send(arg);
        var zreturn=ZAJAXHttpRequest.responseText;
        ZAJAXHttpRequest.abort();
        if(this.onloading!=undefined)
        {
            this.onloading(false);
        }
        return zreturn;
    }
    //异步
    var callbackfunction;
    this.async = function (arg,callback)
    {   
        if(this.onloading!=undefined)
        {
            this.onloading(true);
        }
        temponloading=this.onloading;
        callbackfunction=callback;
        ZAJAXHttpRequest.open('POST', url, true);
        ZAJAXHttpRequest.onreadystatechange = ZAJAXasynccallback;
        ZAJAXHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        ZAJAXHttpRequest.send(arg);
    }
    //回调函数
    function ZAJAXasynccallback()
    {
        if (ZAJAXHttpRequest.readyState == 4)
        {
            if (ZAJAXHttpRequest.status == 200) 
　　　　    {
　　　　        var zreturn=ZAJAXHttpRequest.responseText;
　　　　        ZAJAXHttpRequest.abort();
　　　　        if(temponloading!=undefined)
　　　　        {
　　　　            temponloading(false);
　　　　        }
　　　　　　　　callbackfunction(zreturn);
　　　　　　}
        }
    }
}
//对字符串的操作
function zsplit()
{
    this.converttozarray = zarrayconverter;
    function zarrayconverter(zstr)
    {
        return zstr.split('\u1024');
    }
    this.converttozarraystring = zarraystringconverter;
    function zarraystringconverter(zstr)
    {
        return zstr.split('\u1229');
    }
}
//自定义table类，解析传后台传来的datatable
function ztable(zstr)
{   zsplit.call(this);
    this.converttoztable = ztableconverter;
    function ztableconverter()
    {   
        var tempzarraystring = this.converttozarraystring(zstr);
        this.columns = this.converttozarray(tempzarraystring[0]);
        this.rows = new Array();
        for(var j=1;j<tempzarraystring.length;j++)
        {
            var temp = this.converttozarray(tempzarraystring[j]);
            var newrow = new Array();
            for(var k=0;k<this.columns.length;k++)
            {
                newrow[this.columns[k]] = temp[k];
            }
            this.rows[j-1]=newrow;
        }
    }
    this.converttoztable();
}
