<%@ Page Title="People Development Tool" Language="vb" AutoEventWireup="false" MasterPageFile="~/Admin.Master" CodeBehind="PeopleBalanceSheetNew.aspx.vb" Inherits="RGBC_ScoreCard.PeopleBalanceSheet" MaintainScrollPositionOnPostback="true" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript">
        $(document).ready(function () {

            //$('#radOptions input').change(function () {
            //    // The one that fires the event is always the
            //    // checked one; you don't need to test for this
            //    alert($(this).val());
            //});

            //alert('Arr');
            $("input[type='radio'][id^='radOptions_']").change(
               function () {
                   //alert('Yarr');
                   var lblScore = $('#strScore');
                   var intScore = 0;

                   $("input[type='radio'][id^='radOptions_']:checked").each(function () {
                       intScore += parseFloat(this.value);
                   });
                   lblScore.val(intScore + '/100');
               });
        });

        function jsOptLabel_Click(Category, QID, lbl) {

            var opt = $("tr[id='tr_" + QID + "'] input[type='radio'][value='" + lbl + "']");
            opt.prop('checked', 'true');
            opt.click();

            //update category total 
            jsUpdateCategoryTotal(Category);
        }

        function jsUpdateCategoryTotal(Category) {
            //var intQuestionCount = $("div[id ='divCategoryQuestionCount_" + Category + "'] input[type=hidden]").val();   //get total number of questions for this category
            //alert($("div[id ='divCategoryQuestionIDs_" + Category + "'] input[type=hidden]").val());
            var strcategory = Category.replace("'", "\'");
            //alert(strcategory);
            var strQuestionIDs = $("div[id ='divCategoryQuestionIDs_" + strcategory + "'] input[type=hidden]").val();   //get all question IDs for this category

            var arrQuestionIDs = strQuestionIDs.split(',');

            //alert(arrQuestionIDs.length);
            var sum = 0;
            var value;

            //loop through all questions and re-calculate the category total
            for (i = 0; i < arrQuestionIDs.length; i++) {
                value = $("tr[id='tr_" + arrQuestionIDs[i] + "']").find(":checked").val(); // get checked value
                if (!isNaN(value)) {
                    sum += parseInt(value);
                }
            }

            sum = parseFloat(sum) / arrQuestionIDs.length;

            //if ($('#<%=lblQuestionnnaire.ClientID%>').text() != "Potential")
            //{
            //    sum = sum / 2; //divide by 2 to round category total down to 3
            //}
            sum = Math.round(sum * 100) / 100
            //set updated category total
            //alert($("div[id='divCatgeoryTotal_" + Category + "']").find('[id*=lblCategoryScore]').text());
            $("div[id='divCatgeoryTotal_" + Category + "']").find('[id*=lblCategoryScore]').text(sum);
        }

        function jsTotal10() {
            var lblScore = $('#strScore');
            var intScore = 0;

            $("input[type='radio'][id^='ContentPlaceHolder2_reQuestions_radOptions_']:checked").each(function () {
                intScore += $this.val();
            });

            lblScore.text(intScore + '/100');
        }
    </script>
    <style type="text/css">
        .auto-style1 {
            height: 24px;
        }
    </style>
</asp:Content>

