<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="net.sz600.tool.SzConvert" %>
<%@ page import="net.sz600.formEngine.*" %>
<%@ page import="net.sz600.formEngine2.*" %>
<%@ page import="java.util.*" %>
<%@ include file="sessionInfo2.jsp"%>
<html>
<body>
<%
    String sType = SzConvert.toZeroStr(request.getParameter("type"));
    String sTitle = "";
    if (sType.equals("radio")) {
    	sTitle = "表单引擎单项选择设置窗体";
    } else if (sType.equals("check")) {
    	sTitle = "表单引擎多项选择设置窗体";
    } else if (sType.equals("select")) {
    	sTitle = "表单引擎下拉列表设置窗体";
    }
%>
<head>
<title><%=sTitle %></title>
<base target="_self">
<link href="<%=strSkin_SESSION%>/css2007.css" rel="stylesheet" type="text/css">
</head>



<form name="formUpdate" method="post" action="formSave.jsp">
  <input type="hidden" name="type" value="<%=sType%>">
<table width="245" border="0" align="center" id="table1">
  <tr>
  	<td><b><%=sTitle%></b></td>
  </tr>
  <tr>
    <td colspan="2">
      选项value:<input type="text" name="txtValue" value="" class="txtClass1" style="width:130;">
     <input type="button" value="同下"  class="btnClassShort" OnClick="copyValue(formUpdate.txtValue, formUpdate.txtText);">
    </td>
  </tr>
  <tr>
    <td colspan="2">
      选项text&nbsp;:<input type="text" name="txtText" value=""  class="txtClass1"  style="width:130;">
     <input type="button" value="同上"  class="btnClassShort" OnClick="copyValue(formUpdate.txtText, formUpdate.txtValue);">
    </td>
  </tr> 
  <tr>
    <td width="180">
      <select name="selectOption" size="15" multiple style="width:200;" OnChange="Select();"></select>
    </td>
    <td align="left">
      <input type="button" value="添加"  class="btnClassShort" OnClick="Add();"><br>
      <input type="button" value="删除" class="btnClassShort" OnClick="Delete();"><br>     
      <input type="button" value="上移" class="btnClassShort" OnClick="Up();"><br>
      <input type="button" value="下移" class="btnClassShort" OnClick="Down();">      
    </td>  
  </tr>
  <tr>
    <td colspan="2" align="center">
      <input type="button" value="基础选项" class="btnClass" OnClick="DisplayTable(2);">
      <input type="button" value="确定" class="btnClass" OnClick="Save();">
      <input type="button" value="关闭" class="btnClass" OnClick="window.close();">
    </td>  
  </tr>    
</table>
<table width="100%" border="1" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableClass" cellpadding="1" cellspacing="0"  style="display:none;" id="table2">
  <tr>
  	<td colspan="4"><b><%=sTitle%></b></td>
  </tr>
<%
	BdOptions bdOptions = new BdOptions();
	int i = 0;
	int iLen = 0;
	ArrayList list = bdOptions.getList();
%>
  <tr class="trClassTitle2">
    <td width="30">&nbsp;</td>  
    <td>名称/说明</td>
    <td>选项(value)/选项(text)</td>  
    <td width="40">操作</td>
  </tr>
<%
	String temp = "";
	String str[]=null;
	iLen = list.size();
	int j = 0;
	
    for (i=0; i<iLen; i++ ) {
    	str = (String[])list.get(i);
    	j++;
%>
  <tr onMouseOver="javascript:this.className='trMouseOver'" onMouseOut="javascript:this.className='trMouseOut'">
  	<td width="15" align="center"><%=j%></td>   
    <td><%=str[0]%><br><%=str[1]%>&nbsp;</td>
    <td><%=str[2]%><br><%=str[3]%>&nbsp;</td>          
    <td width="30">
      <a href="#"  OnClick="Save2('<%=str[0]%>', '<%=str[2]%>', '<%=str[3]%>');">确定</a>
    </td>
  </tr>
<%
    }
%>  
  <tr>
    <td colspan="4" align="center">
      <input type="button" value="手动录入" class="btnClass" OnClick="DisplayTable(1);">
      <input type="button" value="关闭" class="btnClass" OnClick="window.close();">
    </td>  
  </tr>    
</table>

