<%@ page import="ro.emid.callcenter.obj.binder.DeranjamentBinder" %>
<%@ 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.types.Deranjament" %>
<%@ page import="ro.emid.common.utils.UserUtils" %>
<%@ page import="ro.emid.lib.view.DeranjamentxFieldsView" %>


<%

    final DeranjamentBinder binder = (DeranjamentBinder) session.getAttribute(DeranjamentConstants.BEAN);
    final Deranjament bean = (binder != null ? (Deranjament) binder.getBean() : new Deranjament());
	
	DeranjamentxFieldsView deranjamentFieldsView = new DeranjamentxFieldsView();
    String[] cauzeOptions = new String[] { "", "Conditii improprii de functionare", "Inundarea instalatiilor", "Suprasarcina", "Uzura", "Neselectivitate", "Sigurante necalibrate la abonat", "Contact necorespunzator", "Actiuni ale unor persoane neautorizate sau utilaje", "Actiuni ale pasarilor sau animalelor", "Vant puternic", "Viscol", "Descarcari atmosferice", "Inundatii", "Alunecari de teren", "Caderi de copaci mod natural", "Furturi din instalatiile electrice", "Caderi de zapada", "loviri sau rupreri de cabluri JT sau conductoare LEA JT", "accidente auto cu ruperi stalpi JT sau conductoare JT", "obiecte cazute pe instalatiile JT", "taieri copaci persoane straine neautorizate", "crengi luate de vant", "vegetatie crescuta in retea aflata pe domeniu privat" };

	
    if( request.getAttribute("refreshParent") != null){
%>
<script type="text/javascript">
    window.parent.location = "<%=AppConstants.APP_NAME%>?action=show-deranjamente&selectedId=<%=bean.getId()%>";
</script>
<%
}else{

    final boolean showLocalizareInputs = bean.getFilialaId() == -1 || bean.getSucursalaId() == -1 || bean.getCentruId() == -1;
%>

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

<form name="zaforma" action="<%=AppConstants.APP_NAME%>?action=save-deranjament" method=post>
<input type="hidden" name="<%=DeranjamentConstants.ID%>" value="<%=bean.getId()%>" />
<table border=1 cellpadding=2 cellspacing=1 width="100%" class="gSAPTHEAD">
<tr>
    <th class="gsapthead" colSpan=2 height=25 noWrap>&nbsp;Deranjament&nbsp;</th>
</tr>
<tr><td>

<%@ include file="../layout/messages.jspf"%>

<fieldset>
    <legend><b>&nbsp;Localizare Deranjament</b></legend>
    <table width="100%" style="font-size:12" >
        <tr>
            <td>
                Filiala:
                <SELECT name="<%=DeranjamentConstants.FILIALA_ID%>" onchange="fill_sucursala_select()" class="gSAPSelect">
                    <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() == bean.getFilialaId()) ? "selected" : ""%> value="<%=filiale[i].getId()%>"><%=filiale[i].getName()%></OPTION>
                    <%} %>
                </SELECT>&nbsp;&nbsp;

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

                C.E.:
             <span id="centruDiv">
                 <SELECT name="<%=DeranjamentConstants.SECTIE_ID%>" class="gSAPSelect">
                     <OPTION value="-1">Selectati:</OPTION>
                 </SELECT>
             </span>&nbsp;&nbsp;

                <script type="text/javascript">

                    function fill_sucursala_select(selectedValue, disabled, firstShow){
                        resetComboLeaveFirstOption( document.zaforma.<%=DeranjamentConstants.SUCURSALA_ID%>);
                        resetComboLeaveFirstOption( document.zaforma.<%=DeranjamentConstants.SECTIE_ID%>);

                        var filialaId = document.zaforma.<%=DeranjamentConstants.FILIALA_ID%>[ document.zaforma.<%=DeranjamentConstants.FILIALA_ID%>.selectedIndex].value;
                        if( filialaId > -1){
                            var params = "parentId="+filialaId + "&selectName=<%=DeranjamentConstants.SUCURSALA_ID%>"+"&selectedValue=" + selectedValue;
                            //alert( params);

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

                                document.zaforma.<%=DeranjamentConstants.SUCURSALA_ID%>.onchange = function(){
                                    fill_centru_select();
                                    clear_papt_on_center_change();
                                }

                                if( disabled){
                                    removeOptionsLeaveThisOne( document.zaforma.<%=DeranjamentConstants.SUCURSALA_ID%>, selectedValue);
                                }

                                if( firstShow){
                                    fill_centru_select(<%=bean.getCentruId()%>, <%=user.getCcLevel() == RoleConstants.NIVEL_CENTRU || user.getCcLevel() == RoleConstants.NIVEL_FORMATIE%>);
                                }
                            });
                        }
                    }

                    function fill_centru_select(selectedValue, disabled){
                        resetComboLeaveFirstOption( document.zaforma.<%=DeranjamentConstants.SECTIE_ID%>);

                        var sucursalaId = document.zaforma.<%=DeranjamentConstants.SUCURSALA_ID%>[ document.zaforma.<%=DeranjamentConstants.SUCURSALA_ID%>.selectedIndex].value;
                        if( sucursalaId > -1){
                            var params = "parentId="+sucursalaId + "&selectName=<%=DeranjamentConstants.SECTIE_ID%>" + "&selectedValue=" + selectedValue;
                            //alert( params);

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

                                document.zaforma.<%=DeranjamentConstants.SECTIE_ID%>.onchange = function(){
                                    clear_papt_on_center_change();
                                    var index = document.zaforma.<%=DeranjamentConstants.SECTIE_ID%>.selectedIndex;
                                    codCE_centru_selectat = document.zaforma.<%=DeranjamentConstants.SECTIE_ID%>.options[ index].cod;
                                }

                                if( disabled){
                                    removeOptionsLeaveThisOne( document.zaforma.<%=DeranjamentConstants.SECTIE_ID%>, selectedValue)
                                }
                            });
                        }
                    }


                    function removeOptionsLeaveThisOne(oCombo, valueToLeave){
                        for (var i = 0; i < oCombo.options.length;) {
                            if( oCombo.options[i].value == valueToLeave){ i++; }else{ oCombo.options[i] = null;}
                        }
                    }
                </script>

            </td>
        </tr>
    </table>
