function form_plzsuche(lang){
	if (lang == 'de'){var title = 'PLZ - Suche f&uuml;r Deutschland';}
	else {
		var title = 'Postal code search for populated places in Germany';
	};
	Ext.apply(Ext.form.VTypes, {  
		'numeric': function(){  
	       
	        /***************************************************************** 
		DESCRIPTION: Validates that a string contains only valid numbers. 
	        PARAMETERS: 
		strValue - String to be tested for validity 
	        RETURNS: 
		True if valid, otherwise false. 
		******************************************************************/  
			var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;  
			return function(strValue){  
	               //check for numeric characters  
				return objRegExp.test(strValue);
			}  
		}(),  
  		'numericText': 'Only numbers are allowed'  
	});
	
	//ProxyHost definieren
	OpenLayers.ProxyHost= "/cgi-bin/proxy.cgi?url=";	
	
	var introTxt = 'Mit dem Formular k&ouml;nnen Postleitzahlen und Orte ';
	introTxt += 'f&uuml;r Deutschland recherchiert werden.</br></br>';
	introTxt += 'Die Grundlage f&uuml;r die Ortsrecherche bildet das Digitale Landschaftsmodell 1:250 000 (DLM250) ';
	introTxt += 'vom <a href="hhttp://www.geodatenzentrum.de/geodaten/gdz_rahmen.gdz_div?gdz_spr=deu&gdz_akt_zeile=4&gdz_anz_zeile=1&gdz_user_id=0" target="_blank">';
	introTxt += 'Bundesamt f&uuml;r Kartographie und Geod&auml;sie</a>. </br></br>';
	introTxt += 'Die Ergebnistabelle wird durch den Mausklick auf den gesuchten Ort angezeigt. Diese zeigt ';
	introTxt += 'die Resulte der Suche auf <a href="http://de.wikipedia.org/wiki/Wikipedia:Hauptseite" target="_blank">Wikipedia.org</a> an, welche durch Mausklick auf die Tabellenzeile ge&ouml;ffnet werden. </br></br></br>';
	
	var searchvalue; //var idort; var umkreis;
	
	var ds = new Ext.data.Store({
		/* for cross domain queries */
		id: 'comboStore',
		proxy: new Ext.data.HttpProxy({
			url: 'php/plzSearch.php?searchstring='+searchvalue
		}),
		reader: new Ext.data.JsonReader({
			root: 'rows'
		}, [
			{name: 'plz', mapping: 'plz'},
			{name: 'name', mapping: 'name'},
			{name: 'bundesland', mapping: 'bundesland'},
			{name: 'kreis', mapping: 'kreis'},
			{name: 'gemeinde', mapping: 'gemeinde'},
			{name: 'gid', mapping: 'gid'},
			{name: 'x', mapping: 'x'},
			{name: 'y', mapping: 'y'}
		]),
		autoLoad: true
	});
	
	var resultTpl = new Ext.XTemplate(
		'<tpl for="."><div class="search-item">',
		'<span><h3>{name}</h3></span>',
 		'PLZ: <b><font color="#4e6378">{plz}</font></b>',	
		' | Gemeinde: <b><font color="#4e6378">{gemeinde}</font></b>',
 		' | Kreis: <b><font color="#4e6378">{kreis}</font></b>',
   		' | Bundesland: <b><font color="#4e6378">{bundesland}</font></b>',
		'</div></tpl>' 
		);
		
	var searchTextField = new Ext.form.ComboBox({
		fieldLabel: 'PLZ oder Ortsname',
		store: ds,
		name: 'searchstring',
		id: 'searchstring',
		typeAhead: false,
		minChars: 3,
		queryDelay: 100,
		autoSelect: true,
		loadingText: 'Searching...',
		width: 400,
		hideTrigger: true,
		triggerAction: 'all',
		hideMode: 'offsets',
		enableKeyEvents: true,
		labelStyle: 'text-align: right; font-weight: bold;',
		tpl: resultTpl,
		itemSelector: 'div.search-item', 
		mode: 'local',
		forceSelection: true,
		listeners: {
			keyup: function(combo,e){
				searchvalue = Ext.getCmp('searchstring').getValue();
				/*combo.store.proxy.url = 'http://www.visdat.de/php/placesSearch.php?searchstring='+searchvalue;*/
				combo.store.proxy.url = 'php/plzSearch.php?searchstring='+searchvalue;
				combo.store.removeAll();
				combo.store.totalLength = 0;
						// force the reload on trigger
				combo.lastQuery = null;
				combo.store.reload({params: { searchstring: combo.getValue()}});
			},
			select: function(combo,record){
				combo.setValue(record.data['plz']+' '+record.data['name']+' ('+ record.data['bundesland']+ ', ' +record.data['kreis'] +')');
				var wikilink = 'http://de.wikipedia.org/wiki/Spezial:Suche/'+record.data['plz']+'_'+record.data['name'];
				var htmlresult = '<table><tr><td style=\'padding:100\'>';
				htmlresult += '<a href='+wikilink+' target=\'_blank\'><b>'+record.data['plz']+' '+record.data['name']+'</b> auf Wikipedia.org</a>';
				htmlresult += '</td></tr></table>';
				
				var c = Ext.getCmp('showresults');
				if (c.items.items.length) {
					c.remove(c.items.items[0]);
				};
				c.add({
					items: [{
						border: false,
      						html: htmlresult 
					}]
				});
				c.doLayout();
				wikisearch(record.data['plz'],record.data['name']);
			}
 		  }
		});
		
	var hiddenValueField = new Ext.form.TextField({
		id: 'hiddenValueField',
   		hidden: true
	});
	
	var searchForm = new Ext.form.FormPanel({
		id: 'searchFormPanel',
     		items: [searchTextField/*, umkreisTextField*/, hiddenValueField]
	});
	
	var tablayout = {
		layout: 'table',
  		layoutConfig: {
	  		columns: 2
  		},
  		items:[{
	 		html: introTxt,
   			colspan: 2
  		},{
	   		items: searchForm
  		}]
	};
	
	var searchresults = {
		id: 'showresults',
		layout: 'table',
		layoutConfig: {
			columns: 1
		},
		items: [{
			border: false
			,html: ''
			}
		]
	};
	
	var coordPanel = new Ext.Panel({
		title: title,
  		id: 'umkreissuchePanel',
  		width: 700,
  		height: 500,
  		frame: true,
  		style: {marginLeft: '10px', marginTop: '10px'},
  		items: [
		  	tablayout
    			,searchresults
		   ]
	});		
	return coordPanel;
}

