// JavaScript Document
function getXMLDoc()
{
	var xmldoc = null;
	if (window.ActiveXObject) {
		xmldoc=new ActiveXObject("Microsoft.XMLDOM");
	} else if (document.implementation&&document.implementation.createDocument) {
		xmldoc=document.implementation.createDocument("","doc",null);
	}
	xmldoc.async = false;
	return xmldoc;
}
//使用方法，直接调用sendRequest(url, Method, HttpMethod, params)方法
//参数说明：url--访问地址；Method--调用的服务方法；HttpMethod--传递方法，默认post；params--需要传递的参数或信息；
//多参数使用&连接，post方法经过测试；
//注意！
//成功传输回响函数为xmlResponse(),请自行在此js引用之前添加，否则无效,回响内容为req.responseText，默认为同步。

function getXMLRequester( ){ //此函数是建立XMLHTTP组件的，可能ie低版本无法使用，请参阅msdn；
　　	var xmlhttp_request = false;
　　	try{
　　		if( window.ActiveXObject ){
　　			for( var i = 5; i; i-- ){
　　				try{
				　　if( i == 2 ){
					　　xmlhttp_request = new ActiveXObject( "Microsoft.XMLHTTP" );
				　　}else{
					　　xmlhttp_request = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
						xmlhttp_request.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
				　　}
		　　			break;
				}
				catch(e){
　　					xmlhttp_request = false;
　　				}
　　			}
　　		}else if( window.XMLHttpRequest ){
　　			xmlhttp_request = new XMLHttpRequest();
　　			if (xmlhttp_request.overrideMimeType) {
　　				xmlhttp_request.overrideMimeType('text/xml');
　　			}
　　		}
　　	}catch(e){
　　		xmlhttp_request = false;
		alert("对不起您的浏览器版本太低，请更新后使用。");
　　	}
　　	return xmlhttp_request ;
}

var req;

function sendRequest(url, Method, HttpMethod, params) {
	url += "/" + Method;
	if (!HttpMethod){
		HttpMethod = "POST"; 
	} 
	req = getXMLRequester(); //建立组件
	
	if (req) {
	
		req.onreadystatechange = processReqChange;//调用进程监视函数
		req.open(HttpMethod, url, true);
		req.setRequestHeader("Host", "localhost");
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req.setRequestHeader("Content-Length",params.length);
		req.send(params); //发送数据参数
	}
}

function processReqChange() {
	// 监视数据传递。
	if (req.readyState == 4) {
		if (req.status == 200) {
			xmlResponse();        // connect OK 执行输出函数out()
		} else { //抛出错误
			alert("无法正常连接服务器，错误：\n" + req.statusText+":"+req.status);
		}
	}
}
//将Element转换成String
function elementToString(node)
{
	var sb = new StringBuffer();
	var type = node.nodeType;
    if (type == 1)
    {
		// open tag
        sb.append("<" + node.tagName);
        // output the attributes if any
        attributes = node.attributes;
        if (attributes)
        {
        	var countAttrs = attributes.length;
            var index = 0;
            while(index < countAttrs)
            {
            	att = attributes[index];
                if (att)
	              sb.append(" " + att.name + "=\"" + att.value + "\"");
                index++;
            }
        }
        // recursively sb.append the children
        if (node.hasChildNodes())
        {
        	// close tag
            sb.append(">\n");
            // get the children
            var children = node.childNodes;
            var length = children.length;
            var count = 0;
            while(count <length) 
            {
            	child = children[count];
                sb.append(elementToString(child));
                count++;
            }
            sb.append("</" + node.tagName + ">\n");
        }
        else 
              sb.append("/>\n");
        }
    else if (type == 3) 
    {
		 // if it's a piece of text just sb.append the text
         sb.append(node.data+"\n");
    }
	return sb.toString();
}

