LISTINIT = 0

//当列表被点击时候获取对应的值
function listLoad(voID,pageStyle) {

	if(pageStyle != "NEW" && pageStyle != 'VIEW' && pageStyle != 'EDIT') {
		alert("页面功能类型设置不正确，必须为：{'NEW'||'VIEW'||'EDIT'}");	
		return;
	}
	//获取配置对象
	var items = parent.getVOITEMS(voID);
	if(items == null) {
		alert("SYSTEMERROR: 没有配置对象！");
		return null;
	}
	
	//根据配置情况，设置相应字段的显示形式，以及附属属性

	setFieldPropertys(pageStyle,voID);
	
	//如果是新增功能类型，在此返回
	if(pageStyle == "NEW") {
		return;
	}
	
	//-------------------------------------------------------------------------------------------------/
	var curBean = _getCurListBean(voID);
	if(curBean==null) return;
	var attrs = curBean.attrs;
	setFieldValues(attrs,voID);				
}


//列表工作区初始化
function listInit(voID,pageStyle) {
	if (LISTINIT == 0) {
		if(pageStyle != "NEW" && pageStyle != 'VIEW' && pageStyle != 'EDIT') {
			alert("页面功能类型设置不正确，必须为：{'NEW'||'VIEW'||'EDIT'}");	
			return;
		}

		//获取配置对象
		var items = parent.getVOITEMS(voID);
		if(items == null) {
			alert("SYSTEMERROR: 没有配置对象！");
			return null;
		}
		
		//根据配置情况，设置相应字段的显示形式，以及附属属性
		setFieldPropertys(voID,pageStyle);
		LISTINIT = 1;//设置为已经初始化
	}
	//-------------------------------------------------------------------------------------------------/
	
}

/**
 * 获取当前焦点Bean.
 */
function _getCurListBean(listID) {
	var LISTWS = parent.getLISTWS(listID);
	//获取值对象
	var beans = LISTWS.BEANS;
	//获取当前值对象对应的KEY
	var key = LISTWS.CURRENTKEY
	
	var curBean = null;
	if(beans != null && key != null) {
		for(var i = 0; i < beans.length; i++) {
			curBean = beans[i];
			if(curBean != null && curBean.key == key) {
				return curBean;
			}
		}
	}
	return null;

}

function setFieldValues(attrs,voID){
	
	//当前页面的配置对象
	var items = parent.getVOITEMS(voID);
	if(attrs.length==0) return;
	
	for(var i = 0; i < attrs.length; i++) {
		//判断是否存在重复字段
		
		if(eval("document.getElementsByName('"+attrs[i][0]+"').length") > 1) {
			alert("页面中有多个重复字段["+attrs[i][0]+"]");
			break;
		}
		//获取tag
		var _curTag = eval("document.getElementById('"+attrs[i][0]+"')");
		
		if(_curTag == null) continue;
		
      			

				
		//如果是下拉列表标签，根据参数optionStr，赋予选择项		
		//var _itemObj = _getItemObj(items, parent._LISTWS._curEditBean.valueArray[i][0]);

		if(_curTag.tagName == "SELECT") {
		   setTimeout("_setTagValue('"+attrs[i][0]+"', '"+attrs[i][1]+"')", 0);		   
	       eval("document.getElementById('"+attrs[i][0]+"')").value = attrs[i][1];
		}
		else{
		//只要有值那么就设置控件中
		
		_curTag.value = attrs[i][1];
		}
	}
}


