<%@ page import="ro.emid.callcenter.CallcenterSessionUtils" %>
<%@ page import="ro.emid.common.constants.ReclamatieConstants" %>
<%@ page import="ro.emid.common.db.ReclamatieAccessDB" %>
<%@ page import="ro.emid.common.types.Reclamatie" %>
<%@ page import="ro.emid.common.utils.MenuConstants" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.callcenter.i10n.Configuration" %>
<%@ page import="ro.emid.callcenter.util.CallcenterHelper" %>
<%@ page import="ro.emid.common.db.adrese.AdreseHelper" %>
<%@ page import="ro.emid.common.components.sugest.base.SugestConstants" %>

<%
    Reclamatie bean = CallcenterSessionUtils.getReclamatie(session);
    if (bean == null) {
        bean = new Reclamatie();
        bean.setId(-1);

        CallcenterSessionUtils.setReclamatie(session, bean);
    }

    Configuration configuration = CallcenterHelper.getConfiguration();
%>


<%@ include file="declaratii.jsp"%>
<%-- DEBUG: badge vizibil pentru confirmarea redeploy --%>
<style>
  #emid-cc-badge {
    position: fixed; top: 8px; right: 8px;
    padding: 6px 10px; border-radius: 6px;
    background: #111; color: #fff; font: 12px/1 monospace;
    z-index: 999999; opacity: .9; box-shadow: 0 2px 6px rgba(0,0,0,.25);
  }
</style>
<div id="emid-cc-badge">
  EMID-CC UPDATE ✔
  <%= new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()) %>
</div>
<script>
  console.log("EMID-CC UPDATE OK @ <%= System.currentTimeMillis() %>");
</script>


<style type="text/css">
    .verificare-adresa{
        color: red;
        position: absolute;
        left: 35%;
        top: 21px;
        font-size: 15px;
    }

    fieldset{
        margin: 0;
    }
</style>

<form name="zaforma" action="<%=AppConstants.APP_NAME%>?action=save-reclamatie&r=<%=System.currentTimeMillis()%>" method="post" style="position: relative">
    <input type="hidden" name="<%=ReclamatieConstants.ID%>" value="<%=bean.getId()%>" />
    <input type="hidden" name="isFromCache" value="<%=bean.isFromCache()%>" />

    <span id="spanLoadingStatusReclamatie" class="verificare-adresa" style="display: none">
        Se verifica adresa ....
    </span>

    <table width="100%"><tr><td valign="top" style="padding: 0;margin: 0;">

    <table border=0 cellPadding=0 cellSpacing=1 width="100%">
    <TR>
        <th style="text-align: center" class="gsapthead" colSpan=2 height=25 noWrap>
            &nbsp;
            <%  if( bean.getId() == -1){ %>
                Înregistrare reclamaţie
            <% } else { %>
                Reclamatie nr.<%=bean.getNiceNumarReclamatie()%>
            <% } %>
        </th>
    </TR>
    <tr id="recuperareReclamatieControls" style="display: none">
        <td align="center">
            <span style="color: red;"> Atentie, aveti posibilitatea de a recupera datele ultimei reclamatii nesalvate</span> <br/>
            [<a class="link" href="#" onclick="getReclamatieFromCookies(); Element.hide('recuperareReclamatieControls'); return false;">Incarca date in formular</a>] &nbsp;
            [<a class="link" href="#" onclick="new function(){
                    //clean reclamatie din cookies
                    try{
                        COOKIE.eraseCookie( '<%=ReclamatieConstants.ID%>');
                    }catch(e){
                        alert('Eroare la stergere cookie: ' + e.message);
                    }
                    window.location.reload();
            }">Renunta</a>]
        </td>
    </tr>

    <tr><td>
    <br/>
    <fieldset style="margin-top: 0; padding-top: 0">
        <legend><b>&nbsp;Identificare client</b></legend>
        <table border="0" width="100%" align="center" class="mt5">
            <tr>
                <td >
                    Numele si prenumele persoanei care a anuntat deranjamentul / Denumire agent economic
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    <input type="hidden" name="<%=ReclamatieConstants.NLC_CLIENT%>" id="<%=ReclamatieConstants.NLC_CLIENT%>" value=""/>
                    <input class="gsapinputtext" type="text" size="70" name="<%=ReclamatieConstants.NUME_CLIENT%>" id="<%=ReclamatieConstants.NUME_CLIENT%>" value="<%=AppConstants.format( bean.getNumeClient())%>"/>
                    <br>
                    <span style="color: blue; display: none" id="clientEligibil">Client eligibil cu alt furnizor</span>
                </td>
            </tr>
        </table>
    </fieldset>