</fieldset>
<br>
&nbsp;&nbsp;&nbsp;Data deranjament: <%=AppConstants.format(bean.getDataCreareDeranjament())%> (data creare) &nbsp;&nbsp;&nbsp; Nume utilizator: <%=bean.getUserInsert()%><br>
<fieldset>
    <table border="0" width="100%" style="font-size:12" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                Data anuntarii reclamatiei
            </td>
            <td>
                Data sosirii echipei
            </td>
            <td>
                Data rezolvarii
            </td>
            <td width="100px">
                DS - DA
            </td>
            <td width="100px">
                DR - DA
            </td>
        </tr>
        <tr>
            <td>
                <input class="gsapinputtext" type="text" id="<%=DeranjamentConstants.DATA_ANUNTARE%>" name="<%=DeranjamentConstants.DATA_ANUNTARE%>" size="15" value="<%=AppConstants.format( bean.getDataAnuntareReclamatie())%>" onblur="onBlurDataAnuntare( this)">
                <a id="trigger<%=DeranjamentConstants.DATA_ANUNTARE%>" href="#"><img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/cal.gif" width="16" height="16" border="0" alt="Apasa pt. a selecta data"></a>
            </td>
            <td >
                <input class="gsapinputtext" type="text" id="<%=DeranjamentConstants.DATA_SOSIRE_ECHIPA%>" name="<%=DeranjamentConstants.DATA_SOSIRE_ECHIPA%>" size="15" value="<%=AppConstants.format( bean.getDataSosireEchipa())%>" onblur="onBlurDataSosireEchipa( this)">
                <a id="trigger<%=DeranjamentConstants.DATA_SOSIRE_ECHIPA%>" href="#"><img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/cal.gif" width="16" height="16" border="0" alt="Apasa pt. a selecta data"></a>
            </td>
            <td >
                <input class="gsapinputtext" type="text" id="<%=DeranjamentConstants.DATA_REZOLVARE%>" name="<%=DeranjamentConstants.DATA_REZOLVARE%>" size="15" value="<%=AppConstants.format( bean.getDataRezolvare())%>" onblur="onBlurDataRezolvare( this)">
                <a id="trigger<%=DeranjamentConstants.DATA_REZOLVARE%>" href="#"><img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/cal.gif" width="16" height="16" border="0" alt="Apasa pt. a selecta data"></a>
            </td>
            <td width="100px">
                <%
                    final long durataSosire = bean.getDurataSosireEchipa();
                    final long durataRemediere = bean.getDurataRemediere();
                %>
                <input class="gsapinputtext" readonly type="text" size="15" id="durataSosire" value="<%=durataSosire/60%>:<%=durataSosire%60%>">
            </td>
            <td width="100px">
                <input class="gsapinputtext" readonly type="text" size="15" id="durataRezolvare" value="<%=durataRemediere/60%>:<%=durataRemediere%60%>">
            </td>
        </tr>

        <script type="text/javascript">
            Calendar.setup(
                    {
                        inputField  : "<%=DeranjamentConstants.DATA_ANUNTARE%>",       // ID of the input field
                        ifFormat    : "%d.%m.%Y %H:%M",    // the date format
                        button      : "trigger<%=DeranjamentConstants.DATA_ANUNTARE%>",        // ID of the button
                        showsTime   : true,
                        onUpdate    : onDataAnuntareUpdate
                    }
            );

            Calendar.setup(
                    {
                        inputField  : "<%=DeranjamentConstants.DATA_SOSIRE_ECHIPA%>",       // ID of the input field
                        ifFormat    : "%d.%m.%Y %H:%M",    // the date format
                        button      : "trigger<%=DeranjamentConstants.DATA_SOSIRE_ECHIPA%>",        // ID of the button
                        showsTime   : true,
                        onUpdate    : onDataSosireUpdate
                    }
            );

            Calendar.setup(
                    {
                        inputField  : "<%=DeranjamentConstants.DATA_REZOLVARE%>",       // ID of the input field
                        ifFormat    : "%d.%m.%Y %H:%M",    // the date format
                        button      : "trigger<%=DeranjamentConstants.DATA_REZOLVARE%>",        // ID of the button
                        showsTime   : true,
                        onUpdate    : onDataRezolvareUpdate
                    }
            );


            function onBlurDataAnuntare( input){
                onDataAnuntareUpdate();
            }

            function onBlurDataSosireEchipa( input){
                onDataSosireUpdate();
            }

            function onBlurDataRezolvare( input){
                onDataRezolvareUpdate();
            }


            function getDate(inputId){
                var strDate = $( inputId).value;
                if( strDate.length != 16) return null;
                return Date.parseDate(strDate, "%d.%m.%Y %H:%M");
            }

            var dataAnuntare  = null;
            var dataSosire    = null;
            var dataRezolvare = null;

            function getDates(){
                dataAnuntare  = getDate( '<%=DeranjamentConstants.DATA_ANUNTARE%>');
                dataSosire    = getDate( '<%=DeranjamentConstants.DATA_SOSIRE_ECHIPA%>');
                dataRezolvare = getDate( '<%=DeranjamentConstants.DATA_REZOLVARE%>');
            }

            function updateDurate(){
                if( dataSosire != null ){
                    var durataSosire = (dataSosire - dataAnuntare)/60000;

                    $('durataSosire').value = "" + Math.floor( durataSosire/60) + ":" + (durataSosire%60);
                }

                if( dataRezolvare != null){
                    var durataRezolvare = (dataRezolvare - dataAnuntare)/60000;

                    $('durataRezolvare').value = "" + Math.floor( durataRezolvare/60) + ":" + (durataRezolvare%60);
                }
            }

            function onDataAnuntareUpdate(){
                getDates();
                dataSosireValida();
                dataRezolvareValida();

                updateDurate();
            }

            function dataSosireValida(){
                try{
                    if( dataAnuntare != null && dataAnuntare < dataSosire){
                        return true;
                    } else {
                        $('<%=DeranjamentConstants.DATA_SOSIRE_ECHIPA%>').value = '';
                        $('durataSosire').value = "0:0";
                        dataSosire = null;
                    }
                }catch (e){
                }
                return false;
            }

            function onDataSosireUpdate(){
                getDates();
                dataRezolvareValida();
                if( dataSosireValida()){
                    updateDurate();
                }
            }

            function dataRezolvareValida(){
                try{
                    if( dataAnuntare != null && dataAnuntare < dataRezolvare && dataSosire != null && dataSosire < dataRezolvare){
                        return true;
                    } else {
                        $('<%=DeranjamentConstants.DATA_REZOLVARE%>').value = '';
                        $('durataRezolvare').value = "0:0";
                        dataRezolvare = null;
                    }
                }catch( e){
                }
                return false;
            }

            function onDataRezolvareUpdate(){
                getDates();
                if( dataRezolvareValida()){
                    updateDurate();
                }
            }
        </script>

    </table>