function wikisearch(plz,ort){
	//Data Store
	var url = 'http://de.wikipedia.org/w/api.php?action=query&list=search&srsearch='+plz+','+ort+'&format=json&srprop=snippet';
	var ds_wiki = new Ext.data.Store(
	{
		autoLoad: true,
 		proxy: new Ext.data.ScriptTagProxy({url: url, method:'GET'}),
   		storeId: 'wikiStore',
		reader: new Ext.data.JsonReader({
			root: 'query.search',
			fields: [{name: 'title', mapping:'title'},{name:'snippet', mapping:'snippet'}]
		})
	});

	var grid = new Ext.grid.GridPanel({
		store: ds_wiki,
		id: 'results_wikisuche',
		height: 290,
		colModel: new Ext.grid.ColumnModel({
			columns: [
			{header: 'Title', dataIndex: 'title', sortable: true, width: 200},
   			{header: 'Snippet', dataIndex: 'snippet', sortable: true, width: 400}
			]
		}),
		selModel: new Ext.grid.RowSelectionModel({
			singleSelect : true,
			listeners: {
				rowselect: {
					fn: function(sm,index,record) {
						var wikiurl = "http://de.wikipedia.org/wiki/Spezial:Suche/"+record.data.title;
						this.deselectRow(index);
						window.open(wikiurl);
					}
				}
			}
		})
	});
	
	
	var c = Ext.getCmp('showresults');
	if (c.items.items.length) {
		c.remove(c.items.items[0]);
	};
	c.add({
		items: grid
	});
	c.doLayout();
}
