function trimp(cp)
{
	alphanum=0;
	valeur="";

	for (i=0; i<cp.length; i++ )
	{
		Char = cp.charAt(i);
		if (Char!=" ") { alphanum=1; }

		if (alphanum==1)
		{
			if (cp.charAt(i)!=" " || (cp.charAt(i)==" " && cp.charAt(i+1)!=" "))
			{
				valeur += Char;
			}
		}
	}
	return (valeur);
}

// Verifie les champs du coté client
// Fonction qui teste les champs
function checkField(typeChamps,nomChamps,valeur,minValue,maxValue)
{
	retour="";
	valeurChamps = trimp(valeur);

	switch(typeChamps)
	{
		// teste les champs de type text
		case "text":
			if (valeurChamps != "")
			{
						if (minValue!=-1 && valeurChamps.length < minValue) { retour += nomChamps+" : doit contenir "+minValue+" caracteres au minimum\n"; }
						if (maxValue!=-1 && valeurChamps.length > maxValue) { retour += nomChamps+" : doit contenir "+maxValue+" caracteres au maximum\n"; }
			} else {
				retour += "A compléter : "+nomChamps+"\n";
			}
		break;

		// verifie que la valeur du champs est bien numerique
		case "isnumeric":
			if (valeurChamps != '')
			{
				var ValidChars = "0123456789";
				var retour='';
				var Char;

				for (i = 0; i < valeurChamps.length && retour == ''; i++)
				{
					Char = valeurChamps.charAt(i);
					if (ValidChars.indexOf(Char) == -1)
					{
						retour += nomChamps+" : doit contenir uniquement des chiffres\n";
					} else {
						if (minValue!=-1 && valeurChamps < minValue) { retour += "La valeur minimum pour le champs : "+nomChamps+" est "+minValue+"\n"; }
						if (maxValue!=-1 && valeurChamps > maxValue) { retour += "La valeur maximum pour le champs : "+nomChamps+" est "+maxValue+"\n"; }
					}
				}
			} else {
				retour += "A compléter : "+nomChamps+"\n";
			}
		break;

		// verifie que le champs contient bien des caracteres numeriques
		case "cnumeric":
			if (valeurChamps != '')
			{
				var ValidChars = "0123456789";
				var retour='';
				var Char;

				for (i = 0; i < valeurChamps.length && retour == ''; i++)
				{
					Char = valeurChamps.charAt(i);
					if (ValidChars.indexOf(Char) == -1)
					{
						retour += nomChamps+" : doit contenir uniquement des chiffres\n";
					} else {
						if (nomChamps=="Code postal de votre bien")
						{
							msg_cp = "\nLe code postal doit être sous la forme 01234";
						} else {
							msg_cp="";
						}

						if (minValue!=-1 && valeurChamps.length < minValue) { retour += nomChamps+" : doit contenir "+minValue+" caracteres au minimum"+msg_cp+"\n"; }
						if (maxValue!=-1 && valeurChamps.length > maxValue) { retour += nomChamps+" : doit contenir "+maxValue+" caracteres au maximum"+msg_cp+"\n"; }
					}
				}
			} else {
				retour += "A compléter : "+nomChamps+"\n";
			}
		break;

		// teste les champs de type numeric
		case "numeric_lng":
			if (valeurChamps != "")
			{
				if (minValue!=-1 && valeurChamps.length < minValue) { retour += nomChamps+" : doit contenir "+minValue+" caracteres au minimum\n"; }
				if (maxValue!=-1 && valeurChamps.length > maxValue) { retour += nomChamps+" : doit contenir "+maxValue+" caracteres au maximum\n"; }
			} else {
				retour += "A compléter : "+nomChamps+"\n";
			}
		break;

		// teste la validité des emails
		case "validmail":
			if (valeurChamps.length>0)
			{
				if (valeurChamps.length>=minValue && valeurChamps.length<=maxValue)
				{
					if (window.RegExp)
					{
						var email = valeurChamps;
						var reg = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/i;
						 
						if(reg.exec(email)==null){
							retour += "L'adresse e-mail n'est pas correcte. Utiliser ce format, svp:\nnom@domaine.ext (ex: emilie.durand@wanadoo.fr)\n";
						}
					} else {
						var arobase = valeurChamps.indexOf("@");
						var point = valeurChamps.lastIndexOf(".");
						if((arobase < 3)||(point + 2 > valeurChamps.length)||(point < arobase+3))
						{
							retour += "L'adresse e-mail n'est pas correcte. Utiliser ce format, svp:\nnom@domaine.ext (ex: emilie.durand@wanadoo.fr)\n";
						}
					}
				} else {
					retour += "L'adresse e-mail n'est pas correcte. Utiliser ce format, svp:\nnom@domaine.ext (ex: emilie.durand@wanadoo.fr)\n";
				}
			} else {
				retour += "A compléter : "+nomChamps+" (obligatoire pour vous répondre)\n";
			}
		break;
	}
	
	return (retour);
}

function verifieBDC()
{
	var msg_alert="";
	var choix="";
	
	msg_alert += checkField("text","Votre nom",document.form_bdc.nom.value,1,500);
	msg_alert += checkField("text","Votre prénom",document.form_bdc.prenom.value,1,500);
	msg_alert += checkField("validmail","Votre email",document.form_bdc.email.value,7,690);
	msg_alert += checkField("text","Votre adresse",document.form_bdc.adresse.value,1,500);
	msg_alert += checkField("text","Votre ville",document.form_bdc.ville.value,1,500);
	msg_alert += checkField("cnumeric","Code postal",document.form_bdc.code_postal.value,5,5);
	if (document.form_bdc.code_postal.value.substring(0,2)=="00")
	{
		msg_alert += "Les 2 premiers chiffres du code postal de votre bien doivent être différents de 00.\n";
	}
	msg_alert += checkField("text","Votre pays",document.form_bdc.pays.value,1,500);

	if (document.form_bdc.site.value.substring(0,1)=="1")
	{
		msg_alert += "A compléter : Comment nous avez-vous connu ?\n";
	}	
	


	if (msg_alert!='')
	{
		alert(msg_alert);
	} else {
		// Validation du formulaire, tt est ok
		document.form_bdc.submit();
	}
}