<%@ page import="ro.emid.common.components.sugest.base.SugestConstants" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.constants.EvenimentDispecerConstants" %>
<%@ page import="ro.emid.common.constants.GlobalAppConstants" %>
<%@ page import="ro.emid.common.db.EvenimentDispecerAccessDB" %>
<%@ page import="ro.emid.common.types.*" %>
<%@ page import="ro.emid.common.types.binder.EvenimentDispecerBinder" %>
<%@ page import="ro.emid.common.utils.EvenimentDispecerUtils" %>
<%@ page import="ro.emid.common.utils.RequestUtils" %>
<%@ page import="ro.emid.common.utils.StringUtils" %>
<%@ page import="ro.emid.lpod.LpodSessionUtils" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="ro.emid.lib.view.EvenimentDispecerxFieldsView" %>

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

<%
    final EvenimentDispecerBinder binder = (EvenimentDispecerBinder) session.getAttribute(EvenimentDispecerConstants.BINDER);
    final EvenimentDispecer bean = (EvenimentDispecer) binder.getBean();

    final CategoryOD locatieCurenta = LpodSessionUtils.OD.getLocatieCurenta(request.getSession());
    final List gestList = EvenimentDispecerAccessDB.getAllGestionari(locatieCurenta.getId());
	
	EvenimentDispecerxFieldsView evView = new EvenimentDispecerxFieldsView();
	ArrayList<HashMap> intervale = evView.getIntervale(bean.getId());
%>

<emid:template-od title="Eveniment">
    <jsp:attribute name="breadcrumb">
        <emid:localizare-od/>
    </jsp:attribute>
    <jsp:attribute name="body">

<FORM name="zaforma" action="<%=AppConstants.APP_NAME%>?action=save-eveniment-dispecer" method=post>
    <INPUT type=hidden name="<%=EvenimentDispecerConstants.JSP.ID%>" value="<%=bean.getId()%>">
    <INPUT type=hidden name="<%=EvenimentDispecerConstants.JSP.FILIALA_ID%>" value="<%=bean.getFilialaId()%>">
    <INPUT type=hidden name="<%=EvenimentDispecerConstants.JSP.SUCURSALA_ID%>" value="<%=bean.getSucursalaId()%>">
    <INPUT type=hidden name="<%=EvenimentDispecerConstants.JSP.CENTRU_ID%>" value="<%=bean.getCentruId()%>">
    <INPUT type=hidden name="<%=EvenimentDispecerConstants.JSP.FORMATIA_DE_LUCRU_ID%>" value="<%=bean.getFormatiaDeLucruId()%>">

