<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.db.DataDB" %>
<%@ page import="ro.emid.common.types.DataObj" %>
<%@ page import="ro.emid.common.db.LFInfoAccessDB" %>
<%@ page import="java.util.List" %>
<%@ page import="ro.emid.common.types.Category" %>
<%@ page import="ro.emid.common.constants.*" %>
<%@ page import="ro.emid.Application" %>
<%@ page import="ro.emid.common.utils.*" %>

<%@ taglib prefix="emid" tagdir="/WEB-INF/tags" %>

<%
    String sortColumn = ObjectUtils.getStringValue(request.getParameter("sortColumn"));
    if( sortColumn == null || sortColumn.length() == 0) sortColumn = "CE";
    String sortType   = ObjectUtils.getStringValue(request.getParameter("sortType"));
    if( sortType == null || sortType.length() == 0) sortType = "ASC";

    FSC fscRaport = FSCUtils.getFsc(request);

    final long filtruFilialaId = fscRaport.getFilialaId();
    final long selectedSucursalaId = fscRaport.getSucursalaId();
    final long centruId = fscRaport.getCentruId();

    Category filiala = CategoryAccessDB.getById(filtruFilialaId);
    if (filiala == null) filiala = new Category();

    final int selectedTipNomenclator = ObjectUtils.getIntValue(request.getParameter(NomConstants.TIP), NomConstants.NOM_STATII);

    String name = "";
    switch (selectedTipNomenclator){
        case NomConstants.NOM_STATII : name = "Statii"; break;
        case NomConstants.NOM_LINII: name = "Linii"; break;
        case NomConstants.NOM_PA_PT: name = "PA-PT"; break;
        case NomConstants.NOM_CELULE: name = "Celule"; break;
        case NomConstants.NOM_BARE: name = "Bare"; break;
        case NomConstants.NOM_BOBINE: name = "Bobine"; break;
        case NomConstants.NOM_TRAFO: name = "Trafo"; break;
        case NomConstants.NOM_DERIVATIE: name = "Derivatii"; break;
    }

    String nomName = "";
    switch (selectedTipNomenclator){
        case NomConstants.NOM_STATII : nomName = "Statii";break;
        case NomConstants.NOM_LINII : nomName = "Linii";break;
        case NomConstants.NOM_PA_PT : nomName = "PA-PT";break;
        case NomConstants.NOM_BARE : nomName = "Bare";break;
        case NomConstants.NOM_CELULE : nomName = "Celule";break;
        case NomConstants.NOM_BOBINE : nomName = "Bobine";break;
        case NomConstants.NOM_TRAFO : nomName = "Trafo";break;
        case NomConstants.NOM_DERIVATIE : nomName = "Derivatii";break;
        default:
            nomName = "";
    }


    Category sucursalaRaport = CategoryAccessDB.getById(selectedSucursalaId);
    if( sucursalaRaport == null) sucursalaRaport  = new Category();

    Category centru = CategoryAccessDB.getById(centruId);
    if (centru == null) centru = new Category();

    String condSql = "";
    if (centru.getId() != -1 && centru.getCodSap() != null) {
        condSql += " AND CE = '" + centru.getCodSap().trim() + '\'';
    } else
    if (sucursalaRaport.getId() != -1) {
        condSql += " AND ULPI ='" + sucursalaRaport.getCodSap() + "' ";
    }else if( filiala.getId() > 0) {
        final StringBuffer codes = new StringBuffer();
        final Category[] categories = CategoryAccessDB.getAllCategoriesByParentId( filiala.getId());
        for (int i = 0; i < categories.length; i++) {
            if (categories[i].getCodSap() != null) {
                codes.append('\'').append(categories[i].getCodSap().trim()).append('\'').append(i < categories.length - 1 ? "," : "");
            }
        }
        condSql += " AND ULPI IN ( " + codes + ')';
    }

    final String cod = ObjectUtils.getStringValue( request.getParameter("cod"));
    if( cod != null && cod.trim().length() > 0){

        final String _cod = cod.trim().replace(' ', '%');

        if( DataDB.isLocFunctional( selectedTipNomenclator)){
            condSql += " AND upper(lf.COD_LF) LIKE upper('%" + _cod + "%')";
        }else{
            condSql += " AND upper(eq.NR_EQ) LIKE upper('%" + _cod + "%')";
        }
    }

    String codEMID = ObjectUtils.getStringValue( request.getParameter("codEMID"));

    if (codEMID != null && codEMID.trim().length() >0){

        String _codEMID = codEMID.trim().replace(' ', '%');

        if( DataDB.isLocFunctional( selectedTipNomenclator)){
            condSql += " AND (lf.tip_producator like upper('%" + _codEMID + "%') OR lf.COD_OPEN_2 like upper('%\" + _codEMID + \"%')" ;
            condSql += " OR lf.COD_OPEN_3 like upper('%" + _codEMID + "%') OR lf.COD_OPEN_4 like upper('%" + _codEMID + "%'))" ;
        }
    }

    //for pagination{
    if (!"nomSap".equals(SessionUtils.getPaginationJsp(session))) {
        SessionUtils.resetPaginationData(session, "nomSap");
    }

    long nrLines = ObjectUtils.getLongValue(request.getParameter("nrLines"), 15);
    if (nrLines == -1) nrLines = SessionUtils.getNrLines(session);

    long currentBeginIndex = ObjectUtils.getLongValue(request.getParameter("currentBeginIndex"), -1);
    if (currentBeginIndex == -1) currentBeginIndex = SessionUtils.getBeginIndex(session);

    final long nrLinesTotal = DataDB.getCount(selectedTipNomenclator, condSql.toString());
    final String direction = ObjectUtils.getStringValue(request.getParameter("direction"));
    if ("backward".equals(direction)) {
        currentBeginIndex -= nrLines;
    } else if ("forward".equals(direction)) {
        currentBeginIndex += nrLines;
    } else if( "first".equals( direction)){
        currentBeginIndex = 1;
    } else if( "last".equals( direction)){
        currentBeginIndex =  nrLines * nrLinesTotal/nrLines + 1;
    }

    if (currentBeginIndex > nrLinesTotal && nrLinesTotal > 1)
        currentBeginIndex -= nrLines; //cazul cind se sterge ultimul element de pe ultima pagina si este singurul pe pagina
    if (currentBeginIndex < 1) currentBeginIndex = 1;

    SessionUtils.setNrLines(session, nrLines);
    SessionUtils.setBeginIndex(session, currentBeginIndex);
    //}for pagination

    final List<DataObj> noms = DataDB.getAllByType(selectedTipNomenclator, condSql, sortColumn, sortType, (int) currentBeginIndex, (int)nrLines);

    final int nrLinesSelected = noms.size();
