<%@ Page Title="" Language="C#" MasterPageFile="~/masterpages/eDashboard.master" AutoEventWireup="true" CodeBehind="BuybackSettings.aspx.cs" Inherits="WebSite.site.HITS.Organization.BuybackSettings"
    Culture="auto" UICulture="auto" Theme="eMain" %>
<%@ Register Assembly="eSecuritel.HITS.UI.Components" Namespace="eSecuritel.HITS.UI.Components" TagPrefix="cc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="eContent" runat="server">

    <link href="<%=ResolveClientUrl("~/style/ModelMatrixForCarriers/css/jquery-ui-1.8.18.custom.css")%>" rel="stylesheet" />
    <link href="<%=ResolveClientUrl("~/style/ModelMatrixForCarriers/css/style.css")%>" rel="stylesheet" />

    <script type="text/javascript" src="<%=ResolveClientUrl("~/script/ModelMatrixForCarriers/js/jquery-ui.js")%>"></script>
    <script language="javascript" type="text/javascript">

        function ShowLoader() {
            $("#loading_dialog").loading();
        }

        function IsPageValid() {
            if (Page_ClientValidate()) {
                ShowLoader();
            }
        }

        function CloseLoader() {
            $("#loading_dialog").loading("loadStop");
        }

        function RowCanBeDeleted(canBeDeleted) {
            if (canBeDeleted) {
                ShowLoader();
            }
            else {
                alert("<%= Resources.AdministrationManagement.ErrorMsgCantBeDeleted %>");
                return false;
            }
        }

        (function ($) {

            $.widget("artistan.loading", $.ui.dialog, {
                options: {
                    // your options
                    spinnerClassSuffix: 'spinner',
                    spinnerHtml: "<%= Resources.ModelMatrixManagement.LabelLoading %>",// allow for spans with callback for timeout...
                    maxHeight: true,
                    maxWidth: false,
                    minHeight: 80,
                    minWidth: 220,
                    height: 80,
                    width: 220,
                    modal: true

                },
                _create: function () {
                    $.ui.dialog.prototype._create.apply(this);
                    // constructor
                    $(this.uiDialog).children('*').hide();
                    var self = this,
                    options = self.options;
                    self.uiDialogSpinner = $('.ui-dialog-content', self.uiDialog).html(options.spinnerHtml).addClass('ui-dialog-' + options.spinnerClassSuffix);
                },
                _setOption: function (key, value) {
                    var original = value;
                    $.ui.dialog.prototype._setOption.apply(this, arguments);
                    // process the setting of options
                    var self = this;

                    switch (key) {
                        case "innerHeight":
                            // remove old class and add the new one.
                            self.uiDialogSpinner.height(value);
                            break;
                        case "spinnerClassSuffix":
                            // remove old class and add the new one.
                            self.uiDialogSpinner.removeClass('ui-dialog-' + original).addClass('ui-dialog-' + value);
                            break;
                        case "spinnerHtml":
                            // convert whatever was passed in to a string, for html() to not throw up
                            self.uiDialogSpinner.html("" + (value || '&#160;'));
                            break;
                    }
                },
                _size: function () {
                    $.ui.dialog.prototype._size.apply(this, arguments);
                },
                // other methods
                loadStart: function (newHtml) {
                    if (typeof (newHtml) != 'undefined') {
                        this._setOption('spinnerHtml', newHtml);
                    }
                    this.open();
                },
                loadStop: function () {
                    this._setOption('spinnerHtml', this.options.spinnerHtml);
                    this.close();
                }
            });
        })(jQuery);
    </script>

    <asp:LinkButton ID="lbtnBackToCarrierDetails" runat="server" Text="<%$ Resources: OrganizationManagement, Back2CarrierDetailsLabel %>" OnClick="lbtnBackToCarrierDetails_Click"></asp:LinkButton>

    <asp:UpdatePanel ID="updPnlBuyback" runat="server" UpdateMode="Always" style="margin-top: 15px;">
        <ContentTemplate>
            <div id="divDeprecationGroupSettings">
                <h2>
                    <asp:Literal runat="server" Text="<%$ Resources: AdministrationManagement, BuybackDeprecationGroupsTitle %>" />
                </h2>
                <asp:ValidationSummary ID="validationSummaryConfiguration" runat="server" ShowSummary="true" DisplayMode="BulletList" Width="99%" ValidationGroup="InsertRequiredGroupName" />
                <asp:ValidationSummary ID="validationSummaryConfigurationEdit" runat="server" ShowSummary="true" DisplayMode="BulletList" Width="99%" ValidationGroup="EditRequiredGroupName" />
                <div class="greyblock">
                    <cc1:HitsSearchGridView ID="gvBuybackConfiguration" runat="server" DataKeyNames="ID" AllowPaging="true" OnRowUpdating="gvBuybackConfiguration_RowUpdating" DataSourceID="objDataSrcBuybackConfiguration" AutoGenerateColumns="false" OnRowDataBound="gvBuybackConfiguration_RowDataBound" 
                         CustomPagerID="HitsPager1" ShowFooter="true" OnSelectedIndexChanging="gvBuybackConfiguration_SelectedIndexChanging" PageSize="10" OnSelectedIndexChanged="gvBuybackConfiguration_SelectedIndexChanged" 
                        OnCustomPageIndexChanging="gvBuybackConfiguration_PageIndexChanging" OnRowCommand="gvBuybackConfiguration_RowCommand">
                        <Columns>
                            
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    <%= Resources.AdministrationManagement.BuybackGroupName %>
                                </HeaderTemplate>
                                <ItemStyle Width="30%" HorizontalAlign="Center" />
                                <ItemTemplate>
                                    <asp:LinkButton ID="lbtnGroupName" runat="server" CommandName="Select" OnClientClick="ShowLoader();" Text='<%# Bind("GroupName") %>' OnClick="lbtnGroupName_Click" CommandArgument='<%# Eval("ID") %>' ToolTip='<%# (int)((HITSWebService.BuybackConfigurationSearchData)Container.DataItem).Status %>' />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtGroupName" runat="server" Text='<%# Bind("GroupName") %>' />  
                                    <asp:RequiredFieldValidator ID="rfvEditGroupName" runat="server" Display="None" ControlToValidate="txtGroupName" ValidationGroup="EditRequiredGroupName" ErrorMessage='<%# Resources.AdministrationManagement.ErrorMsgReqField %>'></asp:RequiredFieldValidator>                                  
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtInsertGroupName" runat="server" />
                                    <asp:RequiredFieldValidator ID="rfvInsertGroupName" runat="server" Display="None" ControlToValidate="txtInsertGroupName" ValidationGroup="InsertRequiredGroupName" ErrorMessage='<%# Resources.AdministrationManagement.ErrorMsgReqField %>'></asp:RequiredFieldValidator>
                                </FooterTemplate>
                            </asp:TemplateField>

                            <asp:BoundField DataField="Status" HeaderText="<%$ Resources: AdministrationManagement, BuybackStatusName %>" ItemStyle-Width="30%" ItemStyle-HorizontalAlign="Center" ReadOnly="true" />

                            <asp:TemplateField>
                                <ItemStyle Width="10" HorizontalAlign="Center" />
                                <ItemTemplate>
                                    <asp:LinkButton ID="btnEdit" runat="server" CommandName="Edit" Text='<%# Resources.AdministrationManagement.EditLinkButton %>' />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:LinkButton ID="btnUpdate" runat="server" OnClientClick="IsPageValid();" CommandName="Update" CausesValidation="true" ValidationGroup="EditRequiredGroupName" Text='<%# Resources.AdministrationManagement.ButtonUpdate %>' />&nbsp;<asp:LinkButton ID="btnCancel" runat="server" CommandName="Cancel" Text='<%# Resources.AdministrationManagement.CancelLinkButton %>' />
                                </EditItemTemplate>
                            </asp:TemplateField>

                              <asp:TemplateField>
                                <ItemStyle Width="10" HorizontalAlign="Center" />
                                <ItemTemplate>
                                    <asp:LinkButton ID="btnDelete" runat="server" OnClientClick='<%# "return RowCanBeDeleted(" + Eval("CanBeDeleted").ToString().ToLower() + ");" %>' CommandName="Delete" Text='<%# Resources.AdministrationManagement.DeleteLinkButton %>' />
                                </ItemTemplate>
                                <FooterStyle Width="10" HorizontalAlign="Center" />
                                <FooterTemplate>
                                    <asp:LinkButton ID="btnAdd" runat="server" OnClientClick="IsPageValid();" CommandName="Add" CausesValidation="true" ValidationGroup="InsertRequiredGroupName" Text='<%# Resources.AdministrationManagement.AddLinkButton %>' />
                                </FooterTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField>
                                <ItemStyle Width="10" HorizontalAlign="Center" />
                                <ItemTemplate>
                                    <asp:LinkButton ID="btnLock" runat="server"  OnClientClick="ShowLoader();" OnClick="btnLock_Click" CommandName="Update" Text='<%# Resources.AdministrationManagement.LockLinkButton %>' Visible='<%# (int)Eval("Status") != (int)HITSWebService.DepreciationGroupStatus.Locked && ((HITSWebService.BuybackConfigurationSearchData)Container.DataItem).DetailsTotalRows >= 2 %>' CommandArgument='<%# Eval("ID") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>

                        </Columns>

                        <PagerTemplate>
                            <cc1:HitsPager ID="HitsPager1" runat="server" GoButtonText="<%$ Resources: Default, GoButtonText %>" />
                        </PagerTemplate>
                        <PagerSettings Mode="NumericFirstLast" />
                        <RowStyle HorizontalAlign="Left" />
                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Top" />
                        <EditRowStyle HorizontalAlign="Left" VerticalAlign="Top" />

                    </cc1:HitsSearchGridView>
                </div>

                <asp:Panel ID="pnlShowGroupDetail" runat="server" Visible="false">
                 <h2>
                    <asp:Literal runat="server" Text="<%$ Resources: AdministrationManagement, BuybackGroupDetailTitle %>" />
                </h2>
                    <asp:ValidationSummary ID="validationSummaryGroupDetails" runat="server" ShowSummary="true" DisplayMode="BulletList" Width="99%" ValidationGroup="GroupDetails" />
                    <asp:ValidationSummary ID="validationSummaryGroupDetailsEdit" runat="server" ShowSummary="true" DisplayMode="BulletList" Width="99%" ValidationGroup="GroupDetailsEdit" />
                <div class="greyblock">
                    <asp:HiddenField ID="hdnGroupID" runat="server" />
                    <asp:HiddenField ID="hdnIsLocked" runat="server" />
                    <asp:GridView ID="grdGroupDetails" runat="server" AutoGenerateColumns="False" DataKeyNames="DeprecationGroupDetailID,GroupID" RowStyle-VerticalAlign="Top" Width="100%" DataSourceID="objDataSrcBuybackGroupDetail" ShowFooter="true" OnRowDataBound="grdGroupDetails_RowDataBound" OnRowCommand="grdGroupDetails_RowCommand" OnPreRender="grdGroupDetails_PreRender">
                        <Columns>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    <%= Resources.AdministrationManagement.BuybackDeprecationCondition %>
                                </HeaderTemplate>
                                <ItemStyle Width="30%" HorizontalAlign="Center" />
                                <ItemTemplate>
                                    <asp:Literal ID="litDepCondition" runat="server" Text='<%# Eval("DeprecationConditionDesc") %>' />
                                    
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:DropDownList ID="ddlEditGroupDetailDeprecationCondition" runat="server" OnInit="ddlEditGroupDetailDeprecationCondition_Init" DataTextField="Description" DataValueField="ID" SelectedValue='<%# Bind("DeprecationConditionId") %>'></asp:DropDownList>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:DropDownList ID="ddlInsertGroupDetailDeprecationCondition" runat="server" OnInit="ddlEditGroupDetailDeprecationCondition_Init" DataTextField="Description" DataValueField="ID"></asp:DropDownList>
                                </FooterTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField>
                                <HeaderTemplate>
                                    <%= Resources.AdministrationManagement.BuybackDeprecationPeriod %>
                                </HeaderTemplate>
                                <ItemStyle Width="30%" HorizontalAlign="Center" />
                                <ItemTemplate>
                                    <asp:Literal ID="litDepPeriod" runat="server" Text='<%# Eval("DeprecationPeriod") %>' />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtEditGroupDetailDeprecationPeriod" runat="server"  Text='<%# Bind("DeprecationPeriod") %>'></asp:TextBox>
                                    <asp:RegularExpressionValidator ID="cvOnlyNumbersAllowedEdit" ControlToValidate="txtEditGroupDetailDeprecationPeriod" runat="server" Display="None" ValidationGroup="GroupDetailsEdit" ErrorMessage='<%# Resources.AdministrationManagement.ErrorMsgOnlyNumbers %>' ValidationExpression="\d+"></asp:RegularExpressionValidator>
                                     <asp:CustomValidator ID="cvEditDepricationValidation" runat="server" ValidationGroup="GroupDetailsEdit" ValidateEmptyText="true" Display="None" ControlToValidate="txtEditGroupDetailDeprecationPeriod" OnServerValidate="cvDepricationValidation_ServerValidate" ErrorMessage='<%# Resources.AdministrationManagement.ErrorMsgDepricationPeriod %>' />
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtInsertGroupDetailDeprecationPeriod" runat="server"></asp:TextBox>
                                    <asp:RegularExpressionValidator ID="cvOnlyNumbersAllowed" ControlToValidate="txtInsertGroupDetailDeprecationPeriod" runat="server" Display="None" ValidationGroup="GroupDetails" ErrorMessage='<%# Resources.AdministrationManagement.ErrorMsgOnlyNumbers %>' ValidationExpression="\d+"></asp:RegularExpressionValidator>
                                    <asp:CustomValidator ID="cvDepricationValidation" runat="server" ValidationGroup="GroupDetails" ValidateEmptyText="true" Display="None" ControlToValidate="txtInsertGroupDetailDeprecationPeriod" OnServerValidate="cvDepricationValidation_ServerValidate" ErrorMessage='<%# Resources.AdministrationManagement.ErrorMsgDepricationPeriod %>' />
                                </FooterTemplate>
                            </asp:TemplateField>
                            
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    <%= Resources.AdministrationManagement.BuybackDeprecationPercentage %>
                                </HeaderTemplate>
                                <ItemStyle Width="30%" HorizontalAlign="Center" />
                                <ItemTemplate>
                                    <%# Eval("DeprecationPercentageDesc") %>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:DropDownList ID="ddlEditGroupDetailDeprecationPercentage" runat="server" OnInit="ddlEditGroupDetailDeprecationPercentage_Init" DataTextField="Description" DataValueField="ID" SelectedValue='<%# Bind("PercentageID") %>'></asp:DropDownList>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:DropDownList ID="ddlInsertGroupDetailDeprecationPercentage" runat="server" OnInit="ddlEditGroupDetailDeprecationPercentage_Init" DataTextField="Description" DataValueField="ID"></asp:DropDownList>
                                </FooterTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField>
                                <ItemStyle Width="10" HorizontalAlign="Center" />
                                <ItemTemplate>
                                    <asp:LinkButton ID="btnGroupDetailEdit" runat="server" CommandName="Edit" Text='<%# Resources.AdministrationManagement.EditLinkButton %>' />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:LinkButton ID="btnGroupDetailUpdate" runat="server" OnClientClick="IsPageValid();" CausesValidation="true" ValidationGroup="GroupDetailsEdit" CommandName="Update" Text='<%# Resources.AdministrationManagement.ButtonUpdate %>' />&nbsp;<asp:LinkButton ID="btnGroupDetailCancel" runat="server" CommandName="Cancel" Text='<%# Resources.AdministrationManagement.CancelLinkButton %>' />
                                </EditItemTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField>
                                <ItemStyle Width="10" HorizontalAlign="Center" />
                                <ItemTemplate>
                                    <asp:LinkButton ID="btnGroupDetailDelete" runat="server" OnClientClick="ShowLoader();" CommandName="Delete" Text='<%# Resources.AdministrationManagement.DeleteLinkButton %>' />
                                </ItemTemplate>
                                <FooterStyle Width="10" HorizontalAlign="Center" />
                                <FooterTemplate>
                                    <asp:LinkButton ID="btnGroupDetailAdd" runat="server" OnClientClick="IsPageValid();" CausesValidation="true" ValidationGroup="GroupDetails" CommandName="Add" Text='<%# Resources.AdministrationManagement.AddLinkButton %>' />
                                </FooterTemplate>
                            </asp:TemplateField>
                        </Columns>
                </asp:GridView>
                </div>
                </asp:Panel>
            </div>

            <asp:ObjectDataSource ID="objDataSrcBuybackGroupDetail" runat="server" SelectMethod="SelectGroupDetails" DataObjectTypeName="HITSWebService.BuybackGroupDetailSearchData" UpdateMethod="UpdateGroupDetails"
                 InsertMethod="InsertGroupDetails" DeleteMethod="DeleteDeprecationGroupDetail" TypeName="BuybackSettingsDataSource" OnSelected="objDataSrcBuybackGroupDetail_Selected" OnInserting="objDataSrcBuybackGroupDetail_Inserting">
                <UpdateParameters>
                    <asp:Parameter Name="editedData" Type="Object" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="newData" Type="Object" />
                </InsertParameters>
            </asp:ObjectDataSource>

            <asp:ObjectDataSource ID="objDataSrcBuybackConfiguration" runat="server" SelectMethod="SelectDeprecationGroups" SelectCountMethod="SelectDeprecationGroupsCount" UpdateMethod="UpdateGroup" InsertMethod="InsertGroup" DeleteMethod="DeleteDeprecationGroup"
                TypeName="BuybackSettingsDataSource" DataObjectTypeName="HITSWebService.BuybackConfigurationSearchData" EnablePaging="True" OnInserting="objDataSrcBuybackConfiguration_Inserting">
                <SelectParameters>
                    <asp:ControlParameter ControlID="gvBuybackConfiguration" Name="startRowIndex" DefaultValue="1" PropertyName="PageIndex" Type="Int32" />
                    <asp:Parameter DefaultValue="10" Name="maximumRows" Type="Int32" />
                    <asp:SessionParameter Name="companySortExpression" SessionField="CompanySortExpression" Type="String" />
                </SelectParameters>
                <UpdateParameters>
                    <asp:Parameter Name="editedData" Type="Object" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="newData" Type="Object" />
                </InsertParameters>
            </asp:ObjectDataSource>

        </ContentTemplate>
    </asp:UpdatePanel>
    <div id="loading_dialog"></div>

</asp:Content>