<fieldset class="mt10">
    <legend><b>&nbsp;Adresa client</b></legend>
    <table>
        <tr>
            <td colspan="2"><%=configuration.getLabelLocalitate1()%></td>
            <td colspan="2"><%=configuration.getLabelLocalitate2()%></td>
            <td colspan="2">Denumire arteră</td>
            <td>Nr</td>
            <td>
                <!--NLC-->
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="hidden" id="<%=ReclamatieConstants.COD_MUNICIPIU%>" name="<%=ReclamatieConstants.COD_MUNICIPIU%>" value="">
                <input class="gsapinputtext" style="width: 160px" id="<%=ReclamatieConstants.MUNICIPIU%>" name="<%=ReclamatieConstants.MUNICIPIU%>" value="" sugestWidth="250" autocomplete="off"/>
            </td>
            <td colspan="2">
                <input type="hidden" id="<%=ReclamatieConstants.COD_LOCAL%>" name="<%=ReclamatieConstants.COD_LOCAL%>" value="" >
                <input class="gsapinputtext" style="width: 150px" type=text id="<%=ReclamatieConstants.LOCALITATE2%>" name="<%=ReclamatieConstants.LOCALITATE2%>" value="" sugestWidth="250" autocomplete="off"/>
            </td>
            <td colspan="2">
                <nobr>
                    <input type="hidden" name="<%=ReclamatieConstants.COD_STRADA%>" id="<%=ReclamatieConstants.COD_STRADA%>"  value="-1">
                    <input class="gsapinputtext" style="width: 230px;"  name="<%=ReclamatieConstants.STRADA%>" id="<%=ReclamatieConstants.STRADA%>" value="" autocomplete="off"/>
                    <input class="gsapinputtext" style="width: 50px" type=text name="<%=ReclamatieConstants.TIP_ARTERA%>" id="<%=ReclamatieConstants.TIP_ARTERA%>" onchange="onChangeTipArtera()" autocomplete="off"/>
                </nobr>
            </td>
            <td>
                <input onfocus="validateForNumar()"  class="gsapinputtext" type="text" value="" size="10" name="<%=ReclamatieConstants.NR%>" id="<%=ReclamatieConstants.NR%>" autocomplete="off"/>
            </td>
            <td>
                <input type="button" onclick="getStatusReclamatie();" value="Verificare" title="Verificare adresa de incidente/deranjamente/lucrari programate"/>
                <%--<input class="gsapinputtext" style="width: 100px" id="NLC" value="">--%>
            </td>
        </tr>
        <tr>
            <td>Bloc</td>
            <td>Scara</td>
            <td>Etaj</td>
            <td>Ap.</td>
            <td>Fix</td>
            <td>Mobil</td>
            <td>Interfon</td>
            <td>Judet</td>
        </tr>
        <tr>
            <td><input onfocus="validateForBloc()" class="gsapinputtext" type="text" size="5" value="" name="<%=ReclamatieConstants.BLOC%>" id="<%=ReclamatieConstants.BLOC%>" ></td>
            <td><input class="gsapinputtext" type="text" size="5" value="" name="<%=ReclamatieConstants.SCARA%>" id="<%=ReclamatieConstants.SCARA%>" autocomplete="off"/></td>
            <td><input class="gsapinputtext" type="text" size="5" value="" name="<%=ReclamatieConstants.ETAJ%>"  id="<%=ReclamatieConstants.ETAJ%>"  autocomplete="off"/></td>
            <td><input class="gsapinputtext" type="text" size="5" value="" name="<%=ReclamatieConstants.AP%>"    id="<%=ReclamatieConstants.AP%>"    autocomplete="off"/></td>
            <td><input class="gsapinputtext" type="text" onblur="limitInputLegth(this, 10, 'Nu puteti introduce mai mult de 10 caractere !')" value="" name="<%=ReclamatieConstants.FIX%>" autocomplete="off"/></td>
            <td><input class="gsapinputtext" type="text" onblur="limitInputLegth(this, 10, 'Nu puteti introduce mai mult de 10 caractere !')" value="" name="<%=ReclamatieConstants.MOBIL%>" autocomplete="off"/></td>
            <td><input class="gsapinputtext" type="text" size="10" value="" name="<%=ReclamatieConstants.INTERFON%>" autocomplete="off"/></td>
            <td><input class="gsapinputtext" type="text" size="15" value="" readonly="true" id="<%=ReclamatieConstants.JUDET%>" name="<%=ReclamatieConstants.JUDET%>"/></td>
        </tr>
        <tr>
            <td colspan="8" valign="top">
                Puncte de reper:<br> <textarea rows="3" style="width: 100%" name="<%=ReclamatieConstants.PUNCTE_REPER%>"></textarea>
            </td>
        </tr>
    </table>
</fieldset>

<fieldset class="mt10">
    <legend><b>&nbsp;Sesizare</b></legend>
    <table cellpadding="1px" border="0" align="center" width="100%">
        <tr>
            <td valign="top">
                Continutul sesizarii 1:
                <select id="<%=ReclamatieConstants.CONTINUT_SESIZARE1%>" name="<%=ReclamatieConstants.CONTINUT_SESIZARE1%>" class="gSAPSelect" onchange="fillSesizare2Select()">
                    <option value="-1">Selectati:</option>
                    <%for (int i = 0; i < ReclamatieConstants.SESIZARI.length; i++){
                        final String sesizare1 = ReclamatieConstants.SESIZARI[i][0]; %>
                        <option value="<%=i%>" <%=i == bean.getContinutSesizare1() ? "selected" : ""%> ><%=sesizare1%></option>
                    <%}%>
                </select>
                &nbsp;&nbsp;
                <span id="<%=ReclamatieConstants.CONTINUT_SESIZARE2%>">
                    Continutul sesizarii 2:
                    <select id="<%=ReclamatieConstants.CONTINUT_SESIZARE2%>" name="<%=ReclamatieConstants.CONTINUT_SESIZARE2%>" class="gSAPSelect">
                        <option value="-1">Selectati:</option>
                    </select>
                </span>
                <span id="<%=ReclamatieConstants.ALTA_SESIZARE%>">
                    <input type="text" size="30" name="<%=ReclamatieConstants.ALTA_SESIZARE%>" class="gsapinputtext" />
                </span>
            </td>
        </tr>
        <tr>
            <td>Comentarii:<br>
                <textarea rows="3" style="width: 95%" name="<%=ReclamatieConstants.COMENTARII%>"><%=AppConstants.format(bean.getComentarii())%></textarea>
            </td>
        </tr>        
    </table>
