/* *********************************************************************************************
 * ********************************** Javascript utils for fabory. ***************************** */  

function setHint(search_hint) {
	if (document.getElementById('searchForm:siteSearch_searchTerm').value=='') {
		document.getElementById('searchForm:siteSearch_searchTerm').value=search_hint;
	}
}


/*================================= GLOBAL VARIABLES ================================== */
var tmpStore = null; 
var stored = new Array();


/*===================================== FUNCTIONS ===================================== */

/*======================================================================================
Description: validates that a field contains data

Parameters: 
	- The field
Return 
	- true: if the field contains data
	- false: otherwise 
========================================================================================*/
function isFieldEmpty(field){
	return field.value.length > 0;
}
/*======================================================================================
Description: validates that a text field contains  a value greater 0.
The field is checked to be an integer.

Parameters: 
	- The field
Return 
	- true: if the field contains only numbers and the value is greater than zero
	- false: if the field is empy, the field contains alphanumeric chars or the number is less than zero. 
========================================================================================*/
function isFieldGtZero(field){
	var re = new RegExp(/^\d+$/);
	if(field.value.match(re)){
		if(parseInt(field.value) > 0){
			return true;
		}
	}
	return false;
}
/*======================================================================================
Description: determines if the field is a type field

Parameters: 
	- The field
Return 
	- true: if the field is the text type
	- false: otherwise.
========================================================================================*/
function isText(field){
	return field.type == 'text';
}
/*======================================================================================
Description: validates that all fields are positive integers

Parameters: 
	- The fields
Return 
	- true: if all fields are positive integers.
	- false: otherwise.
========================================================================================*/
function validateAllTextFieldsPositive(form, msg){
	var index = 0;
	for(element in form.elements){
		if( isText(form.elements[element]) && !isFieldGtZero(form.elements[element]) ){
			alert(msg);
			form.elements[element].focus();
			return false;
		}		

	}
	
	return true;
	
}
/*======================================================================================
Description: Stores a value in the storage map array. IF the key is null, it's stored 
 			 in the temporary variable which gets overwritten by a successive call.

Parameters: 
	- The key
	- The value 
========================================================================================*/
function storeValue(key, value){
	if(key != null)
		stored[key] = value;
	else
		tmpStore = value;
}
/*======================================================================================
Description: gets the value stored. If the key is null, the tmp storage is returned. 

Parameters: 
	- The key 
========================================================================================*/
function getValue(key){
	if(key != null)
		return stored[key];
	else
		return tmpStore;
}
/*======================================================================================
Description: Checks the value of the field to be greater than zero. Otherwise, the
             fallback value is used.

Parameters: 
	- The field to validate
	- The fallback value
========================================================================================*/
function ensurePositive(field, fallback){
	if( !isFieldGtZero(field) ){
		field.value = fallback;
	}
}

function ensureMultipleValue(field, base, msg, fallback) {
	if( field.value%base != 0 ) {
		alert(msg);
		field.value = fallback;
	}
}

/*======================================================================================
Description: Allows only numbers and a backspace to be entered in the field

Parameters: 
	- The field
	- The event (use "event" without the ")
========================================================================================*/
function onlyPositive(field, e){
	
	var keynum;
	var keychar;
	var numcheck;

	keynum = window.event ? e.keyCode : e.which;
	keychar = String.fromCharCode(keynum);
	re = /[\d\b]/;

	if( re.test(keychar) ){
		if(field.value.length == 0 && keychar == '0')
			return false;
		return true;
	}
	return false;
}