function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
  
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function isEmail(email) {				
				var checkOK = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@_.'";
				var checkStr = email;
				var allValid = true;
				var i = 0;

				while ((i < checkStr.length) && (allValid)) {
					allValid = (checkOK.indexOf(checkStr.charAt(i)) == -1)?false:true ;
					i ++;
				}						
				
				if (!allValid) {return false};
											
				posiA = checkStr.indexOf('@');
				posiP = checkStr.lastIndexOf('.');
				if ((posiA < 1) || (posiP < posiA+3) || (checkStr.length < 5) || (posiP > checkStr.length - 3)) {
				    return (false);
				}
				return true;							
}
function isNumber(num)
{
	for (var i=0; i<num.length; i++)
		if ( num.charAt(i) < '0' || num.charAt(i) > '9' ) 
		return false;
	return true;
}
function spaceExist(str) {
	 var spaceTab = str.split(" ");
		if(spaceTab.length > 1) {
				return true;
		}
		return false;
}

function firstLetterUpperCase(obj,n) {
	var ch=obj.elements[n].value;
	
	if (ch!='') {	
		while ((ch.charAt(0)==' ')||(ch.length==1)) {
			ch=ch.substr(1,ch.length);
		}
		
		if (ch!='') {	
			//Suppression des espaces avant s'ils existent
			while(ch.charAt(0)==' ') {
				ch=ch.substr(1,ch.length);
			}

			//Suppression des espaces après
			while(ch.lastIndexOf(" ")==ch.length-1) {
				ch=ch.substr(0,ch.length-1);
			}
		}
		
		if (ch!='') {
			obj.elements[n].value=ch.charAt(0).toUpperCase() + ch.substr(1,ch.length).toLowerCase();
		}
	}
}

