// ---------------------------------------------------------------------------------------------------------------
// FONCTIONS CONNEXION POUR ACCEDER AUX OFFRES DE STAGE ET EMPLOI AVEC FE_LOGIN 
// ---------------------------------------------------------------------------------------------------------------


// JUDITH : supprime tous les espaces dans une chaine de caracteres
// ******************************************************************************
function valid_connexion_promo(vform) {
	var saisieok = false;
	champ = vform.elements["nom"];
	if (verifChampTxt(champ)) { 
		champ = vform.elements["prenom"];
		if (verifChampTxt(champ)) { 
			champ = vform.elements["anneepromo"];
			if (verifChampTxt(champ)) { 
				nom = nettoie_texte(vform.elements["nom"].value);
				prenom = nettoie_texte(vform.elements["prenom"].value);
				anneepromo = no_espace(vform.elements["anneepromo"].value);
				vform.elements["user"].value = nom+"_"+prenom;
				vform.elements["pass"].value = nom.charAt(0)+prenom.charAt(0)+anneepromo;
				// alert ("user : "+vform.elements["user"].value);
				// alert ("pass : "+vform.elements["pass"].value);
				saisieok = true;
			}
		}
	}
	if (!saisieok) alert ("Veuillez saisir vos nom, prénom et année.");
	return saisieok;
}


// JUDITH : supprime tous les espaces dans une chaine de caracteres
// ******************************************************************************
function nettoie_texte (my_string) {
	var new_string = my_string.toLowerCase();
	new_string = no_accent(new_string);
	new_string = no_caracteres_speciaux(new_string);
	new_string = no_espace(new_string);
	return new_string;
}


// JUDITH : supprime tous les espaces dans une chaine de caracteres
// ******************************************************************************
function no_espace (my_string) {
	var new_string = my_string.replace(/\s+/g,"");
	return new_string;
}


// JUDITH : supprime les caracteres accentues d'une chaine de caracteres
// equivalent a ma fonction PHP 'fct_remove_accents'
// ******************************************************************************
function no_accent (my_string) {
	var new_string = my_string;
	var pattern_accent = new Array("À","Á","Â","Ã","Ä","Å","à","á","â","ã","ä","å","Ò","Ó","Ô","Õ","Ö","Ø","ò","ó","ô","õ","ö","ø","È","É","Ê","Ë","è","é","ê","ë","Ç","ç","Ì","Í","Î","Ï","ì","í","î","ï","Ù","Ú","Û","Ü","ù","ú","û","ü","ÿ","Ñ","ñ");
	var pattern_replace_accent = new Array("A","A","A","A","A","A","a","a","a","a","a","a","O","O","O","O","O","O","o","o","o","o","o","o","E","E","E","E","e","e","e","e","C","c","I","I","I","I","i","i","i","i","U","U","U","U","u","u","u","u","y","N","n");
	if (my_string && my_string!= "") {
		for (var n=0;n<pattern_accent.length;n++) {
			new_string = new_string.replace(pattern_accent[n],pattern_replace_accent[n]);
		}
	}
	return new_string;
}


// JUDITH : supprime les caracteres accentues d'une chaine de caracteres
// equivalent a ma fonction PHP 'removeBadChars'
// ******************************************************************************
function no_caracteres_speciaux (my_string) {
	var new_string = my_string;
	var pattern_caracteres_speciaux = new Array("'","`","&",'"',"$","*","+","=","]","[","°","@","#","~","|",".",":","!",",","?",";","/","\\","<",">");
	if (my_string && my_string!= "") {
		for (var n=0;n<pattern_caracteres_speciaux.length;n++) {
			new_string = new_string.replace(pattern_caracteres_speciaux[n],"-");
		}
	}
	return new_string;
}



// ---------------------------------------------------------------------------------------------------------------
// FONCTIONS FORMULAIRES DEPOT OFFRES D'EMPLOI ET DE STAGE 
// ---------------------------------------------------------------------------------------------------------------


