<%@ Page Language="C#" %>

<%@ Register TagPrefix="aspxform" Namespace="XFormDesigner.Framework.Web.UI" Assembly="XFormDesigner.Framework" %>
<script runat="server">

    // Insert page code here
    //

</script>
<html xmlns:xform="xmlns:xform">
<head runat="server">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <link href="/Oimec/Styles/css/StyleSheet.css" rel="stylesheet" type="text/css" />
    <title>BPM Form</title>
    <style>
        BODY {
            FONT-SIZE: 12px;
            FONT-FAMILY: verdana;
        }

        TABLE {
            border-collapse: collapse;
            FONT-SIZE: 12px;
            FONT-FAMILY: verdana;
        }

        P {
            PADDING-RIGHT: 0px;
            PADDING-LEFT: 0px;
            PADDING-BOTTOM: 0px;
            MARGIN: 0px;
            PADDING-TOP: 0px;
            FONT-SIZE: 12px;
            FONT-FAMILY: verdana;
        }

        TD {
            padding: 1px;
        }

            TD.NoPadding {
                PADDING-RIGHT: 0px;
                PADDING-LEFT: 0px;
                PADDING-BOTTOM: 0px;
                PADDING-TOP: 0px;
            }
        /*INPUT {height: 15px}*/
        INPUT.UL {
            BORDER-RIGHT: medium none;
            BORDER-TOP: medium none;
            BORDER-LEFT: medium none;
            BORDER-BOTTOM: #33ff33 1px solid;
        }

        TEXTAREA {
            FONT-SIZE: 12px;
        }

        input {
            vertical-align: middle; /*margin-right:-3px*/
        }
    </style>
    <script>
        Ext.onReady(function () {
            var stepName = agent.Eles[document.getElementById("StepName").getAttribute("xform_xele_index")].getValue();
            var state = request("formstate");
            var tableObj = document.getElementById('tablenew');

            //if (stepName == "填写问卷调查") {
            if (tableObj.rows.length == 0) { // 若数据已保存到数据库则不进行刷新
                //  XFormOnChange(agent.Eles[document.getElementById("XLabel2").getAttribute("xform_xele_index")]);
                XFormOnChange(agent.Eles[document.getElementById("questionnaireId").getAttribute("xform_xele_index")]);
            }
            //}
			
			 //1231改动
            var aa = document.getElementsByTagName("a");

            for (var i = 0; i < aa.length; i++) {
                // aa[i].setAttribute("href", "JavaScript:;");
                var backgroundImage = aa[i].style.backgroundImage;
              //  url(../../../../YZSoft/Forms/Images/hoverbtn_gridr.gif)
                if (backgroundImage == 'url("../../../../YZSoft/Forms/Images/hoverbtn_gridr.gif")') {
                    aa[i].style.display = "none";
                }

            }
        });


        agent.on('beforeSubmit', function (action, actionname) {

            //提交时检查填写信息

            var aDivLineID = document.getElementsByName("LineID");//行号
            var aDivquestionType = document.getElementsByName("questionType");//问题类型
            var aDivanswerOption = document.getElementsByName("answerOption");//问题答案
            var aDivMultiChoiceRestriction = document.getElementsByName("MultiChoiceRestriction");//多选限制

            var aDivanswerText = document.getElementsByName("answerText");//问答题
			 var tableObj = document.getElementById('tablenew');
            for (var i = 0; i < aDivLineID.length; i++) {

                //行号
                var divLineID = aDivLineID[i];
                var vLineID= divLineID.getElementsByTagName('span')[0].innerHTML;
                //问题类型
                var divquestionType = aDivquestionType[i];
                var vquestionType = divquestionType.getElementsByTagName('span')[0].innerHTML;

                //问题答案
                var divanswerOption = aDivanswerOption[i];
                //var vanswerOption = divanswerOption.getElementsByTagName('span')[0].innerHTML;
                 var vanswerOption = agent.Eles[tableObj.rows[i].cells[1].children[4].getAttribute("xform_xele_index")].getValue();
                //多选限制
                var divMultiChoiceRestriction = aDivMultiChoiceRestriction[i];
                var vMultiChoiceRestriction = divMultiChoiceRestriction.getElementsByTagName('span')[0].innerHTML;

                var flag = CheckControl(vLineID, vquestionType, vanswerOption, vMultiChoiceRestriction);

              
               // alert(flag)
                if(flag=="0"){
                    alert("第" + vLineID + "题请选择答案");
                    return false;
                }else if(flag=="1"){
                    alert("第" + vLineID + "题为单选题，只能选择一项");
                      return false;
                } else if (flag == "2") {
                    alert("第" + vLineID + "题为多选题，最多选择" + vMultiChoiceRestriction + "");
                    return false;
                }

                //问答题
                var divanswerText = aDivanswerText[i];
                var idanswerText = divanswerText.getElementsByTagName('textarea')[0].id;
                var vanswerText = document.getElementById(idanswerText).value;
                if (vquestionType == "3")//问答题
                {

                    if (vMultiChoiceRestriction == "必填") {
                        if (vanswerText == "") {
                            alert("第" + vLineID + "题为问答题,请您填写信息");
                            return false;
                        }
                    }
                }

              
            }//结束

            

        });

        //获取选择的答案并检查
        function IsChecked(OptionsID, checkbox, lineNo, questionType, GridLineNo) {

            if (lineNo == 0) return false;
            var stepName = agent.Eles[document.getElementById("StepName").getAttribute("xform_xele_index")].getValue();
            if (stepName == "填写问卷调查") {


                //选择题
                if (questionType == "1" || questionType == "2") {

                    var tableObj = document.getElementById('tablenew');
                    //alert(lineNo);
                    //var row = lineNo + 1;
                    var row = lineNo - 1;
                    if (checkbox == "1") {
                        var Answer = agent.Eles[tableObj.rows[row].cells[1].children[4].getAttribute("xform_xele_index")].getValue();
                        var IsNoAnswer = false;
                        if (Answer == "" || Answer == null || Answer == undefined) IsNoAnswer = true;

                        //单选题保留最后一次选中值
                        if (questionType == "1") {
                            DXControl(lineNo, GridLineNo);
                            Answer = "";
                        }

                        if (Answer == "") {
                            Answer = OptionsID;
                        }
                        else {
                            Answer = Answer + "," + OptionsID;
                        }
						
						 var str = "";
                            if (Answer.length > 0) {
                                var arr = Answer.split(",");
                                var json = {};
                                for (var i = 0; i < arr.length; i++) {
                                    json[arr[i]] = arr[i];
                                }

                                // arr = new Array();
                                var s = "";
                                for (var key in json) {
                                    //  arr.push(key);
                                    if (key != "") {
                                        s += key + ",";
                                    }

                                }
                                str = s.substring(0, s.length - 1);
                                // console.log(str)
                                Answer = str;
                            }
                        agent.Eles[tableObj.rows[row].cells[1].children[4].getAttribute("xform_xele_index")].setValue(Answer);

                    }
                    else {
                        //取消选择则将答案去掉
                        var Answer = agent.Eles[tableObj.rows[row].cells[1].children[4].getAttribute("xform_xele_index")].getValue();
                        Answer = Answer.replace("," + OptionsID, "");
                        Answer = Answer.replace(OptionsID + ",", "");
                        Answer = Answer.replace(OptionsID, "");
						
						 var str = "";
                        if (Answer.length > 0) {
                            var arr = Answer.split(",");
                            var json = {};
                            for (var i = 0; i < arr.length; i++) {
                                json[arr[i]] = arr[i];
                            }

                            // arr = new Array();
                            var s = "";
                            for (var key in json) {
                                //  arr.push(key);
                                if (key != "") {
                                    s += key + ",";
                                }
                                
                            }
                            str = s.substring(0, s.length - 1);
                            // console.log(str)
                            Answer = str;
                        }
                        agent.Eles[tableObj.rows[row].cells[1].children[4].getAttribute("xform_xele_index")].setValue(Answer);
                    }
                }


            }

        }

        //单选题控制
        function DXControl(lineNo, GridLineNo) {
            //获取当前行的table
            var index = lineNo - 1; //第几个问题
            var index1 = GridLineNo - 1;//第几个选项
            var v_table = document.getElementsByName("questiontd")[index].getElementsByTagName('table')[0];
            var v_trs = v_table.getElementsByTagName('tr');

            //设置table中所有选项，未选中
            for (var i = 0; i < v_trs.length; i++) {
                var v_trs_tr = v_table.getElementsByTagName('tr')[i];
                var v_trs_tr_td0_input_id = v_trs_tr.getElementsByTagName('td')[0].getElementsByTagName('input')[0].id;
                document.getElementById(v_trs_tr_td0_input_id).checked = false;
            }
            var v_tr = v_table.getElementsByTagName('tr')[index1];
            var v_td = v_tr.getElementsByTagName('td')[0];//选项选中第几行
            var v_input = v_td.getElementsByTagName('input')[0];
            var v_id = v_input.id;
            //取消其他选择，保留当前选项选中
            document.getElementById(v_id).checked = true;
            //Ext.Msg.alert("提示", "第" + lineNo + "题为单选题，只能选择一项");
            //alert("第" + lineNo + "题为单选题，只能选择一项");

        }

        //判断答案是否填写
        function CheckControl(LineID, questionType, answerOption, MultiChoiceRestriction) {
            //  alert(questionType);
            // console.log(questionType)
            var flag = "";
            if (questionType == "1")//单选
            {
               
                if (answerOption.length == 0) {
                    //alert("第" + LineID + "题请选择答案");
                    //return false;
                    flag = "0";
                }
                else if (answerOption.length > 1) {
                  //  alert("第" + LineID + "题为单选题，只能选择一项");
                    //  return false;
                    flag = "1";
                }
            }

            if (questionType == "2")//多选
            {
                var str="";
                if (answerOption.length>0) {
                  str=  answerOption.split(',');
                }
              
                if (answerOption.length == 0) {
                   // alert("第" + LineID + "题请选择答案");
                    // return false;
                    
                    flag = "0";
                } else if ( MultiChoiceRestriction=="不限") {
                    flag = "不限";
                }
                else if (str.length > MultiChoiceRestriction) {
                  //  alert("第" + LineID + "题为多选题，最多选择" + MultiChoiceRestriction + "");
                    // return false;
                    flag = "2";
                }
            }

          
            return flag;

        }

        //问题类型
        function questionType(questionType) {
            //alert(questionType);
            if (questionType == "1")
                return "【单选题】";
            else if (questionType == "2")
                return "【多选题】";
            else if (questionType == "3")
                return "【问答题】";
        }

        function request(paras) {
            var url = location.href;
            var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
            var paraObj = {};
            for (i = 0; j = paraString[i]; i++) {
                paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
            }
            var returnValue = paraObj[paras.toLowerCase()];
            if (typeof (returnValue) == "undefined")
            { return ""; }
            else
            { return returnValue; }
        }

    </script>