<table class=gSAPTHEAD cellSpacing=1 cellPadding=2 width="100%" border=1>
    <tbody>
    <TR>
        <TH class=gsapthead noWrap colSpan=2 height=25>&nbsp;Fişa Eveniment:&nbsp; </TH>
    </TR>
        <TR>
            <TD width="25%"><b>1.</b> Numar curent eveniment:</TD>
            <TD width="100%">
                <input type="text" readonly name="<%=EvenimentDispecerConstants.JSP.NUMAR%>"
                       value="<%=EvenimentDispecerUtils.getNumar(bean)%>"/>
            </TD>
        </TR>

        <TR>
            <TD width="25%"><b>2.</b> Data producere:</TD>
            <TD width="100%">
                <emid:datetimefield name="<%=EvenimentDispecerConstants.JSP.DATA_PRODUCERE%>" onchange="updateDurataEveniment()" onblur="updateDurataEveniment()" value="<%=bean.getDataProducere()%>"/>
                &nbsp;&nbsp;&nbsp;
                Data realimentare:
                <emid:datetimefield name="<%=EvenimentDispecerConstants.JSP.DATA_REALIMENTARE%>" onchange="updateDurataEveniment()" onblur="updateDurataEveniment()" value="<%=bean.getDataRealimentare()%>"/>
                &nbsp;&nbsp;&nbsp;
                Data Revenire SN:
                <emid:datetimefield name="<%=EvenimentDispecerConstants.JSP.DATA_REVENIRE_SN%>"  value="<%=bean.getDataRevenireSN()%>"/>
				
				
				
				
			</td>
        </TR>
        <TR>
            <TD vAlign=top><b>3.</b> Localizare Eveniment:&nbsp;</TD>
            <TD>
                <TABLE class=gsapthead>
                    <TBODY>
                    <TR valign="top" class=gSAPGroup>
                        <TD>
                            <table style="font-size:13px">
                                <tr>
                                    <td valign="top" nowrap>
                                        <table>
                                            <tr>
                                                <td>
                                                    <input name="<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE_RADIO%>"
                                                           type="radio"
                                                           value="<%=TipInstalatie.STATIE%>"
                                                           id="radioStatie" <%=TipInstalatie.STATIE == bean.getTipInstalatieRadio()?"checked":""%>
                                                           onClick="onClickRadioTipInstalatie( this, 1);"><label
                                                        for="radioStatie">Statie</label>
                                                </td>
                                                <td>
                                                    <input name="<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE_RADIO%>"
                                                           type="radio"
                                                           value="<%=TipInstalatie.LINIE%>"
                                                           id="radioLinie" <%=TipInstalatie.LINIE == bean.getTipInstalatieRadio()?"checked":""%>
                                                           onClick="onClickRadioTipInstalatie( this, 1)"><label
                                                        for="radioLinie">Linie</label>
                                                </td>
                                                <td>
                                                    <input name="<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE_RADIO%>"
                                                           type="radio"
                                                           value="<%=TipInstalatie.PA_PT%>"
                                                           id="radioPAPT" <%=TipInstalatie.PA_PT == bean.getTipInstalatieRadio()?"checked":""%>
                                                           onClick="onClickRadioTipInstalatie( this, 1);"><label
                                                        for="radioPAPT">PA/PT</label>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr>
                                    <td nowrap>
                                        <input type="hidden" id="<%=EvenimentDispecerConstants.JSP.ID_INSTALATIE%>"
                                               name="<%=EvenimentDispecerConstants.JSP.ID_INSTALATIE%>"
                                               value="<%=bean.getIdInstalatie()%>">
                                        <INPUT id="<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE%>"
                                               name="<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE%>"
                                               class=gsapinputtext
                                               size="60" maxlength="100"
                                               value="<%=StringUtils.getNullSafe( bean.getDenInstalatie())%>"/>

                                            <%-- Tip: --%>
                                        <INPUT id="<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE%>"
                                               type="hidden"
                                               name="<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE%>"
                                               class=gsapinputtext size="10" maxlength="100"
                                               value="<%=StringUtils.getNullSafe( bean.getTipInstalatie())%>"/>

                                        &nbsp;&nbsp;&nbsp;
                                        U: <INPUT id="<%=EvenimentDispecerConstants.JSP.U%>"
                                                  name="<%=EvenimentDispecerConstants.JSP.U%>"
                                                  class=gsapinputtext size="7"
                                                  maxlength="100" value="<%=StringUtils.getNullSafe( bean.getU())%>"/>
                                        [kV]
                                    </td>
                                </tr>
                                <tr id="radiosChildStatie">
                                    <td nowrap>
                                        <br/>
                                        <table>
                                            <tr>
                                                <td>
                                                    <input name="<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE_RADIO2%>"
                                                           type="radio"
                                                           value="<%=TipInstalatie.CELULA%>"
                                                           id="radioCelula" <%=TipInstalatie.CELULA == bean.getTipInstalatieRadio2()?"checked":""%>
                                                           onClick="onClickRadioTipInstalatie( this, 2)"><label
                                                        for="radioCelula">Celula</label>
                                                </td>
                                                <td>
                                                    <input name="<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE_RADIO2%>"
                                                           type="radio"
                                                           value="<%=TipInstalatie.TRAFO%>"
                                                           id="radioTrafo" <%=TipInstalatie.TRAFO == bean.getTipInstalatieRadio2()?"checked":""%>
                                                           onClick="onClickRadioTipInstalatie( this, 2)"><label
                                                        for="radioTrafo">Trafo</label>
                                                </td>
                                                <td>
                                                    <input name="<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE_RADIO2%>"
                                                           type="radio"
                                                           value="<%=TipInstalatie.BARA%>"
                                                           id="radioBara" <%=TipInstalatie.BARA == bean.getTipInstalatieRadio2()?"checked":""%>
                                                           onClick="onClickRadioTipInstalatie( this, 2)"><label
                                                        for="radioBara">Bara</label>
                                                </td>
                                                <td>
                                                    <input name="<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE_RADIO2%>"
                                                           type="radio"
                                                           value="<%=TipInstalatie.BOBINA%>"
                                                           id="radioBobina" <%=TipInstalatie.BOBINA == bean.getTipInstalatieRadio2()?"checked":""%>
                                                           onClick="onClickRadioTipInstalatie( this, 2)"><label
                                                        for="radioBobina">Bobina</label>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr id="instalatie2Inputs">
                                    <td>
                                        <input type="hidden" id="<%=EvenimentDispecerConstants.JSP.ID_INSTALATIE2%>"
                                               name="<%=EvenimentDispecerConstants.JSP.ID_INSTALATIE2%>"
                                               value="<%=bean.getIdInstalatie2()%>">
                                        <INPUT id="<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE2%>"
                                               name="<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE2%>"
                                               class=gsapinputtext
                                               size="60" maxlength="100"
                                               value="<%=StringUtils.getNullSafe( bean.getDenInstalatie2())%>"/>

                                            <%--&nbsp;&nbsp;&nbsp;--%>
                                            <%--Tip: --%>
                                        <INPUT id="<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE2%>"
                                               name="<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE2%>"
                                               type="hidden"
                                               class=gsapinputtext size="10" maxlength="100"
                                               value="<%=StringUtils.getNullSafe( bean.getTipInstalatie2())%>"/>
                                        &nbsp;&nbsp;&nbsp;
                                        U: <INPUT id="<%=EvenimentDispecerConstants.JSP.U2%>"
                                                  name="<%=EvenimentDispecerConstants.JSP.U2%>" class=gsapinputtext
                                                  size="7"
                                                  maxlength="100" value="<%=StringUtils.getNullSafe( bean.getU2())%>"/>
                                        [kV]
                                    </td>
                                </tr>
                            </table>
                        </TD>
                    </TR>
                    </TBODY>
                </TABLE>
            </TD>
        </TR>

        <tr>
            <td align="left"><b>4.</b>Descriere eveniment</TD>
            <td>
        <textarea id="<%=EvenimentDispecerConstants.JSP.DESCRIERE%>"
                  name="<%=EvenimentDispecerConstants.JSP.DESCRIERE%>" rows="6" maxLength="4000"
                  cols="90"><%=StringUtils.getNullSafe(bean.getDescriereEveniment())%></textarea>
            </td>
        </TR>
        <tr>
            <td align="left"><b>5.</b>Defect</TD>
            <td>
        <textarea name="<%=EvenimentDispecerConstants.JSP.DEFECT%>" id="<%=EvenimentDispecerConstants.JSP.DEFECT%>"
                  rows="3" maxLength="4000"
                  cols="90"><%=StringUtils.getNullSafe(bean.getDefect())%></textarea>
            </td>
        </TR>
        <tr>
            <td align="left"><b>6.</b>Semnalizari</TD>
            <td>
                Adaugare/Cautare: <input type="text" size="70" id="searchSemnalizare" class="gsapinputtext"
                                         onkeypress="return event.keyCode != 13"/><br/>
        <textarea rows="3" cols="90" id="<%=EvenimentDispecerConstants.JSP.SEMNALIZARI%>" maxLength="4000"
                  name="<%=EvenimentDispecerConstants.JSP.SEMNALIZARI%>"><%=StringUtils.getNullSafe(bean.getSemnalizari())%></textarea>
                    <%--<input type="text" size="60" id="<%=EvenimentDispecerConstants.JSP.SEMNALIZARI%>" name="<%=EvenimentDispecerConstants.JSP.SEMNALIZARI%>" class="gsapinputtext" value="<%=StringUtils.getNullSafe( bean.getSemnalizari())%>"/>--%>
                    <%--<input type="text" size="10" id="<%=EvenimentDispecerConstants.JSP.SEMNALIZARI_COD%>" name="<%=EvenimentDispecerConstants.JSP.SEMNALIZARI_COD%>" class="gsapinputtext" value="<%=StringUtils.getNullSafe( bean.getCodSemnalizare())%>"/>--%>
            </td>
        </TR>
        <tr>
            <td align="left"><b>7.</b>Durata eveniment</TD>
            <td>
                <%
                    final long durataMinute = bean.getDurata();

                    final String rOre = RequestUtils.getString(request, EvenimentDispecerConstants.JSP.DURATA_ORE);
                    final String rMin = RequestUtils.getString(request, EvenimentDispecerConstants.JSP.DURATA_MINUTE);
                %>

                Ore: <input type="text" class=gsapinputtext size="5" style="TEXT-ALIGN: right" autocomplete="off"
                            id="<%=EvenimentDispecerConstants.JSP.DURATA_ORE%>"
                            name="<%=EvenimentDispecerConstants.JSP.DURATA_ORE%>"
                            value="<%=rOre.length() > 0 ? rOre : String.valueOf(durataMinute / 60)%>"/>&nbsp;
                Minute: <input type="text" class=gsapinputtext size="5" style="TEXT-ALIGN: right" autocomplete="off"
                               id="<%=EvenimentDispecerConstants.JSP.DURATA_MINUTE%>"
                               name="<%=EvenimentDispecerConstants.JSP.DURATA_MINUTE%>"
                               value="<%=rMin.length() > 0 ? rMin : String.valueOf(durataMinute % 60)%>"/>
            </td>
        </TR>
        <tr>
            <td align="left"><b>8.</b>Putere Intrerupta</TD>
            <td>
                <input type="text" class="gsapinputtext" style="TEXT-ALIGN: right"
                       id="<%=EvenimentDispecerConstants.JSP.PI%>"
                       name="<%=EvenimentDispecerConstants.JSP.PI%>"
                       value="<%=StringUtils.getNullSafe( bean.getPi())%>"/>&nbsp;[kW]
                <input type="button" title="Selectati instalatiile afectate de eveniment" onclick="browseLFSearch()"
                       value="Browse"/>
					   
				<input type="button" onclick="showHideIntervale()" value="Arata/Ascunde" /><br>
				<div id="intervale-content">
					<table id="intervaleInstalatii">
						<thead>
							<tr>
								<th rowspan="2">Durata</th>
								<th rowspan="2">Nr.instalatii</th>
								<th colspan="3">Consumatori alimentati</th>
								<th colspan="3">Consumatori nealimentati</th>
							</tr>
							<tr>
								<th>JT</th>
								<th>MT</th>
								<th>IT</th>
								<th>JT</th>
								<th>MT</th>
								<th>IT</th>
							</tr>
						</thead>
						<tbody>
						
					<%
						
						for(int i = 0; i < intervale.size(); i++) {
							%>
							<tr>
								<td><%=intervale.get(i).get("DURATA_INTRERUPERE").toString()%></td>
								<td><%=intervale.get(i).get("CNT").toString()%></td>
								<td><%=intervale.get(i).get("JT_A").toString()%></td>
								<td><%=intervale.get(i).get("MT_A").toString()%></td>
								<td><%=intervale.get(i).get("IT_A").toString()%></td>
								<td><%=intervale.get(i).get("JT_N").toString()%></td>
								<td><%=intervale.get(i).get("MT_N").toString()%></td>
								<td><%=intervale.get(i).get("IT_N").toString()%></td>
							</tr>
							<%
						}
					%>
						<%=intervale.size() == 0 ? "<tr><td colspan='8'>Niciun rezultat</td></tr>" : ""%>
						</tbody>
					</table>
				</div>
            </td>
        </TR>
        <tr>
            <td align="left"><b>9.</b>Gestionar</TD>
            <td>
                <select name="SelectGestionar" id="SelectGestionarId" onchange="fillGestionarField(this)">
                    <option value="-1">Selectati gestionar</option>
                    <%
                        if (gestList.size() != 0 && gestList != null) {
                            final Iterator iter = gestList.iterator();
                            while (iter.hasNext()) {
                                final Gestionar gest = (Gestionar) iter.next();
                    %>
                    <option value="<%=gest.get_ID()%>"><%=gest.get_denumire()%>
                    </option>
                    <%
                            }
                        }
                    %>
                </select>
                <input type="text" class="gsapinputtext" size="40"
                       id="<%=EvenimentDispecerConstants.JSP.GESTIONAR%>"
                       name="<%=EvenimentDispecerConstants.JSP.GESTIONAR%>"
                       value="<%=StringUtils.getNullSafe( bean.getGestionar())%>"/>
            </td>
        </TR>
        <tr>
            <td align="left"><b>10.</b>Consumatori intrerupti</TD>
            <td>
                <table>
                    <tr valign="top">
                        <td>
                            <TABLE class=gSAPTHEAD cellSpacing=1>
                                <TBODY>
                                <TR class=gSAPGroup align=middle>
                                    <TD>Inalta(IT)</TD>
                                    <TD>Medie(MT)</TD>
                                    <TD>Joasa(JT)</TD>
                                </TR>
                                <TR>
                                    <TD align=middle><INPUT class=gsapinputtext style="TEXT-ALIGN: right"
                                                            value="<%=binder.getPropertyValue( EvenimentDispecerConstants.JSP.UTILIZATORI_IT)%>"
                                                            onkeyup="updateTotalConsumatori()"
                                                            autocomplete="off"
                                                            id="<%=EvenimentDispecerConstants.JSP.UTILIZATORI_IT%>"
                                                            name="<%=EvenimentDispecerConstants.JSP.UTILIZATORI_IT%>"
                                    ></TD>
                                    <TD align=middle><INPUT class=gsapinputtext style="TEXT-ALIGN: right"
                                                            value="<%=binder.getPropertyValue( EvenimentDispecerConstants.JSP.UTILIZATORI_MT)%>"
                                                            onkeyup="updateTotalConsumatori()"
                                                            autocomplete="off"
                                                            id="<%=EvenimentDispecerConstants.JSP.UTILIZATORI_MT%>"
                                                            name="<%=EvenimentDispecerConstants.JSP.UTILIZATORI_MT%>"
                                    ></TD>
                                    <TD align=middle><INPUT class=gsapinputtext style="TEXT-ALIGN: right"
                                                            value="<%=binder.getPropertyValue( EvenimentDispecerConstants.JSP.UTILIZATORI_JT)%>"
                                                            onkeyup="updateTotalConsumatori()"
                                                            autocomplete="off"
                                                            id="<%=EvenimentDispecerConstants.JSP.UTILIZATORI_JT%>"
                                                            name="<%=EvenimentDispecerConstants.JSP.UTILIZATORI_JT%>"
                                    ></TD>
                                </TR>
                                <tr>
                                    <td align="left">

                                    </td>
                                    <td align="right">
                                        <b>Total:</b>
                                    </td>
                                    <td align="center">
                                        <input type="text" class=gsapinputtext style="TEXT-ALIGN: right"
                                               id="<%=EvenimentDispecerConstants.JSP.CONSUMATORI_INTR%>"
                                               name="<%=EvenimentDispecerConstants.JSP.CONSUMATORI_INTR%>"
                                               value="<%=bean.getConsumatoriIntrerupti()%>"/>
                                    </td>
                                </tr>
                                </TBODY>
                            </TABLE>
                        </td>
                        <td width="10"></td>
                        <td>
                            <TABLE class=gSAPTHEAD cellSpacing=1>
                                <TBODY>
                                <TR class=gSAPGroup align=middle>
                                    <TD>Casnici</TD>
                                    <TD>Mici</TD>
                                    <TD>Mari</TD>
                                </TR>
                                <TR>
                                    <td>
                                        <INPUT class=gsapinputtext style="TEXT-ALIGN: right" autocomplete="off"
                                               size="14"
                                               value="<%=binder.getPropertyValue( EvenimentDispecerConstants.JSP.UTILIZATORI_CASNICI)%>"
                                               id="<%=EvenimentDispecerConstants.JSP.UTILIZATORI_CASNICI%>"
                                               name="<%=EvenimentDispecerConstants.JSP.UTILIZATORI_CASNICI%>"
                                        />
                                    </td>
                                    <td>
                                        <INPUT class=gsapinputtext style="TEXT-ALIGN: right" autocomplete="off"
                                               size="14"
                                               value="<%=binder.getPropertyValue( EvenimentDispecerConstants.JSP.UTILIZATORI_MICI)%>"
                                               id="<%=EvenimentDispecerConstants.JSP.UTILIZATORI_MICI%>"
                                               name="<%=EvenimentDispecerConstants.JSP.UTILIZATORI_MICI%>"
                                        />
                                    </td>
                                    <td>
                                        <INPUT class=gsapinputtext style="TEXT-ALIGN: right" autocomplete="off"
                                               size="14"
                                               value="<%=binder.getPropertyValue( EvenimentDispecerConstants.JSP.UTILIZATORI_MARI)%>"
                                               id="<%=EvenimentDispecerConstants.JSP.UTILIZATORI_MARI%>"
                                               name="<%=EvenimentDispecerConstants.JSP.UTILIZATORI_MARI%>"
                                        />
                                    </td>
                                </tr>
                                </TBODY>
                            </TABLE>
                        </td>
                    </tr>
                </table>
            </td>
        </TR>
        <tr>
            <td align="left"><b>11.</b>Observatii suplimentare</TD>
            <td>
        <textarea rows="3" cols="90" name="<%=EvenimentDispecerConstants.JSP.OBSERVATII%>"
                  id="<%=EvenimentDispecerConstants.JSP.OBSERVATII%>"
                  maxLength="4000"
        ><%=binder.getPropertyValue(EvenimentDispecerConstants.JSP.OBSERVATII)%></textarea>
            </td>
        </TR>
        <tr>
            <td colspan="2" align="center">
                <input type="submit" value="Salvează">&nbsp;&nbsp;&nbsp;
                <input type="button"
                       onClick="window.location = '<%=GlobalAppConstants.APP_NAME%>?action=lista-evenimente-dispecer'"
                       value="Renunţă"/>
            </TD>
        </TR>
