Ext.BLANK_IMAGE_URL = "/img/0.gif";
/*
 * Ausgelagertes Script fuer sie Suche
 * ################################################
 */

function submitSearchForm(form, text)
{
	if(form && form.q && form.q.value != text)
	{
		form.submit();
	}
}


// <!--- wlm | nbsp GmbH | 05.05.2010 10:01:54 AM | Suchvorschlaege --->
function createSearchInput(field, emptywording, loadingtext, floating)
{
	var store = new Ext.data.JsonStore(
	{
		url: 'index.cfm?event=cmp.cst.search.suggestions',
		root: 'items',
		fields: ['key', 'value'],
		method: 'GET',
		autoLoad: false
	});
	var inputBox = new Ext.form.ComboBox(
		{
			id: 'search_' + field,
			store: store,
			mode: 'remote',
			displayField: 'value',
			hideTrigger: true,
			typeAhead: false,
			typeAheadDelay: 250,
			applyTo: field,
			minChars: 2,
			pageSize: 0,
			queryParam: 'word',
			queryDelay: 100,
			autoSelect: false,
			forceSelection: false,
			loadingText: loadingtext,
			emptyText: emptywording,
			enableKeyEvents: true,
			listClass: 'extendedsearchcombolist',
			listeners:
			{
				'keydown': function(combo, event)
				{
					// ENTER
					if(event.keyCode == 13)
					{
						var idx = parseInt(combo.selectedIndex);						
						if(typeof (combo.store.data.items[idx]) != 'undefined' &&
							idx == 0)
						{
							if(combo.getValue().length != combo.store.data.items[idx].data.searchword.lenth)
							{
								combo.store.removeAll();
								combo.collapse();
								return false;
							}
						}
					}
					if (event.keyCode == 40){
						var idx = parseInt(combo.selectedIndex+1);		
						if(typeof combo.store.data.items[idx] != 'undefined'){							
							combo.store.data.items[idx].data.value = combo.store.data.items[idx].data.key;
							var val = combo.store.data.items[idx].data.searchword;
							combo.setRawValue(val);
							combo.setValue(val);
						}
					}
					
					if (event.keyCode == 38){
						var idx = parseInt(combo.selectedIndex-1);
						if(typeof combo.store.data.items[idx] != 'undefined'){
							combo.store.data.items[idx].data.value = combo.store.data.items[idx].data.key;
							var val = combo.store.data.items[idx].data.searchword;
							combo.setRawValue(val);
							combo.setValue(val);
						}
					}
					return;
				}
			}
		});
	
	inputBox.on('beforeselect', function(cb, record, index){
		var idx = parseInt(index);
		cb.store.data.items[idx].data.value = cb.store.data.items[idx].data.key;
		var val = cb.store.data.items[idx].data.value;
		cb.setValue(val);		
	}, this);
	
	if(floating)
	{
		inputBox.wrap.addClass('fll');
	}
}
// <!--- wlm | nbsp GmbH | 17.05.2010 16:02:45 PM | Maximale Anzahl der
// Suchergebnisse pro Seite --->
function updateMaxrows(value, form, text)
{
	if(!isNaN(value) && value > 0)
	{
		form.maxrows.value = value;
		submitSearchForm(form, text);
	}
}
// <!--- wlm | nbsp GmbH | 19.05.2010 9:18:01 AM | Nur eine Einschraenkung der
// Daten (Jahrgang oder von/bis) --->
function toggleDateFilter(items, emptytext)
{
	var i;
	var aItemIds = items.split(',');

	for(i = 0; i < aItemIds.length; i++)
	{
				
		switch(document.getElementById(aItemIds[i]).nodeName.toLowerCase())
		{
			case 'select':
				// Waehlt den ersten Eintrag aus!
				document.getElementById(aItemIds[i]).options[0].selected = true;
				break;
			case 'input':
				document.getElementById(aItemIds[i]).value = '';
				addEmptyText(document.getElementById(aItemIds[i]), emptytext);
				break;
		}
	}
}
// <!--- wlm | nbsp GmbH | 20.05.2010 14:23:34 PM | Sortierung --->
function sortList(type, form, text)
{
	form.o.value = type;
	submitSearchForm(form, text);
}
// <!--- wlm | nbsp GmbH | 21.05.2010 8:05:36 AM | Kopie der Funktion aus
// downloads.js von AF --->
function changeFilterBoxForSearch(id, obj)
{
	var el = Ext.get(id);
	el.setVisibilityMode(Ext.Element.DISPLAY);
	if(el.isVisible())
	{
		Ext.get(obj.id).addClass('off');
		Ext.get(obj.id).removeClass('on');
		// Ext.get(pLink.id).addClass('up');
		el.hide();
	} else
	{
		Ext.get(obj.id).addClass('on');
		Ext.get(obj.id).removeClass('off');
		// Ext.get(pLink.id).addClass('down');
		el.show();
	}
}
// <!--- wlm | nbsp GmbH | 21.05.2010 8:05:36 AM | Kopie der Funktion aus
// downloads.js von AF --->
function checkTypesForSearch(t, e, ty)
{
	if(t == 1 && e.checked)
	{
		var keys = e.parentNode.parentNode;
		if(keys)
		{
			var inputs = keys.getElementsByTagName('input');
			if(inputs)
			{
				for( var i = 0; i < inputs.length; ++i)
				{
					if(inputs[i].type == "checkbox" && inputs[i].value != 0 && inputs[i].name != ty + '_all')
					{
						inputs[i].checked = "";
					}
				}
			}
		}
	} else
	{
		document.getElementById(ty + '_all').checked = "";
	}
}

