function tabForm(id, activeTab, options)
{
	this.tabForm = document.getElementById(id);
	this.activeTab = activeTab;
	this.tabs = {};
	this.tabLegends = {};
	this.checkImages = {};
	this.tabEvents = (options && options['events']) || {};
	this.handlers = (options && options['handlers']) || {};
	this.init();
}

Object.extend(tabForm.prototype,
{
	init: function()
	{
		if (this.tabForm)
		{
			//-- preloads
			preload('tabform_checkbox_disabled', 'images/checkbox_disabled.gif');
			preload('tabform_checkbox_checked', 'images/checkbox_checked.gif');
			preload('tabform_button_go', 'images/button_go.gif');
			preload('tabform_button_go_mouseover', 'images/button_go_mouseover.gif');

			var tabLegend = this.tabForm.getElementsByTagName('ul')[0];
			if (tabLegend)
			{
				var tabLegendItems = tabLegend.getElementsByTagName('li'), tabLegendItem, i = 0;
				var tabLink, href, l, tabId, tab, img;
				while ((tabLegendItem = tabLegendItems[i++]))
				{
					tabLink = tabLegendItem.firstChild;
					if (tabLink && tabLink.nodeName.toLowerCase() == 'a')
					{
						href = tabLink.getAttribute('href'), l = href.lastIndexOf('#') + 1;
						if (l && (tabId = href.substr(l)))
						{
							tab = document.getElementById(tabId);
							if (tab)
							{
								this.tabs[tabId] = tab;
								this.tabLegends[tabId] = tabLegendItem;

								if (!hasClass(tabLegendItem, 'nostate'))
								{
									this.checkImages[tabId] = document.createElement('img');
									this.checkInputs(tabId, true);
									tabLink.insertBefore(this.checkImages[tabId], tabLink.firstChild);
								}

								if (this.activeTab === null || this.activeTab == tabId)
								{
									addClass(tabLegendItem, 'active');
									this.activeTab = tabId;
								}
								else
								{
									tab.style.display = 'none';
								}

								tabLink.onclick = this.switchTab.bind(this, tabId);
							}
						}
					}
				}

				addClass(this.tabForm, 'tabformjs');
			}

			//-- mouseover submitbutton
			var submit = getElementsByClassName('submitImage', 'input', this.tabForm);
			if (submit.length)
				imageHoverSwap(submit[0], 'tabform_button_go', 'tabform_button_go_mouseover');

			addEvent(window, 'unload', this.cleanUp.bind(this));
		}
	},
	cleanUp: function()
	{
		this.tabForm = null;
		this.tabs = null;
		this.tabLegends = null;
		this.checkImages = null;
	},
	checkInputs: function(index, init)
	{
		var elements = this.tabs[index].getElementsByTagName('*');
		var el, i = 0, o, j;
		var hasInput = false;
		var handlers, handlerType = false;
		var handler, params;

		while ((el = elements[i++]))
		{
			if (el.name && !el.disabled)
			{
				switch (el.nodeName.toLowerCase())
				{
					case 'input':
						switch (el.type)
						{
							case 'radio':
							case 'checkbox':
								handlerType = 'click';
								if (el.checked)
									hasInput = true;
								break;
							case 'text':
								handlerType = 'change';
								if (el.value != '')
									hasInput = true;

								//-- hook up suggest functionality
								if (init && hasClass(el, 'suggest') && window.Suggest)
								{
									new Suggest(el);
								}

								break;
						}
						break;
					case 'textarea':
						handlerType = 'change';
						if (el.value != '')
							hasInput = true;
						break;
					case 'select':
						if (init)
						{
							handlerType = 'change';
							o = el.options, j = o.length;
							while (j--)
							{
								if (o[j].defaultSelected)
								{
									hasInput = true;
									break;
								}
							}
						}
						else
						{
							if (el.multiple)
							{
								o = el.options, j = o.length;
								while (j--)
								{
									if (o[j].selected)
									{
										hasInput = true;
										break;
									}
								}
							}
							else
							{
								o = el.options[el.selectedIndex];
								if (o.value != '' || el.selectedIndex)
								{
									hasInput = true;
								}
							}
						}
						break;
				}

				if (init)
				{
					if (handlerType)
					{
						addEvent(el, handlerType, this.checkInputs.bind(this, index, false));
					}

					if ((handlers = this.handlers[el.name]))
					{
						for (handlerType in handlers)
						{
							if (handlers.hasOwnProperty(handlerType))
							{
								handler = handlers[handlerType];
								if (handler instanceof Array)
								{
									params = handler[1];
									handler = handler[0];
								}
								else
								{
									params = this;
								}

								if (handler instanceof Array && handler[0] == 'this')
									addEvent(el, handlerType, this.executeEvent.bind(this, handler, params));
								else
									addEvent(el, handlerType, this.executeEvent.bind(el, handler, params));
							}
						}
					}

					handlerType = false;
				}
				else if (hasInput)
				{
					break;
				}
			}
		}

		if (hasInput)
			this.checkImages[index].src = getPreloadImage('tabform_checkbox_checked');
		else
			this.checkImages[index].src = getPreloadImage('tabform_checkbox_disabled');
	},
	switchTab: function(which)
	{
		if (which != this.activeTab)
		{
			this.tabs[this.activeTab].style.display = 'none';
			removeClass(this.tabLegends[this.activeTab], 'active');

			this.activeTab = which;

			this.onSelectEvent(this.activeTab);

			this.tabs[this.activeTab].style.display = '';
			addClass(this.tabLegends[this.activeTab], 'active');
		}

		return false;
	},
	onSelectEvent: function(tabId)
	{
		if (this.tabEvents[tabId])
		{
			var newContent = this.executeEvent(this.tabEvents[tabId], this.tabForm);
			if (newContent)
			{
				this.tabs[tabId].innerHTML = newContent;
				if (this.checkImages[tabId])
					this.checkInputs(tabId, true);
			}
		}
	},
	executeEvent: function(func, params)
	{
		if (params === undefined) params = [];
		else if (!(params instanceof Array))
			params = [params];

		var object;
		if (func instanceof Array)
		{
			if (func[0] == 'this')
				object = this;
			else
				object = window[object];

			func = func[1];
		}
		else
		{
			object = window;
		}

		if (object && object[func])
			return object[func].apply(this, params);

		return null;
	}
});