</table>
</FORM>
    </jsp:attribute>
</emid:template-od>
<script type="text/javascript" src="<%=AppConstants.WEB_RESOURCE_PATH%>/scripts/functions.js"></script>

<script type="text/javascript">

    var tipInstalatieSelected = <%=bean.getTipInstalatieRadio()%>;
    var tipInstalatieSelected2 = <%=bean.getTipInstalatieRadio2()%>;
    function getTipInstalatieSelected() {
        return tipInstalatieSelected;
    }

    function getTipInstalatieSelected2() {
        return tipInstalatieSelected2;
    }

    function onClickRadioTipInstalatie(oRadio, t) {

        if ($("radioStatie").checked) {
            Element.show("radiosChildStatie");
            Element.show("instalatie2Inputs");

        } else {
            Element.hide("radiosChildStatie");
            Element.hide("instalatie2Inputs");

            $('<%=EvenimentDispecerConstants.JSP.ID_INSTALATIE2%>').value = '';
            $('<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE2%>').value = '';
        }

        if (t == 1) {
            $('<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE%>').disabled = false;
            $('<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE%>').value = "";
            $('<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE%>').value = "";
            $('<%=EvenimentDispecerConstants.JSP.U%>').value = "";
            $('<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE%>').focus();
        } else if (t == 2) {
            $('<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE2%>').disabled = false;
            $('<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE2%>').value = "";
            $('<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE2%>').value = "";
            $('<%=EvenimentDispecerConstants.JSP.U2%>').value = "";
            $('<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE2%>').focus();
        }

        if (oRadio) {
            if (t == 1) {
                tipInstalatieSelected = oRadio.value;
            }

            if (t == 2) {
                tipInstalatieSelected2 = oRadio.value;
            }
        }
    }

    function fillGestionarField(obj) {
        if (obj.selectedIndex == 0) {
            $("<%=EvenimentDispecerConstants.JSP.GESTIONAR%>").value = "";
        } else {
            $("<%=EvenimentDispecerConstants.JSP.GESTIONAR%>").value = obj.options[obj.selectedIndex].text;
        }
    }

    function updateEnergieSiNumarConsumatoriForPT(idPaPt) {
        new Ajax.Request("<%=GlobalAppConstants.APP_NAME%>?action=getInstalatieInfoJSON", {
                    method: "post",
                    parameters: "idInstalatie=" + idPaPt,
                    onComplete: function (originalRequest) {
                        var response = getResponseMesg(originalRequest.responseText);

                        try {
                            var json = eval("(" + response + ")");

                            if (json && json.error) {
                                alert("eroare la get info PA/PT : " + json.error);
                            } else {

                                setTotalConsumatoriFromBrowse(
                                        json.putere,
                                        json.totalConsumatori,
                                        json.consumatori_IT,
                                        json.consumatori_MT,
                                        json.consumatori_JT,
                                        json.consumatori_casnici,
                                        json.consumatori_mici,
                                        json.consumatori_mari,
                                        true
                                );
                            }

                        } catch (e) {
                            alert("eroare la get info PA/PT : " + e.message);
                        }
                    }
                }
        );
    }

    function getIdLfSelected() {
        return $('<%=EvenimentDispecerConstants.JSP.ID_INSTALATIE%>').value;
    }

    var lfDataProvider = new AjaxDataProvider(function () {
        return "<%=AppConstants.APP_NAME%>?action=suggestInstalatieOd&tip=" + getTipInstalatieSelected() + "&<%=SugestConstants.SUGEST_Q_QUERY%>=";
    });

    var eqDataProvider = new AjaxDataProvider(function () {
        return "<%=AppConstants.APP_NAME%>?action=suggestInstalatieOd&tip=" + getTipInstalatieSelected2() + "&idParent=" + getIdLfSelected() + "&<%=SugestConstants.SUGEST_Q_QUERY%>=";
    });

    setTimeout(function () {
        new InputSugest($("<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE%>"), $("<%=EvenimentDispecerConstants.JSP.ID_INSTALATIE%>"), lfDataProvider, {
            isValidSearchInput: function () {
                return true;
            },

            beforeRequest: function () {
                window.status = "before request";
                $("<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE%>").value = "";
            },

            onSelect: function (item) {
                $("<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE%>").value = item.tipInstalatie;
                $("<%=EvenimentDispecerConstants.JSP.U%>").value = item.u;


                if ($("radioPAPT").checked) {
                    updateEnergieSiNumarConsumatoriForPT(item.key);
                }
            }
        });

        new InputSugest($("<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE2%>"), $("<%=EvenimentDispecerConstants.JSP.ID_INSTALATIE2%>"), eqDataProvider, {
            isValidSearchInput: function () {
                return true;
            },

            beforeRequest: function () {
                window.status = "before request";
                $("<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE2%>").value = "";
            },

            onSelect: function (item) {
                $("<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE2%>").value = item.tipInstalatie;
                $("<%=EvenimentDispecerConstants.JSP.U2%>").value = item.u;

                $("radioStatie").checked = true;
                $("<%=EvenimentDispecerConstants.JSP.TIP_INSTALATIE%>").value = item.tipInstalatieParent;
                $("<%=EvenimentDispecerConstants.JSP.ID_INSTALATIE%>").value = item.idInstalatieParent;
                $("<%=EvenimentDispecerConstants.JSP.DEN_INSTALATIE%>").value = item.descInstalatieParent;
                $("<%=EvenimentDispecerConstants.JSP.U%>").value = item.uInstalatieParent;
            }
        });
    }, 200);


    var semnalizareDataProvider = new AjaxDataProvider(function () {
        return "<%=GlobalAppConstants.APP_NAME%>?action=sugestSetValue&tip=<%=SetValue.SEMNALIZARI%>&<%=SugestConstants.SUGEST_Q_QUERY%>=";
    });

    setTimeout(function () {
        new InputSugest($("searchSemnalizare"), $(), semnalizareDataProvider, {
            beforeRequest: function () {

            },

            onSelect: function (item) {
                try {
                    var value = $("<%=EvenimentDispecerConstants.JSP.SEMNALIZARI%>").value;
                    if (value.length > 0) value += "\n";

                    value += item.value;

                    $("<%=EvenimentDispecerConstants.JSP.SEMNALIZARI%>").value = value;
                    $("searchSemnalizare").value = '';
                } catch (e) {
                    alert(e.message);
                }
            }
        });
    }, 200);


    var browseFeatures = "menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes,height=650,left=20,top=10,width=1000";
    function buildParamsForCheckPaPt() {
        var statieId = -1;
        try {
            if (tipInstalatieSelected == <%=TipInstalatie.STATIE%>) {
                statieId = document.zaforma
            .<%=EvenimentDispecerConstants.JSP.ID_INSTALATIE%>.
                value;
            }
        } catch (ex) {
        }

        var linieId = -1;
        try {
            if (tipInstalatieSelected == <%=TipInstalatie.LINIE%>) {
                linieId = document.zaforma
            .<%=EvenimentDispecerConstants.JSP.ID_INSTALATIE%>.
                value;
            }
        } catch (ex) {
        }

        var postId = -1;
        try {
            if (tipInstalatieSelected == <%=TipInstalatie.PA_PT%>) {
                postId = document.zaforma
            .<%=EvenimentDispecerConstants.JSP.ID_INSTALATIE%>.
                value;
            }
        } catch (ex) {
        }

        var derivatieId = -1;
        try {
            if (tipInstalatieSelected2 == <%=TipInstalatie.DERIVATIE%>) {
                derivatieId = document.zaforma
            .<%=EvenimentDispecerConstants.JSP.ID_INSTALATIE2%>.
                value;
            }
        } catch (ex) {
        }

        var durataOre = document.zaforma
    .<%=EvenimentDispecerConstants.JSP.DURATA_ORE%>.
        value;
        var durataMinute = document.zaforma
    .<%=EvenimentDispecerConstants.JSP.DURATA_MINUTE%>.
        value;

        return "&statieId=" + statieId + "&linieId=" + linieId + "&postId=" + postId + "&derivatieId=" + derivatieId + "&durataOre=" + durataOre + "&durataMinute=" + durataMinute;
    }

    var _browseLFSearch = null;
    function browseLFSearch() {
        if (_browseLFSearch != null) _browseLFSearch.close();

        var params = buildParamsForCheckPaPt();

        var url = "<%=GlobalAppConstants.APP_NAME%>?action=openBrowseInstalatii&tipParent=OD&afterSaveAction=saveLfForEvenimentDispecer" + params;
        _browseLFSearch = window.open(url, '', browseFeatures);
    }

    function safeNumber(el) {
        var v = el.value;

        var n = parseFloat(v);

        if (isNaN(n)) {
            return 0;
        } else {
            return n;
        }
    }

    function setTotalConsumatoriFromBrowse(pi, totalConsumatori, it, mt, jt, casnici, mici, mari, notifyOverwrite) {

        var completeaza = true;
        if (notifyOverwrite) {
            var t = safeNumber($("<%=EvenimentDispecerConstants.JSP.PI%>")) +
                    safeNumber($("<%=EvenimentDispecerConstants.JSP.CONSUMATORI_INTR%>")) +
                    safeNumber($("<%=EvenimentDispecerConstants.JSP.UTILIZATORI_IT%>")) +
                    safeNumber($("<%=EvenimentDispecerConstants.JSP.UTILIZATORI_MT%>")) +
                    safeNumber($("<%=EvenimentDispecerConstants.JSP.UTILIZATORI_JT%>")) +
                    safeNumber($("<%=EvenimentDispecerConstants.JSP.UTILIZATORI_CASNICI%>")) +
                    safeNumber($("<%=EvenimentDispecerConstants.JSP.UTILIZATORI_MICI%>")) +
                    safeNumber($("<%=EvenimentDispecerConstants.JSP.UTILIZATORI_MARI%>"));

            if (t > 0) {
                completeaza = confirm(
                        "Exista valori completate in cimpurile Putere Intrerupta si Consumatori intrerupti.\n" +
                        "Doriti sa le suprascrieti cu valorile din nomenclator ?\n" +
                        " - Putere intrerupta : " + pi + "\n" +
                        " - Consumatori  IT : " + it + "\n" +
                        " - Consumatori  MT : " + mt + "\n" +
                        " - Consumatori  JT : " + jt + "\n" +
                        " - Casnici : " + casnici + "\n" +
                        " - Mici : " + mici + "\n" +
                        " - Mari : " + mari
                );
            }
        }

        if (completeaza) {

            $("<%=EvenimentDispecerConstants.JSP.PI%>").value = pi;
            $("<%=EvenimentDispecerConstants.JSP.CONSUMATORI_INTR%>").value = totalConsumatori;
            $("<%=EvenimentDispecerConstants.JSP.UTILIZATORI_IT%>").value = it;
            $("<%=EvenimentDispecerConstants.JSP.UTILIZATORI_MT%>").value = mt;
            $("<%=EvenimentDispecerConstants.JSP.UTILIZATORI_JT%>").value = jt;
            $("<%=EvenimentDispecerConstants.JSP.UTILIZATORI_CASNICI%>").value = casnici;
            $("<%=EvenimentDispecerConstants.JSP.UTILIZATORI_MICI%>").value = mici;
            $("<%=EvenimentDispecerConstants.JSP.UTILIZATORI_MARI%>").value = mari;
        }
    }

    function setTextAreaFeatures(oTextArea) {

        //numarul initial de randuri
        oTextArea.setAttribute('initialRows', oTextArea.rows);

        //elementul pentru randurile remase
        var countEl = document.createElement("span");
        countEl.id = oTextArea.id + "count";
        countEl.title = "Numarul de caractere ramase de completat";
        countEl.style.fontSize = "11px";
        oTextArea.parentNode.appendChild(countEl);

        oTextArea.onkeyup = function (event) {
            if (window.event) {
                event = window.event;
            }

            updateRows(this);
            updateRemainingChars(this);
        };

        updateRows(oTextArea);
        updateRemainingChars(oTextArea);
    }

    function countLines(txt) {
        var tokens = txt.split(/\n/);

        return tokens && tokens.length ? tokens.length : 0;
    }

    function canTypeText(oTextArea) {
        var maxLength = 0 + oTextArea.getAttribute("maxLength");
        var currentLength = $F(oTextArea).length;

        return currentLength < maxLength;
    }

    function updateRows(oTextArea) {
        var initialRows = 0 + oTextArea.getAttribute('initialRows');

        var lines = countLines($F(oTextArea));

        oTextArea.rows = lines >= initialRows ? lines + 1 : initialRows;
    }

    function updateRemainingChars(oTextArea) {
        var countEl = $(oTextArea.id + "count");

        var maxLength = 0 + oTextArea.getAttribute("maxLength");
        var currentLength = $F(oTextArea).length;

        countEl.innerHTML = "" + (maxLength - currentLength);

        countEl.style.color = ( currentLength > maxLength ? "red" : "black");
    }


    function initTextAreas() {
        setTextAreaFeatures($('<%=EvenimentDispecerConstants.JSP.DESCRIERE%>'));
        setTextAreaFeatures($('<%=EvenimentDispecerConstants.JSP.SEMNALIZARI%>'));
        setTextAreaFeatures($('<%=EvenimentDispecerConstants.JSP.DEFECT%>'));
        setTextAreaFeatures($('<%=EvenimentDispecerConstants.JSP.OBSERVATII%>'));
    }

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

    function updateDurataEveniment() {
        var dataProducere = getDate('<%=EvenimentDispecerConstants.JSP.DATA_PRODUCERE%>');
        var dataRealimentare = getDate('<%=EvenimentDispecerConstants.JSP.DATA_REALIMENTARE%>');

        var ore = '';
        var minute = '';

        if (dataProducere != null && dataRealimentare != null) {
            var durataProducere = (dataRealimentare - dataProducere) / 60000;


            ore = Math.floor(durataProducere / 60);
            minute = (durataProducere % 60);
        }

        $("<%=EvenimentDispecerConstants.JSP.DURATA_ORE%>").value = ore;
        $("<%=EvenimentDispecerConstants.JSP.DURATA_MINUTE%>").value = minute;
    }

    function updateTotalConsumatori() {
        var sIT = $('<%=EvenimentDispecerConstants.JSP.UTILIZATORI_IT%>').value;
        var sJT = $('<%=EvenimentDispecerConstants.JSP.UTILIZATORI_JT%>').value;
        var sMT = $('<%=EvenimentDispecerConstants.JSP.UTILIZATORI_MT%>').value;

        var total = 0;
        try {
            total = parseInt(sIT) + parseInt(sJT) + parseInt(sMT);
        } catch (e) {
        }
        $("<%=EvenimentDispecerConstants.JSP.CONSUMATORI_INTR%>").value = total;
    }

    onClickRadioTipInstalatie(null);

    initTextAreas();

</script>