</fieldset>

<table style="font-size:12px">
    <tr>
        <td align="right" width="300" >
            Efectul asupra consumatorilor - <br>
            Cu intreruperea tensiunii
        </td>
        <td width="100">
            <input type="checkbox" name="<%=DeranjamentConstants.EF_ASUPRA_CONS%>" value=1 <%=bean.getEfectulAsupraConsumatorilor() > 0 ? "checked" : ""%>>
        </td>
        <td >
            &nbsp;&nbsp;&nbsp;&nbsp; Tip deranjament: <br>
            <input type="radio" name="<%=DeranjamentConstants.TIP_DERANJAMENT%>" value="<%=Deranjament.TIP_COLECTIV%>" id="radioColectiv"  <%=bean.getTipDeranjament() == Deranjament.TIP_COLECTIV ? "checked" : ""%> > <label for="radioColectiv">Colectiv</label>   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="radio" name="<%=DeranjamentConstants.TIP_DERANJAMENT%>" value="<%=Deranjament.TIP_INDIVIDUAL%>" id="radioIndividual"  <%=bean.getTipDeranjament() == Deranjament.TIP_INDIVIDUAL ? "checked" : ""%>> <label for="radioIndividual">Individual</label>
        </td>

        <td>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input disabled id="fortaMajora" type="checkbox" name="<%=DeranjamentConstants.FORTA_MAJORA%>" value="1" <%=bean.getFortaMajora() == 1 ? "checked" : ""%>>
            <label for="fortaMajora">Forta majora</label>
			
			<span style="width: 150px">&nbsp;</span>
			<input id="conditiiMeteo" type="checkbox" name="conditii_meteo" value="1" <%=deranjamentFieldsView.getConditiiMeteoView(bean.getId()) > 0 ? "checked" : ""%> />
			<label for="conditiiMeteo">Conditii meteo deosebite</label>
        </td>
    </tr>
</table>

<fieldset style="padding: 10px ">
    <legend><b>&nbsp;Categoria consumatorilor intrerupti</b></legend>
    <table  border="0" align="center"  style="font-size:12px">
        <tr>
            <td>
                Nr. Case
            </td>
            <td >
                Nr. Ap.
            </td>
            <td >
                Nr. Mici
            </td>
            <td >
                Nr. Mari
            </td>
            <td width="100%" align="center" colspan="2">
                Mediul in care sunt amplasati consumatorii intrerupti
            </td>
        </tr>
        <tr>
            <td>
                <input class="gsapinputtext" type="text" id="<%=DeranjamentConstants.NR_CASE%>" name="<%=DeranjamentConstants.NR_CASE%>" size="10" value="<%=bean.getNrCase()%>">
            </td>
            <td>
                <input class="gsapinputtext" type="text" id="<%=DeranjamentConstants.NR_AP%>" name="<%=DeranjamentConstants.NR_AP%>" size="10" value="<%=bean.getNrAp()%>">
            </td>
            <td>
                <input class="gsapinputtext" type="text" id="<%=DeranjamentConstants.NR_MICI%>" name="<%=DeranjamentConstants.NR_MICI%>" size="10" value="<%=bean.getNrMici()%>">
            </td>
            <td>
                <input class="gsapinputtext" type="text" id="<%=DeranjamentConstants.NR_MARI%>" name="<%=DeranjamentConstants.NR_MARI%>" size="10" value="<%=bean.getNrMari()%>">
            </td>
            <td align="center" colspan="2">
                <input type="radio" name="<%=DeranjamentConstants.MEDIUL%>" value="<%=Deranjament.MEDIU_URBAN%>" id="radioUrban" <%=bean.getMediul() == Deranjament.MEDIU_URBAN ? "checked" : ""%> > <label for="radioUrban" >Urban</label>
                <input style="margin-left: 50px;" type="radio" name="<%=DeranjamentConstants.MEDIUL%>" value="<%=Deranjament.MEDIU_RURAL%>" id="radioRural" <%=bean.getMediul() == Deranjament.MEDIU_RURAL ? "checked" : ""%>> <label for="radioRural">Rural</label>
            </td>
        </tr>
    </table>
