/**
 * @author:    SnowTao.Li
 * @date:      2008-2-15
 * @time:      13:49:22
 * @E-mail:    SnowTao.Li@gmail.com
 * @desc:      Ajax Class
 */
function checkBrowser() {
    this.version    = navigator.appVersion;
    this.dom        = document.getElementById?1:0;
    this.ie8        = (this.version.indexOf("MSIE 8")>-1 && this.dom)?1:0;
    this.ie7        = (this.version.indexOf("MSIE 7")>-1 && this.dom)?1:0;
    this.ie6        = (this.version.indexOf("MSIE 6")>-1 && this.dom)?1:0;
    this.ie5        = (this.version.indexOf("MSIE 5")>-1 && this.dom)?1:0;
    this.ie4        = (document.all && !this.dom)?1:0;
    this.ns5        = (this.dom && parseInt(this.version) >=  5) ?1:0;
    this.ns4        = (document.layers && !this.dom)?1:0;
    this.mac        = (this.version.indexOf('Mac') > -1) ?1:0;
    this.safari4    = (this.version.indexOf('Safari') > -1) ?1:0;
    this.opera      = (navigator.userAgent.indexOf('Opera')>-1);
    this.ie         = (this.ie8 || this.ie7 || this.ie6 || this.ie5 || this.ie4);
    this.ns         = (this.ns4 || this.ns5);
    this.bw         = (this.ie6 || this.ie5 || this.ie4 || this.ns5 || this.ns4 || this.mac || this.ope);
    this.nbw        = (!this.bw);
    this.safari     = (this.safari4 || this.mac);
    this.chrome     = (this.version.indexOf("Chrome")>-1)?1:0;
    
    if (this.opera) {
        this.ns4    = false;
        this.ns5    = false;
        this.ns     = false;
    }
    
    if (this.ie) {
    	this.browserLanguage   = navigator.browserLanguage;
    } else {
    	this.browserLanguage   = navigator.language;
    }
    return this;
}

function trimEnter(p_str) {
	var return_str	= '';
	return_str	= p_str.replace(/\n\r/g,'<br />');
    return_str  = return_str.replace(/'/g,"\\'");
	return return_str;
}

function Ajax(method,url,type,sendstr,p_element_id,p_func) {

    this.xmlres = '';
    this.doc    = '';
    this.returnstate = '';
    this.element_id = '';   // -- the object element id
    this.func = '';   // -- the function that run when the request has finished
    this.getXMLHttp(method,url,type,sendstr,p_element_id,p_func);
}

Ajax.prototype.getXMLHttp = function(method,url,type,sendstr,p_element_id,p_func) {
    
    this.doc = checkBrowser();
    if ('undefined' == typeof(p_element_id)) {
        this.element_id   = '';
    } else {
        this.element_id   = p_element_id;
    }
    if ('undefined' == typeof(p_func)) {
        this.func   = '';
    } else {
        this.func   = p_func;
    }
    if (window.XMLHttpRequest) {
        this.xmlres = new XMLHttpRequest();
//        if (this.xmlres.overrideMimeType) {
//            this.xmlres.overrideMimeType('text/xml');
//        }
    } else if (window.ActiveXObject) {
        try {
            this.xmlres = new ActiveXObject("MSXML2.XMLHTTP.3.0");
        } catch (e) {
            try {
                this.xmlres = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                this.xmlres = false;
            }
        }
    } else {
        this.xmlres = false;
    }
    
    if (!this.xmlres) {
        window.alert("XMLHttpRequest error.");
        return false;
    }
    
    if ('' == url) {
        return ("URL empty!");
    }    
    if ('' == type) {
        type = false;
    }
    
    
    if ('' == method) {
        return ("method empty!");
    } else if ('POST' == method || 'post' == method) {
        this.xmlres.open(method,url,type);
        this.xmlres.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    } else {
         this.xmlres.open(method,url,type);
    }
    var c = this;
    if (this.doc.ns) {
        this.xmlres.onload = function() {
            if (4 == c.xmlres.readyState) {
                if (200 == c.xmlres.status) {
                    c.returnstate = c.xmlres.responseText;
                } else {
                    c.returnstate   = 'wait';
                }
            } else {
                c.returnstate   = 'wait';
            }
            if ('' != c.element_id) {
                c.ajax_return(c.returnstate);
            }
        };
    } else {
        this.xmlres.onreadystatechange = function() {
            if (4 == c.xmlres.readyState) {
                if (200 == c.xmlres.status) {
                    c.returnstate = c.xmlres.responseText;
                } else {
                    c.returnstate   = 'wait';
                }
            } else {
                c.returnstate   = 'wait';
            }
            if ('' != c.element_id) {
                c.ajax_return(c.returnstate);
            }
        };
    }
    this.xmlres.send(sendstr);
    return c.returnstate;
    
}
// -- XMLHttpRequest
Ajax.prototype.stateChange = function() {

    if (4 == this.xmlres.readyState) {
        if (200 == this.xmlres.status) {
            this.returnstate = this.xmlres.responseText;
        } else {
            this.returnstate   = 'wait';
        }
    } else {
        this.returnstate   = 'wait';
    }
    if ('' != this.element_id) {
        ajax_return(this.returnstate);
    }
}

Ajax.prototype.ajax_return = function(p_str) {
    var obj_element    = document.getElementById(this.element_id);
    if ('undefined' != typeof(obj_element)) {
        if (!(/wait/.test(p_str))) {
            if ('' != this.func) {
                obj_element.style.diaplay   = "none";
                if (typeof this.func=='function') {
                    p_str   = p_str + "";
                    this.func(p_str);
                } else {
    			    p_str	= trimEnter(p_str);
                    eval(this.func+"('"+p_str+"')");
                }
            } else {
                obj_element.style.diaplay   = "block";
                obj_element.innerHTML  = p_str;
            }
        } else {
            obj_element.style.diaplay   = "block";
            obj_element.innerHTML = 'Please Wait...';
        }
        
    } else {
        if ('visible' == obj_element.style.visibility) {
            obj_element.style.visibility  = "hidden";
        } else {
            obj_element.style.display  = "none";
        }
        obj_element.innerHTML  = '';
    }
}
