<%-- $HeadURL$
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2009 by:
 - Department of Geography, University of Bonn -
 and
 - lat/lon GmbH -

 This library is free software; you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation; either version 2.1 of the License, or (at your option)
 any later version.
 This library is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 details.
 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

 Contact information:

 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/

 Department of Geography, University of Bonn
 Prof. Dr. Klaus Greve
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/

 e-mail: info@deegree.org
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="org.deegree.i18n.Messages" %>
<%@ page import="java.util.Locale" %>
<%
    Locale loc = request.getLocale();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta name="author" content="Judit Mays, lat/lon GmbH">
        <meta name="KeyWords" content="deegree iGeoPortal lat/lon" />
        <title>deegree WFS Gazetteer</title>
        <link href="../../css/deegree.css" rel="stylesheet" type="text/css" />
		<style type="text/css">
		<!--
			.sel {width: 200px;}
		-->
		</style>
        <script LANGUAGE="JavaScript1.2" TYPE="text/javascript">
		<!-- 

		var ifdocument = null;
        var type = null;
        var identifier = null;

		function clearList(listID) {
		    var list = document.getElementById( listID );
		    if ( list == null ) {
		    	return;
		    }
		    var ops = list.childNodes;
		    for ( var i = ops.length-1; i > 0; i-- ) {
		        list.removeChild( ops[i] );
		    }
		}
	
		function setMessage( mesg ) {
		    var el = document.getElementById( 'messageline' );
		    var els = el.childNodes;
		    for ( i = 0; i < els.length; i++ ) {
		        el.removeChild( els[i] );
		    }
		    var t = document.createTextNode( mesg );
		    el.appendChild(t)
		}
		
        function createRPC( moduleName, featuretype, querytemplate, filterproperties, resultFormat, methodName, shouldNormalize ) {
			var s = '<?xml version="1.0" encoding="UTF-8"?>'+
	    		'<methodCall><methodName>' + methodName + '</methodName>'+
				'<params><param><value><struct>' +
                    '<member><name>MODULE</name><value><string>' + moduleName + '</string></value></member>' +
                    '<member><name>FEATURETYPES</name><value><string>' + featuretype + '</string></value></member>' +
                    '<member><name>QUERYTEMPLATE</name><value><string>' + querytemplate + '</string></value></member>' +
                    '<member><name>FILTERPROPERTIES</name><value><string><![CDATA[' + filterproperties + ']]></string></value></member>' +
                    '<member><name>RESULTFORMAT</name><value><string>' + resultFormat + '</string></value></member>' +              
                    '<member><name>NORMALIZE</name><value><boolean>' + shouldNormalize + '</boolean></value></member>' +
                    '<member><name>LOCALE</name><value><string>de</string></value></member>' +
				'</struct></value></param></params></methodCall>';
				// s = encodeURIComponent(s); // Don't use this! encodeUriComponent(s) does not work for umlaute! 
			return s;
		}
		
        //function getList( module, featureType, template ) {
        function getList( module, featureType, template, shouldNormalize ) {
            if ( featureType != null && featureType != '' ) {
                // TODO: "app:ZipCodes" has to be adapted to the name of the FeatureType
                if ( featureType == "app:ZipCodes" ) {
                    clearList("resultID");
                }
                var el = document.getElementById( 'SearchString' );
                var geographicIdentifier = el.value;   
                // TODO: change setting to any number of letters             
                if( geographicIdentifier.length < 3 ) {
                    alert('please use at least 3 letters for the search.');
                    el.focus();
                    return;
                }
            }            
            //var filterprop = '{IDENTIFIER=XXX}'; // '{key1=value1}{key2=value2}{...}'
            //var s = createRPC( module, featureType, template, filterprop, 'FEATURECOLLECTION', 'wfs:gazetteer' );
            var filterprop = '{IDENTIFIER=' + geographicIdentifier + '}'
            var s = createRPC( module, featureType, template, filterprop, 'FEATURECOLLECTION', 'wfs:gazetteer', shouldNormalize );
            var tmp = window.location.href.split('/');

            setMessage('... searching ...');
            ifdocument.forms[0].action = tmp[0] +  '//' + tmp[2]  +  '/' + tmp[3]+ '/control?rpc='  + s;
            //ifdocument.forms[0].action = tmp[0] +  '//' + tmp[2]  + '/control?rpc='  + s;
            ifdocument.forms[0].submit();
		}
		
	    function selectFinal( list, tp ) {
	        var idx = list.selectedIndex;
	        var value = list.options[idx].value;
	        var tmp = value.split('|');
	        identifier = tmp[0];
	        type = tp;
	    }
	    
	    function take( module ) {
            if ( identifier == null || identifier.indexOf( '...' ) > -1 ) {
                alert( "no valid selection for zooming" );
                return;
            }
            // TODO: "app:ZipCodes" has to be adapted to the name of the FeatureType
			if ( "app:ZipCodes" == type  ) {
                // TODO: the template has to be the one specified in WMC.
				template = "FreeSearch2MapTemplate";
			}
	        var s = createRPC( module, type, template, '{IDENTIFIER='+identifier+'}', 'FEATURECOLLECTION', 'wfs:take' );
	        var tmp = window.location.href.split('/');
	        ifdocument.forms[0].action = tmp[0] +  '//' + tmp[2] + '/' + tmp[3]+ '/control?rpc='  + s;
	        //ifdocument.forms[0].action = tmp[0] +  '//' + tmp[2]  + '/control?rpc='  + s;
	        ifdocument.forms[0].submit();
	    }
		-->
		</script>
	</head>
    <body class="gaz9 pGazFlavor">
	    <iframe src="./gaz_if.jsp" width="0" height="0" name="gazetteer" id="gazetteer" frameborder="0" marginheight="0" marginwidth="0"></iframe>
        <table>
            <tbody>
                <tr>
                    <td><b>enter search string</b></td>
                </tr>
                <tr>
                    <!-- TODO "app:ZipCodes" has to be adapted to the name of the FeatureType -->
                    <td><input class="gaz9 black inp" type="text" id="SearchString" name="SearchString" value="" size="18" />&nbsp;&nbsp;<a href="javascript:getList( 'GazModule', 'app:ZipCodes', 'FreeSearch2Template', 0 )" style="width:60px" class="buttonArea"><%=Messages.get( loc, "IGEO_STD_BTN_SEARCH" ) %></a></td>
                </tr>
                <tr>
                    <td><b>results</b></td>
                </tr>
                <tr>
                    <td>
                        <!-- TODO "app:ZipCodes" has to be adapted to the name of the FeatureType -->
                        <select class="sel" name="result" id="resultID" onchange="selectFinal( this, 'app:ZipCodes' );" >
                            <option value="... select"></option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td><p class="gaz9" id="messageline">&nbsp;</p></td>
                </tr>
                <tr>
                    <td>
                    	<a href="javascript:take( 'GazModule' );" class="buttonArea" style="width:100px;"><%=Messages.get( loc, "IGEO_STD_BTN_ZOOMTO" ) %></a>
        				&nbsp;
        				<a href="javascript:parent.window.close()" class="buttonArea" style="width:60px;"><%=Messages.get( loc, "IGEO_STD_BTN_CANCEL" ) %></a>
       				</td>
                </tr>
            </tbody>
        </table>
	</body>
</html>
