﻿// 类
var Class = {
    create : function(){
        return function(){
            this.initialize.apply(this,arguments);
        }
    }
}

// Ajax 对象
var Ajax = Class.create();
// 静态成员
Ajax._httpRequest = null;
Ajax.ResultFuncr = null;          // 返回等待方法
Ajax.ResultFuncb = null;          // 返回方法

Ajax.prototype = {
    isPost  : null,             // 请求方式post或get
    isAsy   : null,             // 异步
    url     : null,             // 地址

    initialize : function(url,isPost,isAsy,funread, funback){
        this.url = url;
        this.isPost = isPost;
        this.isAsy = isAsy;
        Ajax.ResultFuncr = funread;
        Ajax.ResultFuncb = funback;
    },
    
    send : function(){
        if(window.XMLHttpRequest){
            // 非IE浏览器
            Ajax._httpRequest = new XMLHttpRequest();
            // Set request mime is text/xml
            if(Ajax._httpRequest.overrideMimeType)
                Ajax._httpRequest.overrideMimeType('text/xml');
        }
        else if(window.ActiveXObject){
            // IE浏览器
            try { Ajax._httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } 
            catch(ex) { Ajax._httpRequest = new ActiveXObject("Msxml2.XMLHTTP");}
        }
        
        if(Ajax._httpRequest != null){
            var mothed = this.isPost ? "POST" : "GET";
            Ajax._httpRequest.open(mothed,this.url,this.isAsy);
            Ajax._httpRequest.onreadystatechange = this.callback;
            if(this.isPost)
                Ajax._httpRequest.setRequestHeader('Content-type','application/x-www-from-urlencoded');
                
            // 清除缓存
            Ajax._httpRequest.setRequestHeader("If-Modified-Since","0");
            Ajax._httpRequest.setRequestHeader("Cache-Control","no-cache");     
            // 发送请求
            Ajax._httpRequest.send(null); 
        }
    },
    
    callback : function(){
        if(Ajax._httpRequest.readyState == 4){
            // 请求状态为4表示成功
            if(Ajax._httpRequest.status == 200){
                // http状态200表示成功
                eval(Ajax.ResultFuncb + "('" + Ajax._httpRequest.responseText.toString() + "');");
            }
        }else{
            eval(Ajax.ResultFuncr + "();");
        }
    }
}

