/**
 * 弹出窗口
 * 属性：
 *
 * 说明：
 * @author  zhaosh
 */

function PopWindowSelectObj(editerObj){
	//定义输入对象
	this.edtObj = editerObj;
	//私有对象
	var ParObj=null;
	var BasObj=null;
}

/**
* 动态构建一个表单，以Post的方式请求url，inputName是表单中的输入项，inputValue是输入项对应的value
* 此方法用于：inputValue非常大，用Get方式请求url会超出长度范围的情况
*/
function dynamicPopupForm(url,inputName,inputValue){
		var form = document.getElementById("tempDynamicPopupForm");
		if(form != undefined){
			//alert("tempDynamicPopupForm exist...");
			form.setAttribute("action",url);
			form.innerHTML = "<input type='hidden' name='" + inputName + "' value='" + inputValue + "'>";
			form.submit();
			return;
		}else{
			form = document.createElement("<form name='tempDynamicPopupForm' id='tempDynamicPopupForm' method='post' target='_blank'></form>");
			form.setAttribute("action",url);
			form.innerHTML = "<input type='hidden' name='" + inputName + "' value='" + inputValue + "'>";
			document.body.appendChild(form);
			form.submit();
		}
}


//公共方法
PopWindowSelectObj.prototype.onvalidate=function () {
	//调用BaseObject中的公用函数检查数据合法性
	if(!this.getBaseObj().commonCheck())	return false;

	if(!this.checkvalidate()){
		alert("["+this.edtObj.prompt + "]输入不合法！");
		return false;
	}

	return true;
}
PopWindowSelectObj.prototype.checkvalidate=function () {
	return true;
}
PopWindowSelectObj.prototype.getParentObj=function (){
	if(this.BasObj==null){
		this.BasObj = new BaseObj(this.edtObj);
	}
	return this.BasObj;
}
PopWindowSelectObj.prototype.getBaseObj=function (){
	if(this.BasObj==null){
		this.BasObj = new BaseObj(this.edtObj);
	}
	return this.BasObj;
}

//行为方法
PopWindowSelectObj.prototype.onReady=function (){
	//调用ParentObj 的初始化方法
	this.getParentObj().onReady();
};
PopWindowSelectObj.prototype.eventBand=function (){

}

function makeMultiSelectParams(obj){
	if(obj == undefined) return;
	if(obj.tagName.toLowerCase() == "select"){
		var params = "";
		var showName = "";
		var saveName = "";

		var writeFields = obj.writeFldNames.split(",");
		var returnFields = obj.returnFldNames.split(",");

		//设定显示名字和保存名字
		for(var i = 0;i < writeFields.length;i++){
			if(writeFields[i].indexOf("[show]") > 0)
				showName = returnFields[i];

			if(writeFields[i].indexOf("[save]") > 0)
				saveName = returnFields[i];
		}

		for(var i = 0;i < obj.options.length;i++){
			params = params + "///" + showName + "^" + obj.options[i].innerText + "$$" + saveName + "^" + obj.options[i].value;
		}
		//alert(params);
		if(params != ""){
			//params = encode64(params);
		}

		return params;
	}
}

//该对象所独具的方法

/**
 * 打开要选择数据的页面
 * @param popSelectFldObj 要打开选择页面的域。
 */