</fieldset>

</td>
</tr>
</TABLE>
</td>
<td valign="top" width="10px" style="padding-top: 50px;">
    <% if( AppConstants.CIR_ACTIV){ %>
        <input type="button" class="callcenter-button" onclick=" identificareCIR()" value="Identificare CIR" /><br>
    <%}%>
    <input type="button" class="callcenter-button" onclick=" consultareClient()" value="Consultare client"/><br>
    <input type="button" style="visibility: hidden;" value="Invizibil"/><br>
    <input type="button" class="callcenter-button" onclick=" salvareReclamatie()" name="saveButton" <%=AppConstants.CIR_ACTIV ? "disabled" : ""%>  value="Salvare reclamaţie" /><br>
    <input type="button" style="visibility: hidden;" value="Invizibil"/><br>

    <input type="button" class="callcenter-button" onclick=" informatii()" value="Informaţii"/><br>
    <input type="button" class="callcenter-button" onclick=" cautareReclamatie()" value="Căutare reclamaţie"/><br>
    <input type="button" class="callcenter-button" onclick=" cautareStrada()" value="Căutare stradă"/><br>
    <input type="button" class="callcenter-button" onclick=" cautareClientByNLC()" value="Căutare NLC"/><br>
    <input type="button" class="callcenter-button" onclick=" browseGEO()" value="Localităţi"/><br>

    <br/>
    <br/>
    <br/>
    <br/>
    <br/>
    <br/>
    <br/>
    <br/>
    <br/>
    <br/>
    <div align="center" style="width: 130px" >
        <span id="appStatusOn" class="appStatusOn" title="Aplicatie disponibila"></span>
        <span id="appStatusOff" class="appStatusOff" title="Aplicatie indisponibila"></span>
    </div>
    <br/>
    <input type="button" class="callcenter-button" onclick=" stergereContinut()" value="Ştergere conţinut" /><br>
</td>
</tr>
    <tr>
        <td>
            <div id="statusReclamatie" style="color: red; text-align: left;">
            </div>
        </td>
    </tr>
</table>
</form>

