/* **************************************************
Copyright (c) 2008, B & H Foto & Electronics Corp. All rights reserved. http://wwww.bhphotovideo.com - Author: Andres Vidal 
************************************************** */

/**
*	Clear Fields
*	The 2 functions will check its parameters against the input field to (clear it and remove the class) or (restore and add the class)
*	USAGE: Usage: <input id="someID" class="default" type="text" value="email address" onclick="clickclear('someID', 'email address', 'default');" onblur="clickrecall('someID', 'email address', 'default');" />
*
*	@library none
*	@onload false
*/

// @variables: element, default text
// @optional: toggleclass = element class
function clickclear(el, defaulttext, class_name) {

	el = document.getElementById(el);
	
	if (el.value == defaulttext) {
		el.value = "";
	}
	
	if ( class_name && el.className.match(class_name) ){
		
		newClass = el.className.replace(class_name, '');
		el.className = newClass;
		//alert(newClass);
	}
}

// @variables: element, default text
// @optional: class name
function clickrecall(el, defaulttext, class_name) {
	
	el = document.getElementById(el);
	
	if (el.value == "") {
		el.value = defaulttext;
	
		if (class_name && !el.className.match(class_name) ){
	
			el.className = el.className + ' ' + class_name;
		}
	
	}
}

/**
*	Validate Search
*	The function will return true or false(with alert) when used with onSubmit. It checks for several allowed conditions. 
*	USAGE: 	Usage: <form action="" method="" onSubmit="return validSearch(this, default, errmsg);"><input type="text" class="input searchStr" /> ... </form>
*			Usage: if(event.which == 13 && validSearch(document.searchForm, '<%=defaultSearchText%>', '<fmt:message key='validSearch'/>', true))
*
*	@library jquery
*	@onload false
*	@return boolean
*	@variables: form element, param (default value), errmsg (error message string), noAlert (bool/string to suppress alert message)
*/

function validSearch ( el, param, errmsg, noAlert )
{	
	var searchString = $(el).find('.searchStr').val().replace(/^\s+|\s+$/g, ""); //find value and apply trim hack
	
	$(el).find('.searchStr').val( searchString ); // set trimmed value

	if( searchString == null || searchString.length < 2 || param == searchString || searchString.search(/(?:[\W_]|^).?\*.?(?:[\W_]|$)/) > -1 )
	{
		if ( noAlert == null )
		{
			alert(errmsg);
		}
		return false;
	}
	
	return true;		
};