PopWindowSelectObj.openSelectWindow=function (popSelectFldObj,winWidth,winHeight){
	if(window.popSelectFldInfo!=undefined && window.popSelectFldInfo!=null
			&& window.popSelectFldInfo.popWindow != undefined 
			&& window.popSelectFldInfo.popWindow.closed != undefined 
			&& !window.popSelectFldInfo.popWindow.closed){
		var flag=confirm("你已打开一个选择数据的窗口，是否要关闭以前的窗口?");
		if(flag){
			window.popSelectFldInfo.popWindow.close();
		}else{
			window.popSelectFldInfo.popWindow.focus();
			return false;
		}
	}
	if(popSelectFldObj==undefined || popSelectFldObj==null){
		alert("调用PopWindowSelectObj类的openSelectWindow的popSelectFldObj参数不能为空。");
	}

	var openPageService=popSelectFldObj.openPageServiceID;
	var openUrl="./controller?SERVICE_ID="+openPageService;
	openUrl+="&newsearch=true&recordperpage=10&fromMainWindow=true";

	var needAppended =  makeMultiSelectParams(popSelectFldObj);
	if(needAppended != null && needAppended != undefined){
		//openUrl = openUrl + "&pageDivideAlreadySelected=" + needAppended;
		window.pageDivideAlreadySelected = needAppended;
	}
	var needSaveParams="";
	var condition=undefined;
	if(popSelectFldObj.callBackFun!=undefined && popSelectFldObj.callBackFun.trim()!=""){
		try{
			condition=eval(popSelectFldObj.callBackFun+"()");
			needSaveParams="&popPresetCondition="+condition;
		}catch(ex){
			alert("可能没有写取查询条件的JS函数。名为："+popSelectFldObj.callBackFun);
			throw "调用回调函数取查询条件时出错。";
		}
	}
	openUrl+=needSaveParams;

	//basic data
	if(popSelectFldObj.baseDataSearch != null && popSelectFldObj.baseDataSearch == 1){
		if(popSelectFldObj.searchFldNames == null || popSelectFldObj.returnFldNames == null){
			alert("基础数据查询条件不够，请仔细检查searchFldNames和returnFldNames属性。");	
			return;
		}
		//openUrl+="&searchFldNames=" + popSelectFldObj.searchFldNames + "&returnFldNames=" + popSelectFldObj.returnFldNames;
	}
	if(popSelectFldObj.searchFldNames != null && popSelectFldObj.searchFldNames != undefined){
		openUrl+="&searchFldNames=" + popSelectFldObj.searchFldNames;
	}
	if(popSelectFldObj.returnFldNames != null && popSelectFldObj.returnFldNames != undefined){
		openUrl+="&returnFldNames=" + popSelectFldObj.returnFldNames;
	}
	
	var strBaseDataParamName=popSelectFldObj.returnFldNames;
	if(strBaseDataParamName!=null && strBaseDataParamName!=""){
		var baseDataParamName=strBaseDataParamName.split(",");
                var strBaseDataParam="";
		for(var i=0; i<baseDataParamName.length; i++){
			strBaseDataParam+="&returnAttrName="+baseDataParamName[i];
		}
		openUrl+=strBaseDataParam;
	}
	
	var myPopWin = null;
	if(arguments.length == 3){//传递打开窗口的大小
		myPopWin=openWindow(openUrl,winWidth,winHeight);
	}else{
		myPopWin=openWindow(openUrl,800,400);
	}
	var popWindow=new PopSelectWindowManager();
	popWindow.popWindow=myPopWin;
	popWindow.popFieldObj=popSelectFldObj;
	popWindow.searchCondition=condition;

	window.popSelectFldInfo=popWindow;
}

/**
 * 把返回值设到相应的域中。
 *	@param fldObj 域对象
 * @param returnData 用户选择后返回的值
 * 注：该域必须要有returnFldNames和writeFldNames，返回的数据的列名和要写到页面上的域的名。
 */
