//----------------------------------------------------------------------------------------------------------------
// Fonction fait par Dominic Mercier - Megavolt - 13 juillet 2005
// Effectue la validation en javascript des formulaires
//----------------------------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------------------------
// Validation d'un champ de type texte
// Vérification si le champ est vide ou contient des espacements au début et la fin
function validText(src){
	src = trim(src);
	if(src == ""){
		return false;
	}
	return true;
}
// Validation du Code postal
function valideCodePostal(src) {
	// Validation Canadienne
	src = removeSpaces(src);
	var code = src;
	if(code.length == 6) {
		if(code.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1){
		} else {
			return false;
		}
	} else {
		return false;
	}
	return true;
}
// Validation Canadienne du numéro de téléphone (xxx) xxx-xxxx
function validPhone(src){
	var tel = new String(src);
	src = tel.replace(/[\(\)-. ]/g,"");
	if(src.length==10 || src.length==11){
	}else{
		return false;
	}
	return true;
}
// Remove all spaces from a string
function removeSpaces(string) {
	var newString = '';
	for (var i = 0; i < string.length; i++) {
		if (string.charAt(i) != ' ') newString += string.charAt(i);
	}
	return newString;
}
// Remove all spaces before and after a string
function trim(string){
	return ltrim(rtrim(string));
}
// Remove all spaces before a string
function ltrim(string){
	str = "";
	for(i=0; i<string.length; i++){
		if(string.charAt(i) == " "){
			str = string.slice(i+1,string.length);
		}else{
			str = string.slice(i,string.length);
			break;
		}
	}
	return str;
}
// Remove all spaces after a string
function rtrim(string){
	str = "";
	for(i=string.length; i>0; i--){
		if(string.charAt(i-1) == " "){
			str = string.slice(0,i-1);
		}else{
			str = string.slice(0,i);
			break;
		}
	}
	return str;
}
// Check that an email address has the form something@something.something
// This is a stricter standard than RFC 821 (?) which allows addresses like postmaster@localhost
function isValidEmailStrict(src) {
	src = removeSpaces(src);
	address = src;
	if (isValidEmail(address) == false){
		return false;
	}
	var domain = address.substring(address.indexOf('@') + 1);
	if (domain.indexOf('.') == -1){
		return false;
	}
	if (domain.indexOf('.') == 0 || domain.indexOf('.') == domain.length - 1){
		return false;
	}
	return true;
}
// Check that an email address is valid based on RFC 821 (?)
function isValidEmail(address) {
	if (address != '' && address.search) {
      if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
      else return false;
	}
   // allow empty strings to return true - screen these with either a 'required' test or a 'length' test
   else return true;
}
// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)){
			return false;
		}
	}
	return true;
}
// Validation des menu déroulants
// La valeur de la sélection vide doit égaler -1
function validDropMenu(src){
	if(src == -1){
		return false;
	}
	return true;
}
// Validation des menu déroulants
// La première option doit être vide
function newValidDropMenu(dropDown){
	
	if(dropDown.selectedIndex == 0){
		return false;
	}
	return true;
}
function validOption(optionsObj){
	
	for($i=0;$i<optionsObj.length;$i++){
			if(optionsObj[$i].checked)
				return true;
	}
	
	return false;
	
}