/**
 * Fügt einem Eingabefeld Focus und Blur Eventshandler hinzu.
 * Der Defaultwert des Feldes wird onFocus gemerkt und onBlur wiederhergstellt.
 *
 * @authors		Martin Widemann
 * @copyright	Copyright 2008 - Netigo GmbH
 * @version		1.2
 * @modified	2008-11-06
 */

/**
 * Konstruktor
 *
 * @param String elem Eingebeelement
 * @param String text Standardwert setzen
 * @param Boolean empty_on_submit Standardwert vor Versundung aus Feld löschen
 * @param Boolean empty_value Abweichender Standardwert zum aktuellen Feldinhalt
 *
 * @return Object
 */
function Searchbox(elem, text, empty_on_submit, empty_value){
	if(elem == void(0)){ throw 'Missing Parameter \'elem\' in Constructor \'Searchbox\'.'; } // if

	var searchbox = document.getElementById(elem);
	searchbox.first = true;
	searchbox.previous = null;
	searchbox.empty_value = empty_value;

	if(text){
		searchbox.value = text;
	} // if

	if(!searchbox.form.searchboxes){
		searchbox.form.searchboxes = new Array();
		searchbox.form.searchbox_status = false;
	} // if

	searchbox.form.searchboxes.push(searchbox);

	if(empty_on_submit){
		searchbox.form.onsubmit = function(){
			if(searchbox.form.searchbox_status == true){
				return null;
			} // if
			
			for(var sb in searchbox.form.searchboxes){
				try{
					var elem = searchbox.form.searchboxes[sb];
					// alert('sb: ' + typeof(elem));

					if(elem.empty_value && (elem.value == elem.empty_value)){
						elem.value = '';
					}else if(elem.first && !elem.empty_value && !elem.previous){
						elem.value = '';
					} // if

				}catch(e){}
			} // for

			/*
			if(searchbox.empty_value && (searchbox.value == searchbox.empty_value)){
				searchbox.value = '';
			}else if(searchbox.first && !searchbox.empty_value && !searchbox.previous){
				searchbox.value = '';
			} // if
			*/
			
			searchbox.form.searchbox_status = true;
		} // function
	} // if

	searchbox.onfocus = function(){
		if(this.first && !this.previous){
			this.previous = this.value;
		}

		if(this.value == this.previous){
			this.value = '';
		}

		this.first = false;
	}
	
	searchbox.onblur = function(){
		if(this.value == ''){
			this.value = this.previous;
		}
	}

	return searchbox;
} // class