<asp:Content ID="topContent" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
    <br />
    <div id="divHelp" style="position: absolute; left: 850px; top: 110px;">
        <asp:HyperLink ID="aHelp" runat="server" NavigateUrl="~/HelpPeopleBalanceSheet.aspx" Target="_blank" ToolTip="Help">
            <asp:Image ID="imhHelp" runat="server" Width="51px" ToolTip="Help" ImageUrl="content/128139-simple-red-square-icon-alphanumeric-information2-ps.png" />
        </asp:HyperLink>
    </div>
    <div class="divQuestions divQuestionnaireHeading">
        <fieldset class="textNowrap divQuestionnaireHeading">
            <legend><b>
                <asp:Label ID="lblQuestionnnaire" runat="server" Text=""></asp:Label>&nbsp;Survey</b></legend>
            <table cellpadding="2" cellspacing="1" width="100%">
                <tr>
                    <td align="right" width="90">User:</td>
                    <td width="55%">
                        <asp:Label ID="lblUser" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td align="right" width="12%">Date:</td>
                    <td>
                        <asp:TextBox ID="strDate" runat="server" BackColor="LightGray" ForeColor="black" Text='<%# Eval("dtDate") %>' CssClass="controlDate" ReadOnly="True" />
                    </td>
                </tr>
                <tr>
                    <td align="right" class="auto-style1">Reviewer:</td>
                    <td class="auto-style1">
                        <asp:Label ID="lblReviewer" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td align="right" class="auto-style1">Survey Total:</td>
                    <td class="auto-style1">
                        <asp:TextBox ID="strScore" value="" runat="server" BackColor="LightGray" ForeColor="black" CssClass="controlDate" ClientIDMode="Static" ReadOnly="True" />
                    </td>
                </tr>
                <tr>
                    <td align="right">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td align="right">Actual Score:</td>
                    <td>
                        <asp:TextBox ID="strActualScore" value="" runat="server" BackColor="LightGray" ForeColor="black" CssClass="controlDate" ClientIDMode="Static" ReadOnly="True" />
                    </td>
                </tr>
            </table>
            <br />

            <br />
            <asp:Label ID="lblMessage" runat="server" ForeColor="red" value="" />
        </fieldset>
        </BR>
      <asp:SqlDataSource ID="sdsRegion" runat="server" ConnectionString="<%$ ConnectionStrings:SuperbowlConnectionString %>"
          SelectCommand="SELECT [strRegionDesc2], [strRegionOrder] FROM [mstBranch] GROUP BY [strRegionDesc2], [strRegionOrder] ORDER BY [strRegionDesc2], [strRegionOrder]"></asp:SqlDataSource>

        <asp:SqlDataSource ID="sds1" runat="server" ConnectionString="<%$ ConnectionStrings:SuperbowlConnectionString %>" SelectCommand="SELECT CONVERT(varchar(10), dwfScoreCard.dtDate, 120) AS dtDate , dwfScoreCard.UserID, mstUser.strUserName, mstRep.RepID, mstRep.strRepName, dwdCustomer.strCustomerNo AS Store FROM dwfScoreCard INNER JOIN mstUser ON dwfScoreCard.UserID = mstUser.UserID INNER JOIN mstRep ON dwfScoreCard.RepID = mstRep.RepID INNER JOIN dwdCustomer ON dwfScoreCard.strCustomerNo = dwdCustomer.strCustomerNo CROSS JOIN mstUserGroup WHERE (mstRep.blnRep = 1) AND (dwfScoreCard.SurveyID = @SurveyID)">
            <SelectParameters>
                <asp:QueryStringParameter DefaultValue="1" Name="SurveyID" QueryStringField="SurveyID" />
            </SelectParameters>
        </asp:SqlDataSource>

        <%--Category Repeater--%>
        <asp:Repeater ID="reCategories" runat="server" ClientIDMode="Predictable" OnItemDataBound="Category_ItemBound">
            <ItemTemplate>
                <div class="lblCategory">
                    <asp:Label ID="strCategory" runat="server" Text='<%# Eval("strCategory")%>'></asp:Label><br>
                    <br>
                </div>
                <div style="border-bottom: 1px solid black; padding-bottom: 10px; margin-bottom: 10px;">
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("strCategoryDescription")%>'></asp:Label>
                </div>
                <%--Question DataSource--%>
                <asp:SqlDataSource
                    ID="sdsQuestions" runat="server" ConnectionString="<%$ ConnectionStrings:SuperbowlConnectionString %>"
                    SelectCommand="SELECT QuestionID, strCategory, REPLACE(strQuestion, CHAR(13),'&lt;BR/&gt;') AS strQuestion, strOptionType, intOrder, blnComments, blnComments AS txtComments, blnActive, strLastUser, strLastEdit, '0' AS intAnswer FROM scrQuestion WHERE (refQuestionnaireID =  @refQuestionnaireID) AND (blnActive = 1) AND (strCategory = @strCategory) ORDER BY intOrder, strQuestion">
                    <SelectParameters>
                        <asp:Parameter Name="strCategory" />
                        <asp:Parameter Name="refQuestionnaireID" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <%--Answer DataSource--%>
                <%--SELECT dwfScoreCard.QuestionID, dwfScoreCard.QuestionnaireID AS refQuestionnaireID, REPLACE(dwfScoreCard.strQuestion, CHAR(13),'<BR/>') AS strQuestion, dwfScoreCard.strCategory, dwfScoreCard.strOptionType, dwfScoreCard.intOrder, dwfScoreCard.blnComments, dwfScoreCard.intAnswer, dwfScoreCard.txtComments FROM scrQuestion INNER JOIN dwfScoreCard INNER JOIN scrSurvey ON dwfScoreCard.QuestionnaireID = scrSurvey.QuestionnaireID AND dwfScoreCard.dtDate = scrSurvey.dtDate AND dwfScoreCard.UserID = scrSurvey.UserID AND dwfScoreCard.RepID = scrSurvey.RepID AND dwfScoreCard.SurveyID = scrSurvey.SurveyID ON scrQuestion.QuestionID = dwfScoreCard.QuestionID WHERE (scrSurvey.SurveyID = @SurveyID) AND (dwfScoreCard.strCategory = @strCategory) ORDER BY dwfScoreCard.intOrder, dwfScoreCard.strQuestion--%>
                <asp:SqlDataSource
                    ID="sdsAnswers" runat="server" ConnectionString="<%$ ConnectionStrings:SuperbowlConnectionString %>"
                    SelectCommand="SELECT dwfScoreCard.QuestionID, dwfScoreCard.QuestionnaireID AS refQuestionnaireID, REPLACE(dwfScoreCard.strQuestion, CHAR(13),'<BR/>') AS strQuestion, dwfScoreCard.strCategory, dwfScoreCard.strOptionType, dwfScoreCard.intOrder, dwfScoreCard.blnComments, dwfScoreCard.intAnswer, dwfScoreCard.txtComments FROM dwfScoreCard WHERE (dwfScoreCard.SurveyID = @SurveyID) AND (dwfScoreCard.strCategory = @strCategory) ORDER BY dwfScoreCard.intOrder, dwfScoreCard.strQuestion">
                    <SelectParameters>
                        <asp:Parameter Name="strCategory" />
                        <asp:Parameter Name="SurveyID" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <%--Question Repeater--%>
                <asp:Repeater ID="reQuestions" runat="server" ClientIDMode="Predictable" OnItemDataBound="FormatRepeaterRow">
                    <ItemTemplate>
                        <div class="lblQuestionHeading">
                            <asp:Label ID="intOrder" runat="server" Text='<%# "Q" + Eval("intOrder").ToString() %>'></asp:Label>
                            <asp:Label ID="strQuestion" runat="server" Text='<%# Eval("strQuestion") %>' TextMode="SingleLine" ReadOnly="True" BorderStyle="None" BorderWidth="0"></asp:Label>
                        </div>
                        <table cellpadding="2" cellspacing="1" class="tblOptions">
                            <%--Range 3 label row 1--%>
                            <tr class="trOptions" id="tr1" style='<%# IIf(Eval("strOptionType").ToString() = "Range3", "", "display: none")%>'>
                                <td>
                                    <label for="" onclick="jsOptLabel_Click('<%# Eval("strCategory")%>', <%# Eval("QuestionID")%>,1);">Low</label></td>
                                <td>
                                    <label for="" onclick="jsOptLabel_Click('<%# Eval("strCategory")%>', <%# Eval("QuestionID")%>,2);">Medium</label></td>
                                <td>
                                    <label for="" onclick="jsOptLabel_Click('<%# Eval("strCategory")%>', <%# Eval("QuestionID")%>,3);">High</label>
                                </td>
                            </tr>
                            <%--Range 6 label row 1--%>
                            <tr class="trOptions" id="tr2" style='<%# IIf(Eval("strOptionType").ToString() = "Range6", "", "display: none")%>'>
                                <td>
                                    <label for="" onclick="jsOptLabel_Click('<%# Eval("strCategory")%>', <%# Eval("QuestionID")%>,3);">1</label></td>
                                <td>
                                    <label for="" onclick="jsOptLabel_Click('<%# Eval("strCategory")%>', <%# Eval("QuestionID")%>,2);">2</label></td>
                                <td>
                                    <label for="" onclick="jsOptLabel_Click('<%# Eval("strCategory")%>', <%# Eval("QuestionID")%>,1);">3</label></td>
                                <td>
                                    <label for="" onclick="jsOptLabel_Click('<%# Eval("strCategory")%>', <%# Eval("QuestionID")%>,1);">4</label></td>
                                <td>
                                    <label for="" onclick="jsOptLabel_Click('<%# Eval("strCategory")%>', <%# Eval("QuestionID")%>,1);">5</label></td>
                                <td>
                                    <label for="" onclick="jsOptLabel_Click('<%# Eval("strCategory")%>', <%# Eval("QuestionID")%>,1);">6</label></td>
                            </tr>
                            <%--Range 3 label row 2--%>
                            <tr class="trOptions" id="tr3" style='<%# IIf(Eval("strOptionType").ToString() = "Range3", "", "display: none")%>'>
                                <td>
                                    <label for="" onclick="jsOptLabel_Click('<%# Eval("strCategory")%>', <%# Eval("QuestionID")%>,1);">(Definitely No)</label></td>
                                <td>
                                    <label for="" onclick="jsOptLabel_Click('<%# Eval("strCategory")%>', <%# Eval("QuestionID")%>,2);">(Maybe / Somewhat)</label></td>
                                <td>
                                    <label for="" onclick="jsOptLabel_Click('<%# Eval("strCategory")%>', <%# Eval("QuestionID")%>,3);">(Definitely Yes)</label>
                                </td>
                            </tr>
                            <%--Range 3 radio button row--%>
                            <tr class="trOptions" id='tr_<%# Eval("QuestionID")%>' style='<%# IIf(Eval("strOptionType").ToString() = "Range3", "", "display: none")%>'>
                                <td colspan="3" align="center">
                                    <%--ClientIDMode="Static"--%>
                                    <asp:RadioButtonList ID='radOptions' runat="server" onclick='<%# String.Format("return jsUpdateCategoryTotal(""{0}"");", Eval("strCategory"))%>' CausesValidation="True" CellPadding="0" CellSpacing="0" RepeatDirection="Horizontal" Width="100%" CssClass="range10" AppendDataBoundItems="True">
                                        <asp:ListItem Value="1" Text=""></asp:ListItem>
                                        <asp:ListItem Value="2" Text=""></asp:ListItem>
                                        <asp:ListItem Value="3" Text=""></asp:ListItem>
                                    </asp:RadioButtonList>
                                </td>
                            </tr>
                            <%--Range 6 radio button row--%>
                            <tr class="trOptions" id='tr_<%# Eval("QuestionID")%>' style='<%# IIf(Eval("strOptionType").ToString() = "Range6", "", "display: none")%>'>
                                <td colspan="6" align="center">
                                    <%--ClientIDMode="Static"--%>
                                    <asp:RadioButtonList ID='radOptionsRange6' runat="server" onclick='<%# String.Format("return jsUpdateCategoryTotal(""{0}"");", Eval("strCategory"))%>' CausesValidation="True" CellPadding="0" CellSpacing="0" RepeatDirection="Horizontal" Width="100%" CssClass="range10" AppendDataBoundItems="True">
                                        <asp:ListItem Value="1" Text=""></asp:ListItem>
                                        <asp:ListItem Value="2" Text=""></asp:ListItem>
                                        <asp:ListItem Value="3" Text=""></asp:ListItem>
                                        <asp:ListItem Value="4" Text=""></asp:ListItem>
                                        <asp:ListItem Value="5" Text=""></asp:ListItem>
                                        <asp:ListItem Value="6" Text=""></asp:ListItem>
                                    </asp:RadioButtonList>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="6" align="left">
                                    <asp:RequiredFieldValidator Display="Dynamic" EnableClientScript="true" ID="rfvQuestion" SetFocusOnError="true" runat="server" ErrorMessage="Please select an option!" ControlToValidate='<%# IIf(Eval("strOptionType").ToString() = "Range6", "radOptionsRange6", "radOptions")%>' CssClass="boxError"></asp:RequiredFieldValidator>
                                    <asp:HiddenField ID="QuestionID" runat="server" Value='<%# Eval("QuestionID") %>' />
                                    <%--<%# IIf(Eval("intAnswer").ToString() Is DBNull.Value Or "0", "-1", Eval("intAnswer").ToString())%>--%>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="100%" align="left">Comments:
                             <asp:TextBox ID="txtComments" runat="server" Height="40px" TextMode="MultiLine" Visible='<%# IIf(Eval("blnComments") = False, False, True)%>' Text='<%# IIf(Eval("txtComments").ToString()="True", "", Eval("txtComments").ToString()) %>'></asp:TextBox>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:Repeater>

                <div id="divCategoryQuestionIDs_<%# Eval("strCategory")%>">
                    <asp:HiddenField ID='CategoryQuestionIDs' runat="server" />
                    <%-- string of all questions IDs per Category--%>
                </div>

                <%-- <%--Category Total--%>
                <div class="lblCategory" id="divCatgeoryTotal_<%# Eval("strCategory")%>">
                    <table cellpadding="2" cellspacing="1" class="tblOptions">
                        <tr style="font-size: small">
                            <td width="14%">Category Total:</td>
                            <td width="1%">
                                <asp:Label ID="lblCategoryScore" runat="server" Text='<%# Eval("intCategoryScore") %>'></asp:Label>
                            </td>
                            <td width="85%">/&nbsp;6</td>
                        </tr>
                    </table>
                </div>
            </ItemTemplate>
        </asp:Repeater>

        <%-- <%--Survey Total--%>
        <div class="lblCategory">
            <table cellpadding="2" cellspacing="1" class="tblOptions">
                <tr style="font-size: medium">
                    <td width="14%">Survey Total:</td>
                    <td width="1%">
                        <asp:Label ID="lblSurveyTotal" runat="server" Text=''></asp:Label>
                    </td>
                    <td width="85%">/&nbsp;6</td>
                </tr>
            </table>
        </div>
        <div>
            <b>What are the individual’s performance & development goals for the next year?:</b></br>
            <asp:TextBox ID="txtManagerComments" runat="server" Height="60px" TextMode="MultiLine"></asp:TextBox>
        </div>
    </div>
    <br />
    <asp:Label ID="lblMessageQuestions" runat="server" ForeColor="red" value="" />
    <br />
    <br />
    <asp:Button ID="btnSave" runat="server" Text="Save" />
    &nbsp;
   <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick =" return confirm('Are you sure you want to submit the Survey to HR?')" Enabled="False" />

    <asp:SqlDataSource ID="sdsCategories" runat="server" ConnectionString="<%$ ConnectionStrings:SuperbowlConnectionString %>" SelectCommand="SELECT strCategory, strCategoryDescription, '' AS intCategoryScore FROM scrQuestion WHERE (refQuestionnaireID = @refQuestionnaireID) AND (strCategoryDescription IS NOT NULL) AND (blnActive = 1) GROUP BY strCategory, strCategoryDescription ORDER BY strCategory">
        <SelectParameters>
            <asp:Parameter Name="refQuestionnaireID" />
        </SelectParameters>
    </asp:SqlDataSource>
    <br>
    <asp:SqlDataSource ID="sdsSurveyCategories" runat="server" ConnectionString="<%$ ConnectionStrings:SuperbowlConnectionString %>" SelectCommand="SELECT dwfScoreCard.SurveyID, dwfScoreCard.strCategory, dwfScoreCard.strOptionType, ROUND(CAST(SUM(dwfScoreCard.intAnswer) AS float) / COUNT(dwfScoreCard.strQuestion), 2) AS intCategoryScore, tblCategoryDescription.strCategoryDescription FROM dwfScoreCard LEFT OUTER JOIN (SELECT strCategory, strCategoryDescription FROM scrQuestion WHERE (strCategoryDescription IS NOT NULL) GROUP BY strCategory, strCategoryDescription) AS tblCategoryDescription ON dwfScoreCard.strCategory = tblCategoryDescription.strCategory WHERE (dwfScoreCard.SurveyID = @SurveyID) GROUP BY dwfScoreCard.SurveyID, dwfScoreCard.strCategory, dwfScoreCard.strOptionType, tblCategoryDescription.strCategoryDescription">
        <SelectParameters>
            <asp:Parameter Name="SurveyID" />
        </SelectParameters>
    </asp:SqlDataSource>
    <br>
    <div id="bottomSection" class="divQuestions divQuestionnaireHeading">
    </div>
    <br />

</asp:Content>