%>

<form name="zaforma" action="<%=AppConstants.APP_NAME%>?action=show-nomenclator-instalatii" method="post">
    <input type="hidden" name="run_report" value="1"/>

    <div class="mt5 mb5">
        <div class="pull-left">
            <emid:filtru-localizare hideLabels="true"/>
            <label class="ml10">
                Nomenclator:
                <select name="<%=NomConstants.TIP%>" id="subs" class="gSAPSelect" onchange="switchSyncNom()">
                    <option value="<%=NomConstants.NOM_STATII %>" <%=selectedTipNomenclator == NomConstants.NOM_STATII ? "selected" : ""%> >Statii</option>
                    <option value="<%=NomConstants.NOM_LINII%>" <%=selectedTipNomenclator == NomConstants.NOM_LINII ? "selected" : ""%> >Linii</option>
                    <option value="<%=NomConstants.NOM_PA_PT%>" <%=selectedTipNomenclator == NomConstants.NOM_PA_PT ? "selected" : ""%> >PA-PT</option>
                    <option value="<%=NomConstants.NOM_CELULE%>" <%=selectedTipNomenclator == NomConstants.NOM_CELULE ? "selected" : ""%> >Celule</option>
                    <option value="<%=NomConstants.NOM_BARE%>" <%=selectedTipNomenclator == NomConstants.NOM_BARE ? "selected" : ""%> >Bare</option>
                    <option value="<%=NomConstants.NOM_BOBINE%>" <%=selectedTipNomenclator == NomConstants.NOM_BOBINE ? "selected" : ""%> >Bobine</option>
                    <option value="<%=NomConstants.NOM_TRAFO%>" <%=selectedTipNomenclator == NomConstants.NOM_TRAFO ? "selected" : ""%> >Trafo</option>
                    <option value="<%=NomConstants.NOM_DERIVATIE%>" <%=selectedTipNomenclator == NomConstants.NOM_DERIVATIE ? "selected" : ""%> >Derivatii</option>
                </select>
            </label>

            <label class="ml10">
                Cod / Nr.: <input type="text" name="cod" value="<%=AppConstants.format(cod)%>" class="gsapinputtext"/>
            </label>

            <label class="ml10">
                Cod EMID: <input type="text" name="codEMID" value="<%=AppConstants.format(codEMID)%>" class="gsapinputtext"/>
            </label>

            <input type="submit" value="Afişează" class="ml5"/>
        </div>

        <div class="pull-right">
            <% final int currentYear = ro.emid.common.utils.Util.getCurrentYear();%>
            <% if(centru.getId() > 0 && LFInfoAccessDB.hasDateLFInfoInYear(currentYear - 1, centru.getCodSap())){  %>
            <a class="link" title="Copiere date cu numarul de consumatori din anul <%=currentYear-1%>"  href="javascript: void( null)" onclick=" copyLfInforFromLastYear(); return false;">Copie datele din anul <%=currentYear - 1%></a>
            <%}%>
        </div>
    </div>
    <div class="spacer mt5" style="clear: both"></div>
    <table border=0 cellpadding=2 cellspacing=1 width="100%" class="table">
        <thead>
            <tr>
                <%if( DataDB.isLocFunctional( selectedTipNomenclator)){%>
                    <th nowrap width=80>&nbsp;<a href="#" onclick="order('CE', '<%=(sortColumn.equals("CE") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>');return false;" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Cod CE</a>&nbsp; <% if(sortColumn.equals("CE")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%> </th>
                    <th nowrap>&nbsp;<a href="#" onclick="order('COD_LF', '<%=(sortColumn.equals("COD_LF") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>');return false;" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Cod LF</a>&nbsp; <% if(sortColumn.equals("COD_LF")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%> </th>
                    <th nowrap>Denumire</th>
                    <% if( !filiala.isMunteniaSud() || (filiala.isMunteniaSud() && (selectedTipNomenclator == NomConstants.NOM_PA_PT || selectedTipNomenclator == NomConstants.NOM_STATII))){%>
                        <th nowrap>Cod EMID</th>
                    <%}%>
                    <th nowrap>Tip</th>
                    <% if ( selectedTipNomenclator == NomConstants.NOM_PA_PT ){ %>
                        <th nowrap>Putere</th>
                    <%}%>
                    <% if ( selectedTipNomenclator == NomConstants.NOM_PA_PT || selectedTipNomenclator == NomConstants.NOM_STATII ){ %>
                        <th nowrap>Fabricant</th>
                    <%}%>
                    <th nowrap>Nr. consumatori</th>
                <%}else{%>
                    <th nowrap width=80>&nbsp;<a href="#" onclick="order('CE', '<%=(sortColumn.equals("CE") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>');return false;" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Cod CE</a>&nbsp; <% if(sortColumn.equals("CE")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%> </th>
                    <th nowrap>&nbsp;<a href="#" onclick="order('COD_LF', '<%=(sortColumn.equals("COD_LF") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>');return false;" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Cod LF</a>&nbsp; <% if(sortColumn.equals("COD_LF")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%> </th>
                    <th nowrap>Nr. Echipament</th>
                    <th nowrap>Denumire</th>
                    <th nowrap>Tip</th>
                    <% if( selectedTipNomenclator == NomConstants.NOM_DERIVATIE && filiala.isMunteniaSud()){ %>
                        <th nowrap>Cod EMID</th>
                    <%}%>
                    <th nowrap>An PIF</th>
                    <th nowrap>Luna PIF</th>
                <%}%>
                <th nowrap width=50>&nbsp;<a href="#" onclick="order('VERSION', '<%=(sortColumn.equals("VERSION") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>');return false;" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">V.</a>&nbsp; <% if(sortColumn.equals("VERSION")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%> </th>
                <th nowrap width=70>&nbsp;<a href="#" onclick="order('STERS', '<%=(sortColumn.equals("STERS") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>');return false;" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Stare</a>&nbsp; <% if(sortColumn.equals("STERS")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%> </th>
                <th width=100>&nbsp;<a href="#" onclick="order('DATA_UPDATE', '<%=(sortColumn.equals("DATA_UPDATE") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>');return false;" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Data actualizare</a>&nbsp; <% if(sortColumn.equals("DATA_UPDATE")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%> </th>
            </tr>
        </thead>
        <tbody>
            <% if( noms == null || noms.size() == 0){  %>
                <tr>
                    <td colspan="10" class="no-data">
                        Nu exista date.
                    </td>
                </tr>
            <% } else for (int i=0; i<noms.size();i++) { %>
                <%  DataObj nom  = noms.get(i); %>
                <tr id="tr_text<%=i%>" valign="top" <%if(nom.esteSters()){%> class="gray-text" <%}%>>
                    <%if( DataDB.isLocFunctional( selectedTipNomenclator)){%>
                        <td  align="left" title="<%= nom.getCodCE() == null ?
                         "" : CategoryAccessDB.getByCode(nom.getCodCE()) == null ?
                         "" : CategoryAccessDB.getByCode(nom.getCodCE()).getName() == null ?
                          "" : CategoryAccessDB.getByCode(nom.getCodCE()).getName()  %>">
                            <%=AppConstants.format(nom.getCodCE())%>
                        </td>
                        <td>
                            <%=AppConstants.format(nom.getCod())%>
                        </td>
                        <td  align="left">
                            <%=AppConstants.format(nom.getNume())%>
                        </td>
                        <%if( filiala.isMunteniaSud()){%>
                            <%if(selectedTipNomenclator == NomConstants.NOM_PA_PT || selectedTipNomenclator == NomConstants.NOM_STATII){%>
                                <!-- Cod identic este si penru derivatiile la Muntenia Sud, (un pic mai jos)-->
                                <td ondblclick="inlineStartEditCodEmid('newCodEmid<%=i%>', 'spanText<%=i%>')" title="Dublu click pentru editare">
                                    <span>
                                        <input class="gsapinputtext" size="10" codLF="<%=nom.getCod()%>" type="text" id="newCodEmid<%=i%>" style="display: none" />
                                        <span id="spanText<%=i%>">
                                            <%=AppConstants.format(nom.getTipProducator())%>
                                        </span>
                                    </span>
                                </td>
                            <%}%>
                        <%}else{%>
                            <td>
                                <%=AppConstants.format(nom.getTipProducator())%>
                                <% if( !StringUtils.isZeroLength(nom.getCodOpen2())){ %>
                                    <br/> <%=nom.getCodOpen2()%>
                                <% } %>
                                <% if( !StringUtils.isZeroLength(nom.getCodOpen3())){ %>
                                    <br/> <%=nom.getCodOpen3()%>
                                <% } %>
                                <% if( !StringUtils.isZeroLength(nom.getCodOpen4())){ %>
                                    <br/> <%=nom.getCodOpen4()%>
                                <% } %>
                            </td>
                        <%}%>
                        <td>
                            <%=AppConstants.format(nom.getTypeStr())%>
                        </td>
                        <% if ( selectedTipNomenclator == NomConstants.NOM_PA_PT ){ %>
                            <td >
                                <%=AppConstants.format(nom.getNrPiesaProducator())%>
                            </td>
                        <%}%>
                        <% if ( selectedTipNomenclator == NomConstants.NOM_PA_PT || selectedTipNomenclator == NomConstants.NOM_STATII ){ %>
                            <td >
                                <%=AppConstants.format(nom.getFabricant())%>
                            </td>
                        <%}%>

                        <td  nowrap align="right">

                            <%if( nom.getLastYearCompletareNrConsumatori() > 0 ){%>
                                <span style="font-size:10px">
                                    <%=nom.getLastYearCompletareNrConsumatori()%>
                                </span>
                                <%if( nom.getLastYearCompletareNrConsumatori() == currentYear){%>
                                    <img id="checkImg<%=nom.getCod()%>" src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/checked_green.gif" alt="S-a completat numaul de consumatori pentru anul <%=nom.getLastYearCompletareNrConsumatori()%>" border="0" />
                                <%}else{%>
                                    <img id="checkImg<%=nom.getCod()%>" src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/checked_grayed.gif"  alt="S-a completat numaul de consumatori pentru anul <%=nom.getLastYearCompletareNrConsumatori()%>" border="0" />
                                <%}%>
                            <%}else{%>
                                <span style="font-size:10px">N/A</span>
                                <img id="checkImg<%=nom.getCod()%>" src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/checked_green.gif" alt="S-a completat numaul de consumatori pentru anul <%=currentYear%>" border="0" style="visibility: hidden;" />
                            <%}%>

                            <a href="javascript: void( null)" class="link" title="Editeaza Numarul de consumatori" onclick="checkImg = 'checkImg<%=nom.getCod()%>'; editNumarConsumatori('<%= nom.getType() %>', '<%=ro.emid.common.utils.Util.escapeToJs(  nom.getCod())%>'); return false;">Alte Informatii</a>
                        </td>
                    <%}else{%>
                        <td align="left">
                            <%=AppConstants.format(nom.getCodCE())%>
                        </td>
                        <td>
                            <%=AppConstants.format(nom.getCodLocFunctional())%>
                        </td>
                        <td >
                            <%try{%>
                                <%=AppConstants.format(Long.parseLong(nom.getCod()))%>
                            <%}catch(Exception e){}%>
                        </td>
                        <td  align="left">
                            <%=AppConstants.format(nom.getNume())%>
                        </td>
                        <td>
                            <%=AppConstants.format(nom.getTypeStr())%>
                        </td>
                        <% if( selectedTipNomenclator == NomConstants.NOM_DERIVATIE && filiala.isMunteniaSud()){ %>
                            <!-- Identic ca la STATII si POSTURI (mai sus) -->
                            <td ondblclick="inlineStartEditCodEmid('newCodEmid<%=i%>', 'spanText<%=i%>')" title="Dublu click pentru editare">
                                <span>
                                    <input class="gsapinputtext" size="10" codLF="<%=nom.getCod()%>" type="text" id="newCodEmid<%=i%>" style="display: none" />
                                    <span id="spanText<%=i%>">
                                        <%=AppConstants.format(nom.getTipProducator())%>
                                    </span>
                                </span>
                            </td>
                        <%}%>
                        <td align="right">
                            <%=AppConstants.format(nom.getAnConstructie())%>
                        </td>
                        <td align="right">
                            <%=AppConstants.format(nom.getLunaConstructie())%>
                        </td>
                    <%}%>
                    <td align="right"><%=nom.getVersion()%></td>
                    <td align="center"><%=nom.getSters() == 1 ? "Sters" : "Activ"%></td>
                    <td align="center"><%=AppConstants.formatDataTime(nom.getDataUpdate())%></td>
                </tr>
            <%}%>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="12" >
                    <div class="pull-left">
                        <% if(!Application.runAsAparatCentral()){ %>
                        <input type="button" value="Sincronizare nomenclator <%=nomName%>" onclick="doSyncronizareBDI( this)" title="Start sincronizare nomenclator <%=nomName%>"/>&nbsp;
                        <input type="button" value="Sincronizare completă" onclick="doSyncronizareBDIAll( this)" title="Start sincronizare completă nomenclatoare"/>&nbsp;
                        <input type="button" value="Istoric sincronizări" data-href="<%=AppConstants.APP_NAME%>?action=sync-bdi-history" title="Deschide pagina istoric sincronizări nomenclator"/>
                        <% } %>
                    </div>
                    <div class="pull-right">
                        <input type="hidden" name="currentBeginIndex" value="<%=currentBeginIndex%>">
                        <input type="hidden" name="direction" value="">
                        <input type="hidden" name="sortColumn" value="<%=sortColumn%>">
                        <input type="hidden" name="sortType" value="<%=sortType%>">
                        Nr. linii:
                        <select name="nrLines" onchange="this.form.submit()" style="font-size: 10px">
                            <option value="10"  <%=nrLines == 10 ? "selected" : ""%> >10</option>
                            <option value="15"  <%=nrLines == 15 ? "selected" : ""%> >15</option>
                            <option value="20"  <%=nrLines == 20 ? "selected" : ""%> >20</option>
                            <option value="30"  <%=nrLines == 30 ? "selected" : ""%> >30</option>
                            <option value="40"  <%=nrLines == 40 ? "selected" : ""%> >40</option>
                            <option value="50"  <%=nrLines == 50 ? "selected" : ""%> >50</option>
                            <option value="100"  <%=nrLines == 100 ? "selected" : ""%> >100</option>
                            <option value="200"  <%=nrLines == 200 ? "selected" : ""%> >200</option>
                        </select>&nbsp;&nbsp;
                        <%=currentBeginIndex%> - <%=currentBeginIndex + nrLinesSelected - 1%> din <%=nrLinesTotal%>&nbsp;&nbsp;&nbsp;
                        <input <%=currentBeginIndex == 1 ? "disabled" : ""%>  title="Primele <%=nrLines%> linii" type="button" onclick="this.form.direction.value='first'; this.form.submit()" style="font-size: 8px; cursor: pointer" value="&lt&lt" />
                        <input <%=currentBeginIndex == 1 ? "disabled" : ""%>  title="Precedentele <%=nrLines%> linii" type="button" onclick="this.form.direction.value='backward'; this.form.submit()" style="font-size: 8px; cursor: pointer" value="&lt" />
                        <input <%=currentBeginIndex + nrLinesSelected - 1 >= nrLinesTotal ? "disabled" : ""%> title="Urmatoarele <%=nrLines%> linii" type="button" onclick="this.form.direction.value='forward'; this.form.submit()" style="font-size: 8px; cursor: pointer" value="&gt" />
                        <input <%=currentBeginIndex + nrLinesSelected - 1 >= nrLinesTotal ? "disabled" : ""%> title="Ultimele <%=nrLines%> linii" type="button" onclick="this.form.direction.value='last'; this.form.submit()" style="font-size: 8px; cursor: pointer" value="&gt&gt" />
                    </div>
                </td>
            </tr>
        </tfoot>
    </table>
</form>

<script type="text/javascript">

    function order( column, type){
        document.zaforma.sortColumn.value = column;
        document.zaforma.sortType.value = type;
        document.zaforma.submit();
    }

    function doSyncronizareBDI( btn){
        if( confirm('Doriti sincronizarea nomenclatorului de <%=name%> cu datele din BDI ?')){
            document.zaforma.action = '<%=AppConstants.APP_NAME%>?action=sync-nomenclator-bdi';
            btn.enabled = false;
            document.zaforma.submit();
        }
    }

    function doSyncronizareBDIAll( btn){
        if( confirm('Doriti sincronizarea tuturor nomenclatoarelor cu datele din BDI ?')){
            btn.enabled = false;
            window.location = '<%=AppConstants.APP_NAME%>?action=sync-nomenclator-bdi&<%=NomConstants.TIP%>=<%=NomConstants.SYNC_ALL%>'
        }
    }

    var editWindow = null;
    function editNumarConsumatori(type_ob, codSapLF, descriereLF){
        if( editWindow != null) editWindow.close();
        editWindow = window.open('<%=AppConstants.APP_NAME%>?action=editLFInfo&<%=LFInfoConstants.COD_SAP_LF%>='+codSapLF+'&type_ob='+type_ob,'editLFInfo', 'top=100,left=100,width=750, height=380, status=yes,resizable=yes,dependent=yes,alwaysRaised=yes, scrollbars=yes');
    }

    var checkImg = null;

    function refreshData( deleted){
        if( deleted){
            $(checkImg).style.visibility = 'hidden';
        }else{
            $(checkImg).style.visibility = '';
        }
    }

    function copyLfInforFromLastYear(){
        var idCE = document.zaforma.centruId.options[ document.zaforma.centruId.selectedIndex].value;
        var ceName = document.zaforma.centruId.options[ document.zaforma.centruId.selectedIndex].text;

        if( idCE <=0 ){
            alert( 'Selectati un centru !');
            return;
        }

        var tipNom = document.zaforma.<%=NomConstants.TIP%>.options[ document.zaforma.<%=NomConstants.TIP%>.selectedIndex].value;
        if( tipNom <= 0){
            alert( "Selectati un tip de nomenclator");
            return;
        }

        if( confirm("Doriti sa copiati datele cu numarul de consumatori pentru centrul \""+ceName+"\" din anul <%=currentYear-1%> pentru anul curent <%=currentYear%> ?")
                && confirm("Atentie! Aceasta operatie va suprascrie datele curente\nContinuati operatiunea ?")){
            window.location = "<%=AppConstants.APP_NAME%>?action=copyAllLfInfoFromLastYear&idCE="+idCE;
        }
    }

    function inlineStartEditCodEmid( inputId, spanTextId){
        var oInput    = $( inputId);
        var oSpanText = $(spanTextId);

        oInput.value = oSpanText.innerText.trim();
        oInput.onkeyup = function( ev){
            var ev = ev || window.event;
//            alert( ev.keyCode);
            switch( ev.keyCode){
                case Event.KEY_ESC : escapeEditCodEmid(inputId, spanTextId); break;
                case Event.KEY_RETURN : changeCodEmid( inputId, spanTextId); break;
            }
        }

        Element.hide( oSpanText);
        Element.show( oInput);
        oInput.select();
        oInput.focus();
    }

    function escapeEditCodEmid( inputId, spanTextId){
        Element.hide( inputId);
        Element.show( spanTextId);
    }

    function changeCodEmid( inputId, spanTextId){
        var codLf = $(inputId).getAttribute("codLF");
        var codEMID = $(inputId).value;

        new Ajax.Request(
                    "<%=AppConstants.APP_NAME%>?action=changeCodEmid",
                    {
                        method : "post",
                        parameters: "codLF=" + codLf + "&newCodEMID="+codEMID+"&inputId="+inputId + "&spanTextId="+spanTextId,
                        onComplete : onChangeCodEMIDResponse
                    });
    }

    function onChangeCodEMIDResponse( originalRequest){
        var responseText = getResponseMesg( originalRequest.responseText).trim();
        var responseData = null; 
        try{
            responseData = eval("("+responseText+")");
        }catch(e){
//            alert("Eroare la parsare Server Reponse :"+ e.message);
        }

        if( responseData != null){
//            alert( "inputId = " + responseData.inputId);
//            alert( "spanId  = " + responseData.spanTextId);
//            alert( "codEMID  = " + responseData.codEMID);

            $( responseData.spanTextId).innerText = responseData.codEMID;
            Element.hide( responseData.inputId);
            Element.show( responseData.spanTextId);
        }else{
            alert("Mesaj Erorare: " + responseText);
        }
    }

    function aplicaActiune( ){
        var combo = $('actiuni');
        var actiune = combo.options[combo.selectedIndex].getAttribute("onclick");

        eval(actiune)();
    }

    function switchSyncNom(){
        var selectedTipNomenclator = document.getElementById("subs").value;

          var nomName = "";
          switch (selectedTipNomenclator){
              case "<%=NomConstants.NOM_STATII%>" : nomName = "Statii";break;
              case "<%=NomConstants.NOM_LINII%>" : nomName = "Linii";break;
              case "<%=NomConstants.NOM_PA_PT%>" : nomName = "PA-PT";break;
              case "<%=NomConstants.NOM_BARE%>" : nomName = "Bare";break;
              case "<%=NomConstants.NOM_CELULE%>" : nomName = "Celule";break;
              case "<%=NomConstants.NOM_BOBINE%>" : nomName = "Bobine";break;
              case "<%=NomConstants.NOM_TRAFO%>" : nomName = "Trafo";break;
              case "<%=NomConstants.NOM_DERIVATIE%>" : nomName = "Derivatii";break;
          }

        document.getElementById("actiuni").options[1].text = "Sincronizare nomenclator " + nomName + " - BDI";

    }

</script>