// détection navigateur
var agt        = navigator.userAgent.toLowerCase();
var is_major   = parseInt(navigator.appVersion);
var is_minor   = parseFloat(navigator.appVersion);
var is_nav     = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav4    = (is_nav && (is_major == 4));
var is_opera   = (is_nav &&(agt.indexOf("opera") != -1));

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_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_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 contact() {
  window.open('contact.html','contact','scrollbars=yes,resizable=no,width=280,height=200')
}
function mentions() {
  window.open('mentions_legales.html','mentions','scrollbars=yes,resizable=no,width=400,height=450')
}
function postuler(offreId) {
  window.open('popup_postuler.asp?offre_id='+offreId,'postuler','scrollbars=yes,resizable=yes,width=400,height=550')
}

function MM_checkPlugin(plgIn, theURL, altURL, autoGo) { //v4.0
  var ok=false; document.MM_returnValue = false;
  with (navigator) if (appName.indexOf('Microsoft')==-1 || (plugins && plugins.length)) {
    ok=(plugins && plugins[plgIn]);
  } else if (appVersion.indexOf('3.1')==-1) { //not Netscape or Win3.1
    if (plgIn.indexOf("Flash")!=-1 && window.MM_flash!=null) ok=window.MM_flash;
    else if (plgIn.indexOf("Director")!=-1 && window.MM_dir!=null) ok=window.MM_dir;
    else ok=autoGo; }
  if (!ok) theURL=altURL; if (theURL) window.location=theURL;
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// validation formulaire ************************

//function to validate by length
function ValidLength(item,len) {
return (item.length >= len);
}

//function to validate an email address
function ValidEmail(item) {
if (!ValidLength(item, 5)) return false;
if (item.indexOf ('@', 0) == -1) return false;
return true;
}

//function to validate select box
function ValidSelect (source)
{
    return (source.selectedIndex > -1);
}

// check la longueur du champs textarea pour le descriptif de l'actu
function CheckLen(Target) {
    var maxLength = 7500;
    if (Target.value.length > maxLength) {
      Target.value = Target.value.substring(0,maxLength);
      CharsLeft = 0;
    }
    else {
      CharsLeft = maxLength - Target.value.length;
    }
    document.edition.caracteres.value = CharsLeft;
}

// check la longueur du champs textarea pour la mission
function CheckLenM(Target) { 
    var maxLength = 7500;
    if (Target.value.length > maxLength) {
      Target.value = Target.value.substring(0,maxLength);
      CharsLeft = 0;
    }
    else {
      CharsLeft = maxLength - Target.value.length;
    }
    document.offre_edition.caracteres1.value = CharsLeft;
}
// check la longueur du champs textarea pour le profil
function CheckLenP(Target) {
    var maxLength = 7500;
    if (Target.value.length > maxLength) {
      Target.value = Target.value.substring(0,maxLength);
      CharsLeft = 0;
    }
    else {
      CharsLeft = maxLength - Target.value.length;
    }
    document.offre_edition.caracteres2.value = CharsLeft;
}

// display an error alert
function error(elem, text) {
// abort if we already found an error
if (errfound) return;
window.alert(text);
errfound = true;
}
// main validation function
function Validate() {
  errfound = false;
  if (!ValidLength(document.postuler.nom.value,3)) 
  error(document.postuler.nom,"Merci de saisir votre nom"); 
  else if (!ValidLength(document.postuler.prenom.value,3)) 
  error(document.postuler.prenom,"Merci de saisir votre prénom");
  else if (!ValidLength(document.postuler.email.value,3)) 
  error(document.postuler.email,"Merci de saisir votre email");
  else if (!ValidEmail(document.postuler.email.value))
  error(document.postuler.email, "Merci de saisir votre mail correctement !!!\n  => pseudo@domaine.com");
  else if (!ValidLength(document.postuler.cv.value,50)) 
  error(document.postuler.cv,"Merci de saisir plus de 50 caractères pour votre CV");
  else if (!ValidLength(document.postuler.lettre_motivation.value,50)) 
  error(document.postuler.lettre_motivation,"Merci de saisir plus de 50 caractères pour votre lettre de motivation");
  return !errfound; /* true if there are no errors */
}

//validation offre
function ValidateOffre()
{
	errfound = false;
  if (document.form1.metier[document.form1.metier.selectedIndex].value == "")
		error(document.form1.metier,"Merci de choisir un métier");
	return !errfound; /* true if there are no errors */
}

//validation news
function ValidateActu()
{
  errfound = false;
   if (!ValidLength(document.edition.synthese.value, 1)) 
   error(document.edition.synthese, "Merci de saisir une synthèse");
   else if (!ValidLength(document.edition.descriptif.value, 1)) 
   error(document.edition.descriptif, "Merci de saisir le descriptif de l'actualité");
   return !errfound;
}

// ajouter une actualité sur la homepage
function addOption(source, destination) {
  var noElementSelected;
  var isExisting;
  noElementSelected = 0;
  isExisting = 0;

  if (source.selectedIndex == -1) {
    source.options[0].selected = 1;
  }

  browsername=navigator.appName;
  if (browsername.indexOf("Netscape")!=-1) {browsername="NS"}
  else {
    if (browsername.indexOf("Microsoft")!=-1) {browsername="MSIE"}
    else {browsername="N/A"}
  };
  
  //transfert
  while (! noElementSelected)
  {
      noElementSelected = 1;
	  for (var j=0; j<source.length; j++) 
	  {
		if (source.options[j].selected) 
		{
		  noElementSelected = 0;
		  for (var i=0; i < (destination.length); i++)
		  {
			  if (source.options[j].value == destination.options[i].value) 
			  {
				isExisting = 1;
			  }
		  }
		  if (source.options[j].value == 0) 
		  {
			alert("Vous ne pouvez pas ajouter cet élément.")
			return;
		  }
		  if (isExisting == 1) 
		  {
			alert ("Vous ne pouvez pas ajouter cet élément.");
			return;
		  }
		  else 
		  {
			if (browsername=="NS") 
			{
			  var option = new Option(source.options[j].text, source.options[j].value);
			  destination.options[destination.length] = option;
			}
			else 
			{
			  var oOption = document.createElement("OPTION");
			  oOption.text=source.options[j].text;
			  oOption.value=source.options[j].value;
			  destination.options.add(oOption);
			}
		  }
		}
		if (is_nav && is_nav4){history.go(0);}
		if (source.options[j].selected) 
		{
		  //supprime
		  source.options[j] = null;
		  break;
		}
	  }//for
  }//while
}

function removeOption(currentSelect) 
{
	if (currentSelect.selectedIndex != -1) 
	{
	  currentSelect.options[currentSelect.selectedIndex] = null;
	}
	if (is_nav && is_nav4){ history.go(0);}
}

// vérifier qu'il n'y ait que 2 choix d'actu et pas plus
function CheckNbActu(list, selection) {
  //alert('ok');
  if (document.mise_avant.selection.length > 2) {
    alert("Vous ne pouvez mettre en avant que \"2\" actualités\nsur la page d'accueil du site");
    return false;
  }
  for (var j=0; j<list.length; j++) 
  {
    list.options[j].selected=1;
  }
  for (var j=0; j<selection.length; j++) 
  {
    selection.options[j].selected=1;
  }

  return true;
}

function ajaxLoader(url,id)
{	if (document.getElementById) 
			{
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();}
			if (x){
				x.onreadystatechange = function()
				{
				if (x.readyState == 4 && x.status == 200){
				el = document.getElementById(id);el.innerHTML = x.responseText;}
				}
				x.open("GET", url, true);
				x.send(null);
			}
}


