/*
名称：ServerCallProxy
用途：调用服务器端信息的公用对象
*/


//构造函数
//参数：
//classID  该系统中存储过程调用对应的类的ID号
//methodID 该系统中存储过程调用对应的类的方法的ID号
function ServerCallProxy(classID , methodID){
	 this.XMLDoc = null;
	 this.parameter="";
	 this.errorFlag=true;
	 this.errorMessage="";
	 this.ESF_resultSet        = new ActiveXObject("Scripting.Dictionary");
	 this.ESF_resultSetForName = new ActiveXObject("Scripting.Dictionary");
	 this.ESF_function         = new ActiveXObject("Scripting.Dictionary");

	 this.flag ="0";
	 this.message ="";
	 this.URL= "ClientAction.ca";
	 this.SYS_FUN_ID = "SYS_FUN_ID";
	 this.SYS_CLASS_ID = "SYS_CLASS_ID";
	 this.SYS_METHOD_ID= "SYS_METHOD_ID";

	 this.SYS_CLASS_ID_VALUE = classID;
	 this.SYS_METHOD_ID_VALUE = methodID;

}
/******************************  方法声明区   *************************************/
//清空环境信息
ServerCallProxy.prototype.resetContext = resetContext;
//提交
ServerCallProxy.prototype.submitForm   = submitForm;

//创建功能数据缓存
ServerCallProxy.prototype.createESF_function = createESF_function;

//创建结果集数据缓存
ServerCallProxy.prototype.createESF_resultSet= createESF_resultSet;
//获得标志
ServerCallProxy.prototype.getFlag = getFlag;
//取返回信息
ServerCallProxy.prototype.getMessage = getMessage;
//取行数
ServerCallProxy.prototype.getRowCount= getRowCount;
//取功能数据
ServerCallProxy.prototype.getFunctionValue = getFunctionValue;
//取结果集数据
ServerCallProxy.prototype.getResultValue          = getResultValue;

//取结果集数据
ServerCallProxy.prototype.getResultValueForName   = getResultValueForName;

//取结果集列名称
ServerCallProxy.prototype.getResultName = getResultName;
//创建参数
ServerCallProxy.prototype.createParameter = createParameter;
//设置错误信息
ServerCallProxy.prototype.setErrorMsg = setErrorMsg;

/******************************  方法实现区   *************************************/
function resetContext()
{
/*
 *--------------- resetContext() -----------------
 * reset()
 * 功能:重置系统信息到默认值.
 * 参数:url,字符串,发送目的.
 * author:聂蛟
 * update:2004-10-06 14:46
 *--------------- resetContext() -----------------
 */
 this.XMLDoc = null;
 this.parameter="";
 this.errorFlag=true;
 this.errorMessage="";
 this.ESF_resultSet = new ActiveXObject("Scripting.Dictionary");
 this.ESF_function = new ActiveXObject("Scripting.Dictionary");

 this.flag ="0";
 this.message ="";
}

function submitForm(functionName)
{
/*
 *--------------- submitForm(url) -----------------
 * submitForm(url) 
 * 功能:通过XMLHTTP执行服务器端方法.
 * 参数:url,字符串,发送目的.
 * author:聂蛟
 * update:2004-10-06 14:46
 *--------------- submitForm(url) -----------------
 */
	this.createParameter(this.SYS_FUN_ID , functionName);
	this.createParameter(this.SYS_CLASS_ID ,  this.SYS_CLASS_ID_VALUE);
	this.createParameter(this.SYS_METHOD_ID , this.SYS_METHOD_ID_VALUE);

	var oXMLDoc = new ActiveXObject("MSXML.DOMDocument");
	oXMLDoc.async=false;
	var returnValue = oXMLDoc.load(this.URL+this.parameter);
	this.XMLDoc = oXMLDoc;
	this.parameter="";
	
	if(returnValue==false)
	{
		alert("加载Servlet异常，请和系统管理员联系!");
		this.setErrorMsg("加载Servlet异常，请和系统管理员联系!");
		return;
	}
	//取执行标志
	this.flag    = oXMLDoc.selectSingleNode("/document/head/flag").text;
	this.message = oXMLDoc.selectSingleNode("/document/head/message").text;
	
	//构造结果集返回值
	this.ESF_resultSet = new ActiveXObject("Scripting.Dictionary");
	this.createESF_resultSet();
	//构造函数返回值
	this.ESF_function = new ActiveXObject("Scripting.Dictionary");
	this.createESF_function();

}

function createESF_function(){
	if(this.XMLDoc==null)
	{
		setErrorMsg("XMLDoc IS NULL!");
		return;
	}
	var nodeList = this.XMLDoc.selectNodes("/document/datas/function/item");
	for(i=0 ; i<nodeList.length ; i++){
		var item = this.XMLDoc.selectSingleNode("/document/datas/function/item["+i+"]");
		var attributes = item.attributes;
		var ESF_key = attributes[0].text.toLowerCase();
		var ESF_element = item.text;
		this.ESF_function.add(ESF_key , ESF_element);
	}
}

function createESF_resultSet(){
	/**
	* <p>用途：构造结果集缓存</p>
	*/
	var rowList = this.XMLDoc.selectNodes("/document/datas/result/row");
	//找指定行
	for(i=0 ; i<rowList.length ; i++){
		var row = this.XMLDoc.selectSingleNode("/document/datas/result/row["+i+"]");
		var attributes = row.attributes;
		var rowNum   = attributes[0].text;

		var tmpArray = new Array();
		//找指定列
		var colList = row.childNodes;

		var rowItem = new ActiveXObject("Scripting.Dictionary");

		for(j=0 ; j<colList.length ; j++)
		{
			

			var col = row.childNodes[j];
			var colAttrs = col.attributes;
			var colNum   = colAttrs[0].text;
			var colName  = colAttrs[1].text;

			rowItem.add(colName.toUpperCase() , colNum);
			tmpArray[colNum*1] = col.text;
		}

		this.ESF_resultSetForName.add(rowNum*1 , rowItem);
		this.ESF_resultSet.add(rowNum*1 , tmpArray);
	}
}


