<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Questionnaire.aspx.cs"
    Inherits="RM.Web.Training.QuesNaire.Questionnaire" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>
        <%=RM.Common.DotNetConfig.LanguageHelper.GetKey("QuesNaire_wjdc")%></title>
    <link href="../Exam/Themes/Css/ExamGlobal.css" rel="stylesheet" type="text/css" />
    <link href="Themes/QuestionnaireCss.css" rel="stylesheet" type="text/css" />
    <link href="../../Themes/Scripts/ShowMsg/msgbox.css" rel="stylesheet" type="text/css" />
    <script src="../../Themes/Scripts/ShowMsg/msgbox.js" type="text/javascript"></script>
    <script src="../../Themes/Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
    <script src="../../Themes/Scripts/jquery.json-2.3.min.js" type="text/javascript"></script>
    <style type="text/css">
        table td { text-align: right; margin: 10px; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="pageContainer">
        <div id="que_title">
            <h1 id="que_name" runat="server">
            </h1>
        </div>
        <div id="container">
            <div id="context">
                <ol>
                    <%=QuestionHtml %>
                </ol>
                <div title="个人信息" style="background: #efefef; margin-bottom: 20px; padding: 5px;">
                    <table border="0" cellpadding="0" cellspacing="0" style="margin: 0 auto;">
                        <tr>
                            <td>
                                用户名:
                            </td>
                            <td>
                                <input type="text" id="stu_name" style="margin-left: 2px;" value="匿名用户" runat="server" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                联系电话:
                            </td>
                            <td>
                                <input type="text" id="Phone" style="margin-left: 2px;" value="" runat="server" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                QQ:
                            </td>
                            <td>
                                <input type="text" id="QQ" style="margin-left: 2px;" value="" runat="server" />
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="submitButton">
                    <a href="javascript:void(0)" id="submitBtn" runat="server"></a>
                </div>
            </div>
            <div id="theEnd">
                <div id="txtPrompt">
                    <%=RM.Common.DotNetConfig.LanguageHelper.GetKey("QuesNaire_endtishi")%>
                </div>
                <div>
                    <a href="javascript:void(0)" onclick="window.close();" id="linkClose" runat="server"></a>
                </div>
            </div>
        </div>
    </div>
    </form>
    <script type="text/javascript">
        $(function () {
            String.prototype.getQueryString = function (name) {//获取地址栏参数，使用方法：location.search.getQueryString("要查询的参数")
                var reg = new RegExp("(^|&|\\?)" + name + "=([^&]*)(&|$)"), r;
                if (r = this.match(reg)) return unescape(r[2]);
                return null;
            }
            if ($("#context>ol>li").length < 1) {//页面异常
                $("#context").html("");
                $("#theEnd").show(0);
                $("#txtPrompt").text('<%=RM.Common.DotNetConfig.LanguageHelper.GetKey("QuesNaire_WebFormatErr")%>');
                $("#linkClose").hide(0);
                return;
            }
            $("input:radio,input:checkbox").removeAttr("checked");
            function resizeimg(objImg, maxWidth, maxHeight) {
                ///	<summary>
                ///     根据图片对象和指定的参数按比例缩放图片
                ///     &#10;1: resizeimg(obj, maxW, maxH) - 根据图片对象和指定的参数按比例缩放图片
                ///	</summary>
                ///	<param name="obj" type="ImageObject">图片对象</param>
                ///	<param name="maxW" type="int">最大宽度</param>
                ///	<param name="maxH" type="int">最大高度</param>
                ///	<returns type="string" />
                var img = new Image();
                img.src = objImg.src;
                var hRatio;
                var wRatio;
                var Ratio = 1;
                var w = img.width;
                var h = img.height;
                wRatio = maxWidth / w;
                hRatio = maxHeight / h;
                if (maxWidth == 0 && maxHeight == 0) {
                    Ratio = 1;
                } else if (maxWidth == 0) {//
                    if (hRatio < 1) Ratio = hRatio;
                } else if (maxHeight == 0) {
                    if (wRatio < 1) Ratio = wRatio;
                } else if (wRatio < 1 || hRatio < 1) {
                    Ratio = (wRatio <= hRatio ? wRatio : hRatio);
                }
                if (Ratio < 1) {
                    w = w * Ratio;
                    h = h * Ratio;
                }
                objImg.height = h;
                objImg.width = w;
            }
            if ($.browser.msie) {
                var imgList = $("#context img");
                for (var i = 0; i < imgList.length; i++) {
                    resizeimg(imgList.get(i), 600, 500);
                }
            } else {
                $("#context img").load(function () {
                    resizeimg(this, 600, 500);
                });
            }
            $("input:radio").change(function () {//单选
                var $parObj = $(this).closest(".qustion");
                $parObj.attr("data", $(this).attr("data"));
            });
            $("input:checkbox").change(function () {//多选
                var $parObj = $(this).closest(".qustion");
                if ($(this).is(":checked")) {
                    $parObj.attr("data", ($parObj.attr("data") ? $parObj.attr("data") : "") + $(this).attr("data"));
                    $parObj.children().first().css("color", "#333");
                } else {
                    $parObj.attr("data", $parObj.attr("data").replace($(this).attr("data"), ""));
                }
            });

            $("textarea").blur(function () {//文本域
                var $parObj = $(this).closest(".qustion");
                if ($(this).val().length < 1 || jQuery.trim($(this).val()).length < 1) {
                    $parObj.removeAttr("data");
                } else {
                    $parObj.attr("data", "TXT");
                    $parObj.children().first().css("color", "#333");
                }
            });
            $("#submitBtn").click(function () {
                var phone = $("#Phone").val();
                var QQ = $("#QQ").val();
                 var regphone = /^0?1[3|4|5|8][0-9]\d{8}$/;
                 
                if (phone == null || phone == ""|| phone.length <=0) {
                    ZENG.msgbox.show("<%=RM.Common.DotNetConfig.LanguageHelper.GetKey("phone")%><%=RM.Common.DotNetConfig.LanguageHelper.GetKey("WeiXinPage_Form_byxczkx")%>",3,2000 );
                    return false;
                }
                
                if (QQ == null || QQ == ""|| QQ.length <=0) {
                    ZENG.msgbox.show("QQ<%=RM.Common.DotNetConfig.LanguageHelper.GetKey("WeiXinPage_Form_byxczkx")%>",3,2000 );
                    return false;
                }
                var  isok = regphone.test(phone);
                if (!isok) {
                    ZENG.msgbox.show("<%=RM.Common.DotNetConfig.LanguageHelper.GetKey("phone")%>格式不正确%>",3,2000 );
                    return false;
                }
             
                $("textarea").blur();
                var check = true;
                $(".qn_Inputs").each(function () {
                    var $parentObj = $(this).parent();
                    if (!$parentObj.attr("data")) {
                        var topPx = parseInt($parentObj.offset().top);
                        $("html,body").animate({ scrollTop: topPx }, 500, function () {//滚动到未答的位置
                            $parentObj.children().first().css("color", "red");
                        });
                        check = false;
                        return false;
                    }
                });
                if (!check)//验证没通过
                    return false;
                //抓取每题对象
                var ques = new Array();
                $(".qustion").each(function () {
                    var qnObj = new queObj();
                    qnObj.TKS_QueNaiDetail_QuestionsId = $(this).attr("id");
                    if ($(this).attr("data") == "TXT") {//问答题 
                        qnObj.Questions_As = $(this).find(".txtInput").val();

                    } else { //多选或单选
                        var array = $(this).attr("data").split('');
                        for (var i = 0; i < array.length; i++) {

                            switch (array[i]) {
                                case 'A':
                                    qnObj.Questions_OptionOne = 1;
                                    break;
                                case 'B':
                                    qnObj.Questions_OptionTwo = 1;
                                    break;
                                case 'C':
                                    qnObj.Questions_OptionThree = 1;
                                    break;
                                case 'D':
                                    qnObj.Questions_OptionFour = 1;
                                    break;
                                case 'E':
                                    qnObj.questions_OptionFive = 1;
                                    break;
                                case 'F':
                                    qnObj.Questions_OptionSix = 1;
                                    break;
                                case 'G':
                                    qnObj.Questions_OptionSeven = 1;
                                    break;
                                case 'H':
                                    qnObj.Questions_OptionEight = 1;
                                    break;
                                case 'I':
                                    qnObj.Questions_OptionNine = 1;
                                    break;
                                case 'J':
                                    qnObj.Questions_OptionTen = 1;
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                    ques.push(qnObj); //添加到数组中
                });
                //判断数组数据
                if (ques.length < 1) {
                    ZENG.msgbox.show('<%=RM.Common.DotNetConfig.LanguageHelper.GetKey("Ques_showExcep")%>', 3, 2000); return false;
                }
                //开始异步请求
                $.ajax({
                    url: "ReplyQuestionnaire.ashx",
                    type: "post",
                    data: { "array": $.toJSON(ques), "hashKey": location.search.getQueryString("HashKey"), "UserName": $("#stu_name").val(),"QQ":QQ,"Phone":phone },
                    dataType: "json",
                    success: function (data) {
                        var result = parseInt(data);
                        if (result < 0) {
                            ZENG.msgbox.show('<%=RM.Common.DotNetConfig.LanguageHelper.GetKey("Abnormal network connection")%>', 5, 2000);
                        } else {
                            $(window).scrollTop(0);
                            $("#context").hide(0);
                            $("#theEnd").show(0);
                            var expires = new Date();
                            expires.setMonth(expires.getMonth() + 1); //一个月后失效
                            document.cookie = "historyQnai=" + encodeURI(location.search.getQueryString("HashKey")) + "; expires=" + expires.toGMTString() + "; path=/; domain=x-life.com; secure";
                        }
                    },
                    error: function () {
                        ZENG.msgbox.show('<%=RM.Common.DotNetConfig.LanguageHelper.GetKey("Abnormal network connection")%>', 5, 2000);
                    }
                });
                return false;
            });
            //问卷明细对象
            function queObj(TKS_QueNaiDetail_QuestionsId, Questions_OptionOne, Questions_OptionTwo, Questions_OptionThree, Questions_OptionFour, questions_OptionFive, Questions_OptionSix, Questions_OptionSeven, Questions_OptionEight, Questions_OptionNine, Questions_OptionTen, Questions_As) {
                ///	<summary>
                ///     题目对象
                ///     date:2013年10月17日 17:02:32
                ///     author:jl
                ///     version:1.0
                ///     &#10;1: queObj(TKS_QueNaiDetail_QuestionsId, Questions_OptionOne, Questions_OptionTwo, Questions_OptionThree, Questions_OptionFour, questions_OptionFive, Questions_OptionSix, Questions_OptionSeven, Questions_OptionEight, Questions_OptionNine, Questions_OptionTen,Questions_As) - 答卷明细对象
                ///	</summary>
                /// <param name="TKS_QueNaiDetail_QuestionsId" type="string">题目ID</param>
                /// <param name="Questions_OptionOne" type="string">回答选项1</param>
                /// <param name="Questions_OptionTwo" type="string">回答选项2</param>
                /// <param name="Questions_OptionThree" type="string">回答选项3</param>
                ///	<returns type="string" />
                this.TKS_QueNaiDetail_QuestionsId = TKS_QueNaiDetail_QuestionsId;
                if (Questions_OptionOne) this.Questions_OptionOne = Questions_OptionOne;
                if (Questions_OptionTwo) this.Questions_OptionTwo = Questions_OptionTwo;
                if (Questions_OptionThree) this.Questions_OptionThree = Questions_OptionThree;
                if (Questions_OptionFour) this.Questions_OptionFour = Questions_OptionFour;
                if (questions_OptionFive) this.questions_OptionFive = questions_OptionFive;
                if (Questions_OptionSix) this.Questions_OptionSix = Questions_OptionSix;
                if (Questions_OptionSeven) this.Questions_OptionSeven = Questions_OptionSeven;
                if (Questions_OptionEight) this.Questions_OptionEight = Questions_OptionEight;
                if (Questions_OptionNine) this.Questions_OptionNine = Questions_OptionNine;
                if (Questions_OptionTen) this.Questions_OptionTen = Questions_OptionTen;
                if (Questions_As) this.Questions_As = Questions_As;
            }; //注意有;

         
        });
    </script>
    <script type="text/javascript">
        $(function () {
            if (location.search.getQueryString("Left_id") != "" && location.search.getQueryString("Left_id") != null) {
                $.ajax({
                    url: "GetUserQuestionnaire.ashx",
                    type: "post",
                    data: { "Left_id": location.search.getQueryString("Left_id") },
                    dataType: "text",
                    success: function (data) {
                        if (data != 1) {
                            var json = eval('(' + data + ')')

                            forListselect(json);
                            $("#submitBtn").hide();
                        } else {
                            ZENG.msgbox.show('<%=RM.Common.DotNetConfig.LanguageHelper.GetKey("Abnormal network connection")%>', 5, 2000);
                        }
                    },
                    error: function () {
                        ZENG.msgbox.show('<%=RM.Common.DotNetConfig.LanguageHelper.GetKey("Abnormal network connection")%>', 5, 2000);
                    }
                });
            }
        });
        ///获取用户答案 
        function forListselect(obj) {
            if (obj != null) {
                $("#stu_name").val(obj[0].TKS_QueNaiDetail_UserName);
                $("#Phone").val(obj[0].Phone);
                $("#QQ").val(obj[0].QQ);
            }
            for (var i = 0; i < obj.length; i++) {

                $("#" + obj[i].TKS_QueNaiDetail_QuestionsId).find("textarea").each(
                function () { $(this).val(obj[i].Questions_As) }
                );
                $("#" + obj[i].TKS_QueNaiDetail_QuestionsId).find("input[name=" + obj[i].TKS_QueNaiDetail_QuestionsId + "]").each(
               function () {
                   if ($(this).attr("data") == "A") {
                       if (obj[i].Questions_OptionOne == 1) {
                           TypeSelect($(this));
                       }
                   }
                   else if ($(this).attr("data") == "B") {
                       if (obj[i].Questions_OptionTwo == 1) {
                           TypeSelect($(this));
                       }
                   } else if ($(this).attr("data") == "C") {
                       if (obj[i].Questions_OptionThree == 1) {
                           TypeSelect($(this));
                       }
                   } else if ($(this).attr("data") == "D") {
                       if (obj[i].Questions_OptionFour == 1) {
                           TypeSelect($(this));
                       }
                   } else if ($(this).attr("data") == "E") {
                       if (obj[i].Questions_OptionFive == 1) {
                           TypeSelect($(this));
                       }
                   } else if ($(this).attr("data") == "F") {
                       if (obj[i].Questions_OptionSix == 1) {
                           TypeSelect($(this));
                       }
                   } else if ($(this).attr("data") == "G") {
                       if (obj[i].Questions_OptionSeven == 1) {
                           TypeSelect($(this));
                       }
                   } else if ($(this).attr("data") == "H") {
                       if (obj[i].Questions_OptionEight == 1) {
                           TypeSelect($(this));
                       }
                   } else if ($(this).attr("data") == "I") {
                       if (obj[i].Questions_OptionNine == 1) {
                           TypeSelect($(this));
                       }
                   } else if ($(this).attr("data") == "J") {
                       if (obj[i].Questions_OptionTen == 1) {
                           TypeSelect($(this));
                       }
                   }
               }
               );
            }
        }
        function TypeSelect(obj) {

            if (obj.attr("type") == "checkbox") {
                obj.attr("checked", true);
            }
            else if (obj.attr("type") == "radio") {
                obj.attr("checked", "checked");
            }
        }
    </script>
</body>
</html>