PopWindowSelectObj.setFieldValue=function (returnData){
	var fldObj=returnData.ownerField;

	if(fldObj.returnFldNames==undefined || fldObj.writeFldNames==undefined
		|| fldObj.returnFldNames=="" || fldObj.writeFldNames==""){
		return;
	}
	var colNames=fldObj.returnFldNames.split(",");
	var fldNames=fldObj.writeFldNames.split(",");
	if(colNames.length!=fldNames.length){
		throw "域"+fldObj.name+"returnFldNames属性和writeFldNames属性的值没有一一对应。"
	}

	for(var i=0; i<fldNames.length; i++){
		var fieldName = fldNames[i];

		if(fldNames[i].indexOf("[show]") > 0){
			fieldName = fieldName.replace("[show]","");
		}
		if(fldNames[i].indexOf("[save]") > 0){
			fieldName = fieldName.replace("[save]","");
		}
		var fldObjTmp=eval("fldObj.form."+fieldName);
		if(fldObjTmp==undefined){
			throw "表单中不存在名为："+fieldName+"的域对象。";
		}

		if(fldObjTmp.tagName.toLowerCase() == "select" && fldObjTmp.type != "select-one"){//当是列表时

			for(var j = fldObjTmp.options.length - 1;j >= 0;j--){//删除原列表中的options
				fldObjTmp.options.remove(j);
			}
			for(var k = 0;k < returnData.getSelectedCount();k++){//增加新的options
				var oOption = top.opener.document.createElement("option");
				fldObjTmp.options.add(oOption);
				for(var w = 0;w < fldNames.length;w++){//
					if(fldNames[w].indexOf("[show]") > 0){
						var thisValue = returnData.getNthRecordColValue(k,colNames[w]);
						oOption.innerText = thisValue;
					}
					if(fldNames[w].indexOf("[save]") > 0){
						var thisValue = returnData.getNthRecordColValue(k,colNames[w]);
						oOption.value = thisValue;
					}
				}
			}
			for(var k = 0;k < fldObjTmp.options.length;k++){
				fldObjTmp.options[k].selected = true;
			}

		     var fldObjShow = eval("fldObj.form."+fieldName + "Show");
		     if(fldObjShow!=undefined){//将匹配的MultiSelect同步更新
			for(var j = fldObjShow.options.length - 1;j >= 0;j--){//删除原列表中的options
				fldObjShow.options.remove(j);
			}
			for(var k = 0;k < returnData.getSelectedCount();k++){//增加新的options
				var oOption = top.opener.document.createElement("option");
				fldObjShow.options.add(oOption);
				for(var w = 0;w < fldNames.length;w++){//
					if(fldNames[w].indexOf("[save]") > 0){
						var thisValue = returnData.getNthRecordColValue(k,colNames[w]);
						oOption.innerText = thisValue;
					}
					if(fldNames[w].indexOf("[show]") > 0){
						var thisValue = returnData.getNthRecordColValue(k,colNames[w]);
						oOption.value = thisValue;
					}
				}
			}
			for(var k = 0;k < fldObjShow.options.length;k++){
				fldObjShow.options[k].selected = true;
			}

		     }
		     break;

		}else{

			var fldValue=returnData.getFirstColValue(colNames[i]);
			setFieldObjValue(fldObjTmp,fldValue,",");		//该方法看CommonOperator.js文件。
		}
	}
}

PopWindowSelectObj.callUserFunction=function(fldObj){
	if(fldObj.runButtonName==undefined || fldObj.runButtonName==""){
		return;
	}
	var buttonObj=eval("fldObj.form."+fldObj.runButtonName);
	if(buttonObj==undefined || buttonObj==null){
		throw "表单中不存在名为："+fldObj.runButtonName+"的按钮对象。";
	}

	buttonObj.click();
}

/**
 * 用户选择完数据后，点击确认按钮执行的方法。
 */
PopWindowSelectObj.selectOk=function(selectBoxName){
	if(!window.opener){
		alert("父窗口已被关闭。");
		window.close();
		return false;
	}
	if(window.opener.popSelectFldInfo==undefined || window.opener.popSelectFldInfo==null){
		alert("父窗口可能被刷新了，请重新打开。");
		window.close();
		return false;
	}
	var myForm=document.forms[0];
	var returnData=SelectPageReturnData.initWithStatus(selectBoxName,myForm);
	//var returnData=SelectPageReturnData.init(selectBoxName,myForm);
	if(returnData==false){
		alert("请选择数据。");
		return false;
	}
	//if(returnData=="overSize"){
	//	alert("已选项过多,系统当前不支持,请修改。");
	//	return false;
	//}

	try{
		window.opener.popSelectedData=returnData;
		PopWindowSelectObj.setFieldValue(returnData);
		PopWindowSelectObj.callUserFunction(returnData.ownerField);
		PopSelectWindowManager.cleanOpenerWindowAttribute();
	}catch(ex){
		alert(ex);
		throw ex;
	}

	window.close();
}


/**
 * 选择域弹出窗口的管理
 */

function PopSelectWindowManager(){
	this.popWindow=null;
	this.popFieldObj=null;
	this.searchCondition=null;
}

/**
 * 关闭当前页面打开的选择数据页面窗口
 */
PopSelectWindowManager.closePopWindow = function (){
	if(window.popSelectFldInfo==undefined){
		return false;
	}

	//关闭弹出窗口
	var myWin=window.popSelectFldInfo.popWindow;
	myWin.close();
}

/**
 * 清空当前页与弹出页面相关属性。
 */
PopSelectWindowManager.cleanOpenerWindowAttribute = function(){
//把父窗口的属性设空
	if(top.opener.popSelectFldInfo!=undefined && top.opener.popSelectFldInfo!=null){
		top.opener.popSelectFldInfo=undefined;
	}
}


PopWindowSelectObj.prototype.beforeSubmit = function(){
	this.getParentObj().beforeSubmit();
}
