function InitAjax() {
    var ajax = false;
    try {
        ajax = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            ajax = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E) {
            ajax = false;
        }
    }
    if (!ajax && typeof XMLHttpRequest != "undefined") {
        ajax = new XMLHttpRequest();
    }
    return ajax;
}

var ajaxActionFlag = "0";
function refreshItem(item) {
    if (ajaxActionFlag == "1") {
        alert("数据提交中，请等待");
        return;
    }
    
    cursor_wait();
    
    document.getElementById("module").value = 'ajax';
    document.getElementById("action").value = 'refresh' + item;
    
    var url = "/ajax";
    var ajax = InitAjax();
    ajax.open("POST", url, true);
    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send(getformdata("mainForm"));
    ajaxActionFlag = "1";
    ajax.onreadystatechange = function () {
        if (ajax.readyState == 1) {
        } else {
            if (ajax.readyState == 2) {
            } else {
                if (ajax.readyState == 3) {
                } else {
                    if (ajax.readyState == 4) {
                        if (ajax.status == 200) {
                            if (item == "hiddenSpan") {
                            	eval(decodeURI(ajax.responseText));
                            } else {
                            	document.getElementById(item).innerHTML = ajax.responseText;
                            }
                            if (item == "windowInsideDIV") {
                                if (document.getElementById('hiddenSpanInWindowInside')) {
                                    eval(decodeURI(document.getElementById('hiddenSpanInWindowInside').innerHTML));
                                }
                                else {
                                    showWindow("infoWindow");
                                }
                            }
                        }
                        ajaxActionFlag = "0";
                        cursor_clear();
                    } else {
                        alert("提交数据出错，请稍后重试！");
                        ajaxActionFlag = "0";
                        cursor_clear();
                    }
                }
            }
        }
    };
}

// Changes the cursor to an hourglass
function cursor_wait() {
	document.body.style.cursor = 'wait';
}

// Returns the cursor to the default pointer
function cursor_clear() {
	document.body.style.cursor = 'default';
}

function getformdata(formobj) {
    var dtype=Array("input","select","textarea"); 
    var dtypeNum = 3;
    var d = document.getElementById(formobj);
    var i = 0;
    var j = 0;
    var qstring = "";
    var nodeArray = new Array();
    for(;i<dtypeNum;i++) {
        nodeArray.push(d.getElementsByTagName(dtype[i]));
    }
    var nodenum = 0;
    for(i=0;i<dtypeNum;i++) {
        nodenum = nodeArray[i].length;
        var tmpobj = nodeArray[i];
        for(j=0;j<nodenum;j++) {
            var stype = tmpobj[j].type;
            if (stype == "text"  || stype == "password" || stype == "hidden" || stype == "textarea") {
                qstring+="&" + tmpobj[j].name + "=" + encodeURIComponent(tmpobj[j].value);  
            }
            else if (stype == "checkbox" || stype == "radio") {
                if (tmpobj[j].checked) qstring+="&" + tmpobj[j].name + "=" + encodeURIComponent(tmpobj[j].value);  
            }
            else if (stype == "select-one" || stype == "select-multiple") {
                var selectNum = tmpobj[j].length;
                for(var k =0;k<selectNum;k++) {
                    if (tmpobj[j][k].selected) qstring+="&" + tmpobj[j].name + "=" + encodeURIComponent(tmpobj[j][k].value);  
                }
            }
        }
    }
    return qstring.substring(1,qstring.length);
}
