﻿// JScript 文件
//建立xmlDocument对象
function createDomDocument()
{
	var xmlData = new ActiveXObject("Msxml2.DOMDocument");
	xmlData.async = false;

	if (arguments.length > 0)
		xmlData.loadXML(arguments[0]);

	return xmlData;
}


//在指定节点上增加一个节点
function appendNode(xmlDoc, root, strNodeName)
{
	var nodeText = "";

	if (arguments.length > 3)
		nodeText = arguments[3];

	var node = xmlDoc.createNode(1, strNodeName, "");
	if (nodeText != "")
		node.text = nodeText;

	root.appendChild(node);

	return node;
}
//向服务器传值
function xmlSend(strURL, xmlDoc)
{
	var xmlResult;
	if (arguments.length > 2)
	{
		xmlResult = xmlSend1(strURL, xmlDoc, arguments[2]);
	}
	else
	{
		xmlResult = xmlSend1(strURL, xmlDoc);
	}
	
	if (xmlResult.documentElement.nodeName == "ResponseError")
	{
		var xmlNode = xmlResult.documentElement.selectSingleNode("Value");
		if (xmlNode != null)
		{
			if (xmlNode.text == "您的连接已断开，请重新登录！")
			{
				//alert('你的连接已断开，请重新连接！');
				var sFeature = "dialogWidth:380px; dialogHeight:300px; center:yes; help:no; resizeble:no; scroll:no; status:no";
				var oInputParams = new Object;
									
				var filePath = getRootDir3(document.URLUnencoded) +"/AdminManage/LogOn_Session.htm";
				var sResult = showModalDialog(filePath, oInputParams, sFeature);
				xmlResult = xmlSend1(strURL, xmlDoc);			
			}
		}
	}
	return xmlResult;
}

//发送xmlHttpRequest
function xmlSend1(strURL, xmlDoc)
{
	innerXmlHttp = new ActiveXObject("Msxml2.XmlHttp");

	var strDoc;

	if (typeof(xmlDoc) == "object")
		strDoc = xmlDoc.xml;
	else
		strDoc = xmlDoc;

	
	var bAysync = false;

	if (arguments.length > 2)
	{
		bAysync = true;
		innerXmlHttp.onreadystatechange = handleStateChange;
		innerHandleStateChange = arguments[2];
	}

	innerXmlHttp.open("POST", strURL, bAysync);
	innerXmlHttp.send(strDoc);

	if (bAysync)
		return innerXmlHttp;
	else
	{
		if (innerXmlHttp.responseXML.xml.length > 0)
			return innerXmlHttp.responseXML;
		else
			return createErrorXML(innerXmlHttp.responseText, "");
	}
}

//构造一个和服务器返回信息相同的XML
function createErrorXML(errValue, errStack)
{
	var xmlDoc = createDomDocument("<ResponseError/>");

	appendNode(xmlDoc, xmlDoc.documentElement, "Value", errValue);
	appendNode(xmlDoc, xmlDoc.documentElement, "Stack", errStack);
	appendNode(xmlDoc, xmlDoc.documentElement, "Client", "true");

	return xmlDoc;
}

//返回一个节点的属性值，如果没有这个属性，则返回空串
function getAttrValue(node, strAttrName)
{
	var attr = node.attributes.getNamedItem(strAttrName);

	if (attr)
		return attr.value;
	else
		return "";
}

