/*
* liste
* choix
* jour (nb)
* mois (nb)
* annee (nb)
* popUp (url, w, h)
* redimChaine (txt)
* chercheRemplace (txt)
* 
*/
/* fonction liste
 * utilisee avec la fonction choix.
 * coordonne le choix d'une liste déroulante avec le code d'une textarea
 */
function liste (c_nom, l_nom) {
	c_nom.value = l_nom.value;
}

/* fonction choix
 * utilisee avec la fonction choix.
 * coordonne un code d'une textarea avec un choix d'une liste déroulante
 */
function choix (c_nom, l_nom) {
	l_nom.value = c_nom.value;
}
/* fonction testVide
 * Test si le champ en parmatére est vide ou non
 * retourne true si vide sinon false
 */
function testVide (c) {
	if (c.value == '' || c.value == null){
		return true;
	}
	else {return false;}
}

/**
 *
 */
function jour(nb){
	if (nb.value.length<2) redimChaine(nb, 2, "0");
/*	if (nb.value=="00") nb.value="01";
	if ((parseInt(nb.value) <1) || (parseInt(nb.value) >31)) {
		alert("Attention le jour de la date est incorrect.");
		nb.value = "01";
	}
*/

	
}
/**
 *
 */
function mois (nb){
	if (nb.value.length<2) redimChaine(nb, 2, "0");
/*
	if ((parseInt(nb.value) <1) || (parseInt(nb.value) >12)) {
		alert("Attention le mois de la date est incorrect.");
		nb.value = "01";
	}
*/
}

//
//
//
function annee(nb){
	if (isNaN(nb.value)) {
		alert('Champ de type numérique. Année sur 4 entiers aaaa.');
		nb.value='';
		nb.focus();
	} else {
		if (nb.value.length==2) {
			nb.value = "20"+nb.value;
		}
		else if (nb.value.length <2 || nb.value.length == 3) {
			nb.value="";
		}

	}
}

/*
 *fonction popUp (url, w, h)
*/
function popUp(url, w, h) {
	//num : indice de l'url
	//h : hauteur et w : largeur
	fenetre2 = window.open(url,"fen02","width="+ w +", height="+h);
}
/*
*
*/
function popUp02 (fen, url, w, h) {
	//num : indice de l'url
	//h : hauteur et w : largeur
	fenetre2 = window.open(url,fen,"width="+ w +", height="+h);
	fenetre2.focus();
}
/*
 * fonction redimChaine
 */
function  redimChaine(chaine, tail, car) {
	var i = 0;
	if (chaine.value.length < tail) {
	 i = chaine.value.length;
	 while (i < tail) {
		chaine.value = car + chaine.value;
		i = i + car.length;
	 }
	}	
}

/*
 * fonction chercheRemplace (txt)
 * cherche les " et les remplace par des ' dans le texte txt
 */
function chercheRemplace (txt) {
	var i=0;
	while (i != -1) {
		var cher = "\"";
		var remp = "'";
		i= txt.value.indexOf(cher,i);
		if (i>=0){
			txt.value = txt.value.substring(0,i)+remp+txt.value.substring(i+cher.length);
			i = i + remp.length;
		}
	}
}


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_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- le champ '+nm+" n'est pas une adresse email valide\n";
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- le champ '+nm+' doit contenir un nombre valide.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' doit contenir un npmbre entre '+min+' et '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- le champ  '+nm+' n\'a pas été renseigné.\n'; }
  } if (errors) alert('Les erreurs suivantes ont été détectées lors de la validation du formulaire:\n'+errors);
  document.MM_returnValue = (errors == '');
}

/*
* Cache un calque et affiche un autre
*/
function ChangeAuto(cache,visible){
	skn = document.getElementById(cache).style;
	skn.visibility = "hidden";
	skn = document.getElementById(visible).style;
	skn.visibility = "visible";
}
/*
* Cache un calque et affiche un autre
* nom du layer a afficher
* tabLayer les layers à rendre invisible (nomLayer1:[nomLayer2]:[nomLayer3]:...:
* terminer par :
*/
function visibleAuto(visible, tabLayer){
	var i	= 0;
	var deb = 0;
	var fin = 0;
	var nomLayer 	= "";
	var cher 		= ":";

	while (deb != -1) {
		fin = tabLayer.indexOf(cher,deb);
		if (fin>=0){
			nomLayer = tabLayer.substring(deb,fin);
			tabLayer = tabLayer.substring(fin+1,tabLayer.length);
			skn = document.getElementById(nomLayer).style;
			skn.visibility = "hidden";
		}
		else 
			deb = -1;
	}
	skn = document.getElementById(visible).style;
	skn.visibility = "visible";
}

