/**
 * Created by CECI.SH.DuanGH
 * User: Duan GaoHui
 * Date: 2008-6-26
 * Time: 12:48:15
 *
 */
function HttpRequest(id) {
    this.id = "HttpRequest_instance_" + id?id:"default";
    this.request = false;
    this.responseText = false;
    this.responseXML = false;
    this.doGet = doGet;
    this.doPost = doPost;
    this.callback = null;
    this.readyStateChangeHandler = readyStateChangeHandler;
    if (window.ActiveXObject) {
        // For Internet Explorer on Windows
        try {
            this.request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                this.request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                this.request = false;
            }
        }
        //FIREFOX, AND OTHER BROWSER
    } else if (window.XMLHttpRequest) {
        try {
            this.request = new XMLHttpRequest();
        } catch (e) {
            this.request = false;
        }
    }
    window[this.id] = this;

    function doGet(url, handler) {
        if (this.request) {
            if (handler) {
                this.callback = handler;
                this.request.open('GET', url, true);
                this.request.setRequestHeader("If-Modified-Since", "0");
                this.request.onreadystatechange = new Function(this.id + ".readyStateChangeHandler();");
                this.request.send(null);
            } else {
                // Synchronous request, wait till we have it all
                this.request.open('GET', url, false);
                this.request.setRequestHeader("If-Modified-Since", "0");
                this.request.send(null);
                this.responseText = this.request.responseText;
                //added by hanst, 2008-06-26,for xml document
                this.responseXML = this.request.responseXML;
            }
        }
    }

	//请求状态 判断是否响应成功
    function readyStateChangeHandler() {
        if (this.request.readyState == 4) {
            if (this.request.status == 200) {
                this.responseText = this.request.responseText;
                //added by hanst , for xml document response, 2008-06-26, __start
                this.responseXML = this.request.responseXML;
                //added by hanst , for xml document response, 2008-06-26, __end
                this.callback();
            } else {
                alert("DuanGh Ajax Error: HTTP status " + this.request.status);
            }
        }
    }

	//发送请求 函数
    function doPost(url, f) {
        if (this.request) {
            this.request.open('POST', url, false);
            this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            //this.request.send(formToContent(f));
            this.responseText = this.request.responseText;
        }
    }
}

//发送xmlHttpRequest请求，解析返回的xml
function makeXmlRequest(url, selectName) {
	//alert('ccccccc');
	var req = new HttpRequest();
	req.doGet(url);
	var xmlDoc = req.responseXML.documentElement;
	//取得XML中的关联关系节点
	var xSel = xmlDoc.getElementsByTagName('select');
	
	buildOptions(xSel, selectName);
}

//根据解析的对象构造关联下拉框
function buildOptions(xSel, selectName) {
    //取得关联下拉框对象
    var select2Obj = document.getElementById(selectName);
    select2Obj.options.length = 0;
    //option的value
    var xValue = null;
    //option的text
    var xText = null;
    //option
    var option = null;
    
    //如果是下拉式单选框,需要加上"请选择"列表
    var i = 0;
    if(select2Obj.multiple == true){
    	i = 1;
    }    
    //设置关联下拉框的值
    for (; i < xSel.length; i++) {    	
    	xValue = xSel[i].attributes[0].value;
    	xText = xSel[i].attributes[1].value;
    	option = new Option(xText, xValue);
    	try {
    		//add不支持friefox浏览器
    		if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){
    			select2Obj.appendChild(option);
    		}else{
    			select2Obj.add(option);
    		}
    	} catch(e) {
    	}
    }
}
var v2Ajax = new HttpRequest("global");