function getFlag()
{
	return this.flag;
}

function getMessage()
{
	return this.message;
}

function getRowCount()
{
	var row = this.XMLDoc.selectSingleNode("/document/datas/result");
	var count;
	if(row!=null)
		count = row.attributes[0].text;
	else
		count = 0;
	return count;
}

function getFunctionValue(valueName)
{
/*
 *--------------- getFunctionValue(valueName) -----------------
 * getFunctionValue(valueName)
 * 功能:分解执行服务器端方法返回的XML信息.
 * 参数:valueName  String  要取的值的名称.
 * 返回信息:
 * 成功:返回指定值
 * 失败:返回NULL
 * author:聂蛟
 * update:2004-10-06 14:46
 *--------------- getFunctionValue(valueName) -----------------
 */
	if(this.XMLDoc==null)
	{
		this.setErrorMsg("XMLDoc IS NULL!");
		return null;
	}
	/*
	if(flag!="0")
	{
		setErrorMsg(message);
		return null;
	}
	*/
	
	var refValue = this.ESF_function.item(valueName.toLowerCase());
	
	if(refValue == null)
		refValue = "";
	
	return refValue;
}

function getResultName(rowNum , colNum){
/**
* <p>用途：根据行号和列号取该列名称</p>
*/
	if(rowNum < 1){
		return null;
	}
	if(colNum < 1){
		return null;
	}
	var rowList = this.XMLDoc.selectNodes("/document/datas/result/row");
	//找指定行
	for(i=0 ; i<rowList.length ; i++){
		var row = this.XMLDoc.selectSingleNode("/document/datas/result/row["+i+"]");
		var attributes = row.attributes;
		if(attributes[0].text==rowNum)
		{
			//找指定列
			var colList = row.childNodes;
			for(j=0 ; j<colList.length ; j++)
			{
				var col = row.childNodes[j];
				var colAttrs = col.attributes;
				if(colAttrs[0].text==colNum)
				{
					return colAttrs[1].text;
				}
			}
		}
	}
	this.setErrorMsg("未找到指定数据!");
	return null;
}

/*
 *--------------- getResultValueForName(rowNum , colName) -----------------
 * getResultValue(rowNum , colNum) 
 * 功能:获得结果集中的数据
 * 参数:
 * rowNum   Number  行号
 * colName  String  列名称
 * 返回信息:
 * 成功:返回指定值
 * 失败:返回NULL
 * author:聂蛟
 * update:2005-10-13
 *--------------- getResultValueForName(rowNum , colNum) -----------------
 */
function getResultValueForName(rowNum , colName){
	colName = colName.toUpperCase();
	//取指定列名称的列号
	var rowItem = this.ESF_resultSetForName.item(rowNum*1);
	if(rowItem == null) return "";
	var columnNum = rowItem.item(colName);
	if(columnNum == null) return "";

	return this.getResultValue(rowNum , columnNum)
}

function getResultValue(rowNum , colNum)
{
/*
 *--------------- getResultValue(rowNum , colNum) -----------------
 * getResultValue(rowNum , colNum) 
 * 功能:分解执行服务器端方法返回的XML信息.
 * 参数:
 * rowNum  Number  行号
 * colNum  Number  列号
 * 返回信息:
 * 成功:返回指定值
 * 失败:返回NULL
 * author:聂蛟
 * update:2004-10-06 14:46
 *--------------- getResultValue(rowNum , colNum) -----------------
 */
	if(rowNum < 1){
		return "";
	}
	if(colNum < 1){
		return "";
	}
	if(this.ESF_resultSet==null) return "";
	var arrayTme = this.ESF_resultSet.item(rowNum*1);
	if(arrayTme==null) return "";
	
	var refValue = arrayTme[colNum*1];
	if(refValue == null)
		refValue = "";
	
	return refValue;
}


function createParameter(name , value)
{
/*
 *--------------- createParameter(name , value) -----------------
 * createParameter(name , value) 
 * 功能:分解执行服务器端方法返回的XML信息.
 * 参数:
 * name  String  要增加的值的名称.
 * value  String 要增加的值.
 * author:聂蛟
 * update:2004-10-06 14:46
 *--------------- createParameter(name , value) -----------------
 */
    if(value!=null) 
    	value=value+"";
    else
    	value="";
 	if(value!=null && value!=""){
		value = value.replace(/%/g,"%25");
	 	value = value.replace(/#/g,"＃");
		value = value.replace(/&/g,"%26");
		value = value.replace(/\+/g,"%2B");
		value = value.replace(/\\/g,"%2F");
		value = value.replace(/\=/g,"%3D");
		value = value.replace(/\?/g,"%3F");
		value = value.replace(/\"/g,"%22");
		value = value.replace(/\'/g,"%27");
		value = value.replace(/</g,"%3C");
		value = value.replace(/>/g,"%3E");
	}
	if(this.parameter=="")
	{
		this.parameter="?"+name+"="+value;
	}else
	{
		this.parameter=this.parameter+"&"+name+"="+value;
	}
}

function setErrorMsg(message)
{
	this.errorFlag=false;
	this.errorMessage=message;
}