</head>
<body>
    <form runat="server">
        <!-- Insert content here -->
        <table width="1360" align="center" style="background: white" border="0" cellspacing="0" cellpadding="0" hiddenexpress="1">
            <tbody>
                <tr>
                    <td class="logo"></td>
                    <td class="biaoti" style="padding-left: 30px">
                        <asp:Label ID="Label1" runat="server" Text="填写问卷调查"></asp:Label></td>
                    <td class="formno">
                        <asp:Label ID="Label6" runat="server" Text="表单号："></asp:Label>
                        <aspxform:XLabel ID="XLabel3" runat="server" Text="--系统自动生成--" XDataBind="DF_OA_Questionnaire_AnswerSheet.FormNo" ValueToDisplayText Value BackColor="#8AB3DF"></aspxform:XLabel>
                        <br />
                    </td>
                </tr>
            </tbody>
        </table>
        <br />
        <table width="1360" height="50" align="center" class="head" style="background-color: white">
            <tbody>
                <tr>
                    <td class="biaoti" style="text-align: center">
                        <aspxform:XLabel ID="title" runat="server" XDataBind="title" ValueToDisplayText BackColor="white"></aspxform:XLabel>
                        <aspxform:XLabel ID="questionnaireId" runat="server" XDataBind="DF_OA_Questionnaire_AnswerSheet.questionnaireId" ValueToDisplayText HiddenExpress="1"></aspxform:XLabel>
                      
                        <aspxform:XTextBox ID="StepName" runat="server" XDataBind="Global.StepName" Width="100px" BorderWidth="1" BorderColor="#DCDCDC" HiddenInput="True"></aspxform:XTextBox>
						
						 <aspxform:XLabel ID="InitiateFormNo" runat="server" XDataBind="DF_OA_Questionnaire_AnswerSheet.InitiateFormNo" ValueToDisplayText HiddenExpress="1"></aspxform:XLabel>
                       
                         <aspxform:XDropDownList ID="XDropDownList1" runat="server" HiddenExpress="1" DataMap="Title->title;QuestionnaireDescription->QuestionnaireDescription" Height="20px" Width="100px" XDataSource="TableName:DF_OA_QuestionnaireApp_Info;Filter:FormNo->DF_OA_Questionnaire_AnswerSheet.InitiateFormNo"></aspxform:XDropDownList>

                    </td>
                </tr>
				 <tr style="top:2px;">
                    <td width="1360" style="padding-top:20px;"  colspan="4">
                     <aspxform:XLabel ID="QuestionnaireDescription"  forecolor="Red" Style="font-size: 15px; color: #444444; font-weight: bold; "    runat="server" XDataBind="QuestionnaireDescription" ValueToDisplayText BackColor="white"></aspxform:XLabel>
                </td>
                </tr>
            </tbody>
        </table>
        <br />
        <table width="1360" align="center" id="tablenew" border="0" cellspacing="0" cellpadding="0" dynamicarea="0,1" emptygrid="KeepEmpty" datamap="Id->DF_OA_Questionnaire_Answer.questionId;questionContent->DF_OA_Questionnaire_Answer.questionContent;questionType->DF_OA_Questionnaire_Answer.questionType;MultiChoiceRestriction->DF_OA_Questionnaire_Answer.MultiChoiceRestriction" xdatasource="TableName:DF_OA_Questionnaire_QuestionInfo;Filter:questionnaireId->DF_OA_Questionnaire_AnswerSheet.questionnaireId">
            <tbody>
                <tr>
                    <td></td>
                    <td width="1360" class="tds1" style="background-color: white" name="questiontd">&nbsp;<aspxform:XGridLineNo ID="XGridLineNo2" name="LineID" runat="server" XDataBind="DF_OA_Questionnaire_Answer.LineID" BackColor="white" Width="15px" BorderWidth="1px" TextAlign="Center" BorderStyle="None">1</aspxform:XGridLineNo>
                        . 
                        <aspxform:XLabel ID="XLabel13" runat="server" Style="font-size: 15px; color: #444444; font-weight: bold; line-height: 20px; width: 2px" XDataBind="DF_OA_Questionnaire_Answer.questionId" BackColor="white" HiddenExpress="1"></aspxform:XLabel>
                        <aspxform:XLabel ID="XLabel1" runat="server" XDataBind="DF_OA_Questionnaire_Answer.questionType" BackColor="white" HiddenExpress="1" name="questionType"></aspxform:XLabel>
                      
                        <aspxform:XLabel ID="XLabel12" runat="server" Style="font-size: 15px; color: #444444; font-weight: bold; line-height: 20px" XDataBind="DF_OA_Questionnaire_Answer.questionContent" ValueToDisplayText BackColor="white"></aspxform:XLabel>
                        <aspxform:XLabel ID="XLabel2" runat="server" XDataBind="DF_OA_Questionnaire_Answer.answerOption"  HiddenExpress="1" BackColor="white" name="answerOption"></aspxform:XLabel>
                        <aspxform:XTextBox ID="XTextBox2" runat="server" ValueToDisplayText Width="50px"  HiddenExpress="1" BorderWidth="1px" BorderColor="Gainsboro" HiddenInput="false" Max="0" Min="0" Express="javascript('CheckControl',DF_OA_Questionnaire_Answer.LineID,DF_OA_Questionnaire_Answer.questionType,DF_OA_Questionnaire_Answer.answerOption,DF_OA_Questionnaire_Answer.MultiChoiceRestriction)" DisableExpress='Global.StepName!="提交信息"'></aspxform:XTextBox>
                        <aspxform:XLabel ID="XLabel6" runat="server" XDataBind="DF_OA_Questionnaire_Answer.MultiChoiceRestriction" BackColor="white" HiddenExpress="1" name="MultiChoiceRestriction"></aspxform:XLabel>
                        <aspxform:XLabel ID="XLabel4" Style="color: #999; font-weight: normal; font-size: 14px" runat="server"  HiddenExpress="1" BackColor="white" Express='javascript("questionType",DF_OA_Questionnaire_Answer.questionType)'></aspxform:XLabel>
                         
                          <br />
                        <br />
                        <table width="100%" id="optionTable" style="border-left: medium none" border="0" cellspacing="0" cellpadding="0" hiddenexpress='DF_OA_Questionnaire_Answer.questionType!="1" &amp;&amp; DF_OA_Questionnaire_Answer.questionType!="2"' dynamicarea="0,1" emptygrid="KeepEmpty" datamap="optionContent->DF_OA_Questionnaire_AnswerOption.optionContent;optionId->DF_OA_Questionnaire_AnswerOption.optionsId;questionId->DF_OA_Questionnaire_AnswerOption.questionId;" xdatasource="TableName:DF_OA_Questionnaire_QuestionOptions;Filter:questionId->DF_OA_Questionnaire_Answer.questionId">
                            <tbody>
                                <%--style="width: 99%; background: rgb(239, 239, 239) none repeat scroll 0% 0%;"--%>
                                <tr>
                                    <td width="30" align="right" style="padding-top:27px;">
                                        <aspxform:XCheckBox ID="XCheckBox1" Style="height: 19px; width: 20px" runat="server" XDataBind="DF_OA_Questionnaire_AnswerOption.IsCheckd" Width="20px" ></aspxform:XCheckBox>
                                        <aspxform:XLabel ID="XLabel5" runat="server" XDataBind="DF_OA_Questionnaire_AnswerOption.optionsId" BackColor="white"></aspxform:XLabel>
                                        <aspxform:XLabel ID="XLabel15" runat="server" XDataBind="DF_OA_Questionnaire_AnswerOption.questionId" BackColor="white" HiddenExpress="1"></aspxform:XLabel>
                                    </td>
                                    <td width="800" align="left" style="padding-top:27px;">
                                        <aspxform:XLabel ID="XLabel11" runat="server" XDataBind="DF_OA_Questionnaire_AnswerOption.optionContent" BackColor="white"></aspxform:XLabel>
                                        <aspxform:XTextBox ID="XTextBox1" runat="server" ValueToDisplayText Width="50px" BorderWidth="1px" BorderColor="Gainsboro" HiddenInput="True" Max="0" Min="0" Express="javascript('IsChecked',DF_OA_Questionnaire_AnswerOption.optionsId,DF_OA_Questionnaire_AnswerOption.IsCheckd,DF_OA_Questionnaire_Answer.LineID,DF_OA_Questionnaire_Answer.questionType,GridLineNo)" DisableExpress='Global.StepName!="提交信息"'></aspxform:XTextBox>
                                        <aspxform:XGridLineNo ID="XGridLineNo1" runat="server" XDataBind="GridLineNo" Style="background-color: white; color: white" Width="0px" BorderWidth="1" TextAlign="Center" BorderStyle="None">1</aspxform:XGridLineNo>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <aspxform:XTextBox ID="answerText" runat="server" name="answerText" XDataBind="DF_OA_Questionnaire_Answer.answerText" HiddenExpress='DF_OA_Questionnaire_Answer.questionType!="3"' Width="100%" BorderWidth="1" BorderColor="#DCDCDC" Rows="3" TextMode="MultiLine"></aspxform:XTextBox>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>