//-----------------------------------------------------//
// 根据配置情况，设置相应字段的显示形式，以及附属属性
// 参数：  items   配置对象数组
//				 pageStyle    页面功能类型
//-----------------------------------------------------//
function setFieldPropertys(pageStyle,voID) {

	var items = parent.getVOITEMS(voID);

	if(items==null) return;
	for(var i = 0; i < items.length; i++) {
	
		var curItem = items[i];
		var _curTag  = eval("document.getElementById('"+curItem.name+"')");
			
		if(_curTag==null) continue;
		//如果是下拉列表标签，根据参数optionStr，赋予选择项
		
		if(_curTag.tagName == "SELECT" && curItem != null && curItem.optionStr != null && curItem.optionStr.length > 0) {
			//生成一个"选择"项	
			
			oNewNode = document.createElement("option");
			oNewNode.value = '';
			oNewNode.innerText= "请选择";
			_curTag.appendChild(oNewNode);
			//直接赋予<option/>不成功，需要解析后生成option
			var _optionStr = curItem.optionStr;
			var _curValue  = "";
			var _curText   = "";
			var pos = 0;
			var _selectLength = 0;
			while(_optionStr.indexOf("<option") != -1) {
				pos = _optionStr.indexOf("<option value='");
				_optionStr = _optionStr.substring(15);
				_curValue = _optionStr.substring(0, _optionStr.indexOf("'"));
			pos = _optionStr.indexOf("</option>");
			_curText  = _optionStr.substring(_optionStr.indexOf(">")+1 , pos);
				//生成节点
				oNewNode = document.createElement("option");
				_curTag.appendChild(oNewNode);
				oNewNode.id = curItem.name+_curValue;
				oNewNode.value = _curValue;
				oNewNode.innerText= _curText;
				if(_curText.length*25 > _selectLength) {
					_selectLength = _curText.length*25;
			}
				_optionStr = _optionStr.substring(pos+9);
			}
			_curTag.style.width = _selectLength;
		}
		
		//设置控件宽度
		if(curItem.showwidth != null && curItem.showwidth != "null") {
			_curTag.style.width = parseFloat(curItem.showwidth);
		}
	}
}
//------------------------------------------------------/
// 如果有对应的选择项，根据值来映射名称
// 参数： _curListItem 			配置对象
// 				_curListItemValue 当前值
// 返回： 与当前值匹配的选择项名称	
//------------------------------------------------------/
function _getListItemMapValue(_curListItem, _curListItemValue) {

	//是否存在选择项
	if(_curListItem.optionStr != null && _curListItem.optionStr != "undefined" && _curListItem.optionStr.length > 0) {
		//直接赋予<option/>不成功，需要解析后生成option
		var _optionStr = _curListItem.optionStr;
		var _curValue  = "";
		var _curText   = "";
		var pos = 0;
		var _selectLength = 0;
		while(_optionStr.indexOf("<option") != -1) {
			
			pos = _optionStr.indexOf("<option value='");
			_optionStr = _optionStr.substring(15);
			_curValue = _optionStr.substring(0, _optionStr.indexOf("'"));
			pos = _optionStr.indexOf("</option>");
			_curText  = _optionStr.substring(_optionStr.indexOf(">")+1 , pos);
			
			//如果有匹配的值，返回对应的显示字符
			if(_curValue == _curListItemValue) {
				return _curText;
			}
			
			if(_curText.length*25 > _selectLength) {
				_selectLength = _curText.length*25;
			}
			
			_optionStr = _optionStr.substring(pos+9);
		}
		
	}
	
	//如果没有匹配的值，直接返回该值
	return _curListItemValue;
		
}
//-----------------------------------------------------//
// 获取name对应的item描述对象
// 参数：  _itemArray    配置对象数组
//				 _itemName     对应字段名称
// 返回：  _itemArray[i] 配置对象
//-----------------------------------------------------//
function _getItemObj(_itemArray, _itemName) {
	var _itemObj = null;

	if(_itemArray != null) {

		for(var i = 0; i < _itemArray.length; i++) {
			if(_itemArray[i].name == _itemName) {
				_itemObj = _itemArray[i];
			}
		}
		
	}
	
	return _itemObj;
}

//设置页面标签的值
function _setTagValue(_curTagID, _value) {
	document.getElementById(_curTagID).value = _value;
}


