function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

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];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


var IB=new Object;
var posX=0;posY=0;
var xOffset=10;yOffset=10;
function AffBulle(texte) {
  contenu="<TABLE border=0 cellspacing=0 cellpadding="+IB.NbPixel+"><TR bgcolor='"+IB.ColContour+"'><TD><TABLE border=0 cellpadding=2 cellspacing=0 bgcolor='"+IB.ColFond+"'><TR><TD><FONT size='-1' face='arial' color='"+IB.ColTexte+"'>"+texte+"</FONT></TD></TR></TABLE></TD></TR></TABLE>&nbsp;";
  var finalPosX=posX-xOffset;
  if (finalPosX<0) finalPosX=0;
  if (document.layers) {
    document.layers["bulle"].document.write(contenu);
    document.layers["bulle"].document.close();
    document.layers["bulle"].top=posY+yOffset;
    document.layers["bulle"].left=finalPosX;
    document.layers["bulle"].visibility="show";}
  if (document.all) {
    //var f=window.event;
    //doc=document.body.scrollTop;
    bulle.innerHTML=contenu;
    document.all["bulle"].style.top=posY+yOffset;
    document.all["bulle"].style.left=finalPosX;//f.x-xOffset;
    document.all["bulle"].style.visibility="visible";
  }
  //modif CL 09/2001 - NS6 : celui-ci ne supporte plus document.layers mais document.getElementById
  else if (document.getElementById) {
    document.getElementById("bulle").innerHTML=contenu;
    document.getElementById("bulle").style.top=posY+yOffset;
    document.getElementById("bulle").style.left=finalPosX;
    document.getElementById("bulle").style.visibility="visible";
  }
}
function getMousePos(e) {
  if (document.all) {
  posX=event.x+document.body.scrollLeft; //modifs CL 09/2001 - IE : regrouper l'évènement
  posY=event.y+document.body.scrollTop;
  }
  else {
  posX=e.pageX; //modifs CL 09/2001 - NS6 : celui-ci ne supporte pas e.x et e.y
  posY=e.pageY; 
  }
}
function HideBulle() {
	if (document.layers) {document.layers["bulle"].visibility="hide";}
	if (document.all) {document.all["bulle"].style.visibility="hidden";}
	else if (document.getElementById){document.getElementById("bulle").style.visibility="hidden";}
}

function InitBulle(ColTexte,ColFond,ColContour,NbPixel) {
	IB.ColTexte=ColTexte;IB.ColFond=ColFond;IB.ColContour=ColContour;IB.NbPixel=NbPixel;
	if (document.layers) {
		window.captureEvents(Event.MOUSEMOVE);window.onMouseMove=getMousePos;
		document.write("<LAYER name='bulle' top=0 left=0 visibility='hide'></LAYER>");
	}
	if (document.all) {
		document.write("<DIV id='bulle' style='position:absolute;top:0;left:0;visibility:hidden'></DIV>");
		document.onmousemove=getMousePos;
	}
	//modif CL 09/2001 - NS6 : celui-ci ne supporte plus document.layers mais document.getElementById
	else if (document.getElementById) {
	        document.onmousemove=getMousePos;
	        document.write("<DIV id='bulle' style='position:absolute;top:0;left:0;visibility:hidden'></DIV>");
	}

}


/* ======================================================================================*/
/*				 FONCTIONS GÉNÉRIQUES DE VALIDATION DE FORMULAIRE 						 */
/* ======================================================================================*/

//---------------------------------------------
// Empeche la saisie d'un caractere onKeypress
//----------------------------------------------
function empechechar()
{
	if( event.keyCode < 48 || event.keyCode > 57 )
 	{ 
 		alert(get_trad_champ("numeric") );
   		event.returnValue = false;
   	}
}

//-------------------------------------
// Verifie la validite d'un email
//-------------------------------------
function isValidEmail ( _fieldId ) {
	
	var email = document.getElmentById(_fieldId).value;		
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if ( !filter.test(email.value) ) 
	{		
		return false;
	}
	else 
	{
		return true;
	}
}