</fieldset>
<br/>
<br/>
<fieldset style="padding: 10px ">
    <legend><b>&nbsp;Energia electrica nelivrata consumatorilor intrerupti (kWh)</b></legend>
    <table   border="0" width="80%" style="font-size:12px">
        <tr align="left">
            <td>
                Casnici
            </td>
            <td>
                Mici consumatori
            </td>
            <td>
                Mari consumatori
            </td>
        </tr>
        <tr align="left">
            <td>
                <input class="gsapinputtext" type="text" id="<%=DeranjamentConstants.ENC_CASNICI%>" name="<%=DeranjamentConstants.ENC_CASNICI%>" size="10" value="<%=bean.getEncCasnici()%>">
            </td>
            <td>
                <input class="gsapinputtext" type="text" id="<%=DeranjamentConstants.ENC_MICI%>" name="<%=DeranjamentConstants.ENC_MICI%>" size="10" value="<%=bean.getEncMici()%>">
            </td>
            <td>
                <input class="gsapinputtext" type="text" id="<%=DeranjamentConstants.ENC_MARI%>" name="<%=DeranjamentConstants.ENC_MARI%>" size="10" value="<%=bean.getEncMari()%>">
            </td>
            <td>
                <input type="button" onclick="calculEnergieNelivrata()" value="Calculeaza">
            </td>
        </tr>
        <tr><td colspan="5"><hr/></td></tr>
        <tr align="left">
            <td>
                <input class="gsapinputtext" type="text" id="<%=DeranjamentConstants.EPC_CASNICI%>" name="<%=DeranjamentConstants.EPC_CASNICI%>" size="10" value="<%=bean.getEpcCasnici()%>">
            </td>
            <td>
                <input class="gsapinputtext" type="text" id="<%=DeranjamentConstants.EPC_MICI%>" name="<%=DeranjamentConstants.EPC_MICI%>" size="10" value="<%=bean.getEpcMici()%>">
            </td>
            <td>
                <input class="gsapinputtext" type="text" id="<%=DeranjamentConstants.EPC_MARI%>" name="<%=DeranjamentConstants.EPC_MARI%>" size="10" value="<%=bean.getEpcMari()%>">
            </td>
            <td>(Per consumator [kW])</td>
        </tr>
    </table>
</fieldset>
<br/>
<br/>

<script type="text/javascript">

    function calculEnergieNelivrata() {
        if ( confirm("Calculare energie nelivrata. Se vor suprascrie valorile curente. Continuati ?")) {
            getDates();

            if (dataRezolvare == null) {
                alert("Data rezolvare trebuie sa fie completata pentru a putea calcula energia nelivrata.");
                return;
            }

            var durataRezolvare = (dataRezolvare - dataAnuntare) / 60000;

            var nr_case = 1.0*$("<%=DeranjamentConstants.NR_CASE%>").value;
            var nr_ap = 1.0*$("<%=DeranjamentConstants.NR_AP%>").value;
            var nr_mici = 1.0*$("<%=DeranjamentConstants.NR_MICI%>").value;
            var nr_mari = 1.0*$("<%=DeranjamentConstants.NR_MARI%>").value;

            var epc_case = 1.0*$("<%=DeranjamentConstants.EPC_CASNICI%>").value;
            var epc_mici = 1.0*$("<%=DeranjamentConstants.EPC_MICI%>").value;
            var epc_mari = 1.0*$("<%=DeranjamentConstants.EPC_MARI%>").value;



            try{
                var r = (nr_case + nr_ap)*durataRezolvare*epc_case/60;
                $("<%=DeranjamentConstants.ENC_CASNICI%>").value = Math.round (r*1000) / 1000; //r.toFixed( 3);


                var r = (nr_mici)*durataRezolvare*epc_mici/60;
                $("<%=DeranjamentConstants.ENC_MICI%>").value = Math.round (r*1000) / 1000; //r.toFixed( 3);



                var r = (nr_mari)*durataRezolvare*epc_mari/60;
                $("<%=DeranjamentConstants.ENC_MARI%>").value = Math.round (r*1000) / 1000; //r.toFixed( 3);
            }catch(e){
                console.log("Eroare calcujl energie", e);
            }
        }
    }

</script>

