var mint = {
   Request : function(){
        var newRequestObject = {
                        xmlHttpRequest : null,
                        responseText : null,
                        responseXML : null,
                        responseJSON : null,
                        getJSON : false,
                        params : new Array(),
                        url : "",
                        async : true,
                        method : "GET",
                        contentType : "text/plain",
                        username : "",
                        password : "",
                        form : null,
                        disableForm : true,
                        status : null,
                        statusText : null,
                        reqDone : false,
                        retryCount : 0,
                        retryNum : 3,
                        timeout : 5000,
                        OnStateChange : function() {},
                        OnLoading : function() {},
                        OnLoaded : function() {},
                        OnInteractive : function() {},
                        OnComplete : function() {},
                        OnSuccess : function() {},
                        OnError : function() {},
                        OnAbort : function() {},
                        OnRetry : function() {},
                        OnTimeout : function() {},
                        Send : function(url, target) {
                                var paramStr = "";
                                this.reqDone = false;
                                !url ? url = this.url : this.url = url;
                                if(window.XMLHttpRequest) this.xmlHttpRequest = new XMLHttpRequest();
                                else if(window.ActiveXObject) {
                                        try {
                                                this.xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                                        }
                                        catch(e) {
                                                this.xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                                        }
                                }
                                for(var i in this.params){
                                        if(i != 0) paramStr += "&";
                                        paramStr += this.params[i].name+"="+this.params[i].value;
                                }
                                if(this.method == "post") this.xmlHttpRequest.open(this.method, url, this.async, this.username, this.password);
                                else this.xmlHttpRequest.open(this.method, url+(!/\?/.test(url) ? "?"+paramStr : "&;"+paramStr), this.async, this.username, this.password);
                                try {
                                if(this.method == "post")
                                        this.xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                                else
                                        this.xmlHttpRequest.setRequestHeader("Content-Type", this.contentType);
                                } catch(e) {}

                                try {
                                        this.xmlHttpRequest.setRequestHeader("If-Modified-Since", "Sat, 11 Jan 1977 00:00:00 GMT");
                                } catch(e) {}
                                var that = this;
                                this.xmlHttpRequest.onreadystatechange =
                                function() {
                                        that.OnStateChange();
                                        switch(that.xmlHttpRequest.readyState) {
                                                case 1: that.OnLoading(); break;
                                                case 2: that.OnLoaded(); break;
                                                case 3: that.OnInteractive(); break;
                                                case 4:
                                                        that.OnComplete();
                                                        if(that.xmlHttpRequest.status == 200){
                                                                that.reqDone = true;
                                                                that.responseText = that.xmlHttpRequest.responseText;
                                                                that.responseXML = that.xmlHttpRequest.responseXML;
                                                                that.status = that.xmlHttpRequest.status;
                                                                that.statusText = that.xmlHttpRequest.statusText;
                                                                if(target) $(target).innerHTML = that.responseText;
                                                                if(that.getJSON) that.responseJSON = eval('(' + that.responseText + ')');
                                                                if(that.form && that.disableForm) {
                                                                        for(var i = 0; i < that.form.elements.length; i++) {
                                                                                that.form.elements[i].disabled = false;
                                                                        }
                                                                }
                                                                that.OnSuccess();
                                                        }
                                                        else that.OnError(that.xmlHttpRequest.status);
                                                        break;
                                        }
                                }
                                if(this.method == "post") this.xmlHttpRequest.send(paramStr);
                                else this.xmlHttpRequest.send(null);
                                setTimeout(
                                                function() {
                                                if(!that.reqDone) {
                                                                that.xmlHttpRequest.onreadystatechange = function() {};
                                                                that.xmlHttpRequest.abort();
                                                                that.OnTimeout();

                                                                if(that.retryCount < that.retryNum) {
                                                                        that.retryCount++;
                                                                        that.Send();
                                                                        that.OnRetry();
                                                                } else {
                                                                        that.retryCount = 0;
                                                                        that.OnAbort();
                                                                }
                                                        }
                                                },
                                                this.timeout);

                                this.params.length = 0;
                        },
                        SendForm : function(form, url, method) {
                                this.form = $(form);
                                method ? this.method = method : this.method = this.form.method
                                if(!url) url = this.form.action;
                                var input = this.form.elements;
                                for(var i = 0; i < input.length; i++) {
                                        if(this.disableForm) input[i].disabled = true;
                                        switch(input[i].type) {
                                                case "radio":
                                                case "checkbox": if(input[i].checked) this.AddParam(input[i].name, input[i].value); break;
                                                case "select-one": this.AddParam(input[i].name, input[i].options[input[i].selectedIndex].value); break;
                                                case "select-multiple":
                                                        for(var x = 0; x < input[i].options.length; x++) {
                                                                if(input[i].options[x].selected)
                                                                        this.AddParam(input[i].name, input[i].options[x].value);
                                                        }
                                                break;
                                                default: this.AddParam(input[i].name, input[i].value);
                                        }
                                }
                                this.Send(url);
                        },
                        AddParam : function(name, value){
                                var newParam = {
                                        name : name,
                                        value : value
                                }
                                this.params.push(newParam);
                        }
                }
        return newRequestObject;
    }
};