function addDateListener () {
	
	cal1 = Ext.get('begin_cf_calendar0');
	if(cal1) {
		arr = cal1.select('a', true);
		for(i=0; i<arr.elements.length; i++) {
			arr.elements[i].addListener('click', function() { toggleDateFilter('year'); })
		}
	}
	
	cal1 = Ext.get('end_cf_calendar1');
	if(cal1) {
		arr = cal1.select('a', true);
		for(i=0; i<arr.elements.length; i++) {
			arr.elements[i].addListener('click', function() { toggleDateFilter('year'); })
		}
	}
	
}



//SUCH FELD AUTOVERVOLLSTÄNDIGUNG
Ext.namespace('Ext.gs');

Ext.gs.SimpleSearchfield = function(config){
	if(typeof config == 'undefined'){
		config = {};
	}

	/* call parent constructor */
	Ext.gs.SimpleSearchfield.superclass.constructor.call(this, config);

	this.node 				= config.node				|| '';
	this.valueField 		= config.valueField 		|| 'value';
	this.displayField 		= config.displayField 		|| 'value';
	this.totalproperty 		= config.totalproperty 		|| 'totalcount';
	this.root 				= config.root 				|| 'items';
	this.fields 			= config.fields 			|| ['key', 'value'];
	this.url 				= config.url 				|| "/index.cfm" + '?event=cmp.cst.search.suggestions';
	this.events				= ['searchtermchanged'];
	this.searchterm			= '';
	this.hideTrigger 		= config.hideTrigger 		|| false;
	this.queryDelay 		= config.queryDelay 		|| 150;
	this.minChars			= config.minChars 			|| 3;
	this.width 				= config.width 				|| 220;
    this.typeAhead			= false;
//    this.loadingText		= getLanguageText('Suggestions.Message.Wait');
    this.loadingText		= 'Generierung der Vorschl&auml;ge...';

	this.enableKeyEvents 	= config.enableKeyEvents 	|| true;
	this.editable 			= true;
	this.rawValue			= config.rawValue || '';

	/* Store der ComboBox */
	this.store = new Ext.data.JsonStore({
		url: this.url,
		root: this.root,
		remoteSort: true,
		totalProperty: this.totalproperty,
		autoLoad: false,
		fields: this.fields
	})

	this.handleTriggerClass = function(){
		if(this.searchterm == ''){
			this.trigger.removeClass('x-form-search-trigger');
			this.trigger.removeClass('x-form-clear-trigger');

			this.trigger.addClass('x-form-search-trigger');

		}else{
			this.trigger.removeClass('x-form-search-trigger');
			this.trigger.removeClass('x-form-clear-trigger');

			this.trigger.addClass('x-form-clear-trigger');
		}
	}

	this.setAndFire = function(){
			
		this.rawValue = this.el.dom.value;
		this.searchterm = this.rawValue;
		this.value = this.searchterm;

		this.handleTriggerClass();
		this.fireEvent('searchtermchanged');
		this.store.removeAll();
		this.collapse();
	}

	/* Rueckgabe des Suchbegriffs */
	this.getSearchTerm = function(){
		return(this.searchterm);
	}

	/* Key-Handler */
	this.on('keydown', function(cb, e){
		if (e.keyCode == 13) {
			this.setAndFire();
			this.store.removeAll();
			this.collapse();
		}
		
		if (e.keyCode == 40){
			var idx = parseInt(cb.selectedIndex+1)
			if(typeof cb.store.data.items[idx] != 'undefined'){
				cb.store.data.items[idx].data.value = cb.store.data.items[idx].data.key;
				var val = cb.store.data.items[idx].data.value;
				cb.setRawValue(val);
				cb.setValue(val);
			}
		}
		
		if (e.keyCode == 38){
			var idx = parseInt(cb.selectedIndex-1)
			if(typeof cb.store.data.items[idx] != 'undefined'){
				cb.store.data.items[idx].data.value = cb.store.data.items[idx].data.key;
				var val = cb.store.data.items[idx].data.value;
				cb.setRawValue(val);
				cb.setValue(val);
			}
		}
		
	}, this);

	this.on('render', function(cb, e){
		this.trigger.addClass('x-form-search-trigger');
	}, this)
	this.on('beforeselect', function(cb, record, index){
		var idx = parseInt(index);
		cb.store.data.items[idx].data.value = cb.store.data.items[idx].data.key;
		var val = cb.store.data.items[idx].data.value;
		cb.setValue(val);		
	}, this);
};

