<%@ page import="ro.emid.common.db.OpenSCGAccessDB" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.constants.GlobalAppConstants" %>
<%@ page import="ro.emid.common.types.Strada" %>
<%@ page import="ro.emid.common.db.adrese.AdreseHelper" %>
<%@ page import="ro.emid.callcenter.util.CallcenterHelper" %>
<%@ page import="ro.emid.callcenter.i10n.Configuration" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>

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

<%
    final boolean completareForLucrareProgramata = ObjectUtils.getStringValue(request.getParameter("forCompletare")).trim().length() > 0;
    final String whatToSearch = ObjectUtils.getStringValue(request.getParameter("whatToSearch")).toUpperCase();

    Strada []strazi = new Strada[0];
    if (whatToSearch != null && whatToSearch.trim().length() > 0) {
//        strazi = OpenSCGAccessDB.searchStrada( null, null, whatToSearch.replace(' ', '%'));
        strazi = AdreseHelper.getAdreseDao().searchStrada(null, null, whatToSearch.replace(' ', '%'));
    }

    Configuration configuration = CallcenterHelper.getConfiguration();
%>

<TABLE align=center border=0 cellPadding=5 cellSpacing=0 width="100%" class="gsaptd">
    <tr><td align="center" valign="center">
    <FORM NAME="zaforma" ACTION="<%=AppConstants.APP_NAME%>?action=search-street-form" METHOD=POST>
        <INPUT TYPE="hidden" NAME="forCompletare" VALUE="<%=(completareForLucrareProgramata ? "true" : "")%>" />
        <table width="100%" border="0" height="100%">
        <tr height="20">
            <td>Introduceti o parte din numele starzii:</td>
        </tr>
        <TR height="20">
            <TD align="left" >
                <input class="gsapinputtext" type="text" name="whatToSearch" style="width:98%" value="<%=AppConstants.format( whatToSearch)%>" >
            </td>
            <td width="1%" nowrap>
                <input type="submit" value="Caută"> &nbsp;
                <%if( completareForLucrareProgramata){%>
                    <button onclick="completareAdresa()" <%=strazi.length > 0 ? "" : "disabled"%>>Completeaza adresa</button>
                <%}else{%>
                    <button onclick="window.close()" >Inchide</button>
                <%}%>     
            </td>
        </tr>
        <tr><td colspan="2">
            <TABLE id="table" border=0 cellPadding=2 cellSpacing=1 width="100%" height="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white; margin-top: 20px ">
                <TR noSelect="true">
                    <%if( completareForLucrareProgramata){%>
                        <TH class="gsapthead" noWrap width="1%">&nbsp;</TH>
                    <%}%>
                    <TH class="gsapthead" noWrap width="40">&nbsp;Cod:&nbsp;</TH>
                    <TH class="gsapthead" noWrap >&nbsp;<%=configuration.getLabelLocalitate1()%>:&nbsp;</TH>
                    <TH class="gsapthead" noWrap >&nbsp;<%=configuration.getLabelLocalitate2()%>:&nbsp;</TH>
                    <TH class="gsapthead" noWrap >&nbsp;Tip artera:&nbsp;</TH>
                    <TH class="gsapthead" noWrap >&nbsp;Denumire:&nbsp;</TH>
                </TR>
                <%for (int i = 0; i < strazi.length; i++) {
                    final Strada strada = strazi[i];          %>
                    <tr>
                        <%if( completareForLucrareProgramata){%>
                            <td><input type="checkbox" name="select" value="<%=GlobalAppConstants.format( strada.getMunicipiu()) + ", " + GlobalAppConstants.format( strada.getTipArtera()) + ' ' + GlobalAppConstants.format( strada.getDenumire()) + ';'%>"></td>
                        <%}%>
                        <td><%=GlobalAppConstants.format(strada.getCod())%></td>
                        <td><%=GlobalAppConstants.format(strada.getMunicipiu())%></td>
                        <td><%=GlobalAppConstants.format(strada.getLocalitate())%></td>
                        <td><%=GlobalAppConstants.format(strada.getTipArtera())%></td>
                        <td><%=GlobalAppConstants.format(strada.getDenumire())%></td>
                    </tr>
                <%}%>
            </TABLE>
        </td></tr>
        </table>
    </FORM>
</td></tr></TABLE>

<script type="text/javascript">
    var tableStrazi = new Table('table', "tableStrazi");

    function completareAdresa(){
        var arr = document.zaforma.select;
        if( arr == null) return;
        
        var length = 0;
        try{ if( arr.length) length = arr.length; }catch(e){}
        var textToPut = "";
        if( length == 0){
            if( arr.checked) textToPut = arr.value;
        }else{
            for( var i = 0; i < length; i++){
                if( arr[i].checked) textToPut += arr[i].value + "\n";
            }
        }

        if( textToPut.trim().length == 0){
            alert( "Selectati cel putin o strada");
            return;
        }

//        alert( length);
//        alert( textToPut);

        try{
            window.opener.completeazaAdresa(textToPut);
        }catch( e){}
        
        window.close();
    }

    document.zaforma.whatToSearch.focus();
    document.zaforma.whatToSearch.select();
</script>

