var encours=0;
var envoye=0;
var maildemande=0;

/* Fonctions gÚnÚrales */
/* ******************* */

function isVide( val)
{
  var espaces= /^([\s]*)$/;
  if( val.match( espaces) )
    return true;
  else 
    return false;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

/* ********************************* */
/* Fonctions SpÚcifiques formulaires */
/* ********************************* */

function GetLibelle(theForm,ElemName)
{
	var Libelle=ElemName; //Par dÚfaut
	var strToEval='if (theForm.libelle_'+ElemName+') ';
	strToEval+='if (theForm.libelle_'+ElemName+'.value!="") '
	strToEval+='Libelle=theForm.libelle_'+ElemName+'.value;';
	eval(strToEval);
	return Libelle;
}

function IsRequired(theForm,ElemName)
{
	var bIsRequired=0;
	var strToEval='if (theForm.presence_'+ElemName+') ';
	strToEval+='if (theForm.presence_'+ElemName+'.value==\'_OBLIGATOIRE\') '
	strToEval+='bIsRequired=1;';
	eval(strToEval);
	return bIsRequired;
};


function GetSpecialControlType(theForm,ElemName)
{
	var CtrlType=""; //Par defaut
	var strToEval='if (theForm.type_'+ElemName+') ';
//	strToEval+='if (theForm.type_'+ElemName+'.value!="") '
	strToEval+='if ( !isVide( theForm.type_'+ElemName+'.value) ) '
	strToEval+='CtrlType=theForm.type_'+ElemName+'.value;';
	eval(strToEval);
	return CtrlType;
}

function emailCheck(emailStr)
{
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)

//  var re = /^([a-zA-Z0-9_])([a-zA-Z0-9_\.\-])*\@(([a-zA-Z0-9_\-])+\.)+([a-zA-Z0-9]{2,4})$/;

	if (matchArray==null)
	{
	 	alert("Le courriel n'est pas valide.")
		return false
	}

	var user=matchArray[1]
	var domain=matchArray[2]

	// See if "user" is valid
	if (user.match(userPat)==null)
	{
    	// user is not valid
    	alert("Le courriel n'est pas valide.")
    	return false
	}

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null)
	{
    	// this is an IP address
		for (var i=1;i<=4;i++)
		{
	    	if (IPArray[i]>255)
			{
	        	alert("Le courriel n'est pas valide!")
				return false
			}
		}
    	return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null)
	{
		alert("Le nom de domaine du courriel n'est pas valide.")
    	return false
	}

	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4)
	{
   		// the address must end in a two letter, a three letter or a four letter word.
   		alert("Le courriel doit se terminer soit par trois ou quatre lettres de domaine (.info,.com,.net,.org,...), soit par les deux lettres du pays (.fr,.de.,us,...).")
   		return false
	}

	// Make sure there's a host name preceding the domain.
	if (len<2)
	{
		var errStr="Le nom de domaine du courriel n'est pas précisé."
		alert(errStr)
   		return false
	}

	// If we've gotten this far, everything's valid!
	return true;
}

function ValiderFormulaire(MyDocument,MyForm)
{
  var problem = 0;
  
  if( encours !=0 )
  { 
  }
  else
  {
  encours=1;
	if (MyForm)
	{
		if (MyForm.elements)
		{
			for(i=0; i < MyForm.elements.length; i++)
			{
				var theElement=MyForm.elements[i];
				var name=theElement.name;
				var value=theElement.value;
				var type=theElement.type;
				if( type=='select-one')
  				var value=theElement.options[ theElement.selectedIndex].value;
				else
	   			var value=theElement.value;

				var libelle=theElement.name;	//par dÚfaut
				
        if (type!='hidden')
				{
					if ((name=="objet") )
						  {
						  if (value == "Sélectionner une rubrique")
						    {
                alert('Le champ objet est obligatoire');
                encours = 0;
                problem = 1;
                }
						  }//fin du if objet
					          
					if ((name=="corpus") )
						  {
						  if (value == "")
						    {
                alert('Le champ commentaires est obligatoire');
                encours = 0;
                problem = 1;
                }
						  }//fin du if objet
					

					var CtrlType=GetSpecialControlType(MyForm,name);
					if (CtrlType!="")
					{  
              //Check e-mail
						  if ((CtrlType=="_EMAIL") )
						  {
						    if (MyForm.info.checked == 1)
						    {
						    MyForm.info.value = "true";
                if (value == "") 
                  {
                  alert('Le champ courriel est obligatoire');
							    encours=0;
                  problem = 1;
                 }
                else if (!emailCheck(value))
							   {
							 	  theElement.focus();
                  encours=0;
                  problem = 1;
							   }
							  }
							  else 
                {
                 MyForm.info.value = "false";
                  if (value != "") 
                      if (!emailCheck(value))
      							   {
      							 	  theElement.focus();
                        encours=0;
                        problem = 1;
      							   }
      					}
							 }
					  } 
				}
			}
			}
			
		  if( (envoye != 1) && (problem == 0))
      {
        envoye=1;
        //alert('envoi');
        MyForm.submit();
        envoye=0;
      }
    }
    encours=0;
  }
}

