<%@ page import="ro.emid.common.constants.GlobalAppConstants" %>
<%@ page import="ro.emid.callcenter.obj.binder.ClientEligibilBinder" %>
<%@ page import="ro.emid.callcenter.obj.*" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>

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

<%
    final ClientEligibilBinder binder = (ClientEligibilBinder) session.getAttribute(ClientEligibilConstants.BEAN);
    final ClientEligibil bean = (binder != null ? ( ClientEligibil) binder.getBean() : new ClientEligibil());
%>

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

<TABLE cellSpacing=1 cellPadding=5 width="100%">
    <TR>
        <TD>
            <TABLE class=gSAPTHEAD cellSpacing=1 cellPadding=2 width="100%" border=0>
                <tr>
                    <td align="left">
                        <span style="FONT-WEIGHT: bold; FONT-SIZE: 16px;"> Client Eligibil:</span>
                    </td>
                </tr>
            </TABLE>
            <TABLE  border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD">

             <FORM NAME="zaforma" ACTION="<%=GlobalAppConstants.APP_NAME%>?action=saveClientEligibil" METHOD=POST>
                <INPUT TYPE="hidden" NAME="<%=ClientEligibilConstants.ID%>" VALUE="<%=bean.getId()%>" />

                <tr class="compartiment"><td colspan="2">Client:</td></tr>
                <tr><td>
                     <table width="100%">
                         <TR>
                           <TD>
                            Nume client:
                            <INPUT class="gsapinputtext" TYPE="TEXT" size="40" name="<%=ClientEligibilConstants.NUME_CLIENT%>" id="<%=ClientEligibilConstants.NUME_CLIENT%>" VALUE="<%=AppConstants.format( bean.getNume())%>">
                           </TD>
                           <TD>
                            Telefon:
                            <INPUT class="gsapinputtext" TYPE="TEXT" size="30" name="<%=ClientEligibilConstants.TELEFON%>" VALUE="<%=AppConstants.format( bean.getTelefon())%>">
                           </TD>
                           <TD>
                            Sucursala/Sectie:
                            <INPUT class="gsapinputtext" TYPE="TEXT" size="30" name="<%=ClientEligibilConstants.FAX%>" VALUE="<%=AppConstants.format( bean.getFax())%>">
                           </TD>
                           <td>
                             Furnizor:
                             <% Furnizor f = bean.getFurnizor(); %>
                             <% if( f == null) f = new Furnizor(); %>
                             <INPUT TYPE="hidden" size="30" id="<%=FurnizorConstants.ID%>" name="<%=FurnizorConstants.ID%>" VALUE="<%=f.getId()%>">
                             <INPUT class="gsapinputtext" TYPE="TEXT" size="30" id="<%=FurnizorConstants.NUME%>" name="<%=FurnizorConstants.NUME%>" VALUE="<%=AppConstants.format( f.getNume())%>">
                           </td>
                         </TR>
                     </table>
             </td></tr>
             <tr><td>&nbsp;</td></tr>
             <tr class="compartiment"><td colspan="2">Locuri Consum:</td></tr>
            <tr><td style="padding: 5px">
                <button onclick="adaugaNLC()">Adauga</button>
                
                    <table width="100%" style="margin-top:5px">
                        <tr>
                            <TH class="gsapthead">NLC</TH>
                            <TH class="gsapthead">Localitate</TH>
                            <TH class="gsapthead">Adresa</TH>
                            <TH class="gsapthead" width="1%" nowrap>Nivel Tensiune</TH>
                            <TH class="gsapthead" width="1%" >Op.</TH>
                        </tr>
                        <%
                            final LocConsum[] locuri = bean.getLocuriConsum();
                            for ( int i = 0; i < locuri.length; i++){
                                %>
                                <tr id="tr<%=i%>">
                                    <input type="hidden" name="visiblex" value="1">
                                    <td><input class="gsapinputtext" type="text" name="<%=LocConsumConstants.NLC%>" value="<%=locuri[i].getNlc()%>"></td>
                                    <td><input class="gsapinputtext" type="text" name="<%=LocConsumConstants.LOCALITATE%>" value="<%=AppConstants.format( locuri[i].getLocalitate())%>"></td>
                                    <td>
                                        <textarea cols="50" rows="1" name="<%=LocConsumConstants.ADRESA%>" ><%=AppConstants.format(locuri[i].getAdresa())%></textarea>
                                    </td>
                                    <td align="center">
                                        <select name="<%=LocConsumConstants.NIVEL_TENSIUNE%>" class="gSAPSelect">
                                            <option value="-1">Selectati:</option>
                                            <option value="<%=LocConsumConstants.NIVEL_TENS_JT%>" <%=locuri[i].getNivelTensiune() == LocConsumConstants.NIVEL_TENS_JT ? "selected" : ""%> >JT</option>
                                            <option value="<%=LocConsumConstants.NIVEL_TENS_MT%>" <%=locuri[i].getNivelTensiune() == LocConsumConstants.NIVEL_TENS_MT ? "selected" : ""%> >MT</option>
                                            <option value="<%=LocConsumConstants.NIVEL_TENS_MT_JT%>" <%=locuri[i].getNivelTensiune() == LocConsumConstants.NIVEL_TENS_MT_JT ? "selected" : ""%> >MT&JT</option>
                                            <option value="<%=LocConsumConstants.NIVEL_TENS_IT%>" <%=locuri[i].getNivelTensiune() == LocConsumConstants.NIVEL_TENS_IT ? "selected" : ""%> >IT</option>
                                        </select>
                                    </td>
                                    <td nowrap align="center">
                                        <a href="javascript: del(<%=i%>)">Delete</a>
                                    </td>
                                </tr>
                                <%
                            }

                           for( int i = 0; i < 50; i++){ %>
                                <tr style="display: none" id="tr<%=i+locuri.length%>">
                                    <input type="hidden" name="visiblex" value="0">
                                    <td><input class="gsapinputtext" type="text" name="<%=LocConsumConstants.NLC%>" value=""></td>
                                    <td><input class="gsapinputtext" type="text" name="<%=LocConsumConstants.LOCALITATE%>" value=""></td>
                                    <td>
                                        <textarea cols="50" rows="1" name="<%=LocConsumConstants.ADRESA%>" ></textarea>
                                    </td>
                                    <td align="center">
                                        <select name="<%=LocConsumConstants.NIVEL_TENSIUNE%>" class="gSAPSelect">
                                            <option value="-1">Selectati:</option>
                                            <option value="<%=LocConsumConstants.NIVEL_TENS_JT%>" >JT</option>
                                            <option value="<%=LocConsumConstants.NIVEL_TENS_MT%>" >MT</option>
                                            <option value="<%=LocConsumConstants.NIVEL_TENS_MT_JT%>" >MT&JT</option>
                                            <option value="<%=LocConsumConstants.NIVEL_TENS_IT%>" >IT</option>
                                        </select>
                                    </td>
                                    <td nowrap align="center">
                                        <a href="javascript: del(<%=i+locuri.length%>)">Delete</a>
                                    </td>
                                </tr>
                       <%  }%>
                    </table>
               
            </td></tr>
            <tr>
                <TD align="left" valign=top>
                    <input type="button" onclick="if( onSubmit()) this.form.submit() " value="Salveaza"/> &nbsp;&nbsp;&nbsp;
                    <input type="button" onclick="window.location = '<%=GlobalAppConstants.APP_NAME%>?action=show-clienti-eligibili'" value="Renunta"/>
                </TD>
            </TR>
        </FORM>
    </TABLE>