<script type="text/javascript">

    function checkAppStatus(){
        return;

        new Ajax.Request('<%=AppConstants.APP_NAME%>?action=callcenter_checkAppStatus',
        {
            method: 'post',
            parameters: "t="+new Date().getTime(),
            onComplete: function( originalRequest){
                var response = getResponseMesg( originalRequest.responseText);

                if( "OK" == response){
                    setAppStatusOn();
                }else{
                    setAppStatusOff();
                }

                setTimeout("checkAppStatus()", 2000);
            }
        });
    }

    function setAppStatusOn(){
        Element.show("appStatusOn");
        Element.hide("appStatusOff");
    }

    function setAppStatusOff(){
        Element.hide("appStatusOn");
        Element.show("appStatusOff");
    }

    function checkLocalitateData(){
        if( document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.value.trim().length == 0){
            alert( 'Selectati localitatea');
            document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.focus();
            return false;
        }

        if( document.zaforma.<%=ReclamatieConstants.JUDET%>.value.trim().length == 0){
            alert( 'Judet necompletat, selectati localitatea din lista sugerata');
            document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.focus();
            return false;
        }

        <% if( AppConstants.CIR_ACTIV){%>
            if( document.zaforma.<%=ReclamatieConstants.TIP_ARTERA%>.value.trim().lentgh == 0){
                alert( 'Selectati tip artera !');
                document.zaforma.<%=ReclamatieConstants.TIP_ARTERA%>.focus();
                return false;
            }

            if( document.zaforma.<%=ReclamatieConstants.STRADA%>.value.trim().length == 0){
                alert( 'Selectati strada !');
                document.zaforma.<%=ReclamatieConstants.STRADA%>.focus();
                return false;
            }
        <%}%>
        
        return true;
    }

    function identificareCIR(){
        if( checkLocalitateData()){
            var params =
                    '&loc=' + document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.value +
                    '&tipArtera=' + document.zaforma.<%=ReclamatieConstants.TIP_ARTERA%>.value +
                    '&strada=' + document.zaforma.<%=ReclamatieConstants.STRADA%>.value +
                    '&nr='+ document.zaforma.<%=ReclamatieConstants.NR%>.value +
                    "&bloc=" + document.zaforma.<%=ReclamatieConstants.BLOC%>.value;
            new Ajax.Request('<%=AppConstants.APP_NAME%>?action=callcenter_checkCIR',
                        {
                            method: 'post',
                            parameters: params,
                            onComplete: checkCIR_Response
                        });
        } 
    }

    function checkCIR_Response(originalRequest){
        var response = getResponseMesg( originalRequest.responseText);
        if( response != null){
            if( response.indexOf("CIR_OK") != -1){
                response = response.replace("CIR_OK", "");
                alert( response);
                document.zaforma.saveButton.disabled = false;
            }else{
                alert( response);
            }
        }
    }

    function consultareClient(){
        if( document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.value.length == 0){
            alert( 'Selectati o localitate !');
            document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.focus();
            return;
        }
        if( document.zaforma.<%=ReclamatieConstants.STRADA%>.value.length == 0){
            alert( 'Selectati o strada !');
            document.zaforma.<%=ReclamatieConstants.STRADA%>.focus();
            return;
        }

        if( consultareClientWindow != null) consultareClientWindow.close();

        consultareClientWindow = window.open('<%=AppConstants.APP_NAME%>?action=consultare-client' +
                    '&numeClientFromParent=' + document.zaforma.<%=ReclamatieConstants.NUME_CLIENT%>.value +
                    '&judet=' + document.zaforma.<%=ReclamatieConstants.JUDET%>.value +
                    '&loc=' + document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.value +
                    '&loc2=' + document.zaforma.<%=ReclamatieConstants.LOCALITATE2%>.value +
                    '&tipArtera=' + document.zaforma.<%=ReclamatieConstants.TIP_ARTERA%>.value +
                    '&codStrada=' + document.zaforma.<%=ReclamatieConstants.COD_STRADA%>.value +
                    '&strada=' + document.zaforma.<%=ReclamatieConstants.STRADA%>.value +
                    '&nr=' + document.zaforma.<%=ReclamatieConstants.NR%>.value +
                    '&bloc=' + document.zaforma.<%=ReclamatieConstants.BLOC%>.value +
                    '&scara=' + document.zaforma.<%=ReclamatieConstants.SCARA%>.value +
                    '&etaj=' + document.zaforma.<%=ReclamatieConstants.ETAJ%>.value +
                    '&ap=' + document.zaforma.<%=ReclamatieConstants.AP%>.value+
                    '&codLoc2=' + document.zaforma.<%=ReclamatieConstants.COD_LOCAL%>.value
                , 'consultareClinet', 'top=50,left=50,width=1000 height=800,scrollbars=yes, status=yes,resizable=yes,dependent=yes,alwaysRaised=yes');
    }

    function checkInputData(){
        var numeClient = document.zaforma.<%=ReclamatieConstants.NUME_CLIENT%>.value;
        if( numeClient == null || numeClient.trim().length == 0){
            alert( 'Introduceti datele clientului');
            document.zaforma.<%=ReclamatieConstants.NUME_CLIENT%>.focus();
            return false;
        }

        return checkLocalitateData();
    }

    function storeReclamatieToCookies(){
        setFormValueInCookie( '<%=ReclamatieConstants.ID%>');
        setFormValueInCookie( '<%=ReclamatieConstants.NUME_CLIENT%>');
        setFormValueInCookie( '<%=ReclamatieConstants.JUDET%>');
        setFormValueInCookie( '<%=ReclamatieConstants.COD_MUNICIPIU%>');
        setFormValueInCookie( '<%=ReclamatieConstants.MUNICIPIU%>');

        setFormValueInCookie( '<%=ReclamatieConstants.COD_LOCAL%>');
        setFormValueInCookie( '<%=ReclamatieConstants.LOCALITATE2%>');

        setFormValueInCookie( '<%=ReclamatieConstants.COD_STRADA%>');
        setFormValueInCookie( '<%=ReclamatieConstants.STRADA%>');
        setFormValueInCookie( '<%=ReclamatieConstants.TIP_ARTERA%>');

        setFormValueInCookie( '<%=ReclamatieConstants.NR%>');
        setFormValueInCookie( '<%=ReclamatieConstants.BLOC%>');
        setFormValueInCookie( '<%=ReclamatieConstants.SCARA%>');
        setFormValueInCookie( '<%=ReclamatieConstants.ETAJ%>');
        setFormValueInCookie( '<%=ReclamatieConstants.AP%>');
        setFormValueInCookie( '<%=ReclamatieConstants.FIX%>');
        setFormValueInCookie( '<%=ReclamatieConstants.MOBIL%>');
        setFormValueInCookie( '<%=ReclamatieConstants.INTERFON%>');
        setFormValueInCookie( '<%=ReclamatieConstants.COMENTARII%>');
        setFormValueInCookie( '<%=ReclamatieConstants.PUNCTE_REPER%>');

        try{
            COOKIE.setCookie('<%=ReclamatieConstants.CONTINUT_SESIZARE1%>', getSelectedValue( document.zaforma.<%=ReclamatieConstants.CONTINUT_SESIZARE1%>), 5);
        }catch(e){
        }

        try{
            COOKIE.setCookie('<%=ReclamatieConstants.CONTINUT_SESIZARE2%>', getSelectedValue( document.zaforma.<%=ReclamatieConstants.CONTINUT_SESIZARE2%>), 5);
        }catch(e){
        }

        COOKIE.setCookie("niceNumarReclamatie", "<%=bean.getNiceNumarReclamatie()%>" , 5);
    }

    function getReclamatieFromCookies(){
        try{
            setFormValueFromCookie( '<%=ReclamatieConstants.ID%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.NUME_CLIENT%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.JUDET%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.COD_MUNICIPIU%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.MUNICIPIU%>');

            setFormValueFromCookie( '<%=ReclamatieConstants.COD_LOCAL%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.LOCALITATE2%>');

            setFormValueFromCookie( '<%=ReclamatieConstants.COD_STRADA%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.STRADA%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.TIP_ARTERA%>');

            setFormValueFromCookie( '<%=ReclamatieConstants.NR%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.BLOC%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.SCARA%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.ETAJ%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.AP%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.FIX%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.MOBIL%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.INTERFON%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.COMENTARII%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.ALTA_SESIZARE%>');
            setFormValueFromCookie( '<%=ReclamatieConstants.PUNCTE_REPER%>');



            try{
                var s1 = COOKIE.getCookie('<%=ReclamatieConstants.CONTINUT_SESIZARE1%>');
                selectOption( document.zaforma.<%=ReclamatieConstants.CONTINUT_SESIZARE1%>, s1);
                fillSesizare2Select();
            }catch(e){
            }

            try{
                var s2 = COOKIE.getCookie('<%=ReclamatieConstants.CONTINUT_SESIZARE2%>');
                selectOption( document.zaforma.<%=ReclamatieConstants.CONTINUT_SESIZARE2%>, s2);
            }catch(e){
            }

        }catch(e){
            alert('Eroare la preluare reclamatie din cookies:' + e.message);
        }
        document.zaforma.isFromCache.value = 'true';
    }

    function setFormValueInCookie(name){
        COOKIE.setCookie(name, document.zaforma.elements[name].value, 5);
    }

    function setFormValueFromCookie(name){
        var value = COOKIE.getCookie( name);
        if( !value ) value = '';
        document.zaforma.elements[name].value = value;
    }

    function checkLastCookies(){
        var value = COOKIE.getCookie( '<%=ReclamatieConstants.ID%>');
        if( value ){
            Element.show("recuperareReclamatieControls");
        }
    }

    function salvareReclamatie(){
        if( checkInputData()){
            try{
                storeReclamatieToCookies();
            }catch(e){
                alert( 'Eroare la stocare date reclamatie in cookies:' + e.message);
            }
            document.zaforma.submit();
        }
    }

    var informatiiWindow = null;
    function informatii(){
        if( informatiiWindow != null) informatiiWindow.close();
        informatiiWindow = window.open('<%=AppConstants.APP_NAME%>?action=callcenter_informatii','filtru', 'top=50,left=50,width=900, height=500, status=yes,scrollbars=yes,resizable=yes,dependent=yes,alwaysRaised=yes');
    }

    var cautareReclamatieWindow = null;
    function cautareReclamatie(){
        if( cautareReclamatieWindow != null) cautareReclamatieWindow.close();
        cautareReclamatieWindow = window.open('<%=AppConstants.APP_NAME%>?action=cautare-reclamatii','filtru', 'top=50,left=50,width=850 height=500, status=yes,scrollbars=yes,resizable=yes,dependent=yes,alwaysRaised=yes');
    }

    function cautareStrada(){
       window.open('<%=AppConstants.APP_NAME%>?action=search-street-form','filtru', 'width=550 height=300, status=yes,resizable=yes,scrollbars=yes,top=100,left=100,dependent=yes,alwaysRaised=yes');
    }

    function stergereContinut(){
        document.zaforma.reset();
        $('statusReclamatie').innerHTML = '';
        fillSesizare2Select();

        Element.hide( "clientEligibil");
        Element.hide( "spanLoadingStatusReclamatie");

        document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.focus();
    }



    var team = new Array(
            <% for( int i = 0; i < ReclamatieConstants.SESIZARI.length; i++){
                final String[] sesizari2 = ReclamatieConstants.SESIZARI[i];%>
                new Array(
                    new Array("Selectati", -1)
                    <% for( int j = 1; j < sesizari2.length; j++){ %>
                        <%=j==1 ? "," : ""%>
                        new Array("<%=sesizari2[j]%>", <%=j%>)<%=j < sesizari2.length-1 ? "," : ""%>
                    <%}%>
                    )<%=i < ReclamatieConstants.SESIZARI.length-1 ? "," : ""%>
            <% }%>
        );

    function fillSesizare2Select(){
        fillSelectFromArray(document.zaforma.<%=ReclamatieConstants.CONTINUT_SESIZARE2%>, team[document.zaforma.<%=ReclamatieConstants.CONTINUT_SESIZARE1%>.selectedIndex-1]);

        if( document.zaforma.<%=ReclamatieConstants.CONTINUT_SESIZARE1%>.selectedIndex == <%=ReclamatieConstants.INDEX_ALTA_SESIZARE%> + 1){
            Element.show( '<%=ReclamatieConstants.ALTA_SESIZARE%>');
            Element.hide( '<%=ReclamatieConstants.CONTINUT_SESIZARE2%>');
        } else {
            if( document.zaforma.<%=ReclamatieConstants.CONTINUT_SESIZARE2%>.options.length <= 1){
                Element.hide( '<%=ReclamatieConstants.CONTINUT_SESIZARE2%>');
            }else{
                Element.show( '<%=ReclamatieConstants.CONTINUT_SESIZARE2%>');
            }
            Element.hide( '<%=ReclamatieConstants.ALTA_SESIZARE%>');
        }
    }


    fillSesizare2Select();
    selectOption( document.zaforma.<%=ReclamatieConstants.CONTINUT_SESIZARE2%>, <%=bean.getContinutSesizare2()%>);

    setTimeout("checkAppStatus()", 2000);

    setAppStatusOn();

    checkLastCookies();