//根据id获得cache中对应的名称，返回的值：value$$name
function _getNameByID(voID,itemName){
	var items = parent.getVOITEMS(voID);
	var itemValue="";
	if(items==null) return;
    //获得当前itemName对应的值
	var curBean = _getCurListBean(voID);
	if(curBean==null) return;
	var attrs = curBean.attrs;
     for (var i = 0; i < attrs.length; i++)
     {
		 if (attrs[i][0] == itemName)
		 {
			 itemValue=attrs[i][1];
		 }
     }
	for(var i = 0; i < items.length; i++) {
    var curItem = items[i];
		if(curItem.name == itemName && curItem != null && curItem.optionStr != null && curItem.optionStr.length > 0) {
		    
			var _optionStr = curItem.optionStr;
			var _curValue  = "";
			var _curText   = "";
			var pos = 0;
			var _selectLength = 0;
			var _retrunValue ="";
			while(_optionStr.indexOf("<option") != -1) {
				pos = _optionStr.indexOf("<option value='");
				_optionStr = _optionStr.substring(15);
				_curValue = _optionStr.substring(0, _optionStr.indexOf("'"));
			    pos = _optionStr.indexOf("</option>");
			   _curText  = _optionStr.substring(_optionStr.indexOf(">")+1 , pos);
				//返回值				
				if(_curValue==itemValue){
				   _retrunValue=_curValue+"$$"+_curText;
				}
				if(_curText.length*25 > _selectLength) {
			     _selectLength = _curText.length*25;
		        }		
		         _optionStr = _optionStr.substring(pos+9);					
			}			
			return _retrunValue;
			break;
		}
	}
}

//根据id获得cache中对应的名称串，返回的值："value$$name;value$$name;value$$name;"
function _getNameByStr(voID,itemName){
	var items = parent.getVOITEMS(voID);
	var itemValue="";
	if(items==null) return;
    //获得当前itemName对应的值
	var curBean = _getCurListBean(voID);
	if(curBean==null) return;
	var attrs = curBean.attrs;
     for (var i = 0; i < attrs.length; i++)
     {
		 if (attrs[i][0] == itemName)
		 {   //取得itemName项对应的值，即多个id的字符串
			 itemValue=attrs[i][1];
		 }
     }    

    var _itemValue=itemValue.split(" ");	
	var _retrunValue ="";
    for(var m=0;m<_itemValue.length;m++){			
		for(var i = 0; i < items.length; i++) {
		var curItem = items[i];
			if(curItem.name == itemName && curItem != null && curItem.optionStr != null && curItem.optionStr.length > 0) {
				
				var _optionStr = curItem.optionStr;
				var _curValue  = "";
				var _curText   = "";
				var pos = 0;
				var _selectLength = 0;				
					while(_optionStr.indexOf("<option") != -1) {
						pos = _optionStr.indexOf("<option value='");
						_optionStr = _optionStr.substring(15);
						_curValue = _optionStr.substring(0, _optionStr.indexOf("'"));
						pos = _optionStr.indexOf("</option>");
					   _curText  = _optionStr.substring(_optionStr.indexOf(">")+1 , pos);
						//返回值						
						if(_curValue==_itemValue[m]){
						   _retrunValue+=_curValue+"$$"+_curText+";";
						}
						if(_curText.length*25 > _selectLength) {
						 _selectLength = _curText.length*25;
						}		
						 _optionStr = _optionStr.substring(pos+9);				
						}						
			}

		}
		
    }

	return _retrunValue;
}
//根据itemName得到value
function getValueByItemName(voID,itemName){
var items = parent.getVOITEMS(voID);
	var itemValue="";
	if(items==null) return;
    //获得当前itemName对应的值
	var curBean = _getCurListBean(voID);
	if(curBean==null) return;
	var attrs = curBean.attrs;
     for (var i = 0; i < attrs.length; i++)
     {
		 if (attrs[i][0] == itemName)
		 {
			 itemValue=attrs[i][1];
		 }
     }
	 return itemValue;
}