/**
	fonction qui prend la date saisie jjmmaaaa
	et la met au format jj-mm-aaaa
**/

function testDate (d) { 
	var bool = false;
	if (d.value.length != 10) { 
		if (d.value.length == 8 && !isNaN(d.value)) {  
			d.value = d.value.substring(0,2)+"-"+d.value.substring(2,4)+"-"+d.value.substring(4,8);
			bool= true;
		}
		else  {
			bool = false;			
		}
	}
	else if (d.value.length == 10) {
		if ( isNaN(d.value.substring(0,2)) || isNaN(d.value.substring(3,5)) || isNaN(d.value.substring(6,10)) ){
			bool = false;
		}
		else{
			bool = true;
		}
	}
	if (!bool) {
		alert ("Erreur. Saisissez la date au format jjmmaaaa");   
			d.value = "00-00-0000";
	}
}
/**
	testDateAAMM
	fonction qui vérifie la date saisie mm/aaaa-mm/aaaa
**/

function testDateAAMM(d) {  
	if ( d.value.length >0)  { 
		if (d.value.length == 15) { 
			if ( (d.value.substring(2,3) != '/') || (d.value.substring(7,8) != '-') || (d.value.substring(10,11) != '/')){
				alert('Respectez le format de saisie mm/aaaa-mm/aaaa.   s.v.p.');
				  
			}
			else if( (isNaN(d.value.substring(0,2)) == true) || (isNaN(d.value.substring(3,7)) == true) || (isNaN(d.value.substring(8,10)) == true) || (isNaN(d.value.substring(11,15)) == true) ) {
				alert('Indiquez les mois et années en chiffre (mm/aaaa-mm/aaaa).  s.v.p. ');
				
			}
			return true;
		}else {
			d.focus();
			return false;
		}
	}else{
		return true;
	}
}
/**
	testAnneeUniversitaire
	fonction qui vérifie la date saisie aaaaa/aaaa
**/

function testAnneeUniversitaire(d) {  
	if ( d.value.length >0)  { 
		if (d.value.length == 9) { 
			if ( d.value.substring(4,5) != '/' ){
				alert('Respectez le format de saisie aaaa/aaaa.  s.v.p.');
				d.focus();	
			}
			else if( (isNaN(d.value.substring(0,4)) == true) || (isNaN(d.value.substring(5,9)) == true) ) {
				alert('Indiquez les années en chiffre (aaaa/aaaa).  s.v.p. ');
				d.focus();				
			}
			return true;
		}else {
			d.value="";
			d.focus();
			return false;
		}
	}else{
		return true;
	}
}
function heure5 (h) { 
	var bool = true;
	if (h.value.length == 0) { 
		bool=false }
	else {
		if (h.value.length == 1) { 
			if ( isNaN(h.value.substring(0,1)) ){
				bool = false;
			}else {
				h.value = "0"+h.value+":00";
			}
		} else if (h.value.length == 2) { 
			if ( isNaN(h.value.substring(0,2)) ){
				bool = false;
			}else {
				h.value += ":00";
			}
		} else if (h.value.length == 4) {  
			if ( isNaN(h.value.substring(0,2)) || isNaN(h.value.substring(2,4)) ) {
				bool = false;
			}else {		
				h.value = h.value.substring(0,2)+":"+h.value.substring(2,4);
			}
	
		} else if (h.value.length==5) {
			if (isNaN(h.value.substring(0,2)) || (h.value.substring(2,3)!=':') || isNaN(h.value.substring(3,5)) ) {
				bool = false;
			}
		}
	}
	if (bool == false) {
		alert ("Erreur. Saisissez l'heure au format 00:00. svp");   
		h.value = "00:00";
	}

}  
function testSaisieDemandeur () {
	var bool = true;
	var i=0;
	// enleve tous les " de tous les champs du formulaire
	while ( i < parseInt(document.saisie.elements.length) ){ 
		chercheRemplace(document.saisie.elements[i]);
		i++; 
	}	
	MM_validateForm('nom','','R','prenom','','R','nationalite','','R','dateNaissance','','R','lieuNaissance','','R','paysNaissance','','R','adresse1','','R','codePostal','','R','ville','','R','pays','','R','motDePasse','','R');
	bool = document.MM_returnValue;
	if (bool==true) saisie.submit();         
}       
function testDateNaissance (d) {
	var bool = false;
	if (d.value.length != 10) { 
		if (d.value.length == 8 && !isNaN(d.value)) {  
			d.value = d.value.substring(0,2)+"-"+d.value.substring(2,4)+"-"+d.value.substring(4,8);
			bool= true;
		}
		else  {
			bool = false;			
		}
	}
	else if (d.value.length == 10) {
		if ( isNaN(d.value.substring(0,2)) || isNaN(d.value.substring(3,5)) || isNaN(d.value.substring(6,10)) ){
			bool = false;
		}
		else{
			bool = true;
		}
	}
	if (!bool) {
		alert ("Erreur date de naissance. Saisissez la date au format jjmmaaaa");   
			d.value = "00-00-0000";
	}
}	
/**
*Fonction qui return la date du jour actuel.
* réalisée le 03-03-2006
*/
function getDateNow() 
{
	var hoy; 
	var vfecha = new Date(); 
	var anio = vfecha.getYear(); 
	var mes = parseInt(vfecha.getMonth())+1; 
	if(mes<10)	
	{
		mes="0" +mes;
	}
	var dia = vfecha.getDay(); 
	var num = vfecha.getDate(); 
	if(num<10)	
	{
		num="0" + num; 
	}
	hoy = num + "-" + mes + "-" + anio; 
	return hoy;	
} 

