﻿function Ajax() { 
    this.xmlHttp = this.GetXmlHttpRequest();
}

Ajax.prototype.GetXmlHttpRequest = function() { 
    if(window.XMLHttpRequest){ 
        return new XMLHttpRequest();
    }
    else if( window.ActiveXObject){ 
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return false;
}

Ajax.prototype.DoCallBack = function(url, urlparam, xmlFile) { 
    if(!this.xmlHttp){  
        return;
    }
    var iThis=this;
    this.xmlHttp.open("POST", url, true);
    this.xmlHttp.onreadystatechange = function() { 
        iThis.OnReadyStateChange();
    }
    if(xmlFile != undefined && xmlFile.constructor == "String") { 
        this.xmlHttp.setRequestHeader("Content-Type", "text/xml");
        this.xmlHttp.send(xmlFile);
    }
    else { 
        this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        this.xmlHttp.send(urlparam);
    }
    
}

Ajax.prototype.OnReadyStateChange = function() { 
    switch(this.xmlHttp.readyState){
        case 0:
        case 1:
        break;
        case 2:
            this.OnStart();
        break;
        case 3:
            this.OnLoading();
        break;
        case 4:
            switch(this.xmlHttp.status) { 
                case 0:
                break;
                case 200:
                    this.OnComplete(this.xmlHttp.responseText, this.xmlHttp.responseXML);
                break;
                default:
                       this.OnError(this.xmlHttp.status, this.xmlHttp.statusText);
                break;
            }
        break;
    }
}

Ajax.prototype.OnStart =function() { 
    //sand......
}

Ajax.prototype.OnLoading = function() { 
    //loading.......
}

Ajax.prototype.OnComplete = function(responseText, responseXML) { 
    //完成
}

Ajax.prototype.OnError = function(status, statusText) {
    //错误
}

function replaceValue(str) { 
    if(str.indexOf("<") != -1) { 
        str=str.replace(/</g,"&lt;");
    }
    if(str.indexOf(">") != -1) { 
        str=str.replace(/>/g,"&gt;");
    }
    if(str.indexOf("'") != -1) { 
        str=str.replace(/\'/g,"‘");
    }
    if(str.indexOf("\"") != -1) { 
        str=str.replace(/\"/g,"&quot;");
    }
    str=str.replace(/(^\s*)|(\s*$)/g, "");
    return str;
}
