<%@ page import="org.apache.log4j.Logger" %>
<%@ page import="ro.emid.common.components.sugest.base.SugestConstants" %>
<%@ page import="ro.emid.common.constants.*" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.db.NomAccessDB" %>
<%@ page import="ro.emid.common.types.Category" %>
<%@ page import="ro.emid.common.types.Nomenclator" %>
<%@ page import="ro.emid.common.utils.Util" %>
<%@ page import="ro.emid.incidente.IncidenteSessionUtils" %>

<%@ include file="../incidente/declaratii.jsp"%>

<%  final Logger LOG = Logger.getLogger("rapoarteFiltru_jsp");

    Category centruFiltru = IncidenteSessionUtils.getCentru(session);

    if(null == centruFiltru) centruFiltru = new Category();

    if( centruFiltru.getId() == -1){

        Category sCentru = (Category) session.getAttribute("centru");
        if(null == sCentru) sCentru = new Category();
        if(sCentru != null && sCentru.getId() > 0){
            centruFiltru = sCentru;
        }
    }

    String codCe = centruFiltru.getCodSap();
    if (codCe == null){
        codCe = " '' OR lf.CE LIKE '%' ";
    }

    if (request.getAttribute("act") !=null) {%>
<script type="text/javascript">
    opener.reloadPage();
    window.close();
</script>
<%}else{%>

<%
    Category sucursalaFiltru = IncidenteSessionUtils.getSucursala(session);
    if(null == sucursalaFiltru) sucursalaFiltru = new Category();

    if( sucursalaFiltru.getId() == -1){
        Category sSucursala = (Category) session.getAttribute("sucursala");
        if(null == sSucursala) sSucursala = new Category();
        if( sSucursala != null && sSucursala.getId() > 0){
            sucursalaFiltru = sSucursala;
        }
    }

    final boolean raportIncidente   = IncidenteSessionUtils.getTipRaport(session).equals("INCIDENTE");
    final boolean raportEchipamente = IncidenteSessionUtils.getTipRaport(session).equals("ECHIPAMENTE");

    Long sessionLong;
    sessionLong = (Long) session.getAttribute("f_clasificare_incident");
    long f_clasificare_incident = -1; if (sessionLong != null) { f_clasificare_incident = sessionLong.longValue(); }

    sessionLong = (Long) session.getAttribute("f_clasificare");
    long f_clasificare = -1; if (sessionLong != null) { f_clasificare = sessionLong.longValue(); }

    sessionLong = (Long) session.getAttribute("f_formatie");
    long f_formatia = -1; if (sessionLong != null) { f_formatia = sessionLong.longValue(); }

    sessionLong = (Long) session.getAttribute("f_ansamblu");
    long f_ansamblu = -1; if (sessionLong != null) { f_ansamblu = sessionLong.longValue(); }

    sessionLong = (Long) session.getAttribute("f_echipamentComponenta");
    long f_echipamentComponenta = -1;
    if (sessionLong != null) {
        f_echipamentComponenta = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_statie");
    long f_statie = -1; if (sessionLong != null) { f_statie = sessionLong.longValue(); }

    sessionLong = (Long) session.getAttribute("f_bobina");
    long f_bobina = -1; if (sessionLong != null) { f_bobina= sessionLong.longValue(); }

    sessionLong = (Long) session.getAttribute("f_celula");
    long f_celula = -1; if (sessionLong != null) { f_celula = sessionLong.longValue(); }

    sessionLong = (Long) session.getAttribute("f_bara");
    long f_bara = -1; if (sessionLong != null) { f_bara= sessionLong.longValue(); }

    sessionLong = (Long) session.getAttribute("f_linie");
    long f_linie = -1; if (sessionLong != null) { f_linie = sessionLong.longValue(); }

    sessionLong = (Long)session.getAttribute("f_derivatie");
    long f_derivatie = -1; if (sessionLong != null) { f_derivatie = sessionLong.longValue(); }

    sessionLong = (Long)session.getAttribute("f_pa_pt");
    long f_pa_pt = -1; if (sessionLong != null) { f_pa_pt = sessionLong.longValue(); }

    sessionLong = (Long)session.getAttribute("f_trafo");
    long f_trafo = -1; if (sessionLong != null) { f_trafo = sessionLong.longValue(); }

    String sessionString;

    sessionString = (String) session.getAttribute("f_statie_cod");
    String f_statie_cod = "";
    if (sessionString != null) {
        f_statie_cod = sessionString;
    }

    sessionString = (String) session.getAttribute("f_bobina_cod");
    String f_bobina_cod = "";
    if (sessionString != null) {
        f_bobina_cod = sessionString;
    }

    sessionString = (String) session.getAttribute("f_celula_cod");
    String f_celula_cod = "";
    if (sessionString != null) {
        f_celula_cod = sessionString;
    }

    sessionString = (String) session.getAttribute("f_bara_cod");
    String f_bara_cod = "";
    if (sessionString != null) {
        f_bara_cod = sessionString;
    }

    sessionString = (String) session.getAttribute("f_linie_cod");
    String f_linie_cod = "";
    if (sessionString != null) {
        f_linie_cod = sessionString;
    }

    sessionString = (String) session.getAttribute("f_derivatie_cod");
    String f_derivatie_cod = "";
    if (sessionString != null) {
        f_derivatie_cod = sessionString;
    }

    sessionString = (String) session.getAttribute("f_pa_pt_cod");
    String f_pa_pt_cod = "";
    if (sessionString != null) {
        f_pa_pt_cod = sessionString;
    }

    sessionString = (String)  session.getAttribute("f_trafo_cod");
    String f_trafo_cod = "";
    if (sessionString != null) {
        f_trafo_cod = sessionString;
    }


    sessionString = (String) session.getAttribute("f_statie_den");
    String f_statie_den = "";
    if (sessionString != null) {
        f_statie_den = sessionString;
    }

    sessionString = (String) session.getAttribute("f_bobina_den");
    String f_bobina_den = "";
    if (sessionString != null) {
        f_bobina_den = sessionString;
    }

    sessionString = (String) session.getAttribute("f_celula_den");
    String f_celula_den = "";
    if (sessionString != null) {
        f_celula_den = sessionString;
    }

    sessionString = (String) session.getAttribute("f_bara_den");
    String f_bara_den = "";
    if (sessionString != null) {
        f_bara_den = sessionString;
    }

    sessionString = (String) session.getAttribute("f_linie_den");
    String f_linie_den = "";
    if (sessionString != null) {
        f_linie_den = sessionString;
    }

    sessionString = (String) session.getAttribute("f_derivatie_den");
    String f_derivatie_den = "";
    if (sessionString != null) {
        f_derivatie_den = sessionString;
    }

    sessionString = (String) session.getAttribute("f_pa_pt_den");
    String f_pa_pt_den = "";
    if (sessionString != null) {
        f_pa_pt_den = sessionString;
    }

    sessionString = (String)  session.getAttribute("f_trafo_den");
    String f_trafo_den = "";
    if (sessionString != null) {
        f_trafo_den = sessionString;
    }



    sessionLong = (Long)session.getAttribute("f_nr_componente_afectate");
    long f_nr_componente_afectate = -1; if (sessionLong != null) { f_nr_componente_afectate = sessionLong.longValue(); }

    sessionLong = (Long)session.getAttribute("f_mod_tr_neutru");
    long f_mod_tr_neutru = -1; if (sessionLong != null) { f_mod_tr_neutru = sessionLong.longValue(); }

    sessionLong = (Long)session.getAttribute("f_manifestare");
    long f_manifestare = -1; if (sessionLong != null) { f_manifestare = sessionLong.longValue(); }

    sessionLong = (Long)session.getAttribute("f_prilej");
    long f_prilej = -1; if (sessionLong != null) { f_prilej = sessionLong.longValue(); }

    sessionLong = (Long)session.getAttribute("f_grupa");
    long f_grupa = -1; if (sessionLong != null) { f_grupa = sessionLong.longValue(); }

    sessionLong = (Long)session.getAttribute("f_cauza");
    long f_cauza = -1; if (sessionLong != null) { f_cauza = sessionLong.longValue(); }

    sessionLong = (Long)session.getAttribute("f_fenomen_meteo");
    long f_fenomen_meteo = -1; if (sessionLong != null) { f_fenomen_meteo = sessionLong.longValue(); }

    sessionLong = (Long)session.getAttribute("f_nivel_tensiune");
    long f_nivel_tensiune = -1; if (sessionLong != null) { f_nivel_tensiune = sessionLong.longValue(); }

    sessionLong = (Long)session.getAttribute("f_tensiune");
    long f_tensiune = -1; if (sessionLong != null) { f_tensiune = sessionLong.longValue(); }

    sessionLong = (Long)session.getAttribute("f_forta_majora");
    long f_forta_majora = -1; if (sessionLong != null) { f_forta_majora = sessionLong.longValue(); }
%>


<TABLE class="gsapthead" align=center border=0 cellPadding=10 cellSpacing=0 width="100%">
<TBODY>
<TR>
<TD>
<TABLE align=center border=0 cellPadding=5 cellSpacing=0 width="100%" class="gsaptd">
<FORM NAME="zaforma" ACTION="<%=AppConstants.APP_NAME%>" METHOD=POST>
<INPUT TYPE="hidden" NAME="action" VALUE="adaugaFiltru" />
<input type="hidden" name="<%=IncidentConstants.SECTIE_ID%>" value="<%=centruFiltru.getId()%>">
<input type="hidden" name="<%=IncidentConstants.SECTIE_ID%>h" value="<%=centruFiltru.getCodSap()%>" codSapOriginal="<%=centruFiltru.getCodSap()%>">
<TBODY>
<TR>
    <TD colspan="2">
        <CENTER><h3>Configurare FILTRU rapoarte.</h3></CENTER>
    </TD>
</TR>
<TR>
<TD colspan="2">
<%
    long filialaId = IncidenteSessionUtils.getFilialaId(session);
    long sucursalaId = IncidenteSessionUtils.getSucursalaId(session);
    long centruId = IncidenteSessionUtils.getCentruId(session);

    Category sFiliala = (Category) session.getAttribute("filiala");
    if(sFiliala == null){
        sFiliala = new Category();
    }
    Category sSucursala = (Category) session.getAttribute("sucursala");
    if(sSucursala == null){
        sSucursala = new Category();
    }
    Category sCentru = (Category) session.getAttribute("centru");
    if(sCentru == null){
        sCentru = new Category();
    }
    if (sFiliala != null && sFiliala.getId() > 0) filialaId = sFiliala.getId();
    if (sSucursala != null && sSucursala.getId() > 0) sucursalaId = sSucursala.getId();
    if (sCentru != null && sCentru.getId() > 0) centruId = sCentru.getId();

    Category filialaRaport = CategoryAccessDB.getById(filialaId);
    if (filialaRaport == null) filialaRaport = new Category();

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

    Category centru = CategoryAccessDB.getById(centruId);
    if (centru == null) centru = new Category();
    final ro.emid.common.types.CategoryCheckData checkData = IncidenteSessionUtils.getCategoriiSelectate(session);


    if( !checkData.someDataChecked()){

%>
<table cellpadding="0" cellspacing="0">
 <tr>
     <td nowrap>
        Filiala:
        <SELECT name="filiala" onchange="fill_sucursala_select()" class="gSAPSelect" <%=user.getIeLevel() == ro.emid.common.constants.RoleConstants.NIVEL_FULL ? "" : "disabled"%> >
            <OPTION value="-1">ELECTRICA SA:</OPTION>
            <%  final Category [] filiale = CategoryAccessDB.getAllCategoriesByParentId( -1);
                if (filiale != null) for(int i=0;i<filiale.length;i++) {%>
            <OPTION <%=(filiale[i].getId() == filialaRaport.getId()) ? "selected=\"true\" " : ""%>value="<%=filiale[i].getId()%>"><%=filiale[i].getName()%></OPTION>
            <%} %>
        </SELECT>&nbsp;&nbsp;

        Sucursala:
        <span id="sucursalaDiv">
        <SELECT name="sucursala" class="gSAPSelect" onchange="fill_centru_select()" >
            <option value="-1">Selectati:</option>
        </SELECT></span>&nbsp;&nbsp;

        </td>
        <td nowrap>
            C.E.:
            <span id="centruDiv">
                <SELECT name="centruId" class="gSAPSelect" onchange="fill_formatii_select()">
                    <OPTION value="-1">Selectati:</OPTION>
                </SELECT>
            </span>&nbsp;&nbsp;
         </td>
         </tr>
         <tr>
             <td></td>
             <td nowrap>
                 <div class="spacer"></div>
                 <%=sucursalaFiltru.isCuSectiiSiCentre() ? "Centru exploatare" : "Formatia de lucru"%>:
                 <span id="formatiaDiv">
                    <select class="gSAPSelect"  name="<%=IncidentConstants.FORMATIA_DE_LUCRU_ID%>">
                        <OPTION value="-1">Selectati:</OPTION>
                    </select>
                 </span>
             </td>
         </tr>
     </table>


<script type="text/javascript">
function fill_sucursala_select(selectedValue, disabled, firstShow){
    resetLocalizareSelects( true, firstShow);
    resetComboLeaveFirstOption( document.zaforma.sucursala);
    resetComboLeaveFirstOption( document.zaforma.centruId);
    resetComboLeaveFirstOption( document.zaforma.<%=IncidentConstants.FORMATIA_DE_LUCRU_ID%>);

    var filialaId = document.zaforma.filiala[ document.zaforma.filiala.selectedIndex].value;
    if( filialaId > -1){
        var params = "parentId="+filialaId + "&selectName=sucursala&disabled=" + disabled + (selectedValue ? "&selectedValue=" + selectedValue : "");
        //alert( params);

        getCategoriesFromServer(params, function(originalRequest){
            //alert( getResponseMesg (originalRequest.responseText));
            $( 'sucursalaDiv').innerHTML = getResponseMesg (originalRequest.responseText);

            $('sucursala').onchange=fill_centru_select;

            if( firstShow){
                fill_centru_select(<%=centru.getId()%>, <%=user.getIeLevel() == RoleConstants.NIVEL_CENTRU || user.getIeLevel() == RoleConstants.NIVEL_FORMATIE%>, firstShow);
            }
        });
    }
}

function fill_centru_select(selectedValue, disabled, firstShow){
    resetLocalizareSelects( true, firstShow);
    resetComboLeaveFirstOption( $('centruId'));

    var sucursala = $('sucursala');
    var sucursalaId = sucursala.options[ sucursala.selectedIndex].value;
    if( sucursalaId > -1){
        var params = "parentId="+sucursalaId + "&selectName=centruId&disabled=" + disabled + (selectedValue ? "&selectedValue=" + selectedValue : "");
        //alert( params);

        getCategoriesFromServer(params,function(originalRequest){
            //alert( getResponseMesg (originalRequest.responseText));
            $('centruDiv').innerHTML = getResponseMesg (originalRequest.responseText);

            $('centruId').onchange = onCentruChanged;

            if( firstShow){
                <% if( f_formatia > 0){%>
                    fill_formatii_select( <%=f_formatia%>, false, firstShow);
                <%}else{%>
                    fillStatiiOnFirstShow();
                    fill_formatii_select();
                <%}%>
            }
        });
    }
}

function onCentruChanged(){
    fill_formatii_select();
    resetLocalizareSelects( true);
}

function fill_formatii_select(selectedValue, disabled, firstShow){
    setHiddenCodSAPOfCentru();

    resetComboLeaveFirstOption( $('<%=IncidentConstants.FORMATIA_DE_LUCRU_ID%>'));
    var centru = $('centruId');
    var centruId = centru.options[centru.selectedIndex].value;

    if( centruId > -1){
        var params = "parentId="+centruId+ "&selectName=<%=IncidentConstants.FORMATIA_DE_LUCRU_ID%>&disabled=" + disabled + (selectedValue ? "&selectedValue=" + selectedValue : "");
        //alert( params);

        getCategoriesFromServer(params,function(originalRequest){
            //alert( getResponseMesg (originalRequest.responseText));
            $( 'formatiaDiv').innerHTML = getResponseMesg (originalRequest.responseText);
            $('<%=IncidentConstants.FORMATIA_DE_LUCRU_ID%>').onchange=onFormatieChanged;

            if( firstShow){
                fillStatiiOnFirstShow();
            }
        });
    }
}

function setHiddenCodSAPOfCentru(){
    var centru = $('centruId');
    var codSapOfCentru = centru.options[ centru.selectedIndex].getAttribute("codSAP");
    if( codSapOfCentru ){
        var f = new Function( "return '" + codSapOfCentru + "';");
        codSapOfCentru  = f();
    }else{
        codSapOfCentru = '';
    }

    document.zaforma.<%=IncidentConstants.SECTIE_ID%>h.value=codSapOfCentru;
    document.zaforma.<%=IncidentConstants.SECTIE_ID%>h.setAttribute("codSapOriginal", codSapOfCentru);
    //alert(" setHiddenCodSAPOfCentru : " + document.zaforma.<%=IncidentConstants.SECTIE_ID%>h.value);
}


function onFormatieChanged(){
    var sucursala = $('sucursala');
    if( sucursala.options[ sucursala.selectedIndex].getAttribute("isCuSectiiSiCentre")){
        resetLocalizareSelects( true);
        setHiddenCodSectieIfIsC1_or_C2();
    }
}

function resetLocalizareSelects( disableLocalizareSelects, firstShow){
    if( firstShow){
        selectOption( document.zaforma.<%=IncidentConstants.ANSAMBLU_ID%>, <%=f_ansamblu%>);
    }else{
        document.zaforma.<%=IncidentConstants.ANSAMBLU_ID%>.selectedIndex = 0;
    }

    resetComboLeaveFirstOptionAndDisable( document.zaforma.<%=IncidentConstants.STATIE_ID%>, disableLocalizareSelects);
    resetComboLeaveFirstOptionAndDisable( document.zaforma.<%=IncidentConstants.BOBINA_ID%>, disableLocalizareSelects);
    resetComboLeaveFirstOptionAndDisable( document.zaforma.<%=IncidentConstants.CELULA_ID%>, disableLocalizareSelects);
    resetComboLeaveFirstOptionAndDisable( document.zaforma.<%=IncidentConstants.BARA_ID%>, disableLocalizareSelects);
    resetComboLeaveFirstOptionAndDisable( document.zaforma.<%=IncidentConstants.LINIE_ID%>, disableLocalizareSelects);
    resetComboLeaveFirstOptionAndDisable( document.zaforma.<%=IncidentConstants.DERIVATIE_ID%>, disableLocalizareSelects);
    resetComboLeaveFirstOptionAndDisable( document.zaforma.<%=IncidentConstants.PAPT_ID%>, disableLocalizareSelects);
    resetComboLeaveFirstOptionAndDisable( document.zaforma.<%=IncidentConstants.TRAFO_ID%>, disableLocalizareSelects);
}

function fillStatiiOnFirstShow(){
    enable_ansamble_selects();
    fill_statii_selects( <%=f_statie%>, true);
}
</script>
<%}%>
</TD>
</TR>
<%if( raportIncidente){%>
<TR class="gSAPGroup">
    <TD>
        Clasificare:
    </TD>
    <TD>
        <INPUT name="clasificareincident" type=radio value="1" id="izolat" <%= ( f_clasificare_incident == 1) ? "checked" : ""%> /><label for="izolat">Izolat</label>
        <INPUT name="clasificareincident" type=radio value="2" id="primar" <%= ( f_clasificare_incident == 2) ? "checked" : ""%> /><label for="primar">Primar</label>
        <INPUT name="clasificareincident" type=radio value="3" id="asociat" <%= ( f_clasificare_incident == 3) ? "checked" : ""%> /><label for="asociat">Asociat</label>
    </TD>
</TR>
<%}%>

<TR class="gSAPGroup">
    <TD nowrap>
        Ansamblu functional:
    </TD>
    <TD>
        <SELECT name="<%=IncidentConstants.ANSAMBLU_ID%>" class="gSAPSelect" onChange="on_ansamblu_changed();">
            <OPTION value="-1">Selectati:</OPTION>
            <% Util.showNomOptions(out, NomConstants.NOM_ANSAMBLURI, f_ansamblu); %>
            <OPTION value="<%=NomConstants.ID_ANSAMBLU_VIRTUAL%>" style="font-weight: bold;"><%=NomConstants.DESC_ANSAMBLU_VIRTUAL%></OPTION>
        </SELECT>
        <SELECT name="<%=IncidentConstants.ANSAMBLU_ID%>x" class="gSAPSelect" style="display:none;">
            <OPTION value="-1">Selectati:</OPTION>
            <% Util.showNomOptionsCod(out, NomConstants.NOM_ANSAMBLURI, f_ansamblu); %>
            <OPTION value="<%=NomConstants.ID_ANSAMBLU_VIRTUAL%>" style="font-weight: bold;"><%=NomConstants.DESC_ANSAMBLU_VIRTUAL%></OPTION>
        </SELECT>
    </TD>
</TR>

<%if( raportEchipamente){%>
    <TR class="gSAPGroup">
      <TD width="25%">
       Echipament / componenta:
      </TD>
      <TD wdth="75%">
          <SELECT name="<%=EchipamentConstants.COMPONENTA_ID%>" class="gSAPSelect">
              <option value=-1>Selectati:</option>
          </SELECT>
       </TD>
    </TR>
<%}%>

<TR class="gSAPGroup">
    <TD colspan="2">

    <table border = 1 class="gsapthead" width="100%">
        <tr class="gSAPGroup">
            <TD>
                Statie:
            </TD>

            <TD>
                <input  type="hidden" id="<%=IncidentConstants.STATIE_ID%>" name="<%=IncidentConstants.STATIE_ID%>"
                        value=""/>
                <input  type="hidden" id="<%=IncidentConstants.STATIE_COD%>" name="<%=IncidentConstants.STATIE_COD%>"
                        value="<%=f_statie_cod%>"/>
                <input class="gsapinputtext" id="<%=IncidentConstants.STATIE_DEN%>"
                       name="<%=IncidentConstants.STATIE_DEN%>" maxlength="100" style="width: 285px" sugestWidth="285"
                       value="<%=f_statie_den%>"/>
            </TD>
            <td nowrap="nowrap">
                Bobina:
            </td>
            <td >
                <input type="hidden" id="<%=IncidentConstants.BOBINA_ID%>" name="<%=IncidentConstants.BOBINA_ID%>"
                       value=""/>
                <input type="hidden" id="<%=IncidentConstants.BOBINA_COD%>" name="<%=IncidentConstants.BOBINA_COD%>"
                       value="<%=f_bobina_cod%>"/>
                <input class="gsapinputtext" id="<%=IncidentConstants.BOBINA_DEN%>"
                       name="<%=IncidentConstants.BOBINA_DEN%>" maxlength="100" style="width: 285px" sugestWidth="285"
                       value="<%=f_bobina_den%>"/>
            </TD>
        </TR>
        <TR class="gSAPGroup">
            <TD>
                Celula:
            </TD>
            <TD>
                <input type="hidden" id="<%=IncidentConstants.CELULA_ID%>" name="<%=IncidentConstants.CELULA_ID%>"
                       value=""/>
                <input type="hidden" id="<%=IncidentConstants.CELULA_COD%>" name="<%=IncidentConstants.CELULA_COD%>"
                       value="<%=f_celula_cod%>"/>
                <input class="gsapinputtext" id="<%=IncidentConstants.CELULA_DEN%>"
                       name="<%=IncidentConstants.CELULA_DEN%>" maxlength="200" style="width: 285px" sugestWidth="285"
                       value="<%=f_celula_den%>"/>
            </TD>
            <td nowrap="nowrap">
                Bara:
            </td>
            <td>
                <input type="hidden" id="<%=IncidentConstants.BARA_ID%>" name="<%=IncidentConstants.BARA_ID%>"
                       value=""/>
                <input type="hidden" id="<%=IncidentConstants.BARA_COD%>" name="<%=IncidentConstants.BARA_COD%>"
                       value="<%=f_bara_cod%>"/>
                <input class="gsapinputtext" id="<%=IncidentConstants.BARA_DEN%>" name="<%=IncidentConstants.BARA_DEN%>"
                       maxlength="200" style="width: 285px" sugestWidth="285"
                       value="<%=f_bara_den%>"/>
            </TD>
        </TR>
        <TR class="gSAPGroup">
            <TD>
                Linie:
            </TD>
            <TD>
                <input type="hidden" id="<%=IncidentConstants.LINIE_ID%>" name="<%=IncidentConstants.LINIE_ID%>"
                       value=""/>
                <input type="hidden" id="<%=IncidentConstants.LINIE_COD%>" name="<%=IncidentConstants.LINIE_COD%>"
                       value="<%=f_linie_cod%>"/>
               <input class="gsapinputtext" id="<%=IncidentConstants.LINIE_DEN%>"
                       name="<%=IncidentConstants.LINIE_DEN%>" maxlength="100" style="width: 285px" sugestWidth="285"
                       value="<%=f_linie_den%>"/>

            </TD>
            <td nowrap="nowrap">
                Derivatie:
            </td>
            <td>
                <input type="hidden" id="<%=IncidentConstants.DERIVATIE_ID%>" name="<%=IncidentConstants.DERIVATIE_ID%>"
                       value=""/>
                <input type="hidden" id="<%=IncidentConstants.DERIVATIE_COD%>" name="<%=IncidentConstants.DERIVATIE_COD%>"
                       value="<%=f_derivatie_cod%>"/>
                <input class="gsapinputtext" id="<%=IncidentConstants.DERIVATIE_DEN%>"
                       name="<%=IncidentConstants.DERIVATIE_DEN%>" maxlength="100" style="width: 285px"
                       sugestWidth="285" value="<%=f_derivatie_den%>"/>
            </TD>
        </TR>
        <TR class="gSAPGroup">
            <TD>
                PA/PT:
            </TD>
            <TD colspan="3">
                <input type="hidden" id="<%=IncidentConstants.PAPT_ID%>" name="<%=IncidentConstants.PAPT_ID%>"
                       value=""/>
                <input type="hidden" id="<%=IncidentConstants.PAPT_COD%>" name="<%=IncidentConstants.PAPT_COD%>"
                       value="<%=f_pa_pt_cod%>"/>
                <input class="gsapinputtext" id="<%=IncidentConstants.PAPT_DEN%>" name="<%=IncidentConstants.PAPT_DEN%>"
                       maxlength="100" style="width: 285px" sugestWidth="285"
                       value=""/<%=f_pa_pt_den%>>
            </TD>
        </TR>
        <TR class="gSAPGroup">
            <TD>
                Trafo:
            </TD>
            <TD colspan="3">

                <input type="hidden" id="<%=IncidentConstants.TRAFO_ID%>" name="<%=IncidentConstants.TRAFO_ID%>"
                       value=""/>
                <input type="hidden" id="<%=IncidentConstants.TRAFO_COD%>" name="<%=IncidentConstants.TRAFO_COD%>"
                       value="<%=f_trafo_cod%>"/>
                <input class="gsapinputtext" id="<%=IncidentConstants.TRAFO_DEN%>"
                       name="<%=IncidentConstants.TRAFO_DEN%>" maxlength="100" style="width: 285px" sugestWidth="285"
                       value="<%=f_trafo_den%>"/>

            </TD>
        </TR>
        </tr>
    </table>

    </TD>
</TR>

<%if( raportIncidente ){%>
<TR class="gSAPGroup">
    <TD>
        Nivel:
    </TD>
    <TD>
        <nobr>
            <INPUT name="clasificare" type=radio value="1" id="IT" <%= ( f_clasificare == 1) ? "checked" : ""%> /> <label for="IT">IT</label>&nbsp;&nbsp;
            <INPUT name="clasificare" type=radio value="2" id="MT" <%= ( f_clasificare == 2) ? "checked" : ""%> /> <label for="MT">MT</label>&nbsp;&nbsp;
            <INPUT name="clasificare" type=radio value="3" id="toate"  <%= ( f_clasificare == 3) ? "checked" : ""%> /> <label for="toate">Toate</label>

            &nbsp;&nbsp;&nbsp;&nbsp;
            Tensiune:
            <SELECT name="<%=IncidentConstants.TENSIUNE%>" class="gSAPSelect">
                <option value="0">-</option>
                <option value="6" <%=f_tensiune == 6 ? "selected" : ""%>>6</option>
                <option value="10" <%=f_tensiune == 10 ? "selected" : ""%>>10</option>
                <option value="15" <%=f_tensiune == 15 ? "selected" : ""%>>15</option>
                <option value="20" <%=f_tensiune == 20 ? "selected" : ""%>>20</option>
                <option value="27.5" <%=f_tensiune == 27.5 ? "selected" : ""%>>27.5</option>
                <option value="35" <%=f_tensiune == 35 ? "selected" : ""%>>35</option>
                <option value="110" <%=f_tensiune == 110 ? "selected" : ""%>>110</option>
            </SELECT> kV
        </nobr>
    </TD>
</TR>
<%}%>

<%if( raportIncidente ){%>
<TR>
    <TD>
        Nr. componente afectate:
    </TD>
    <TD>
        <INPUT name="nr_componente_afectate" type=text size="2" value="<%= (f_nr_componente_afectate != -1) ? f_nr_componente_afectate : 0%>">

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="fortaMajora" value="1" <%=f_forta_majora > 0 ? "checked" : ""%>   id="checkFortaMajora" /><label for="checkFortaMajora">Incidente cauzate de forta majora</label>
    </TD>
</TR>

<TR class="gSAPGroup">
    <TD nowrap>
        Mod de tratare neutru:
    </TD>
    <TD>
        <SELECT name="mod_tr_neutru" class="gSAPSelect">
            <OPTION value="-1">Selectati:</OPTION>
            <% Util.showNomOptions(out, NomConstants.NOM_MOD_TRATARE_NEUTRU, f_mod_tr_neutru); %>
        </SELECT>
    </TD>
</TR>

<TR>
    <TD>
        Manifestare:
    </TD>
    <TD>
        <SELECT name="forma_manifestare" class="gSAPSelect">
            <OPTION value="-1">Selectati:</OPTION>
            <% Util.showNomOptions(out, NomConstants.NOM_MANIFESTARI, f_manifestare); %>
        </SELECT>
    </TD>
</TR>

<TR class="gSAPGroup">
    <TD>
        Prilej:
    </TD>
    <TD>
        <SELECT name="prilej" class="gSAPSelect">
            <OPTION value="-1">Selectati:</OPTION>
            <% Util.showNomOptions(out, NomConstants.NOM_PRILEJURI, f_prilej); %>
        </SELECT>
    </TD>
</TR>
<%}%>

<TR>
    <TD>
        Grupa:
    </TD>
    <TD>
        <SELECT name="grupa" class="gSAPSelect" onChange="fill_cauze_select();">
            <OPTION value="-1">Selectati:</OPTION>
            <% Util.showNomOptions(out, raportIncidente ? NomConstants.NOM_CAUZE_GRUPE : NomConstants.NOM_GRUPE_CAUZE_ECH, f_grupa); %>
        </SELECT>
    </td>
</tr>

<TR>
    <TD>
        Cauza:
    </TD>
    <TD>
        <SELECT name="cauza" class="gSAPSelect" length=25>
            <OPTION value="-1">Selectati:</OPTION>
            <% Nomenclator []cauze = NomAccessDB.getAllNom(" AND COD_GRUPA = " + f_grupa); %>
            <% Util.showNomOptions(out, cauze, f_cauza); %>
        </SELECT>
    </TD>
</TR>

<%if( raportIncidente ){%>
<TR class="gSAPGroup">
    <TD>
        Meteo:
    </TD>
    <TD valign="MIDDLE" width="100%" >
        <SELECT name="fenomen_meteo" class="gSAPSelect">
            <OPTION value="-1">Selectati:</OPTION>
            <% Util.showNomOptions(out, NomConstants.NOM_METEO, f_fenomen_meteo); %>
        </SELECT>
    </TD>
</TR>
<%}%>

<%if( raportEchipamente) {%>
<TR>
    <TD title="Tensiune nominala a instalatiei (campul 6.1.1 din Fisa Echipament)">
        Nivel Tensiune :
    </TD>
    <TD>
        <INPUT name="nivel_tensiune" type=radio value="6"  <%=f_nivel_tensiune == 6 ? "checked" : ""%>  > 6&nbsp;&nbsp;
        <INPUT name="nivel_tensiune" type=radio value="10" <%=f_nivel_tensiune == 10 ? "checked" : ""%> > 10&nbsp;&nbsp;
        <INPUT name="nivel_tensiune" type=radio value="15" <%=f_nivel_tensiune == 15 ? "checked" : ""%> > 15&nbsp;&nbsp;
        <INPUT name="nivel_tensiune" type=radio value="20" <%=f_nivel_tensiune == 20 ? "checked" : ""%>  > 20&nbsp;&nbsp;
        <INPUT name="nivel_tensiune" type=radio value="-275" <%=f_nivel_tensiune == -275 ? "checked" : ""%>  > 27.5&nbsp;&nbsp;
        <INPUT name="nivel_tensiune" type=radio value="35" <%=f_nivel_tensiune == 35 ? "checked" : ""%>  > 35&nbsp;&nbsp;
        <INPUT name="nivel_tensiune" type=radio value="110" <%=f_nivel_tensiune == 110 ? "checked" : ""%>  > 110&nbsp;&nbsp;
    </TD>
</TR>
<%}%>


<TR>
    <TD align="center" valign=top colspan="2">
        <input type="submit" onclick="" value=". OK ." />
        <input type="button" onclick="resetFiltru()" value="Reset">
        <input type="button" onClick="window.close()" value="Închide">
    </TD>
</TR>
</TBODY>
</FORM>
</TABLE>
</TD>
</TR>
</TBODY>
</TABLE>


<%@ include file="../incidente/functiiLocalizare.jsp"%>

<script type="text/javascript">

    <%if( raportEchipamente){%>
        function fill_componente_select(){
            <% final Nomenclator[] grupAnsambluri = NomAccessDB.getAllNom( NomConstants.NOM_ANSAMBLURI); %>
            ansambluri = new Array( <%=grupAnsambluri.length%>);
            <%for(int i = 0; i < grupAnsambluri.length; i++) {
                final Nomenclator grupCauza = grupAnsambluri[i]; %>
                ansambluri[<%=i%>]=new Array(<%=grupCauza.getId()%>, <%=grupCauza.getCod()%>);
            <%}%>

            <% final Nomenclator[] componente = NomAccessDB.getAllNom( NomConstants.NOM_COMPONENTE); %>
            cauze_select = new Array( <%=componente.length%>);
            <%for(int i = 0; i < componente.length; i++) {
                final Nomenclator cauza = componente[i]; %>
                cauze_select[<%=i%>]=new Array(<%=cauza.getId()%>, <%=cauza.getCodGrupa()%>, <%=cauza.getCod()%>, "<%=cauza.getDescriere()%>");
            <%}%>
            var optNewOption;


            resetComboLeaveFirstOption( zaforma.<%=EchipamentConstants.COMPONENTA_ID%>);

            cauzaGrupId = zaforma.<%=EchipamentConstants.ANSAMBLU_ID%>.options[zaforma.<%=EchipamentConstants.ANSAMBLU_ID%>.selectedIndex].value;
            codGrupa = -1;
            for(i=0;i<ansambluri.length;i++){
                if( ansambluri[i][0] == cauzaGrupId){
                    codGrupa = ansambluri[i][1];
                    break;
                }
            }

            indexx=0;
            for(i=0;i<cauze_select.length;i++)
            {
                if(codGrupa == cauze_select[i][1]  || ( (codGrupa == 50 || codGrupa == 51) && (cauze_select[i][1] == 50 || cauze_select[i][1] == 51) )) {
                    //window.document.zaforma.cauza[indexx].value = cauze_select[i][1];
                    //window.document.zaforma.cauza[indexx].text = cauze_select[i][2];
                    optNewOption = new Option(cauze_select[i][2] + " - " + cauze_select[i][3], cauze_select[i][0], false, false);
                    zaforma.<%=EchipamentConstants.COMPONENTA_ID%>.options[++indexx] = optNewOption;
                    //indexx++;
                }
            }
        }
    <%}%>


    function fill_cauze_select(){
        <% final Nomenclator[] grupCauze = NomAccessDB.getAllNom(  raportIncidente ? NomConstants.NOM_CAUZE_GRUPE : NomConstants.NOM_GRUPE_CAUZE_ECH ); %>
            grup_cauze_select = new Array( <%=grupCauze.length%>);
        <%for(int i = 0; i < grupCauze.length; i++) {
            final Nomenclator grupCauza = grupCauze[i]; %>
            grup_cauze_select[<%=i%>]=new Array(<%=grupCauza.getId()%>, <%=grupCauza.getCod()%>);
        <%}%>

        <% cauze = NomAccessDB.getAllNom( raportIncidente ? NomConstants.NOM_CAUZE : NomConstants.NOM_CAUZE_ECH); %>
            cauze_select = new Array( <%=cauze.length%>);
        <%for(int i = 0; i < cauze.length; i++) {
            final Nomenclator cauza = cauze[i]; %>
            cauze_select[<%=i%>]=new Array(<%=cauza.getId()%>, <%=cauza.getCodGrupa()%>, <%=cauza.getCod()%>, "<%=cauza.getDescriere()%>");
        <%}%>
        var optNewOption;

        resetComboLeaveFirstOption( zaforma.cauza);


        cauzaGrupId = zaforma.grupa.options[zaforma.grupa.selectedIndex].value;
        codGrupa = -1;
        for(i=0;i<grup_cauze_select.length;i++){
            if( grup_cauze_select[i][0] == cauzaGrupId){
                codGrupa = grup_cauze_select[i][1];
                break;
            }
        }

        indexx=0;
        for(i=0;i<cauze_select.length;i++)
        {
            if(codGrupa == cauze_select[i][1]) {
                //window.document.zaforma.cauza[indexx].value = cauze_select[i][1];
                //window.document.zaforma.cauza[indexx].text = cauze_select[i][2];
                optNewOption = new Option(cauze_select[i][2] + " - " + cauze_select[i][3], cauze_select[i][0], false, false);
                zaforma.cauza.options[++indexx] = optNewOption;
                //indexx++;
            }
        }
    }


    function on_ansamblu_changed(){
        if( $('centruId').selectedIndex > 0){
            enable_ansamble_selects();


            fill_statii_selects();
            fill_linii_selects();
            fill_pa_pt_selects();

            fillEqOrfane();
        }

        <%if( raportEchipamente){%>
            fill_componente_select();
        <%}%>
    }

    function fillAllOnEdit(){
        //alert( 'fill all selects' );
        fill_celula_selects( <%=f_celula%>);
        fill_linii_selects( <%=f_linie%>, true, <%=f_derivatie%>);
        fill_pa_pt_selects( <%=f_pa_pt%>);
        fill_trafo_selects( <%=f_trafo%>);
        fill_bobine_selects( <%=f_bobina%>);
        fill_bare_selects( <%=f_bara%>);
    }





    function onChangeSelect(oSelect){
        //        alert( oSelect.name);
    }


    function resetFiltru(){
        var elements = Form.getElements(document.zaforma);
        for( var i = 0; i < elements.length; i++){
            var el = elements[i];
            if( el.type == "checkbox") el.checked =  false;
            else if( el.type == "text") el.value = "";
            else if( el.type == "radio") el.checked = false;
        }

        var selects = document.zaforma.getElementsByTagName("select");
        for( var i = 0; i < selects.length; i++){
            var selectu = selects[i];
            selectu.selectedIndex = 0;
        }
    }

    <% if( !checkData.someDataChecked()){ %>
        fill_sucursala_select(<%=sucursalaRaport.getId()%>, <%=user.getIeLevel() == RoleConstants.NIVEL_SUCURSALA || user.getIeLevel() == RoleConstants.NIVEL_CENTRU || user.getIeLevel() == RoleConstants.NIVEL_FORMATIE%>, true);
    <%}%>

    fill_cauze_select();
    selectOption( document.zaforma.cauza, <%=f_cauza%>);

    <%if( raportEchipamente){%>
        fill_componente_select();
        selectOption( document.zaforma.<%=EchipamentConstants.COMPONENTA_ID%>, <%=f_echipamentComponenta%>);
    <%}%>
</script>
<%}%>

<script type="text/javascript">

/* Suggesters */

function getCodCentruSelectat(){
    var codCeSelectat = '';

    var s = document.getElementById("centruId");
    var opt = s.options[ s.selectedIndex];

    codCeSelectat = opt.getAttribute("codSAP");

    return codCeSelectat;
}

var statiiDataProvider = new AjaxDataProvider(function () {
    return "<%=AppConstants.APP_NAME%>?action=suggestStatii&codCe="+getCodCentruSelectat()+"&<%=SugestConstants.SUGEST_Q_QUERY%>=";
});

var bobineDataProvider = new AjaxDataProvider(function () {

    // bobina's parent is statie

    var codStatie = $("<%=IncidentConstants.STATIE_COD%>").value;

    return "<%=AppConstants.APP_NAME%>?action=suggestBobine&codCe="+getCodCentruSelectat()+"&codParent=" + codStatie + "&<%=SugestConstants.SUGEST_Q_QUERY%>=";
});

var celuleDataProvider = new AjaxDataProvider(function () {
    return "<%=AppConstants.APP_NAME%>?action=suggestCelule&codCe="+getCodCentruSelectat()+"&<%=SugestConstants.SUGEST_Q_QUERY%>=";
});

var bareDataProvider = new AjaxDataProvider(function () {
    return "<%=AppConstants.APP_NAME%>?action=suggestBare&codCe="+getCodCentruSelectat()+"&<%=SugestConstants.SUGEST_Q_QUERY%>=";
});

var liniiDataProvider = new AjaxDataProvider(function () {

    // linie's parent is statie

    var parentId = $("<%=IncidentConstants.STATIE_ID%>").value;

    return "<%=AppConstants.APP_NAME%>?action=suggestLinii&codCe="+getCodCentruSelectat()+"&codParent=" + parentId + "&<%=SugestConstants.SUGEST_Q_QUERY%>=";
});

var derivatiiDataProvider = new AjaxDataProvider(function () {

    // derivatie's parent is linie

    var codLinie = $("<%=IncidentConstants.LINIE_COD%>").value;

    return "<%=AppConstants.APP_NAME%>?action=suggestDerivatii&codCe="+getCodCentruSelectat()+"&codParent=" + codLinie + "&<%=SugestConstants.SUGEST_Q_QUERY%>=";
});

var paptDataProvider = new AjaxDataProvider(function () {

    // papt's parent is linie

    var parentLinieId = $("<%=IncidentConstants.LINIE_ID%>").value;

    var parentStatieId = $("<%=IncidentConstants.STATIE_ID%>").value;

    return "<%=AppConstants.APP_NAME%>?action=suggestPapt&codCe="+getCodCentruSelectat()+"&parentLinieId="
            + parentLinieId + "&parentStatieId=" + parentStatieId + "&<%=SugestConstants.SUGEST_Q_QUERY%>=";
});

var trafoDataProvider = new AjaxDataProvider(function () {
    return "<%=AppConstants.APP_NAME%>?action=suggestTrafo&codCe="+getCodCentruSelectat()+"&<%=SugestConstants.SUGEST_Q_QUERY%>=";
})

function initSugestsLocalizareIncident() {
    new InputSugest($("<%=IncidentConstants.STATIE_DEN%>"), $("<%=IncidentConstants.STATIE_ID%>"), statiiDataProvider, {
        isValidSearchInput: function () {
            return true;
        },

        beforeRequest: function () {
            $('<%=IncidentConstants.STATIE_COD%>').value = '';
        },

        onSelect: function (item) {

            // clear bobine, linii, posturi, derivatii
            $('<%=IncidentConstants.STATIE_COD%>').value = item.cod;

            $('<%=IncidentConstants.LINIE_ID%>').value = -1;
            $('<%=IncidentConstants.LINIE_DEN%>').value = '';

            $('<%=IncidentConstants.BOBINA_ID%>').value = -1;
            $('<%=IncidentConstants.BOBINA_DEN%>').value = '';

            $('<%=IncidentConstants.PAPT_ID%>').value = -1;
            $('<%=IncidentConstants.PAPT_DEN%>').value = '';

            $('<%=IncidentConstants.DERIVATIE_ID%>').value = -1;
            $('<%=IncidentConstants.DERIVATIE_DEN%>').value = '';
        }

    });

    new InputSugest($("<%=IncidentConstants.BOBINA_DEN%>"), $("<%=IncidentConstants.BOBINA_ID%>"), bobineDataProvider, {
        isValidSearchInput: function () {
            return true;
        },

        beforeRequest: function () {
            $("<%=IncidentConstants.BOBINA_COD%>").value = '';

        },

        onSelect: function (item) {
            $("<%=IncidentConstants.BOBINA_COD%>").value = item.cod;
            $('<%=IncidentConstants.STATIE_ID%>').value = item.idInstalatieParent;
            $('<%=IncidentConstants.STATIE_DEN%>').value = item.descInstalatieParent;

        }
    });
    new InputSugest($("<%=IncidentConstants.CELULA_DEN%>"), $("<%=IncidentConstants.CELULA_ID%>"), celuleDataProvider, {
        isValidSearchInput: function () {
            return true;
        },

        beforeRequest: function () {
            $("<%=IncidentConstants.CELULA_COD%>").value = '';

        },

        onSelect: function (item) {
            $("<%=IncidentConstants.CELULA_COD%>").value = item.cod;
        }
    });
    new InputSugest($("<%=IncidentConstants.BARA_DEN%>"), $("<%=IncidentConstants.BARA_ID%>"), bareDataProvider, {
        isValidSearchInput: function () {
            return true;
        },

        beforeRequest: function () {
            $("<%=IncidentConstants.BARA_COD%>").value = '';

        },

        onSelect: function (item) {
            $("<%=IncidentConstants.BARA_COD%>").value = item.cod;
        }
    });

    new InputSugest($("<%=IncidentConstants.LINIE_DEN%>"), $("<%=IncidentConstants.LINIE_ID%>"), liniiDataProvider, {
        isValidSearchInput: function () {
            return true;
        },

        beforeRequest: function () {
            $("<%=IncidentConstants.LINIE_COD%>").value = '';

        },

        onSelect: function (item) {
            $("<%=IncidentConstants.LINIE_COD%>").value = item.cod;

            $('<%=IncidentConstants.PAPT_ID%>').value = -1;
            $('<%=IncidentConstants.PAPT_DEN%>').value = '';

            $('<%=IncidentConstants.DERIVATIE_ID%>').value = -1;
            $('<%=IncidentConstants.DERIVATIE_DEN%>').value = '';

            <%--$('<%=IncidentConstants.STATIE_ID%>').value = item.idInstalatieParent;--%>
            <%--$('<%=IncidentConstants.STATIE_DEN%>').value = item.descInstalatieParent;--%>

        }
    });

    new InputSugest($("<%=IncidentConstants.DERIVATIE_DEN%>"), $("<%=IncidentConstants.DERIVATIE_ID%>"), derivatiiDataProvider, {
        isValidSearchInput: function () {
            return true;
        },

        beforeRequest: function () {
            $("<%=IncidentConstants.DERIVATIE_COD%>").value = '';

        },

        onSelect: function (item) {

            $("<%=IncidentConstants.DERIVATIE_COD%>").value = item.cod;
            $('<%=IncidentConstants.LINIE_ID%>').value = item.idInstalatieParent;
            $('<%=IncidentConstants.LINIE_DEN%>').value = item.descInstalatieParent;


        }
    });

    new InputSugest($("<%=IncidentConstants.PAPT_DEN%>"), $("<%=IncidentConstants.PAPT_ID%>"), paptDataProvider, {
        isValidSearchInput: function () {
            return true;
        },

        beforeRequest: function () {
            $("<%=IncidentConstants.PAPT_COD%>").value = '';

        },

        onSelect: function (item) {
            $("<%=IncidentConstants.PAPT_COD%>").value = item.cod;
            <%--$('<%=IncidentConstants.LINIE_ID%>').value = item.idInstalatieParent;--%>
            <%--$('<%=IncidentConstants.LINIE_DEN%>').value = item.descInstalatieParent;--%>

        }
    });

    new InputSugest($("<%=IncidentConstants.TRAFO_DEN%>"), $("<%=IncidentConstants.TRAFO_ID%>"), trafoDataProvider, {
        isValidSearchInput: function () {
            return true;
        },

        beforeRequest: function () {
            $("<%=IncidentConstants.TRAFO_COD%>").value = '';

        },

        onSelect: function (item) {
            $("<%=IncidentConstants.TRAFO_COD%>").value = item.cod;
        }
    });

    //alert( "suggests initialized");
}
setTimeout(initSugestsLocalizareIncident, 1000);

enable_ansamble_selects();

</script>