/**
* Function validarNumero(e)
* Permet d'écrire que numéros sur la boit de texte.
*/
function validarNumero(e) {
	tecla=(document.all) ? e.keyCode : e.which;
	if((tecla<48 || tecla>57))
	return false;
}
/**
* Function validarNumeroPoint(e)
* Permet d'écrire que numéros de telephone sur la boit de texte.
*/
function validarNumeroPoint(e) {
	tecla=(document.all) ? e.keyCode : e.which;
	if(tecla<46 || tecla>57)
	return false;
}
/**
* Function validarNumeroTiret(e)
* Permet d'écrire les dates sur la boit de texte.
*/
function validarNumeroTiret(e) {
	tecla=(document.all) ? e.keyCode : e.which;
	//alert(tecla);
	if((tecla>47 && tecla<=57) || (tecla==45))
		return true;
	else 
	return false;
}
/**
* Function validarNumeroLettre(e) avec lettres
* Permet d'écrire que numéros sur la boit de texte.
*/
function validarNumerolettre(e){
	//alert (tecla);
	tecla=(document.all) ? e.keyCode : e.which;
	if((tecla<47 || tecla>57)&& (tecla<97 || tecla>122)&& (tecla<65 || tecla>90)&& (tecla<192 || tecla>214)&& (tecla<217 || tecla>221)&& (tecla<224 || tecla>246) && (tecla<249 || tecla>253)&&(tecla!=45 && tecla!=9 && tecla!=10 && tecla!=11 && tecla!=32))
	return false;
}
/**
* Function validarAlpha(e) avec lettres
* Permet d'écrire chiffres, lettres, apostrophe et tiret.
*/
function validarAlpha(e) {
	tecla=(document.all) ? e.keyCode : e.which;
	if( tecla==34  || tecla==47 || tecla==92)	return false;
}

/**
* Function validarLettre(e) 
* Permet d'écrire que lettres sur la boîte de texte.
*/
function validarLettre(e)
{
	tecla=(document.all) ? e.keyCode : e.which;
	if(((tecla<97 || tecla>122)&& (tecla<65 || tecla>90)&& (tecla<192 || tecla>214)&& (tecla<217 || tecla>221)&& (tecla<224 || tecla>246) && (tecla<249 || tecla>253))&&(tecla!=45 && tecla!=9 && tecla!=10 && tecla!=11 && tecla!=14 && tecla!=15 && tecla!=32))
	return false;
}

