﻿///String.prototype.replaceAll = stringReplaceAll;

function StringReplaceAll(oldStr, AFindText, ARepText)
{
	var ttt = new String(oldStr);
	raRegExp = new RegExp(AFindText,"g");
	return ttt.replace(raRegExp,ARepText)
}
function ReplaceContent(oldStr)
{
	var str = new String(oldStr);
	var reg1 = new RegExp("%", "g");
	var reg2 = new RegExp("\\+", "g");
	return str.replace(reg1, "%26").replace(reg2, "%2B");
}

function HTML2Decode(str)
{
	var s = "";
	if (str.length == 0)
		return "";
	s = str.replace(/&amp;/g,"&");
	s = s.replace(/&lt;/g,"<");
	s = s.replace(/&gt;/g,">");
	s = s.replace(/&nbsp;/g," ");
	s = s.replace(/&#39;/g,"\'");
	s = s.replace(/&quot;/g,"\"");
	return s;
}

var MAX_THREAD = 1;
var CURRENT_THREAD = 0;
var THREAD_LIST = new Array();

setTimeout(ServerProcQueryWorks_Cycle, 10000);

function ServerProcQueryWorks_Cycle() {
    ServerProcQueryWorks();
    
    setTimeout(ServerProcQueryWorks_Cycle, 10000);
}
	
function ServerProcQueryWorks()
{
	if (THREAD_LIST.length > 0 && CURRENT_THREAD < MAX_THREAD)
	{
		var obj = THREAD_LIST.shift();
		CURRENT_THREAD = CURRENT_THREAD + 1;
		obj.invokeInternal();
	}
}

function ServerProc()
{
	ServerProc.prototype.url = null;
	ServerProc.prototype.name = null;
	ServerProc.prototype.returnedXml = null;
	ServerProc.prototype.onreturned = null;
	ServerProc.async = true;

	ServerProc.prototype.paramNames = new Array();
	ServerProc.prototype.paramValues = new Array();			
	ServerProc.prototype.oReq = null;
	ServerProc.prototype.resultNames = new Array();
	ServerProc.prototype.resultValues = new Array();
	ServerProc.prototype.errors = null;
	ServerProc.prototype.warnings = null;
	var theObj = this;
    
    ServerProc.prototype.hasErrors = function() {
        return theObj.errors != null && theObj.errors.length > 0;
    }
    
    ServerProc.prototype.hasWarnings = function() {
        return theObj.warnings!=null&&theObj.warnings.length>0;
    }

	ServerProc.prototype.addParameter = function(paramName,paramValue) {
		theObj.paramNames[theObj.paramNames.length] = paramName;
		theObj.paramValues[theObj.paramValues.length] = paramValue;
	}
	
	ServerProc.prototype.clearParameters = function() {
		theObj.paramNames = new Array();
		theObj.paramValues = new Array();
		theObj.resultNames = new Array();
		theObj.resultValues = new Array();
	}
	
	ServerProc.prototype.getParameter = function(index) {
		return theObj.paramValues[index];
	}
	
	ServerProc.prototype.getParameterName = function(index) {
		return theObj.paramNames[index];
	}
	/*
	 * RequestStatChange
	 */
	ServerProc.prototype._onreqstatechange = function() {
		if (theObj.oReq) {
			if (theObj.oReq.readyState == 4) {
				theObj.returnedXml = theObj.oReq.responseText;
				
				theObj._analyzeResults();	
				if(theObj.onreturned) {
					theObj.onreturned();
				}
				//document.getElementById("lblMsg").innerText = "Complete";
				CURRENT_THREAD = CURRENT_THREAD - 1;
				ServerProcQueryWorks();
			}
		//	else
				//document.getElementById("lblMsg").innerText = "Loadding...";
		}
	}
	
	ServerProc.prototype.invoke = function()
	{
	    if (CURRENT_THREAD < MAX_THREAD)
	    {
			CURRENT_THREAD = CURRENT_THREAD + 1;
			theObj.invokeInternal();
		}
		else
		{
			THREAD_LIST.push(theObj);
		}
	}
	
	ServerProc.prototype.invokeInternal = function() {
		theObj = this;
		theObj.oReq = new ActiveXObject("MSXML2.XMLHTTP");
		var param = "";
		if(theObj.paramNames&&theObj.paramNames.length>0) {
			var i = 0;
			for(;i<theObj.paramNames.length;i++) {
				var name = theObj.paramNames[i];
				var value = theObj.paramValues[i];
				
				param = param + "&" + name + "=" + StringReplaceAll(encodeURI(value), "&", "%26");
				///param = param + "&" + name + "=" + ReplaceContent(encodeURI(value));
			}
		}
		
		var content = "_moduleName=" + theObj.name + param;
		theObj.oReq.open("POST", theObj.url, theObj.async);
		
		theObj.oReq.setRequestHeader("Content-Length", content.length);
		theObj.oReq.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded;text/xml;charset=utf-8");
		
		if (!theObj.async)
		{
			theObj.oReq.onreadystatechange = theObj._onreqstatechange;
		}
		
		theObj.oReq.send(content);
		
		if (!theObj.async)
		{
			theObj._onreqstatechange();
		}
	}
	
	ServerProc.prototype.getResultNames = function() {
		return theObj.resultNames;
	}
	
	ServerProc.prototype.getResultValues = function() {
		return theObj.resultValues;
	}
	
	ServerProc.prototype.getResult = function(name) {
		if(!theObj.resultNames)
			return null;
		if(!name)
			return null;
		var i=0;				
		for(;i<theObj.resultNames.length;i++) {
			if(name.toLowerCase()==theObj.resultNames[i])
				return theObj.resultValues[i];
		}
		return null;
	}
	
	ServerProc.prototype._analyzeResults = function()
	{
		theObj = this;
		var s = new String();
		if (!theObj.returnedXml)
		{
			return;
		}
		var doc = new ActiveXObject("MSXML2.DOMDocument");
		try
		{
			doc.loadXML(theObj.returnedXml);
		}
		catch(e)
		{
			theObj.errors = new Array();
			theObj.errors[0] = "[提示] 服务器忙，请稍后再试。";
			return;
		}
		var root = doc.selectSingleNode("ajaxDocument/results");

		var nodes = root.childNodes;

		var i = 0;
		for(;i<nodes.length;i++)
		{
			var node = nodes.item(i);
			var name = node.getAttributeNode("key").value;

			var value = decodeURI(AJAX_Parse_Result(node));
			value = HTML2Decode(value);

			theObj.resultNames[ theObj.resultNames.length ] = name.toLowerCase();
			theObj.resultValues[ theObj.resultValues.length ] = value;
		}
	
		var errorRoot = doc.selectSingleNode("ajaxDocument/errors");
		if (errorRoot != null && errorRoot.childNodes.length > 0)
		{
			theObj.errors = new Array();
			i=0;
			for(;i<errorRoot.childNodes.length;i++) {
				var value = errorRoot.childNodes.item(i).getAttributeNode("value").value;
				theObj.errors[i] = value;
			}
		}
	}
}