/*
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);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	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;
		}
	}	
}



function codeTouche(evenement)
{
		for (prop in evenement)
		{
				if(prop == 'which') return(evenement.which);
		}
		return(evenement.keyCode);
}


function scanTouche(evenement)
{
		var reCarSpeciaux = /[\x00\x08\x0D\x03\x16\x18\x1A]/;
		var reCarValides = /\d/;

		var codeDecimal  = codeTouche(evenement);
		var car = String.fromCharCode(codeDecimal);
		var autorisation = reCarValides.test(car) || reCarSpeciaux.test(car);

		return autorisation;
}


/**********************************************/
/*                CLIENT 
/**********************************************/
function regClient(enregClient)
{
	if (enregClient.email.value == "")
	{
		alert("Veuillez renseigner le champs Email");
		enregClient.email.focus();
		return false;
	}
	
	if (enregClient.mdp.value == "")
	{
		alert("Veuillez renseigner le champs Mot de passe");
		enregClient.mdp.focus();
		return false;
	}
	if (enregClient.nom.value == "")
	{
		alert("Veuillez renseigner le champs Nom");
		enregClient.nom.focus();
		return false;
	}
	if (enregClient.prenom.value == "")
	{
		alert("Veuillez renseigner le champs Prénom");
		enregClient.prenom.focus();
		return false;
	}
	if (enregClient.adresse.value == "")
	{
		alert("Veuillez renseigner le champs Adresse");
		enregClient.adresse.focus();
		return false;
	}
	if (enregClient.cpostal.value == "")
	{
		alert("Veuillez renseigner le champs Code postal");
		enregClient.cpostal.focus();
		return false;
	}
	if (enregClient.ville.value == "")
	{
		alert("Veuillez renseigner le champs Ville");
		enregClient.ville.focus();
		return false;
	}
}

function changepw(formchangepw)
{
	if (formchangepw.newmdp.value != formchangepw.newmdp2.value)
	{
		alert("Les deux mots de passe ne correspondent pas !");
		formchangepw.newmdp.focus();
		return false;
	}
}