/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

function checkImputLength(formElement, message) 
{
	_ImputLength = false;
	formElementVal = trim(formElement.value);	
	
	if (formElementVal.length <= 5) {
		 alert(message);
		_ImputLength=true;
		formElement.focus();		
	}
	
	return 	_ImputLength;
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmptyField(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

function isEmptyRadio(formElement, message) {

	_isEmptyRadio = true;

	for (i=0; i < formElement.length; i++) {
		if (formElement[i].checked){
			_isEmptyRadio = false;
		}
	}	
	if (_isEmptyRadio){
		alert(message);
	}
	return _isEmptyRadio;
}
function isEmptyCheckbox(formElement, message) {

	_isEmptyCheckbox = true;

	for (i=0; i < formElement.length; i++) {
		if (formElement[i].checked){
			_isEmptyCheckbox = false;
		}
	}	

	if (_isEmptyCheckbox){
		alert(message);
	}
	return _isEmptyCheckbox;
}


function isNotSiret(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isNotSiret = false;
	if (!EstSiretValide(formElement.value)) {
		_isNotSiret = true;
		alert(message);
		formElement.focus();
	}
	
	return _isNotSiret;
}

function isNotEmail(formElement, message){
	formElement.value = trim(formElement.value);

	_isNotEmail = false;
	if (!isValidEmail(formElement.value)) {
		_isNotEmail = true;
		alert(message);
		formElement.focus();
        }

	return _isNotEmail;
}

function checkemail2(object_value) {
	if (!(object_value.indexOf("@") > -1 && object_value.indexOf(".") > -1))return false;    
	return true;
}

function isValidEmail(str) {

   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 


}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}
