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 ++;
				}						
											
				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 verify(obj) {
	var msg  = "";
	var empty_fields = "";
	var errors = "";
	
	if(obj.Nom) {	
		if(obj.NomReq) {
			if (!trim(obj.Nom.value))	{	
				 empty_fields += "* " + obj.Nom.name + " \r";
			}
		}
	}
	if(obj.Prenom) {
		if(obj.PrenomReq) {
			if (!trim(obj.Prenom.value))	{	
				 empty_fields += "* " + obj.Prenom.name + " \r";
			}
		}
	}
	if(obj.Societe) {
		if(obj.SocieteReq) {
			if (!trim(obj.Societe.value))	{	
				 empty_fields += "* " + obj.Societe.name + " \r";
			}
		}
	}
	if(obj.CP) {
		if(obj.CPReq) {
			errors += "- Le code postal est obligatoire.\n";
		} else {
			if(!isNumber(trim(obj.CP.value))) {
				errors += "- Code postal " + obj.CP.value + " doit être numérique.\n";
			}
		}
	}
	if(obj.Email) {	
		if (!trim(obj.Email.value))	{	
			if(obj.EmailReq) {
				if(obj.Email) {
					 empty_fields += "* " + obj.Email.name + " \r";
				}
			}
		} else {
			if (!isEmail(trim(obj.Email.value)))	{	
					errors += "- Email " + obj.Email.value + " non valide.\n";
			 }
		}
	}
	if(obj.Telephone) {
		if(obj.TelephoneReq) {
			if (!trim(obj.Telephone.value))	{	
				 empty_fields += "* " + obj.Telephone.name + " \r";
			}
		}
	}
	if (obj.Objet) {
		if (obj.ObjetReq) {
			if (!trim(obj.Objet.options[obj.Objet.selectedIndex].value))	{	
				 empty_fields += "* " + obj.Objet.name + " \r";
			}
		}
	}
	if(obj.Message) {	
		if(obj.MessageReq) {
			if (!trim(obj.Message.value))	{	
				 empty_fields += "* " + obj.Message.name + " \r";
			}
		} else {			
			if(obj.Message.value.length > 1000) {
				errors += "- Message , 1000 caractères maximum.\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:\r" 
              + empty_fields + "\n";
      if (errors) msg += "\n";
  }
  msg += errors;
  alert(msg);
  return false;
}