</script>

<script type="text/javascript">

    function setStradaData(codStrada, tipArtera, denumire){
        $('<%=ReclamatieConstants.COD_STRADA%>').value = codStrada;
        $('<%=ReclamatieConstants.TIP_ARTERA%>').value = tipArtera;
        $('<%=ReclamatieConstants.STRADA%>').value = denumire;
    }

    function completeazaAdresa(
            judet,
            codMunicipiu, municipiu,
            codLocalitate, localitate,
            tipArtera, denStrada, nr, bloc, scara, etaj, apartament, nlc, isClientEligibil
    ){
        document.zaforma.<%=ReclamatieConstants.JUDET%>.value = judet;
        document.zaforma.<%=ReclamatieConstants.COD_MUNICIPIU%>.value = codMunicipiu;
        document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.value = municipiu;
        document.zaforma.<%=ReclamatieConstants.COD_LOCAL%>.value = codLocalitate;
        document.zaforma.<%=ReclamatieConstants.LOCALITATE2%>.value = localitate;
        document.zaforma.<%=ReclamatieConstants.TIP_ARTERA%>.value = tipArtera;
        document.zaforma.<%=ReclamatieConstants.STRADA%>.value = denStrada;
        document.zaforma.<%=ReclamatieConstants.NR%>.value  = nr;
        document.zaforma.<%=ReclamatieConstants.BLOC%>.value  = bloc;
        document.zaforma.<%=ReclamatieConstants.SCARA%>.value = scara;
        document.zaforma.<%=ReclamatieConstants.ETAJ%>.value  = etaj;
        document.zaforma.<%=ReclamatieConstants.AP%>.value    = apartament;

        if( isClientEligibil) Element.show("clientEligibil")
        else Element.hide("clientEligibil");
    }

    function validateForNumar(){
        if( document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.value.length == 0){
            alert( 'Selectati o localitate !');
            document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.focus();
            return false;
        }

        if( document.zaforma.<%=ReclamatieConstants.STRADA%>.value.length == 0){
            alert( 'Selectati o strada !');
            document.zaforma.<%=ReclamatieConstants.STRADA%>.focus();
            return false;
        }
        return true;
    }

    function validateForBloc(){
        if( validateForNumar()){
            <%--if( document.zaforma.<%=ReclamatieConstants.NR%>.value.length == 0){--%>
                <%--alert( 'Introduceti un numar !');--%>
                <%--document.zaforma.<%=ReclamatieConstants.NR%>.focus();--%>
                <%--return false;--%>
            <%--}--%>
        }
        return true;
    }

    function initSuggesters(){
        //LOC CONSUM SUGGEST
        /*
        locConsumAjaxDataProvider = new AjaxDataProvider( function(){
            return (
                    document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.value.trim().length > 0
                    && document.zaforma.<%=ReclamatieConstants.STRADA%>.value.trim().length > 0
                    //&& document.zaforma.<%=ReclamatieConstants.NUME_CLIENT%>.value.trim().length > 0
                ? "<%=AppConstants.APP_NAME%>?action=caut-client-ajax" +
                    '&loc=' + document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.value +
                    '&loc2=' + document.zaforma.<%=ReclamatieConstants.LOCALITATE2%>.value +
                    '&tipArtera=' + document.zaforma.<%=ReclamatieConstants.TIP_ARTERA%>.value +
                    '&codStrada=' + document.zaforma.<%=ReclamatieConstants.COD_STRADA%>.value +
                    '&strada=' + document.zaforma.<%=ReclamatieConstants.STRADA%>.value +
                    '&nr=' + document.zaforma.<%=ReclamatieConstants.NR%>.value +
                    '&bloc=' + document.zaforma.<%=ReclamatieConstants.BLOC%>.value +
                    '&scara=' + document.zaforma.<%=ReclamatieConstants.SCARA%>.value +
                    '&etaj=' + document.zaforma.<%=ReclamatieConstants.ETAJ%>.value +
                    '&ap=' + document.zaforma.<%=ReclamatieConstants.AP%>.value +
                    '&codLoc2=' + document.zaforma.<%=ReclamatieConstants.COD_LOCAL%>.value +
                    '&q='
                : null
            );
        });

        new InputSugest( $("<%=ReclamatieConstants.NUME_CLIENT%>"), $("<%=ReclamatieConstants.NLC_CLIENT%>"), locConsumAjaxDataProvider, {
            onSelect : function( item){
                completeazaAdresa(
                        item.judet,
                        item.codMunicipiu,
                        item.municipiu,
                        item.codLocalitate,
                        item.localitate,
                        item.tipArtera,
                        item.artera,
                        item.nr,
                        item.bl,
                        item.sc,
                        item.et,
                        item.ap
                );
            }
        });
        */

        //MUNICIPIU SUGGEST
        municipiuAjaxDataProvider = new AjaxDataProvider( function(){
            return "<%=AppConstants.APP_NAME%>?action=suggest-localitate&<%=SugestConstants.SUGEST_Q_QUERY%>=";
        });

        var municipiuSelectat = null;

        new InputSugest( $("<%=ReclamatieConstants.MUNICIPIU%>"), $("<%=ReclamatieConstants.COD_MUNICIPIU%>"), municipiuAjaxDataProvider, {
            onSelect : function( item){
                municipiuSelectat = item;

                $("<%=ReclamatieConstants.MUNICIPIU%>").value = item.denMunicipiu;
                $('<%=ReclamatieConstants.COD_LOCAL%>').value ='';
                $('<%=ReclamatieConstants.LOCALITATE2%>').value ='';
                $('<%=ReclamatieConstants.STRADA%>').value ='';
                $('<%=ReclamatieConstants.TIP_ARTERA%>').value ='';

                $('<%=ReclamatieConstants.JUDET%>').value = item.denJudet;

                if( item.countCopii == 1 ){
                    //completam denumirea si codul localitatii data este doar una singura
                    $("<%=ReclamatieConstants.LOCALITATE2%>").value = item.denCopil;
                    $("<%=ReclamatieConstants.COD_LOCAL%>").value = item.codCopil;

                    //setam focusul direct pe strada
                    $('<%=ReclamatieConstants.STRADA%>').value = '';
                    $('<%=ReclamatieConstants.TIP_ARTERA%>').value = '';
                    $('<%=ReclamatieConstants.STRADA%>').focus();

                } else if (item.countCopii == 0){
                        //setam focusul direct pe strada
                        $('<%=ReclamatieConstants.STRADA%>').value = '';
                        $('<%=ReclamatieConstants.TIP_ARTERA%>').value = '';
                        $('<%=ReclamatieConstants.STRADA%>').focus();
                }else{
                    $('<%=ReclamatieConstants.LOCALITATE2%>').focus();
                }
            }
        });


        //localitate sugest
        var localitate2AjaxDataProvider = new AjaxDataProvider( function(){
            return "<%=AppConstants.APP_NAME%>?action=suggest-localitate2"
                    +"&mun=" + $("<%=ReclamatieConstants.MUNICIPIU%>").value
                    +"&codMun=" + $("<%=ReclamatieConstants.COD_MUNICIPIU%>").value
                    + "&<%=SugestConstants.SUGEST_Q_QUERY%>=";
        });

        new InputSugest( $("<%=ReclamatieConstants.LOCALITATE2%>"), $("<%=ReclamatieConstants.COD_LOCAL%>"), localitate2AjaxDataProvider, {
            onSelect : function( item){
                $('<%=ReclamatieConstants.LOCALITATE2%>').value = item.denLocalitate;
                $('<%=ReclamatieConstants.COD_MUNICIPIU%>').value = item.codMunicipiu;
                $('<%=ReclamatieConstants.MUNICIPIU%>').value = item.denMunicipiu;
                $('<%=ReclamatieConstants.JUDET%>').value = item.denJudet;

                $('<%=ReclamatieConstants.STRADA%>').value = '';
                $('<%=ReclamatieConstants.TIP_ARTERA%>').value = '';
                $('<%=ReclamatieConstants.STRADA%>').focus();
            }
        });



        //tip artera sugest
        var tipArteraAjaxDataProvider = new AjaxDataProvider( function(){
            return "<%=AppConstants.APP_NAME%>?action=suggest-tip-artera&<%=SugestConstants.SUGEST_Q_QUERY%>=";
        });

        var tipArteraAjaxSugest = new InputSugest( $("<%=ReclamatieConstants.TIP_ARTERA%>"), null, tipArteraAjaxDataProvider, {
            onSelect : function( item){
                //item.key;
                //item.value;
                $('<%=ReclamatieConstants.STRADA%>').value ='';
                $('<%=ReclamatieConstants.STRADA%>').focus();
            }
        });


        //strada sugest
        var stradaAjaxDataProvider = new AjaxDataProvider( function(){
            var nrCopiiLocalitate1 = municipiuSelectat != null ? municipiuSelectat.countCopii : 0;
            var codMun = $('<%=ReclamatieConstants.COD_MUNICIPIU%>').value;
            var mun = $('<%=ReclamatieConstants.MUNICIPIU%>').value;
            var codLoc = $('<%=ReclamatieConstants.COD_LOCAL%>').value;
            var loc = $('<%=ReclamatieConstants.LOCALITATE2%>').value;
            var tipArtera = $('<%=ReclamatieConstants.TIP_ARTERA%>').value;

            if( mun != null  && mun.trim().length > 0)
                return "<%=AppConstants.APP_NAME%>?action=suggest-artera"
                        + "&codLocalitate=" + codMun
                        + "&localitate=" + mun
                        + "&codLocalitate2=" + codLoc
                        + "&localitate2=" + loc
                        + "&tipArtera=" + tipArtera
                        + "&<%=SugestConstants.SUGEST_Q_QUERY%>=";
            else return null;
        });

        new InputSugest( $("<%=ReclamatieConstants.STRADA%>"), $("<%=ReclamatieConstants.COD_STRADA%>"), stradaAjaxDataProvider, {
            onSelect : function( item){
                setStradaData( item.cod, item.tipArtera, item.denumire);
                $('<%=ReclamatieConstants.NR%>').value='';
                $('<%=ReclamatieConstants.NR%>').focus();

                getStatusReclamatie();
            }
        });
    }


    function validForGetStatusReclamatie(){
        if( document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.value == 0) return false;
        <%--if( document.zaforma.<%=ReclamatieConstants.LOCALITATE2%>.value == 0) return false;--%>
        //if( document.zaforma.<%=ReclamatieConstants.TIP_ARTERA%>.value == 0) return false;
        //if( document.zaforma.<%=ReclamatieConstants.STRADA%>.value == 0) return false;

        return true;
    }

    function getStatusReclamatie(){
        if( !validForGetStatusReclamatie()) return;

        Element.show( "spanLoadingStatusReclamatie");
        Element.addClassName( "spanLoadingStatusReclamatie", "loadingIndicator");

        $("spanLoadingStatusReclamatie").innerHTML = "Se verifica adresa ...";

        var localitate = document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.value;
        var localitate2 = document.zaforma.<%=ReclamatieConstants.LOCALITATE2%>.value;
        var tipArtera  = document.zaforma.<%=ReclamatieConstants.TIP_ARTERA%>.value;
        var strada     = document.zaforma.<%=ReclamatieConstants.STRADA%>.value;
        var nr         = document.zaforma.<%=ReclamatieConstants.NR%>.value;
        var bloc       = document.zaforma.<%=ReclamatieConstants.BLOC%>.value;

        var pars = "localitate="+localitate.trim() +
                      "&localitate2="+localitate2.trim()+
                      "&tipArtera="+tipArtera.trim()+
                      "&strada="+strada.trim() +
                      "&nr="+nr.trim() +
                      "&bloc="+bloc.trim();

        new Ajax.Request( "<%=AppConstants.APP_NAME%>?action=callcenter_getStatusReclamatie",{
                method: 'post',
                parameters: pars,
                onComplete: onGetStatusResponse,
                onFailure: function(){
                    Element.removeClassName( "spanLoadingStatusReclamatie", "loadingIndicator");
                    $("spanLoadingStatusReclamatie").innerHTML = "Eroare la verificare adresa";
                }
        });
    }

    function onGetStatusResponse( originalRequest){
        var loadingEl = $("spanLoadingStatusReclamatie");
        Element.removeClassName( loadingEl, "loadingIndicator");

        var response = getResponseMesg( originalRequest.responseText);

        $('statusReclamatie').innerHTML = response;

        if( response != null && response.trim().length > 0){
            Element.show( loadingEl);
            loadingEl.innerHTML = "Exista evenimente la aceasta adresa.";
        } else {
            Element.hide( loadingEl);
        }
    }


    function alertCodOficom(){
        var codStrada = document.zaforma.<%=ReclamatieConstants.COD_STRADA%>.value;
        if( codStrada != null && codStrada.length > 0 && codStrada != "-1"){
            new Ajax.Request(
                    "<%=AppConstants.APP_NAME%>?action=callcenter_getCodOficom",
                    {
                        method : "post",
                        parameters: "codStrada=" + codStrada + "&nr=" + document.zaforma.<%=ReclamatieConstants.NR%>.value + "&bloc=" + document.zaforma.<%=ReclamatieConstants.BLOC%>.value,
                        onComplete : onGetCodOficomResponse
                    });
        }
    }

    function onGetCodOficomResponse( originalRequest){
        alert( getResponseMesg( originalRequest.responseText));
    }

    function onChangeTipArtera(){
        document.zaforma.<%=ReclamatieConstants.COD_STRADA%>.value = '-1';
        document.zaforma.<%=ReclamatieConstants.STRADA%>.value = '';
    }

    var consultareClientWindow = null;
    function cautareClientByNLC(){
        if( consultareClientWindow != null) consultareClientWindow.close();
        consultareClientWindow = window.open('<%=AppConstants.APP_NAME%>?action=consultare-client' +
                    '&ByNLC=true'
                , 'consultareClinet', 'top=50,left=50,width=850 height=500,scrollbars=yes, status=yes,resizable=yes,dependent=yes,alwaysRaised=yes');
    }

    var browseAdreseWindow = null;
    function browseGEO(){
        if( browseAdreseWindow != null) browseAdreseWindow.close();

        browseAdreseWindow = window.open(
                '<%=AppConstants.APP_NAME%>?action=<%=AdreseHelper.isIntegratedWithOpen() ? "callcenter_browseGeo" : (AdreseHelper.isIntegratedWithSap() ? "browse-adrese-view" : "unknown-integration")%>',
                'browseAdrese',
                'top=50,left=50,width=850 height=550,scrollbars=yes, status=yes,resizable=yes,dependent=yes,alwaysRaised=yes'
        );
    }

    document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.focus();

    setTimeout(function(){
        try {
            initSuggesters();
        }catch(e){
            if(console) console.error("Eroare la initializare suggestere", e);
            alert("Eroare la initializare suggestere " + e.message);
        }
    }, 500);
</script>