//-------------------------------------
// Verifie la validite d'une date
//-------------------------------------
function isValidDate ( _fieldId )
{
    if ( _fieldId == "" )
        return true;

    // Récupération de la valeur
    var totalDate    = document.getElementById( _fieldId ).value;
    if ( totalDate == "" )
        return false;
   
    // Découpage de la date récupérée
    var aDate = totalDate.split( "/" );
    if ( aDate.length != 3 )
        return false;

    // Création d'un objet date
    var generatedDate = new Date ( aDate[ 2 ], aDate[ 1 ] - 1, aDate[ 0 ] );
   
    // Test
    if (     generatedDate.getFullYear() == aDate[ 2 ]
        &&    generatedDate.getMonth() + 1 == aDate[ 1 ]
        &&    generatedDate.getDate() == aDate[ 0 ] )
        return true;
   
    return false;
}

//-------------------------------------
// Test criteres form
//-------------------------------------
//---test champ obligatoire
function testObl(fieldValue,fieldName, isList)
{	
	if(fieldValue.trim()=="")
	{	
		inlineMsg(fieldName,get_trad_champ(fieldName),2);
		return false;
		
	}
	else
	{	
		if(isList)
		{
			
			if(fieldValue.trim()==0)
			{
				inlineMsg(fieldName,get_trad_champ(fieldName),2);
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{	
		return true;
		}
	}
}
//--test match expression reg
function testMatchRegex(fieldValue,fieldName,myRegex)
{	
	if(!fieldValue.match(myRegex)) 
  	{
	    inlineMsg(fieldName,get_trad_champ(fieldName+"_incorrect"),2);
	    return false;
  	}  
  	else
  	{
  		return true;
  	}
}

//--test longueur du champ
function testLength(fieldValue,fieldName,fieldLength)
{
	if( parseInt(fieldValue.trim().length) < fieldLength)
	{			
	  	inlineMsg(fieldName,get_trad_champ(fieldLength+"_caracteres_min"),2);
	  	return false;
	}
	else
	{
		return true;
	}
}

/* ======================================================================================*/
/*							 FONCTIONS GÉNÉRIQUES										 */
/* ======================================================================================*/

String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g,"");
}


//--------------------------------------
// Validation form canditature 
//--------------------------------------
function validFormCandidature(form)
{	
	
  var nameRegex = /^[^0-9]*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,10}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  var telRegex = /^[0-9\(\)\+ ]*$/;
  
  //==========================  
  //declaration de variables
  //==========================
  
  var region = form.region.value;  
  var nom = form.nom.value;
  var prenom = form.prenom.value;  
  var cv = form.cv.value;  
  var lm = form.lm.value;  
//  var message = form.message.value;  

  var dptChecked = false;
  var nbDptChecked = 0;
  var dpts = document.getElementsByName('dpt[]')
  
  if( document.getElementsByName('dpt[]')[0] == null) // si pas de dpts affiches
  {
  	inlineMsg("region",get_trad_champ("reselect_region"),2);
  	return false;   
  }
  for(var i=0;i<dpts.length;i++)
  {
	  if(document.getElementsByName('dpt[]')[i].checked)
	  {
	  	dptChecked = true;
	  	nbDptChecked = nbDptChecked+1;
	  }
	 if(i==0) var dptId = "span_" + document.getElementsByName('dpt[]')[i].id;
  }
  
  //==========================  
  //controles JS
  //==========================      
	
  // region  
  if(!testObl(region,"region",true)) return false;  
  
  // dpt
  //if(!testObl(dpt,"dpt",true)) return false;
  if(nbDptChecked>3)
  {
  	inlineMsg(dptId,get_trad_champ("3_dpts_max"),2);
  	return false;
  }
  
  if(!dptChecked) 
  {
  	inlineMsg(dptId,get_trad_champ("dpt"),2);
  	return false;
  }
  
      
  //nom-----
  if(!testObl(nom,"nom",false)) return false;
  if(!testMatchRegex(nom,"nom",nameRegex)) return false;  
  
  
  //prenom ------
  if(!testObl(prenom,"prenom",false)) return false;
  if(!testMatchRegex(prenom,"prenom",nameRegex)) return false;
  
    
  //cv ------
  if(!testObl(cv,"cv",false)) return false;
  
  //lm ------
//  if(!testObl(lm,"lm",false)) return false;
  
  return true;
}

function submitFormCandidature()
{		
	
	if(validFormCandidature(document.getElementById('candidatureForm'))) 
	{					
		document.getElementById('candidatureForm').submit();
	}
	else
	{			
		return false;
	}
}