
/**
 * 
 *	JavaScript for indexed_searchbar plugin
 *
 *	@author		Alex Buchgeher
 *	@package 	tx_indexedsearchbar_pi1
 *	@version	0.0.1		2006-05-12 - initial version 
 *	@version	0.1.0		2006-05-14 - added clearVal functionality
 *
 */


/* caching flag (dooh!) */
var cached = false;

/**
 *	Initialises the attachment of
 *	an Eventhandler to the indexedsearchbar-extension
 *	searchfield.
 *
 *	@return void
 */
function init_tx_indexedsearchbar() {
	var p = document.getElementById("tx-indexedsearchbar-pi1-searchfield");
	var p2 = document.getElementById("tx-indexedsearchbar-pi1-searchform");
	
	addEvent(p, "focus", function(e) { clearVal(p, null); }, false);
	addEvent(p2, "submit", function(e) { clearVal(p, p2); }, false);
	return;
}	

/**
 *	Clears the value of the given
 *	DOM Node object (input element)
 *
 *	@param obj
 *	@return void
 */
function clearVal(obj, obj2) {
	if(!cached && obj.nodeName != "form") {
		cached = true;
		obj.value = "";
	} else if(!cached && obj.nodeName == "form") {
		obj2.value = "";
	}
}
	
/** 
 *	Adds an eventListener to a DOM-node Object.
 *
 *	@param node-Obj.	The node-object.
 *	@param string			The event-type.
 *	@param string			The function to call.
 *	@param string
 */
function addEvent(obj, evType, fn, useCapture){
	if (obj.addEventListener){	// Mozilla
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){	// IE
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		// alert("Handler could not be attached");
		window.status = "Handler couldn\'t be attached";
	}
}