/* Extend */
Ext.extend(Ext.gs.SimpleSearchfield, Ext.form.ComboBox, {
    onLoad: function(){
		if (!this.hasFocus) {
			return;
		}
		if (this.store.getCount() > 0) {
			this.expand();
			this.restrictHeight();
		}
		this.rawValue = this.el.dom.value;
	},
	onTriggerClick: function(){
		if(this.searchterm != ''){
			this.el.dom.value = '';
			this.searchterm = '';
		}
		this.handleTriggerClass
		this.setAndFire();	
	}
});


function doFormPanel(i) {

	if (document.getElementById("search_input" + i)) {		
		searchForm = new Ext.FormPanel({
			renderTo: 'search_input' + i,			
			items: [			
			new Ext.gs.SimpleSearchfield(
			{
				id: 'searchterm' + i,
				hideLabel: true,
				value: '',
				name: 'searchterm',
				ctCls: 'text'
			})
			],
			keys: {
				key: [10, 13],
				fn: function() {
					doSearch(i);
				}
				
			}
		});
	
	};
};

function doSearch(i) {
	if(document.getElementById('searchterm' + i))
		location.href='/?cid='+cid_sitesearch+'&q='+document.getElementById('searchterm' + i).value;
}

/*
 * Ausgelagertes Script fuer sie Suche
 * ################################################
*/	
	function removeEmptyText(field, text) {
		if (field && field.value == text) {
			field.style.color = 'black';
			field.style.fontStyle = 'normal';
			field.value = '';
		}
	}

	function addEmptyText(field, text) {
		if (field && field.value == '' || field.value == text) {
			field.style.color = 'gray';
			field.style.fontStyle = 'italic';
			field.value = text;
		}
	}

	function submitSearchForm(form, text) {
		if (form && form.q && form.q.value != text) {
			form.submit();
		}
	}
