<%@ page language="C#" masterpagefile="~/MasterPage.master" autoeventwireup="true" enableeventvalidation="true" inherits="Personnel_PersonnelBrowser, CardAccess.Web.UI_deploy" title="Personnel Search" theme="Office2007Black" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <script language="javascript" type="text/javascript">
        var appPath = "<%= Request.ApplicationPath %>";
        var defaultAspx = "/Default.aspx";

        function chkValidity(oButton, oEvent) {
            var startV = parseInt(document.getElementById("<%=this.srcBadgeForm.ClientID %>").value);
            var endV = parseInt(document.getElementById("<%=this.srcBadgeTo.ClientID %>").value);
            var chkValue = document.getElementById("<%=this.srcChkBox.ClientID %>").checked;
            if (startV > endV && chkValue) {
                alert("<%=this.StartGreaterEnd %>");
                oEvent.cancel = true;
                oEvent.cancelPostBack = true;
            }
        }

        function itemExpanded(oWebPanel, oEvent) {
            oWebPanel.getExplorerBarItems().getItem(0).set_text('<%=CollapseMessage %>');
        }

        function itemCollapsed(oWebPanel, oEvent) {
            oWebPanel.getExplorerBarItems().getItem(0).set_text('<%=ExpandMessage %>');
        }

        function SearchPanel_ExpandedStateChanged(oWebPanel, oEvent) {
            if (oWebPanel.getExpanded() == true) {
                oWebPanel.getHeader()._element.childNodes[1].nodeValue = '<%=CollapseMessage %>';
            }
            else {
                oWebPanel.getHeader()._element.childNodes[1].nodeValue = '<%=ExpandMessage %>';
            }
        }

        function btnEnable(gridName, cellId) {
            var securityLevel = 0;
            $.ajax({
                type: "POST",
                url: "PersonnelSearch.aspx/GetSecurityLevel",
                data: JSON.stringify({}),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                async: false,
                success: function (msg) {
                    securityLevel = msg.d;
                },
                error: function (data, status, jqXHR) {
                }
            });

            if (securityLevel == -1) {
                window.location.href = appPath + defaultAspx;
            }
            else {
                var oToolbar = $find("<%=this.PersonalToolbar.cltId %>");
                oToolbar.getItems().getItem(0).set_enabled(true);
                oToolbar.getItems().getItem(1).set_enabled(true);
                oToolbar.getItems().getItem(2).set_enabled(true);

                if (securityLevel == 4) {
                    //Read Only
                    oToolbar.getItems().getItem(0).set_enabled(false);
                    oToolbar.getItems().getItem(1).set_enabled(false);
                }
                else if (securityLevel == 5) {
                    //Create Only
                    oToolbar.getItems().getItem(1).set_enabled(false);
                    oToolbar.getItems().getItem(2).set_enabled(false);
                }
                else if (securityLevel == 6) {
                    //Edit Only
                    oToolbar.getItems().getItem(0).set_enabled(false);
                    oToolbar.getItems().getItem(1).set_enabled(false);
                }
                else if (securityLevel == 7) {
                    //Create or Edit Only
                    oToolbar.getItems().getItem(1).set_enabled(false);
                }
            }
        }

        function grd_DblClickHandler(gridName, cellId) {
            var grid_id = $find('<%=this.PersonalGrid.ConfigGrid.ClientID %>');
            var columns = grid_id.get_behaviors().get_selection().get_selectedColumns().getItem(0);
            if (columns != null)
                return;
            var row = grid_id.get_behaviors().get_selection().get_selectedRows().getItem(0);
            if (row == null) {
            }
            else {
                var nLocation;
                var PersonObjectID = row.get_cell(7).get_value();
                var aGroupNo = row.get_cell(10).get_value(); //Dedicated Access Groups
                var facility = row.get_cellByColumnKey("KeyValue1").get_value();
                var BadgeID = row.get_cellByColumnKey("KeyValue2").get_value();
//                if (aGroupNo != '')
//                    nLocation = "PersonnelInsertUpdate.aspx?personId=" + PersonObjectID + "&FCode=" + facility + "&BNo=" + BadgeID + "&aGroupNo=" + aGroupNo;
//                else
                nLocation = "PersonnelInsertUpdate.aspx?personId=" + PersonObjectID + "&FCode=" + facility + "&BNo=" + BadgeID;
                setTimeout(function () { window.location = nLocation }, 500);
            }
        }
        function EditAction(ToolBarName, cellId) {
            //Edit
            if (ToolBarName.getItems().getItem(2).get_isActive()) {
                grd_DblClickHandler(null, null);
            }
            //Add
            if (ToolBarName.getItems().getItem(0).get_isActive()) {
                var grid = $find('<%=this.PersonalGrid.ConfigGrid.ClientID %>');
                var row = grid.get_behaviors().get_selection().get_selectedRows().getItem(0);
                var nLocation;
                if (row != null)
                    nLocation = "PersonnelInsertUpdate.aspx?NewBadge=true&personId=" + row.get_cell(7).get_value();
                else
                    nLocation = "PersonnelInsertUpdate.aspx";
                setTimeout(function () { window.location = nLocation }, 500);
            }
            //Delete
            if (ToolBarName.getItems().getItem(1).get_isActive()) {
                $.ajax({
                    type: "POST",
                    url: appPath + "/WebService/CAWebService.asmx/CheckSessionAlive",
                    data: JSON.stringify({}),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    cache: false,
                    async: false,
                    success: function (msg) {
                        var result = msg.d;
                        if (result) {
                            var r = confirm("<%=this.DeletePrompt%>");
                            if (r == true) {
                                var grid = $find('<%=this.PersonalGrid.ConfigGrid.ClientID %>');
                                var row = grid.get_behaviors().get_selection().get_selectedRows().getItem(0);
                                if (row != null) {
                                    var PersonObjectID = row.get_cell(7).get_value();
                                    var Facility = row.get_cellByColumnKey("KeyValue1").get_value();
                                    var BadgeID = row.get_cellByColumnKey("KeyValue2").get_value();
                                    $.ajax({
                                        url: "PersonnelSearch.aspx?Type=AjaxDelete",
                                        data: "personId=" + PersonObjectID + "&FCode=" + Facility + "&BNo=" + BadgeID + "&" + Math.random(),
                                        success: function (data) {
                                            alert('<%=SuccessMessage %>');
                                            __doPostBack("<%=WARPToolbar.ClientID %>", "");
                                        }
                                    });
                                }
                                else
                                    alert('<%=NullValueCheck %>');
                            }
                        }
                        else {
                            window.location.href = appPath + defaultAspx;
                        }
                    },
                    error: function (data, status, jqXHR) {
                    }
                });
            }
        }

    </script>
    <style type="text/css">
        .PagerCenter
        {
            text-align: center !important;
        }
    </style>
    <asp:UpdatePanel ID="WARPToolbar" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <AEL:ConfigurationToolbar ID="PersonalToolbar" runat="server" ClientSideEventButton="EditAction" />
            <asp:HiddenField ID="PersonObjectID" EnableViewState="true" runat="server" />
            <asp:HiddenField ID="facility" EnableViewState="true" runat="server" />
            <asp:HiddenField ID="BadgeID" EnableViewState="true" runat="server" />
            <asp:HiddenField ID="caObjectID" EnableViewState="true" runat="server" />
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="WebPanel1" />
        </Triggers>
    </asp:UpdatePanel>
    <table width="100%">
        <tr>
            <td>
                <ig:WebExplorerBar ID="WebPanel1" runat="server" Width="100%">
                    <ClientEvents ItemExpanded="itemExpanded" ItemCollapsed="itemCollapsed"></ClientEvents>
                    <Groups>
                        <ig:ExplorerBarGroup Expanded="true" Text="Search For Badges... (Click to Collapse...)">
                            <Items>
                                <ig:ExplorerBarItem>
                                    <Template>
                                        <asp:UpdatePanel ID="WARPSearch" runat="server" UpdateMode="Conditional">
                                            <ContentTemplate>
                                                <table width="100%">
                                                    <tr valign="bottom">
                                                        <td>
                                                            <table>
                                                                <tr>
                                                                    <td style="width: 65px; padding-left: 7px;" align="right">
                                                                        <asp:Label ID="lblSrcFacilityNo" runat="server" Text="Facility No"></asp:Label>
                                                                    </td>
                                                                    <td>
                                                                        <ig:WebTextEditor Width="140px" ID="srcFacility" Visible="true" runat="server">
                                                                            <ClientEvents KeyPress="txtNum_KeyPress" />
                                                                        </ig:WebTextEditor>
                                                                    </td>
                                                                    <td style="width: 65px; text-align: right;">
                                                                        <asp:Label ID="lblSrcBadgeForm" runat="server" Text="Badge No"></asp:Label>
                                                                    </td>
                                                                    <td>
                                                                        <ig:WebTextEditor Width="140px" ID="srcBadgeForm" runat="server">
                                                                            <ClientEvents KeyPress="txtNum_KeyPress" />
                                                                        </ig:WebTextEditor>
                                                                    </td>
                                                                    <td style="width: 85px; text-align: right;">
                                                                        <asp:CheckBox ID="srcChkBox" AutoPostBack="true" runat="server" Text="To" OnCheckedChanged="srcChkBox_CheckedChanged" />
                                                                    </td>
                                                                    <td style="padding-left: 2px;">
                                                                        <ig:WebTextEditor Width="140px" ID="srcBadgeTo" runat="server">
                                                                            <ClientEvents KeyPress="txtNum_KeyPress" />
                                                                        </ig:WebTextEditor>
                                                                    </td>
                                                                    <td style="width: 55px; text-align: right;">
                                                                        <asp:Label ID="lblField" runat="server" Text="Field"></asp:Label>
                                                                    </td>
                                                                    <td>
                                                                        <ig:WebDropDown ID="srcField" runat="server" Visible="true" DisplayMode="DropDown"
                                                                            Width="165px" EnableDropDownAsChild="false" OnValueChanged="srcField_ValueChanged"
                                                                            AutoPostBack="true">
                                                                        </ig:WebDropDown>
                                                                    </td>
                                                                    <td colspan="4">
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <td style="width: 65px; padding-left: 7px;" align="right">
                                                                        <asp:Label ID="lblSrcFrstName" runat="server" Text="First Name"></asp:Label>
                                                                    </td>
                                                                    <td>
                                                                        <ig:WebTextEditor Width="140px" ID="srcFrstName" Visible="true" runat="server">
                                                                        </ig:WebTextEditor>
                                                                    </td>
                                                                    <td style="width: 65px; text-align: right;">
                                                                        <asp:Label ID="lblSrcLastName" runat="server" Text="Last Name"></asp:Label>
                                                                    </td>
                                                                    <td>
                                                                        <ig:WebTextEditor Width="140px" ID="srcLastName" Visible="true" runat="server">
                                                                        </ig:WebTextEditor>
                                                                    </td>
                                                                    <td style="width: 85px; text-align: right;">
                                                                        <asp:Label ID="lblSrcGrpNo" runat="server" Text="Access Group"></asp:Label>
                                                                    </td>
                                                                    <td>
                                                                        <ig:WebDropDown ID="srcGrpNo" runat="server" Visible="true" DisplayMode="DropDown"
                                                                            Width="145px" EnableDropDownAsChild="false">
                                                                        </ig:WebDropDown>
                                                                    </td>
                                                                    <td style="width: 55px; text-align: right;">
                                                                        <asp:Label ID="lblFieldValue" runat="server" Text="Value"></asp:Label>
                                                                    </td>
                                                                    <td>
                                                                        <ig:WebDropDown ID="srcFieldValue" runat="server" Visible="true" DisplayMode="DropDown"
                                                                            Width="165px" EnableDropDownAsChild="false">
                                                                        </ig:WebDropDown>
                                                                    </td>
                                                                    <td>
                                                                        <igtxt:WebImageButton ID="btnFind" runat="server" Text="Search" OnClick="btnFind_Click" Width="80px">
                                                                            <ClientSideEvents Click="chkValidity" />
                                                                        </igtxt:WebImageButton>
                                                                    </td>
                                                                    <td>
                                                                        <igtxt:WebImageButton ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" Width="80px"/>
                                                                    </td>
                                                                    <td>
                                                                        <igtxt:WebImageButton ID="btnShowAll" runat="server" Text="Show All Badges" OnClick="btnShowAll_Click" Width="80px" />
                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        </td>
                                                        <td align="right" style="padding-bottom: 5px;">
                                                            <ig:WebDropDown ID="cboTimeFormat" runat="server" Visible="true" Width="120px" DisplayMode="DropDownList"
                                                                EnableDropDownAsChild="false" DropDownContainerHeight="60px" AutoPostBack="true"
                                                                OnSelectionChanged="cboTimeFormat_SelectedRowChangedEventArgs">
                                                            </ig:WebDropDown>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </ContentTemplate>
                                        </asp:UpdatePanel>
                                    </Template>
                                </ig:ExplorerBarItem>
                            </Items>
                        </ig:ExplorerBarGroup>
                    </Groups>
                </ig:WebExplorerBar>
            </td>
        </tr>
        <tr style="width: 100%">
            <td width="100%">
                <asp:UpdatePanel ID="WARPGrid" runat="server">
                    <ContentTemplate>
                        <AEL:BaseItemDisplayGrid ID="PersonalGrid" runat="server" ClientSideEvent="btnEnable"
                            DataKeyField="caObjectId" EnabledPaging="true" ClientSideEvent_DoubleClick="grd_DblClickHandler"
                            OnInitializeRow="PersonalGrid_OnInitializeRow" OnCustomDataBinding="PersonalGrid_CustomDataBinding" />
                    </ContentTemplate>
                </asp:UpdatePanel>
            </td>
        </tr>
    </table>
</asp:Content>
