var refeshPageTimer = null;

function $(id){
        if(typeof(id) == "string") return document.getElementById(id);
        else return id;
}

function RefeshPage(){
    if(typeof(isPost) == 'undefined'){
        window.location.reload();
    }
}

refeshPageTimer = setTimeout("RefeshPage()", 12000000);

function setHomePage(){
    var isIE= navigator.userAgent.indexOf('MSIE') != -1;
    if(isIE) {
        var homePage = document.body;
        homePage.style.behavior = 'url(#default#homepage)';
        homePage.setHomePage('http://'+site_url);
    } else {
        alert('Nhấn tổ hợp phím [Ctrl+D] để đặt '+site_name+' làm homepage!');
    }
}

function addFavorite(){
    var url = window.location.href;
    var title = document.title;
    if(document.all) {
        window.external.AddFavorite(url, title);
    } else if(window.sidebar) {
        window.sidebar.addPanel(title, url, '');
    }
}

function logout(){
        ht = document.getElementsByTagName("html");
        ht[0].style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
    if(confirm('Bạn thật sự muốn đăng xuất!?')) return true;
    else {
        ht[0].style.filter = "";
        return false;
    }
}

NewWindowPopUp = null;
function OpenWindow(src, wdth, hght, wcenter) {
   if (NewWindowPopUp != null) {
        NewWindowPopUp.close();
        NewWindowPopUp = null;
   }
   if (wcenter == false) {
      wtop = 0;
      wleft = 0;
   } else {
        wtop = (screen.availHeight-hght)/2;
        wleft = (screen.availWidth-wdth)/2;
   }
   NewWindowPopUp = window.open(src, "","toolbar=no,menubar=no,location=no,personalbar=no,scrollbars=yes,status=no,directories=no,resizable=yes,height="+hght+",width="+wdth+",top="+wtop+",left="+wleft+"");
   NewWindowPopUp.focus();
}
function display_tab(tab){
        $('co_info').style.display='none';
        $('site_cfg').style.display='none';
        $('log_info').style.display='none';
        $(tab).style.display='';
}
function show_more_img(id, img_url){
        $(id).src='sanpham/'+id+'/'+img_url;
}
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;
    }
};