function verify(obj) {
	var msg  = "";
	var empty_fields = "";
	var errors = "";
	// Coordonnées perso
	if(obj.Titre) {
		if (!trim(obj.Titre.options[obj.Titre.selectedIndex].value))	{	
			 empty_fields = "* " + obj.Titre.name + " \r";
		}
	}
	if(obj.Nom) {	
		if (!trim(obj.Nom.value))	{	
			 empty_fields += "* " + obj.Nom.name + " \r";
		}
	}
	if(obj.NomExposant) {	
		if (!trim(obj.NomExposant.value))	{	
			 empty_fields += "* " + obj.NomExposant.name + " \r";
		}
	}
	if(obj.NomContactExposant) {	
		if (!trim(obj.NomContactExposant.value))	{	
			 empty_fields += "* Nom du contact \r";
		}
	}		
	if(obj.Prenom) {
		if (!trim(obj.Prenom.value))	{	
			 empty_fields += "* Prénom \r";
		}
	}
	if(obj.NumeroProfessionel) {
		if (!trim(obj.NumeroProfessionel.value))	{	
			 empty_fields += "* N° Adeli ou Conseil de l'ordre \r";
		}
	}
	if(obj.PrenomContactExposant) {
		if (!trim(obj.PrenomContactExposant.value))	{	
			 empty_fields += "* Prénom \r";
		}
	}	
	if(obj.Profession) {
		if (!trim(obj.Profession.options[obj.Profession.selectedIndex].value))	{	
			 empty_fields += "* " + obj.Profession.name + " \r";
		}
	}
	if(obj.LIProfession) {
		if (!trim(obj.LIProfession.options[obj.LIProfession.selectedIndex].value))	{	
			empty_fields += "* Profession \r";
		}
		if ((obj.LIProfession.options[obj.LIProfession.selectedIndex].value=="Autre")&&(!trim(obj.AutreProfession.value))) {
			empty_fields += "* Autre profession \r";
		}
	}	
	if(obj.Specialite) {	
		if (!trim(obj.Specialite.options[obj.Specialite.selectedIndex].value))	{	
			 empty_fields += "* " + obj.Specialite.name + " \r";
		} 
		else if (obj.Specialite.options[obj.Specialite.selectedIndex].value=='9999') {
			if(obj.AutreSpecialite) {
				if(!trim(obj.AutreSpecialite.value)) {
					empty_fields += "* Autre spécialité \r";
				}
			}
		}
	}
	//if(obj.Etablissement) {
	//	if (!trim(obj.Etablissement.value))	{	
	//		 empty_fields += "* " + obj.Etablissement.name + " \r";
	//	}
	//}
	//if(obj.Service) {
	//	if (!trim(obj.Service.value))	{	
	//		 empty_fields += "* " + obj.Service.name + " \r";
	//	}
	//}
	if(obj.LIEtablissement) {
		if (!trim(obj.LIEtablissement.value))	{	
			 empty_fields += "* Etablissement \r";
		}
	}	
	if(obj.Adresse1) {
		if (!trim(obj.Adresse1.value))	{	
			 empty_fields += "* Adresse \r";
		}
	}
	if(obj.Adresse1Exposant) {
		if (!trim(obj.Adresse1Exposant.value))	{	
			 empty_fields += "* Adresse1 \r";
		}
	}	
	if(obj.Code_Postal) {	
		if (!trim(obj.Code_Postal.value))	{	
			 empty_fields += "* Code postal \r";
		} else {
			if (obj.Code_Postal.value.indexOf("-")!= -1) {
				errors += "- Le code postal ne doit pas comporter de caractère '-' !.\n";
			}
		}
	}
	if(obj.CodePostalExposant) {	
		if (!trim(obj.CodePostalExposant.value))	{	
			 empty_fields += "* Code postal \r";
		}
	}	
	
	if(obj.Ville) {
		if (!trim(obj.Ville.value))	{	
			 empty_fields += "* " + obj.Ville.name + " \r";
		}
	}
	if(obj.VilleExposant) {
		if (!trim(obj.VilleExposant.value))	{	
			 empty_fields += "* Ville \r";
		}
	}
	
	if(obj.ztPays) {
		if (!trim(obj.ztPays.value))	{	
			 empty_fields += "* Pays \r";
		}
	}
		
	if(obj.Pays) {
		if (!trim(obj.Pays.options[obj.Pays.selectedIndex].value))	{	
			 empty_fields += "* " + obj.Pays.name + " \r";
		}
	}

	if(obj.Nationalite) {
		if (!trim(obj.Nationalite.value))	{	
			 empty_fields += "* Nationalité \r";
		}
	}

	if(obj.NumeroPasseport) {
		if (!trim(obj.NumeroPasseport.value))	{	
			 empty_fields += "* N° de passeport \r";
		}
	}	
	
	if(obj.PaysExposant) {
		if (!trim(obj.PaysExposant.options[obj.PaysExposant.selectedIndex].value))	{	
			 empty_fields += "* Pays \r";
		}
	}	
	if(obj.Telephone) {
		if (!trim(obj.Telephone.value))	{	
			 empty_fields += "* " + obj.Telephone.name + " \r";
		}
	}
	// Coordonnées Facturation facultatives
	if(obj.Etablissement_Facturation) {
		if (!trim(obj.Etablissement_Facturation.value))	{	
			 empty_fields += "* " + obj.Etablissement.name + " \r";
		}
	}
	if(obj.Service_Facturation) {
		if (!trim(obj.Service_Facturation.value))	{	
			 empty_fields += "* " + obj.Service_Facturation.name + " \r";
		}
	}
	if(obj.Adresse1_Facturation) {
		if (!trim(obj.Adresse1_Facturation.value))	{	
			 empty_fields += "* Adresse facturation\r";
		}
	}	
	if(obj.Code_Postal_Facturation) {	
		if (!trim(obj.Code_Postal_Facturation.value))	{	
			 empty_fields += "* Code postal facturation\r";
		} else {
			if(!isNumber(trim(obj.Code_Postal_Facturation.value))) {
				errors += "- Le code postal de facturation doit être numérique.\n";
			}
		}
	}
	if(obj.Cedex_Facturation) {	
		if (trim(obj.Cedex_Facturation.value))	{	
			if(!isNumber(trim(obj.Cedex_Facturation.value))) {
				errors += "- Cedex facturation doit être numérique.\n";
			}
		}
	}
	if (obj.Ville_Facturation) {	
		if (!trim(obj.Ville_Facturation.value))	{	
			 empty_fields += "* " + obj.Ville_Facturation.name + " \r";
		}
	}
	if (obj.Pays_Facturation) {
		if (!trim(obj.Pays_Facturation.options[obj.Pays_Facturation.selectedIndex].value))	{	
			 empty_fields += "* " + obj.Pays_Facturation.name + " \r";
		}
	}
	if (obj.Telephone_Facturation) {
		if (!trim(obj.Telephone_Facturation.value))	{	
			 empty_fields += "* " + obj.Telephone_Facturation.name + " \r";
		}
	}
	if(obj.Email) {	
		if (!trim(obj.Email.value))	{	
			 empty_fields += "* " + obj.Email.name + " \r";
		} else {
			if (!isEmail(trim(obj.Email.value)))	{	
					errors += "- Email " + obj.Email.value + " non valide.\n";
			 }
		}
		if(obj.Email.value!=obj.ConfirmationEmail.value) {
			errors += "- L'email confirmé est incorrect";
		}		
	}
	if(obj.EmailExposant) {	
		if (!trim(obj.EmailExposant.value))	{	
			 empty_fields += "* Email \r";
		} else {
			if (!isEmail(trim(obj.EmailExposant.value)))	{	
					errors += "- Email " + obj.EmailExposant.value + " non valide.\n";
			 }
		}
	}	

	if(obj.DateNaissance) {
		if (!trim(obj.DateNaissance.value)) {
			empty_fields += "* Date de naissance \r";
		} else {
			var reg=new RegExp ("^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$","g");
			if (!reg.test(obj.DateNaissance.value)) {
				errors += "- Date de naissance " + obj.DateNaissance.value + " non valide.\n";
			}
		}
	}
	
	
  if (!empty_fields && !errors) return true;
	  msg  = "______________________________________________________\n\n"
	  msg += "Le formulaire ne peut être validé à cause des erreurs suivantes\n";
	  msg += " (Merci de recommencer l'opération après correction.)\n";
	  msg += "______________________________________________________\n\n"
  if (empty_fields) {
      msg += "- Les champs obligatoires suivants ne sont pas renseignés:\n" + empty_fields + "\n";
      if (errors) msg += "\n";
  }
  msg += errors;
  alert(msg);
  return false;
}