</td></TR>
</TABLE>

<script type="text/javascript">

    function adaugaNLC(){
        var n = document.zaforma.visiblex.length;
        for( var i = n-1; i >= 0 ; i--){
            if( document.zaforma.visiblex[i].value=="1") break;
        }

        i++;
        document.zaforma.visiblex[i].value="1";
        Element.show("tr"+i);
    }

    function del(index){
        document.zaforma.visiblex[ index].value="0";

        document.zaforma.<%=LocConsumConstants.NLC%>[ index].value = "";
        document.zaforma.<%=LocConsumConstants.LOCALITATE%>[ index].value = "";
        document.zaforma.<%=LocConsumConstants.ADRESA%>[ index].value = "";
        document.zaforma.<%=LocConsumConstants.NIVEL_TENSIUNE%>[ index].selectedIndex = 0;
        Element.hide("tr"+index);
    }

    function onSubmit(){
        try{
            var arr1 = document.zaforma.visiblex;
            var arr2 = document.zaforma.<%=LocConsumConstants.NLC%>;
            for( var i=0; i < arr1.length; i++){
                
                if( arr1[i].value == "1"){
                    var message = null;
                    var obj;
                    if( arr2[i].value.length == 0){
                        message = "Introduceti NLC-ul";
                        obj = arr2[i];
                    }else if( isNaN( arr2[i].value) || arr2[i].value <= 0){
                        message = "Introduceti corect NLC-ul";
                        obj = arr2[i];
                    }else{

                        if( document.zaforma.<%=LocConsumConstants.LOCALITATE%>[i].value.trim().length == 0){
                            message = "Introduceti localitatea";
                            obj = document.zaforma.<%=LocConsumConstants.LOCALITATE%>[i];
                        }else if( document.zaforma.<%=LocConsumConstants.ADRESA%>[i].value.trim().length == 0){
                            message = "Introduceti adresa";
                            obj = document.zaforma.<%=LocConsumConstants.ADRESA%>[i];
                        }
                    }

                    if( message != null){
                        alert( message);
                        obj.select();
                        obj.focus();
                        return false;
                    }
                }
            }

            return true;
        } catch (e){
            alert( e.message);
        }
        return false;
    }

    var ac_furnizor = new AutoAssist("<%=FurnizorConstants.NUME%>",
        function() {
		    return "<%=GlobalAppConstants.APP_NAME%>?action=callcenter_cautaFurnizor&q=" + this.txtBox.value;
	    },
        function(){
            $('<%=FurnizorConstants.ID%>').value="-1"; 
        }
    );


    document.zaforma.<%=ClientEligibilConstants.NUME_CLIENT%>.select();
    document.zaforma.<%=ClientEligibilConstants.NUME_CLIENT%>.focus();
</script>