function $(id){
    if(typeof(id) == "string") return document.getElementById(id);
    else return id;
}

function EBookDownload(id, logged) {
    if(logged == 0 || logged == ""){
        alert('Vui lòng đăng nhập để có thể tải sách này!');
        return false;
    } else {
        var req = mint.Request();
        req.OnSuccess = function() {
            $('thank_'+id).innerHTML = this.responseText;
        }
        req.Send('../ajax.php?action=downebook&id='+id);
        return true;
    }
}
function EBookViewMore(id){
    var req = mint.Request();
    req.OnSuccess = function() {
        $('viewmore_'+id).innerHTML = this.responseText;
    }
    req.Send('../ajax.php?action=viewmoroebook&id='+id);
}
function topic4rum(){
    var req = mint.Request();
    req.OnSuccess = function() {
        $('topic4rum').innerHTML = this.responseText;
    }
    req.Send('ajax.php?action=topic4rum');
}
function checkUsername(userName){
    if(userName.length > 4){
        var req = mint.Request();
        req.OnSuccess = function() {
            $('checkUsername').innerHTML = this.responseText
        }
        req.Send('ajax.php?action=checkUsername&username='+userName);
    }
}
function checkUseremail(email, username){
    if(email.length > 10){
        var req = mint.Request();
        req.OnSuccess = function() {
            $('checkUseremail').innerHTML = this.responseText
        }
        req.Send('ajax.php?action=checkUseremail&email='+email+'&username='+username);
    }
}
function resetPass(newpass, email, username){
    var req = mint.Request();
    req.OnSuccess = function() {};
    req.Send('ajax.php?action=resetPass&email='+email+'&username='+username+'&newpass='+newpass);
}
function update_hs_giasu(id, luong_thoathuan, lephi, thoigianbatdau, fullname, da_co_lop, lop){
    var req = mint.Request();
    req.OnSuccess = function() {
    	alert(this.responseText);
    };
    req.Send('../ajax.php?action=update_hs_giasu&id='+id+'&luong_thoathuan='+luong_thoathuan+'&thoigianbatdau='+thoigianbatdau+'&lephi='+lephi+'&fullname='+fullname+'&da_co_lop='+da_co_lop+'&lop='+lop);
    return true;
}
function update_hs_phuhuynh(id, fullname, da_co_giasu){
    var req = mint.Request();
    req.OnSuccess = function() {
    	alert(this.responseText);
    };
    req.Send('../ajax.php?action=update_hs_phuhuynh&id='+id+'&fullname='+fullname+'&da_co_giasu='+da_co_giasu);
    return true;
}
function autoGetWeather(city){
	var req = mint.Request();
    req.OnStateChange = function() {
        $('weatherHTML').innerHTML = "<div align='left'><img src='images/loading23.gif' border=0></div>";
    };
    req.OnSuccess = function() {
        $('weatherHTML').innerHTML = this.responseText;
    };
    req.Send('ajax.php?action=weather&city='+city);
}

