//--------------------------------------------------------//
//  Validation de formulaire
//--------------------------------------------------------//
function isEmail(email){
	var verif     = /^[\.a-zA-Z0-9_\-]+@+[\.a-zA-Z0-9\-_]{2,}[.][a-zA-Z]{2,3}[\.a-zA-Z]{0,3}$/
	if (verif.exec(email) == null){
		return false;
	}else{
		return true;
	}
}
function isPostalCode( countryName, postalCode ){
	postalCode = nospace( postalCode ).toUpperCase();
	switch( countryName.toUpperCase() ){
		case "CANADA" :
			ok = postalCode.match( /^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/ ); 
			break;
		case "UNITED STATES" :
			ok = postalCode.match( /^[0-9]{5}$/ ); 
			break;
		default : // always true
			ok = true ;
			break ;
	}
	return ok ;
}
function nospace( str ){
	str += "" ;
	str = str.replace( /^ +/gi, "" );
	str = str.replace( / +$/gi, "" );
	str = str.replace( / +/gi, "" );
	str = str.replace( "-", "" );
	return str;
}
function FormValidateAlert(idForm){
	// One Roar instance for our notofications, positioned in the top-right corner of our demo.
	var error = new Roar({
		container: $(idForm),
		position: 'bottomLeft',
		duration: 3000
	});	
	var send = true;
	var msg = '';
	var listForm = $$('#'+idForm+' input');
	for(var x=0;x<listForm.length;x++){
		if(listForm[x].get('req') == 'true'){
			if(listForm[x].value == ''){
				send = false;
				msg += 'Le champ <strong>"'+listForm[x].get('desc')+'"</strong> est vide'+"<br />";
			}
		}
		if(listForm[x].get('req') == 'true' && listForm[x].get('validation') == 'same'){
			if(listForm[x].value != listForm[x-1].value){
				send = false;
				msg += "Confirmer : "+listForm[x-1].value+"<br />";
			}
		}
		if(listForm[x].get('req') == 'true' && listForm[x].get('validation') == 'email'){
			if(isEmail(listForm[x].value) == false){
				send = false;
				msg += 'Le courriel n\'est pas valide ( Ex: exemple@gmail.com )'+"<br />"
			}
		}
		if(listForm[x].get('req') == 'true' && listForm[x].get('validation') == 'checked'){
			if(listForm[x].checked == false){
				send = false;
				msg += 'Cocher la case "<strong>'+listForm[x].get('desc')+'</strong>"<br />';
			}
		}
		if(listForm[x].get('req') == 'true' && listForm[x].get('validation') == 'postal'){
			if(!isPostalCode('CANADA',listForm[x].value)){
				send = false;
				msg += 'Le code postal n\'est pas valide ( Ex: A1B2C3 )'+"<br />"
			}else{
				listForm[x].value = nospace(listForm[x].value).toUpperCase();
			}
		}
		if(listForm[x].get('req') == 'true' && listForm[x].get('validation') == 'zip'){
			if(!isPostalCode('UNITED STATES',listForm[x].value)){
				send = false;
				msg += 'Le code postal n\'est pas valide ( Ex: 12345 )'+"<br />"
			}
		}
	}
	var listForm = $$('#'+idForm+' select');
	for(var x=0;x<listForm.length;x++){
		if(listForm[x].get('req') == 'true'){
			if(listForm[x].value == ''){
				send = false;
				msg += 'Le champ <strong>"'+listForm[x].get('desc')+'"</strong> est vide'+"<br />";
			}
		}
	}
	var listForm = $$('#'+idForm+' textarea');
	for(var x=0;x<listForm.length;x++){
		if(listForm[x].get('req') == 'true'){
			if(listForm[x].value == ''){
				send = false;
				msg += 'Le champ <strong>"'+listForm[x].get('desc')+'"</strong> est vide'+"<br />";
			}
		}
	}
	if(send == true){
		return true;
	}else{
		error.alert('Erreur',msg);
		return false;
	}
}