<fieldset style="padding: 10px  " >
    <legend><b>&nbsp;Constatari ale echipei care a rezolvat deranjamentul</b></legend>
    <table border="0" style="font-size:12px" >
        <tr>
            <td colspan="10">
                Descriere Deranjament<br>
                <textarea rows="3" cols="70" name="<%=DeranjamentConstants.DESCRIERE%>"><%=AppConstants.format(bean.getDescriere())%></textarea>
            </td>
        </tr>
    </table>

    <table style="font-size: 12px" width="100%">
        <tr>
            <td>
                Localizare defect (initial)
            </td>
        </tr>
        <tr>
            <td>
                Categoria de instalatii
            </td>
            <td>
                Nr. / Denumire instalatie
            </td>
            <td>PT</td>
            <td>
                Interventie la echipamentele
            </td>
        </tr>
        <tr>
            <td>
                <select class="gSAPSelect" name="<%=DeranjamentConstants.CAT_INSTALATII%>" onChange="fillElementeSelect(this)">
                    <OPTION VALUE="-1">Selectati:</OPTION>
                    <% for (int i = 1; i < DeranjamentConstants.ARR_CAT_INSTALATII.length; i++) {
                        final String catInstalatii = DeranjamentConstants.ARR_CAT_INSTALATII[i];%>
                    <option value="<%=i%>" <%=bean.getCatInstalatii() == i ? "selected" : ""%> ><%=catInstalatii%></option>
                    <% }%>
                </select>
            </td>
            <td>
                <input type="text" size="20" class="gsapinputtext" name="<%=DeranjamentConstants.NR_DENUMIRE%>" value="<%=AppConstants.format( bean.getNrDenumire())%>">
            </td>
            <td>

                <input type="hidden" id="<%=DeranjamentConstants.PAPT_ID%>" name="<%=DeranjamentConstants.PAPT_ID%>"
                       value="<%=bean.getPtId()%>"/>
                <input type="hidden" id="<%=DeranjamentConstants.PAPT_COD%>" name="<%=DeranjamentConstants.PAPT_COD%>"
                       value="<%=AppConstants.format( bean.getPtDesc() )%>"/>
                <input class="gsapinputtext" id="<%=DeranjamentConstants.PAPT_DESC%>" name="<%=DeranjamentConstants.PAPT_DESC%>"
                       maxlength="100" style="width: 285px" sugestWidth="285"
                       value="<%=AppConstants.format( bean.getPtDesc() )%>"/>


                <script type="text/javascript">
                    var codCE_centru_selectat = null;



                    function initPaPtSuggest(){

                        /* Suggesters */
                        var paptDataProvider = new AjaxDataProvider(function () {
                            // papt's parent is linie
                            var parentLinieId = -2;
                            var parentStatieId = -2;

                            if ( !codCE_centru_selectat){
                                try {
                                    var index = $('<%=DeranjamentConstants.SECTIE_ID%>').selectedIndex;
                                    codCE_centru_selectat = $('<%=DeranjamentConstants.SECTIE_ID%>').options[ index].getAttribute("codSAP");
                                }catch(err){
                                    alert('Selectati un centru.');
                                }
                            }

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

                        new InputSugest($("<%=DeranjamentConstants.PAPT_DESC%>"), $("papt<%=DeranjamentConstants.PAPT_ID%>"), paptDataProvider, {
                            isValidSearchInput: function () {
                                return true;
                            },
                            beforeRequest: function () {
                                $("<%=DeranjamentConstants.PAPT_ID%>").value = '';
                                $("<%=DeranjamentConstants.PAPT_COD%>").value = '';
                            },

                            onSelect: function (item) {

                                $("<%=DeranjamentConstants.PAPT_COD%>").value = item.cod;
                                $("<%=DeranjamentConstants.PAPT_ID%>").value = item.key;
                                $("<%=DeranjamentConstants.PAPT_DESC%>").value = item.value;

                            }
                        });
                    }

                    function clear_papt_on_center_change(){
                        $("<%=DeranjamentConstants.PAPT_COD%>").value = '';
                        $("<%=DeranjamentConstants.PAPT_ID%>").value = '';
                        $("<%=DeranjamentConstants.PAPT_DESC%>").value = '';
                    }

                </script>
            </td>
            <td>
                <input type="checkbox" size="20" name="<%=DeranjamentConstants.INTERVENTIE%>" value="1" <%=bean.getInterventieLaEchipamente() > 0 ? "checked" : ""%>  > de masura
            </td>
        </tr>

        <tr>
            <td>
                Instalatia
            </td>
            <td>
                Elementul defect
            </td>
            <td>
                Componenta defecta
            </td>
        </tr>
        <tr>
            <td>
                <SELECT NAME="<%=DeranjamentConstants.INSTALATIA%>" class="gSAPSelect">
                    <option value="-1">Selectati:</option>
                    <% for (int i = 1; i < DeranjamentConstants.INSTALATII.length; i++) {
                        final String instalatia = DeranjamentConstants.INSTALATII[i];%>
                    <option value="<%=i%>" <%=bean.getInstalatia() == i ? "selected" : ""%> ><%=instalatia%></option>
                    <% } %>
                </select>
            </td>
            <td>
                <select name="<%=DeranjamentConstants.EL_DEFECT%>" class="gSAPSelect" onChange="fillComponentaSelect(this);">
                    <OPTION value="-1">Selectati:</OPTION>
                </select>
            </td>
            <td>
                <select name="<%=DeranjamentConstants.COMPONENTA_DEFECTA%>" class="gSAPSelect">
                    <option value="-1">Selectati:</option>
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                Cauza
            </td>
            <td>
                Factorii favorizati
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <select name="<%=DeranjamentConstants.CAUZA%>" class="gSAPSelect">
                    <option value="-1">Selectati:</option>
                    <% for (int i = 1; i < cauzeOptions.length; i++) {
						if(i != 8) {
							final String s = cauzeOptions[i]; %>
                    <option value="<%=i%>" <%=bean.getCauza() == i ? "selected" : ""%> ><%=s%></option>
                    <%}} %>
                </select>
            </td>
            <td>
                <select name="<%=DeranjamentConstants.FACTORII_FAVORIZATI%>" class="gSAPSelect">
                    <option value="-1">Selectati:</option>
                    <% for (int i = 1; i < DeranjamentConstants.FACTORI_FAVORIZATI.length; i++) {
                        final String s = DeranjamentConstants.FACTORI_FAVORIZATI[i]; %>
                    <option value="<%=i%>" <%=bean.getFactoriiFavozizati() == i ? "selected" : ""%> ><%=s%></option>
                    <%} %>
                </select>
            </td>
        </tr>
    </table>
    <br/>

    <fieldset style="width:95%; padding: 10px ">
        <legend><b>&nbsp;Apartenenta instalatiilor in care a fost localizata cauza deranjamentului</b></legend>
        <table  style="font-size:12px">
            <tr>
                <td align="center">
                    <input type="radio" name="<%=DeranjamentConstants.APARTENENTA%>" id="radioApartineElectrica"  value="<%=Deranjament.APARTINE_ELECTRICA%>" <%=bean.getApartenentaInstalatie() == Deranjament.APARTINE_ELECTRICA ? "checked" : ""%>   >
                    <label for="radioApartineElectrica">"Electrica .... "</label>
                </td>
                <td width="70px">
                </td>
                <td>
                    <input type="radio" name="<%=DeranjamentConstants.APARTENENTA%>" id="radioApartineConsumator" value="<%=Deranjament.APARTINE_CONSUMATOR%>" <%=bean.getApartenentaInstalatie() == Deranjament.APARTINE_CONSUMATOR ? "checked" : ""%>   >
                    <label for="radioApartineConsumator">Consumator</label>
                </td>
            </tr>
        </table>
    </fieldset>
    <br/>
    <br/>
</fieldset>
<br/>
<br/>

<fieldset style="padding: 10px">
    <table style="font-size:12px">
        <tr>
            <td  colspan="3">
                Mod rezolvare<br>
                <textarea rows="3" cols="70" name="<%=DeranjamentConstants.MOD_REZOLVARE%>"><%=AppConstants.format(bean.getModRezolvare())%></textarea>
            </td>
        </tr>
        <tr valign="bottom">
            <td>
                Masuri luate
            </td>
            <td nowrap>
                Echipa care a rezolvat deranjamentul &nbsp;&nbsp;<br>[Numele si prenumele sefului]
            </td>
            <td>Provizorat</td>
        </tr>
        <tr>
            <td>
                <select class="gSAPSelect" name="<%=DeranjamentConstants.MASURI_LUATE%>" onchange="onMasuraSelected()">
                    <option value="-1">Selectati:</option>
                    <% for (int i = 1; i < DeranjamentConstants.MASURI_LUATE_ARR.length; i++) {
                        final String s = DeranjamentConstants.MASURI_LUATE_ARR[i]; %>
                    <option value="<%=i%>" <%=bean.getMasuriLuate() == i ? "selected" : ""%> ><%=s%></option>
                    <%} %>
                </select>
            </td>

            <script type="text/javascript">
                function onMasuraSelected(){
                    document.zaforma.<%=DeranjamentConstants.PROVIZORAT%>.disabled = (document.zaforma.<%=DeranjamentConstants.MASURI_LUATE%>.selectedIndex == 1);
                    //REMEDIAT IN SOLUTIE DEFINITIVA
                }
            </script>
            <td>
                <input class="gsapinputtext" type="text" size="50" name="<%=DeranjamentConstants.ECHIPA%>" value="<%=AppConstants.format( bean.getEchipa())%>">
            </td>
            <td>
                <input type="checkbox" name="<%=DeranjamentConstants.PROVIZORAT%>" value="1" <%=bean.getProvizorat() > 0 ? "checked" : ""%> >
            </td>
        </tr>
        <tr>
            <td colspan="3">
                Observatii<br>
                <textarea rows="3" cols="70" name="<%=DeranjamentConstants.OBSERVATII%>"><%=AppConstants.format(bean.getObservatii())%></textarea>
            </td>
        </tr>
    </table>
</fieldset>

<div align="center">

    <%if( UserUtils.hasRoleAdminCallcenter(user) || (UserUtils.hasRoleUtilizatorCallcenter(user) && UserUtils.hasCcRightEditareDeranjamente(user))){%>
    <input type="button" value="Salvează" onclick="if( dataOK()) document.zaforma.submit()" >
    <%}%>

    <% if( bean.getId() == -1){%> &nbsp;
    <input type="button" value="Renunță" onclick=" window.location = '<%=AppConstants.APP_NAME%>?action=show-deranjamente';" >
    <%}%>
</div>
</td>
</tr>
</TABLE>
</FORM>

<script type="text/javascript" src="<%=AppConstants.WEB_RESOURCE_PATH%>/scripts/deranjamentDetail.js"></script>

<script type="text/javascript">
    var elementeDefecte = new Array(
            <% for(int i = 1; i < DeranjamentConstants.ARR_ELEMENTE_DEFECTE.length; i++) {
                final String[] strings = DeranjamentConstants.ARR_ELEMENTE_DEFECTE[i];
                %>
            new Array(
                    new Array("Selectati", -1),
                    <%for(int j = 1; j < strings.length; j++) {
                        final String string = strings[j];%>
                    new Array("<%=ro.emid.common.utils.Util.escapeToJs( string)%>", <%=j%>) <%=j < strings.length - 1 ? "," : ""%>
                    <%}%>
            )<%=i < DeranjamentConstants.ARR_ELEMENTE_DEFECTE.length - 1 ? "," : ""%>
            <%
            }
            %>
    );

    var mesajAvertizare = null;
    var countMesageAvertizare = 0;
    function appendMesajAvertizare( newMessage){
        if( mesajAvertizare == null) mesajAvertizare = "Atentie. Verificati datele introduse.";
        mesajAvertizare += "\n   - "+newMessage;
        countMesageAvertizare++;
    }

    function dataOK(){
        countMesageAvertizare = 0;
        mesajAvertizare = null;
        if( getSelectedValue( document.zaforma.<%=DeranjamentConstants.FILIALA_ID%>) == -1){
            alert( 'Selectati o filiala!');
            document.zaforma.<%=DeranjamentConstants.FILIALA_ID%>.focus();
            return false;
        }

        if( getSelectedValue( document.zaforma.<%=DeranjamentConstants.SUCURSALA_ID%>) == -1){
            alert( 'Selectati o sucursala !');
            document.zaforma.<%=DeranjamentConstants.SUCURSALA_ID%>.focus();
            return false;
        }

        if( getSelectedValue( document.zaforma.<%=DeranjamentConstants.SECTIE_ID%>) == -1){
            alert( 'Selectati o sectie/un centru !');
            document.zaforma.<%=DeranjamentConstants.SECTIE_ID%>.focus();
            return false;
        }

        if( !(document.zaforma.<%=DeranjamentConstants.TIP_DERANJAMENT%>[0].checked || document.zaforma.<%=DeranjamentConstants.TIP_DERANJAMENT%>[1].checked)){
            alert( 'Selectati tipul deranjamentului (Colectiv/Individual)');
            return false;
        }

        if( !(document.zaforma.<%=DeranjamentConstants.MEDIUL%>[0].checked || document.zaforma.<%=DeranjamentConstants.MEDIUL%>[1].checked)){
            alert( 'Selectati Mediul in care sunt amplasati consumatorii intrerupti (urban / rural)');
            return false;
        }

        if( !(document.zaforma.<%=DeranjamentConstants.APARTENENTA%>[0].checked || document.zaforma.<%=DeranjamentConstants.APARTENENTA%>[1].checked)){
            alert( 'Selectati apartenenta instalatiilor in care a fost localizata cauza deranjamentului');
            return false;
        }


        function getDiferentaOre( data1, data2){
            if( data1 == null || data2 == null) return 0;

            return (data1 - data2)/(60000*60);
        }

        getDates();
        if( 12 < getDiferentaOre( dataSosire, dataAnuntare)){
            appendMesajAvertizare( "Durata peste 12h intre data sosire si data anuntare !");
        }

        if( 12 < getDiferentaOre( dataRezolvare, dataAnuntare)){
            appendMesajAvertizare( "Durata peste 12h intre data rezolvare si data anuntare !");
        }

        if( document.zaforma.<%=DeranjamentConstants.EF_ASUPRA_CONS%>.checked){
            //check categoria consumatorilor intrerupti
            var categoriaConsumatoriValid = Validation.batchValidation(
                    function(){
                        return Validation.validate( document.zaforma.<%=DeranjamentConstants.NR_CASE%>, Validation.isIntAndNotNegativ, "Introduceti corect Nr. case. " )
                    },
                    function(){
                        return Validation.validate( document.zaforma.<%=DeranjamentConstants.NR_AP%>, Validation.isIntAndNotNegativ, "Introduceti corect Nr. Ap. " );
                    },

                    function(){
                        return Validation.validate( document.zaforma.<%=DeranjamentConstants.NR_MICI%>, Validation.isIntAndNotNegativ, "Introduceti corect Nr. mici. " );
                    },

                    function(){
                        return Validation.validate( document.zaforma.<%=DeranjamentConstants.NR_MARI%>, Validation.isIntAndNotNegativ, "Introduceti corect Nr. mari. " )
                    }
            );

            if( !categoriaConsumatoriValid) return false;

            var nrConsumatori = 0;
            try{
                nrConsumatori = Validation.getIntValue( document.zaforma.<%=DeranjamentConstants.NR_AP%>) +
                        Validation.getIntValue( document.zaforma.<%=DeranjamentConstants.NR_CASE%>) +
                        Validation.getIntValue( document.zaforma.<%=DeranjamentConstants.NR_MICI%>) +
                        Validation.getIntValue( document.zaforma.<%=DeranjamentConstants.NR_MARI%>);
            }catch(e){}

            if( nrConsumatori <= 0 ){
                //pt camp obligatoriu
                //alert( "Introducedti valori la Categoria consumatorilor intrerupti ");
                //setFocus( document.zaforma.<%=DeranjamentConstants.NR_CASE%>);
                //return false;
                appendMesajAvertizare( "Introducedti valori la Categoria consumatorilor intrerupti ");
            }


            if(
                    !Validation.validate( document.zaforma.<%=DeranjamentConstants.ENC_CASNICI%>, Validation.isFloatAndNotNegativ, "Introduceti corect Energia nelivrata pentru consumatori casnici. " ) ||
                            !Validation.validate( document.zaforma.<%=DeranjamentConstants.ENC_MICI%>, Validation.isFloatAndNotNegativ, "Introduceti corect Energia nelivrata mici consumatori. " ) ||
                            !Validation.validate( document.zaforma.<%=DeranjamentConstants.ENC_MARI%>, Validation.isFloatAndNotNegativ, "Introduceti corect Energia nelivrata pentru mari consumatori." )
                    ){
                return false;
            }

            var enNelivrata = 0;
            try{
                enNelivrata = Validation.getFloatValue( document.zaforma.<%=DeranjamentConstants.ENC_CASNICI%>) +
                        Validation.getFloatValue( document.zaforma.<%=DeranjamentConstants.ENC_MARI%>)
                Validation.getFloatValue( document.zaforma.<%=DeranjamentConstants.ENC_MICI%>);
            }catch(e){}

            if( enNelivrata <= 0){
                //alert("Introduceti valori la Energia electrica nelivrata consumatorilor intrerupti (kWh)");
                //setFocus( document.zaforma.<%=DeranjamentConstants.ENC_CASNICI%>);
                //return false;
                appendMesajAvertizare( "Introduceti valori la Energia electrica nelivrata consumatorilor intrerupti (kWh)" )
            }
        }

        if( document.zaforma.<%=DeranjamentConstants.CAT_INSTALATII%>.selectedIndex == 0){
            //alert("Selectati Categoria de Instalatii");
            //setFocus( document.zaforma.<%=DeranjamentConstants.CAT_INSTALATII%>);
            //return false;
            appendMesajAvertizare( "Selectati 'Categoria de Instalatii'");
        }

        if( document.zaforma.<%=DeranjamentConstants.INSTALATIA%>.selectedIndex == 0){
            //alert("Selectati Instalatia");
            //setFocus( document.zaforma.<%=DeranjamentConstants.INSTALATIA%>);
            //return false;
            appendMesajAvertizare( "Selectati 'Instalatia'");
        }

        if( document.zaforma.<%=DeranjamentConstants.CAUZA%>.selectedIndex == 0){
            //alert("Selectati Cauza producerii deranjamentului");
            //setFocus( document.zaforma.<%=DeranjamentConstants.CAUZA%>);
            //return false;
            appendMesajAvertizare("Selectati 'Cauza' producerii deranjamentului");
        }

        if( $("radioColectiv").checked && Validation.isEmpty( document.zaforma.<%=DeranjamentConstants.NR_DENUMIRE%>)){
            appendMesajAvertizare("Ati selectat deranjament colectiv. Completati 'Nr./ Denumire'");
        }

        if( countMesageAvertizare > 0){
            mesajAvertizare += "\n\nContinuati operatiunea ?";
            return confirm( mesajAvertizare);
        }
        return true;
    }

    function fillElementeSelect(oParentSelect){
        resetComboLeaveFirstOption(oParentSelect.form.<%=DeranjamentConstants.COMPONENTA_DEFECTA%>);
        fillSelectFromArray(oParentSelect.form.<%=DeranjamentConstants.EL_DEFECT%>, elementeDefecte[oParentSelect.selectedIndex-1])
    }

    function fillComponentaSelect(oParentSelect){
        clearCombo( oParentSelect.form.<%=DeranjamentConstants.COMPONENTA_DEFECTA%>);
        if( oParentSelect.form.<%=DeranjamentConstants.CAT_INSTALATII%>.selectedIndex > 0){
            fillSelectFromArray(oParentSelect.form.<%=DeranjamentConstants.COMPONENTA_DEFECTA%>, componente[ oParentSelect.form.<%=DeranjamentConstants.CAT_INSTALATII%>.selectedIndex - 1][oParentSelect.selectedIndex-1]);
        }
    }


    <%if( user.getCcLevel() != RoleConstants.NIVEL_FULL && bean.getFilialaId() > 0){%>
    removeOptionsLeaveThisOne( document.zaforma.<%=DeranjamentConstants.FILIALA_ID%>, <%=bean.getFilialaId()%>);
    <%--removeOptionsLeaveThisOne( document.zaforma.<%=DeranjamentConstants.FILIALA_ID%>, -1);--%>
    <%}%>
    fill_sucursala_select(<%=bean.getSucursalaId()%>, <%=user.getCcLevel() == RoleConstants.NIVEL_SUCURSALA || user.getCcLevel() == RoleConstants.NIVEL_CENTRU || user.getCcLevel() == RoleConstants.NIVEL_FORMATIE%>, true);

    fillElementeSelect( document.zaforma.<%=DeranjamentConstants.CAT_INSTALATII%>);
    selectOption( document.zaforma.<%=DeranjamentConstants.EL_DEFECT%>, <%=bean.getElementulDefect()%>);

    fillComponentaSelect( document.zaforma.<%=DeranjamentConstants.EL_DEFECT%>);
    selectOption( document.zaforma.<%=DeranjamentConstants.COMPONENTA_DEFECTA%>, <%=bean.getComponentaDefecta()%>);


    setTimeout(initPaPtSuggest, 500);

</script>

<%}%>