</form>
<script language="javascript" src="../../share/js/commonTool.js"></script>
<script language="javascript">
  function DisplayTable(i) {
    if (i == 2) {
      document.getElementById("table1").style.display = "none";
      document.getElementById("table2").style.display = "";
    } else {
      document.getElementById("table1").style.display = "";
      document.getElementById("table2").style.display = "none";    
    }
  }
  function Add() {
    var vText = formUpdate.txtText.value;
    var vValue = formUpdate.txtValue.value;
    AddOption(vText, vValue);
    formUpdate.txtText.value = "";
    formUpdate.txtValue.value = "";     
  }
  
  function AddOption(vText, vValue) {
    var vLen = formUpdate.selectOption.length;
    formUpdate.selectOption.options[vLen] = new Option(vText,vValue);   
  }
  
  function Delete() {
    var vIndex = formUpdate.selectOption.selectedIndex;
    if (vIndex >=0 ) {
    	formUpdate.selectOption.options[vIndex] = null;
    	formUpdate.txtText.value = "";
    	formUpdate.txtValue.value = "";     	
    }
  }
  
  function Select() {
    var vIndex = formUpdate.selectOption.selectedIndex;
    if (vIndex >=0 ) {
		formUpdate.txtText.value = formUpdate.selectOption.options[vIndex].text;
    	formUpdate.txtValue.value = formUpdate.selectOption.options[vIndex].value;
    }  
  }
  
  function Down() {
    var vIndex = formUpdate.selectOption.selectedIndex;
    var vLen = formUpdate.selectOption.length;
    if (vIndex< vLen -1) {
		var vText = formUpdate.selectOption.options[vIndex + 1].text;
    	var vValue = formUpdate.selectOption.options[vIndex  + 1].value;  
    	
    	formUpdate.selectOption.options[vIndex + 1].text 
    	=  formUpdate.selectOption.options[vIndex].text;
    	formUpdate.selectOption.options[vIndex + 1].value 
    	=  formUpdate.selectOption.options[vIndex].value;  
    	
    	formUpdate.selectOption.options[vIndex].text 
    	=  vText;
    	formUpdate.selectOption.options[vIndex].value 
    	=  vValue;  	
    	
    	formUpdate.selectOption.options[vIndex +1].selected = true;    	 
    }  
  }
  
  function Up() {
    var vIndex = formUpdate.selectOption.selectedIndex;
    if (vIndex>0) {
		var vText = formUpdate.selectOption.options[vIndex].text;
    	var vValue = formUpdate.selectOption.options[vIndex].value;  
    	
    	formUpdate.selectOption.options[vIndex].text 
    	=  formUpdate.selectOption.options[vIndex -1].text;
    	formUpdate.selectOption.options[vIndex].value 
    	=  formUpdate.selectOption.options[vIndex -1].value;  
    	
    	formUpdate.selectOption.options[vIndex -1].text 
    	=  vText;
    	formUpdate.selectOption.options[vIndex -1].value 
    	=  vValue;  	
    	
    	formUpdate.selectOption.options[vIndex -1].selected = true;    	 
    }   
  }
  
  function Save() {
    //var oWindow = window.dialogArguments;
		var oWindow = window.opener;    
    if (oWindow == null) {
      return;
    }
    var vLen = formUpdate.selectOption.length; 
	var vText = "";
    var vValue = "";   
    var vHtml = "";
    var type = formUpdate.type.value;
    var id = "0";
    if (type == "radio") {
      id = oWindow.getNewObjectCount("31");
    } else if (type == "check") {
      id = oWindow.getNewObjectCount("32");
    } else if (type == "select") {
      id = oWindow.getNewObjectCount("133");
    }
      
    if (type == "select") {
	  vHtml += "<select name='" + id + "' id='" + id + "' class='txtClass1'>";
    }
    for (var i=0; i<vLen; i++) {
	  vText = formUpdate.selectOption.options[i].text;
      vValue = formUpdate.selectOption.options[i].value; 
      if (type == "radio") {
        vHtml += "<input type='radio' ";
        vHtml += " name='" + id + "' id='" + id + "' value='" + vValue + "' text='" + vText + "'>" + vText; 
      } else if (type == "check") {
        vHtml += "<input type='checkbox' ";
        vHtml += " name='" + id + "' id='" + id + "' value='" + vValue + "' text='" + vText + "'>" + vText; 
      } else if (type == "select") {
		vHtml += "<option value='" + vValue + "'>";
		vHtml += vText;
		vHtml += "</option>";
      }      
    }
    
    if (type == "select") {
	  vHtml += "</select>";
    }  
        
    oWindow.SetHtml(vHtml);
    window.close();
  }
  
  function Save2(name, optionsValue, optionText) {
    //var oWindow = window.dialogArguments;
		var oWindow = window.opener;
    if (oWindow == null) {
      return;
    }
    var str_value = optionsValue.split(",");
    var str_text = optionText.split(",");
    
    
    var vLen = str_value.length;
     
		var vText = "";
    var vValue = "";   
    var vHtml = "";
    var type = formUpdate.type.value;
    var id = "0";
    if (type == "radio") {
      id = oWindow.getNewObjectCount("31");
    } else if (type == "check") {
      id = oWindow.getNewObjectCount("32");
    } else if (type == "select") {
      id = oWindow.getNewObjectCount("133");
    }
      
    if (type == "select") {
	  vHtml += "<select name='" + id + "' id='" + id + "' class='txtClass1'>";
    }
    for (var i=0; i<vLen; i++) {
	  vValue = str_value[i];
      vText = str_text[i];
      if (vText == "" && vValue == "" || vValue == "undefined") {
        continue;
      }
       
      if (type == "radio") {
        vHtml += "<input type='radio' ";
        vHtml += " name='" + id + "' id='" + id + "' value='" + vValue + "' text='" + vText + "'>" + vText; 
      } else if (type == "check") {
        vHtml += "<input type='checkbox' ";
        vHtml += " name='" + id + "' id='" + id + "' value='" + vValue + "' text='" + vText + "'>" + vText; 
      } else if (type == "select") {
		vHtml += "<option value='" + vValue + "'>";
		vHtml += vText;
		vHtml += "</option>";
      }      
    }
    
    if (type == "select") {
	  vHtml += "</select>";
    }  
        
    oWindow.SetHtml(vHtml);
    window.close();
  }
   
  
  //2008-09-07新增加，修改的时候直接把选项带过去。	
  //var oWindow = window.dialogArguments;
  var oWindow = window.opener;
  if (oWindow != null) {
    //获取已经选中的html
    var oCell   = oWindow.getCellObject();
    var vObject = null;
    var vText   = "";
    var vValue  = "";
    if (oCell != null) {
      var iLenObject = oCell.childNodes.length;
      for (var m = 0; m<iLenObject; m++ ) {
        vObject = oCell.childNodes[m];
        if (vObject != null && vObject.id != null && vObject.id.indexOf("XX") != -1) {
		  if (vObject.type == "radio" || vObject.type == "checkbox" || vObject.type == "select") {
			//加到option中			
			vText  = vObject.text;
			vValue = vObject.value;
			AddOption(vText, vValue);
		  }
        } 
      }//for m      
    }
  }
</script>
</body>
</html>