// JUDITH : verifie champ de saisie pour formulaire employeur
// ******************************************************************************
function verif_saisie_employeur (vform) {
	var saisieok = false;
	var emailok = true;
	champ = vform.elements["emp_raison_sociale"];
	if (verifChampTxt(champ)) { 
		champ = vform.elements["emp_adresse"];
		if (verifChampTxt(champ)) { 
			champ = vform.elements["emp_codepostal"];
			if (verifChampTxt(champ)) { 
				champ = vform.elements["emp_ville"];
				if (verifChampTxt(champ)) { 
					champ = vform.elements["emp_telephone"];
					if (verifChampTxt(champ)) { 
						champ.value = formatNumeroTelephone (champ.value);
						champ = vform.elements["emp_nom_contact"];
						if (verifChampTxt(champ)) { 
							champ = vform.elements["emp_prenom_contact"];
							if (verifChampTxt(champ)) {  
								champ = vform.elements["emp_email_contact"];
								if (!verifEmail (champ)) {
									emailok = false;
								} else {
									champ = vform.elements["emp_domaine_activite"];
									if (verifChampTxt(champ)) { 
										saisieok = true;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	if (!emailok) alert ("L'adresse email de la personne à contacter n'est pas correcte.");
	else if (!saisieok) alert ("Veuillez saisir les champs obligatoires.");
	return saisieok;
}


// JUDITH : verifie champ de saisie pour formulaire offre d'emploi
// ******************************************************************************
function verif_saisie_offre_emploi (vform) {
	var saisieok = false;
	if (vform.elements["annuler"].value == "true") {
		saisieok = true;
	} else {
		champ = vform.elements["titre_poste"];
		if (verifChampTxt(champ)) { 
			champ = vform.elements["site_ieqt"];
			if (verifChampTxt(champ)) { 
				saisieok = true;
			}
		}
	}
	if (!saisieok) alert ("Veuillez saisir les champs obligatoires.");
	return saisieok;
}


// JUDITH : verifie champ de saisie pour formulaire offre d'emploi
// ******************************************************************************
function verif_saisie_offre_stage (vform) {
	var saisieok = false;
	if (vform.elements["annuler"].value == "true") {
		saisieok = true;
	} else {
		champ = vform.elements["titre_offre"];
		if (verifChampTxt(champ)) { 
			champ = vform.elements["site_ieqt"];
			if (verifChampTxt(champ)) { 
				saisieok = true;
			}
		}
	}
	if (!saisieok) alert ("Veuillez saisir les champs obligatoires.");
	return saisieok;
}



// ---------------------------------------------------------------------------------------------------------------
// FONCTIONS GENERIQUES 
// ---------------------------------------------------------------------------------------------------------------


// verif adresse email
// ******************************************************************************
function verifEmail (vEmail) {
   email = vEmail.value;
   var reg = /^([a-zA-Z0-9\-_]+[a-zA-Z0-9\.\-_]*@[a-zA-Z0-9\-_]+\.[a-zA-Z\.\-_]{1,}[a-zA-Z\-_]+)$/;
   return reg.test(email);
}

// format num tel (retire le . , / - et remplace par un espace ou ajoute espace) ex. 0561146454 ou 05.61.14.64.54 devient 05 61 14 64 54
// ******************************************************************************
function formatNumeroTelephone(NumTel){
   var Tel;
   var i = 0;
   while (i < NumTel.length-1) {
      if ((NumTel.charAt(i) == ".")||(NumTel.charAt(i) == ",")||(NumTel.charAt(i) == "/")||(NumTel.charAt(i) == "-")||(NumTel.charAt(i) == " ")) {
         Tel = NumTel.substring(0,i) + NumTel.substring(i+1,NumTel.length);
         NumTel = Tel;
      } else {
         i++;
      }
   }
   var NewNumTel = '';
      for(var i=0; i<NumTel.length; i++) {
         if(i/2 == Math.round(i/2)) NewNumTel += NumTel.charAt(i);
         else NewNumTel += NumTel.charAt(i) + ' ';
   }
   return (NewNumTel);
} 

// verif btn radio
// ******************************************************************************
function verifBtnRadio(vBtn) {
	ok=false;
	for (var n=0;n<vBtn.length;n++) {
		if (vBtn[n].checked) {
			ok=true;
			n=vBtn.length;
		}
	}
	return ok;
}

// verif champs texte non vide
// ******************************************************************************
function verifChampTxt(vChampTxt) {
   var txtsansespaces = vChampTxt.value;
   txtsansespaces = suppEspacesDebut(txtsansespaces);
   txtsansespaces = suppEspacesFin(txtsansespaces);
   var veriftxt = addslashes(addbr(txtsansespaces));
   if (veriftxt=="") {
     // vide champs txt pour supprimer espaces eventuels
     vChampTxt.value = '';
	 return false;
   } else {
	 vChampTxt.value = txtsansespaces;
     return true;
   }
}

// supprime les espaces en début de texte
// ******************************************************************************
function suppEspacesDebut(vTxt) {
  if (vTxt!="") {
    if (vTxt.charAt(0)==" ") {
	  while (vTxt.charAt(0) == " ") {
	    vTxt = vTxt.substring(1,vTxt.length);
	 }
    }
  }
  return vTxt;
}


// supprime les espaces en fin de texte
// ******************************************************************************
function suppEspacesFin(vTxt) {
  if (vTxt!="") {
    if (vTxt.charAt(vTxt.length-1)==" ") {
	  while (vTxt.charAt(vTxt.length-1) == " ") {
	    vTxt = vTxt.substring(0,(vTxt.length-1));
	 }
    }
  }
  return vTxt;
}

// remplace les retours à la ligne par des <br>
// ******************************************************************************
function addbr(ch) {
   ch = ch.replace(String.fromCharCode(13),"<br>");
   ch = ch.replace(String.fromCharCode(10),"");
   return ch
}


// ajoute des slashes pour les apostrophes
// ******************************************************************************
function addslashes(ch) {
   ch = ch.replace(/\\/g,"\\\\") 
   ch = ch.replace(/\'/g,"\\'") 
   ch = ch.replace(/\"/g,"\\\"")
   return ch
}