function Suggest(input, options)
{
	this.input = input;
	this.options = options ? options : {};

	this.suggestions = {};
	this.altsuggestions = {};
	this.suggestActive = false;
	this.previousValue = '';

	this.suggestContainer = null;
	this.suggestDiv = null;
	this.suggestIndex = null;

	this.ajax = null;
	this.suggestXmlHttpBusy = false;
	this.suggestXmlHttpUrl = null;
	this.suggestXmlHttpAltUrl = null;

	if(this.options['dependsOn'])
		this.dependingInput = this.options['dependsOn'];
	else
		this.dependingInput = null;

	this.dependingValue = null;

	this.hasAltSuggestions = Set('location');

	if (this.options['onselect'])
		this.onselect = this.options['onselect'];
	else
		this.onselect = null;

	this.init();
}
Object.extend(Suggest.prototype,
{
	init: function()
	{
		if (this.input && this.input.name)
		{
			// ajax enabled?
			this.ajax = new Ajax();
			if (this.ajax.getRequestObject(true))
			{
				// set XmlHttp URL's
				this.suggestXmlHttpUrl = getXmlHttpUrl('frontpage', 'suggest', this.input.name, 'char=');
				if (this.input.name in this.hasAltSuggestions)
					this.suggestXmlHttpAltUrl = getXmlHttpUrl('frontpage', 'suggest', 'alt' + this.input.name, 'start=');

				// create elements and set handlers
				this.suggestContainer = document.createElement('div');
				this.suggestContainer.className = 'suggestContainer';

				this.suggestDiv = document.createElement('div');
				this.suggestDiv.className = 'suggestDiv';
				this.suggestDiv.style.width = (this.input.offsetWidth - 2) + 'px';
				addEvent(this.suggestDiv, 'click', function(e) { e.preventDefault(); });

				this.suggestContainer.appendChild(this.suggestDiv);
				this.input.parentNode.appendChild(this.suggestContainer);

				addEvent(document, 'click', this.hideSuggest.bind(this));
				addEvent(this.input, 'focus', this.hideSuggest.bind(this));
				addEvent(this.input, 'keyup', this.suggest.bind(this));
				addEvent(this.input, 'keydown', this.handlekeydown.bind(this));

				this.input.setAttribute('autocomplete', 'off');
			}

			addEvent(window, 'unload', this.cleanUp.bind(this));
		}
	},
	cleanUp: function()
	{
		this.input = null;
		this.suggestContainer = null;
		this.suggestDiv = null;
		this.ajax = null;
	},
	suggest: function(e)
	{
		if (!e.keyCode || e.keyCode > 47 || e.keyCode in Set(8,32,46))
		{
			var value = this.input.value.toLowerCase();
			if (value.length)
			{
				if (e === true || value !== this.previousValue)
				{
					var suggestions = this.getSuggestions(value);
					if (suggestions.length)
					{
						var suggestion, i = 0;

						// clear suggestDiv
						while (this.suggestDiv.hasChildNodes())
							this.suggestDiv.removeChild(this.suggestDiv.lastChild);

						// re-fill with new suggestions
						while ((suggestion = suggestions[i++]))
							this.suggestDiv.appendChild(suggestion);

						this.suggestIndex = null;
						this.suggestContainer.style.visibility = 'hidden';
						this.suggestContainer.style.display = 'block';
						this.suggestDiv.scrollTop = 0;
						this.suggestActive = true;

						setRelativePosition(this.suggestContainer, this.input, this.input.offsetHeight, 0);
						this.suggestContainer.style.visibility = '';
					}
					else
					{
						this.hideSuggest();
					}

					this.previousValue = value;
				}
			}
			else
			{
				this.previousValue = '';
				this.hideSuggest();
			}
		}
	},
	getSuggestions: function(value)
	{
		// Check to see if the depending input's value has changed
		if(this.dependingInput)
		{
			var currentDependingValue = this.dependingInput.value;
			if(currentDependingValue == '')
			{
				// Skip going on, it won't do much good with an empty depending value
				return [];
			}
			else if(currentDependingValue != this.dependingValue)
			{
				this.dependingValue = currentDependingValue;
				this.suggestions = {};
			}
		}

		var c = value.charAt(0), search = this.suggestions[c];

		if (search === undefined)
		{
			this.loadSuggestions(c);
			return [];
		}
		else if (search)
		{
			var len = value.length, suggestions = [], i = 0, check, suggestion, suggestionText;
			while ((suggestionText = search[i++]))
			{
				check = suggestionText.substr(0, len).toLowerCase();
				if (check === value)
				{
					suggestion = document.createElement('div');
					addEvent(suggestion, 'mouseover', this.suggestionHover.bind(this, suggestion));
					addEvent(suggestion, 'mousedown', this.suggestionSelect.bind(this, suggestion));

					suggestion.appendChild(document.createTextNode(suggestionText));
					suggestions.push(suggestion);
				}
				else if (check > value)
					break;
			}
		}

		// add altsuggestions
		if (this.suggestXmlHttpAltUrl && suggestions.length < 10)
		{
			search = this.altsuggestions[value];
			if (search === undefined)
			{
				this.loadAltSuggestions(value);
				return [];
			}
			else if (search)
			{
				i = 0;
				while ((suggestionText = search[i++]))
				{
					suggestion = document.createElement('div');
					suggestion.style.fontStyle = 'italic';
					addEvent(suggestion, 'mouseover', this.suggestionHover.bind(this, suggestion));
					addEvent(suggestion, 'mousedown', this.suggestionSelect.bind(this, suggestion));

					suggestion.appendChild(document.createTextNode(suggestionText[0]));
					suggestion.appendChild(document.createTextNode(' (' + suggestionText[1] + ')'));
					suggestions.push(suggestion);
				}
			}
		}

		return suggestions;
	},
	loadSuggestions: function(c)
	{
		if (this.suggestions[c] === undefined)
		{
			if (!this.suggestXmlHttpBusy)
			{
				var urlPostfix = (this.dependingInput ?
										'&' + encodeURIComponent(this.dependingInput.name) + '=' + encodeURIComponent(this.dependingInput.value)
										:
										'');

				this.suggestXmlHttpBusy = true;
				this.ajax.sendRequest(
					this.suggestXmlHttpUrl + encodeURIComponent(c) + urlPostfix,
					{
						type:		'json',
						async:		true,
						handler:	this.addSuggestions.bind(this, c)
					}
				);
			}
			else
			{
				window.setTimeout(this.loadSuggestions.bind(this, c), 100);
			}
		}
	},
	addSuggestions: function(c, suggestions)
	{
		if (suggestions && suggestions instanceof Array)
		{
			this.suggestions[c] = suggestions.length ? suggestions : false;
			this.suggest(true);
		}

		this.suggestXmlHttpBusy = false;
	},
	loadAltSuggestions: function(value)
	{
		//-- scope: 'this' refers to the suggest instance
		if (this.altsuggestions[value] === undefined)
		{
			if (!this.suggestXmlHttpBusy)
			{
				this.suggestXmlHttpBusy = true;
				this.ajax.sendRequest(
					this.suggestXmlHttpAltUrl + encodeURIComponent(value),
					{
						type:		'json',
						async:		true,
						handler:	this.addAltSuggestions.bind(this, value)
					}
				);
			}
			else
			{
				window.setTimeout(this.loadAltSuggestions.bind(this, value), 100);
			}
		}
	},
	addAltSuggestions: function(value, suggestions)
	{
		//-- scope: 'this' refers to the suggest instance
		if (suggestions && suggestions instanceof Array)
		{
			this.altsuggestions[value] = suggestions.length ? suggestions : false;
			this.suggest(true);
		}

		this.suggestXmlHttpBusy = false;
	},
	hideSuggest: function()
	{
		this.suggestContainer.style.display = 'none';
		this.suggestIndex = null;
		this.suggestActive = false;
	},
	suggestionHover: function(suggestion)
	{
		if (this.suggestIndex)
			removeClass(this.suggestIndex, 'selected');

		this.suggestIndex = suggestion;
		addClass(this.suggestIndex, 'selected');
	},
	suggestionSelect: function(suggestion, dontHideSuggest)
	{
		if (suggestion)
		{
			var suggestionText = suggestion.firstChild.nodeValue;
			this.input.value = suggestionText;
			this.previousValue = suggestionText.toLowerCase();
			if (!dontHideSuggest) this.hideSuggest();

			if(this.onselect)
				this.onselect(this.input);
		}
	},
	handlekeydown: function(e)
	{
		if (this.suggestActive)
		{
			var keycode = e.keyCode;
			var nextIndex = null, cancelEvent = true, n;

			switch (keycode)
			{
				case 9:
					// tab
					cancelEvent = false;
					this.hideSuggest();
					break;
				case 13:
					// enter
					this.suggestionSelect(this.suggestIndex);
					break;
				case 33:
					// page up
					if (this.suggestIndex)
					{
						n = Math.floor(this.suggestDiv.clientHeight / this.suggestIndex.clientHeight) - 1;
						nextIndex = this.suggestIndex.previousSibling;
						while (nextIndex && --n)
							nextIndex = nextIndex.previousSibling;
					}
					if (!nextIndex) nextIndex = this.suggestDiv.firstChild;
					break;
				case 34:
					// page down
					if (!this.suggestIndex)  this.suggestIndex = this.suggestDiv.firstChild;
					if (this.suggestIndex)
					{
						n = Math.floor(this.suggestDiv.clientHeight / this.suggestIndex.clientHeight) - 1;
						nextIndex = this.suggestIndex.nextSibling;
						while (nextIndex && --n)
							nextIndex = nextIndex.nextSibling;
					}
					if (!nextIndex) nextIndex = this.suggestDiv.lastChild;
					break;
				case 35:
					//end
					nextIndex = this.suggestDiv.lastChild;
					break;
				case 36:
					//home
					nextIndex = this.suggestDiv.firstChild;
					break;
				case 38:
					// key up
					if (!this.suggestIndex) nextIndex = this.suggestDiv.lastChild;
					else nextIndex = this.suggestIndex.previousSibling;
					break;
				case 40:
					// key down
					if (!this.suggestIndex) nextIndex = this.suggestDiv.firstChild;
					else nextIndex = this.suggestIndex.nextSibling;
					break;
				default:
					cancelEvent = false;
			}

			if (nextIndex)
			{
				var top = nextIndex.offsetTop;
				if (top > this.suggestDiv.clientHeight + this.suggestDiv.scrollTop - nextIndex.clientHeight)
					this.suggestDiv.scrollTop = top - this.suggestDiv.clientHeight + nextIndex.clientHeight;
				else if (top < this.suggestDiv.scrollTop)
					this.suggestDiv.scrollTop = top;

				this.suggestionHover(nextIndex);
				this.suggestionSelect(nextIndex, true);
			}

			if (cancelEvent)
				e.preventDefault();
		}
	}
});

function generateArchiveSearchAction(form)
{
	var catId = parseInt(form.catId.value);
	if (isNaN(catId))
	{
		catId = '';
	}

	var itemType = form.itemType.value;

	if (!itemType)
	{
	    for (var i = 0; form.itemType[i]; i++ )
		{
        	if (form.itemType[i].checked == true)
			{
				var itemType = form.itemType[i].value;
			}
		}
	}

	if (itemType != 'nieuws' && itemType != 'redactieblogs' && itemType != 'reviews')
	{
		itemType = '';
	}

	var action = 'archieven/cat/' + catId + '/' + itemType;

	var keyword = form.keyword.value;
	var dateStart = form.dateStart.value;
	var dateEnd = form.dateEnd.value;

	if (keyword != '' || dateStart != '' || dateEnd != '')
	{
		action = action + '?';
		
		if (keyword != '')
			action = action + 'keyword=' + keyword;
			
		if (dateStart != '')
			action = action + '&dateStart=' + dateStart;
			
		if (dateEnd != '')
			action = action + '&dateEnd=' + dateEnd;
	}

	window.location.href = BaseURL + action;

	return false;
}
