// JavaScript Document
function getHTTPObject(data_cut)
{
  var xmlhttp = false;
  //
  /* on essaie de crÚer l'objet si ce n'est pas dÚjÓ fait */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
     try
     {
        xmlhttp = new XMLHttpRequest();
     }
     catch (e)
     {
        xmlhttp = false;
     }
  }else{
    if (window.ActiveXObject) { 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
  }
  //alert(xmlhttp)
  if (xmlhttp)
  {
     /* on dÚfinit ce qui doit se passer quand la page rÚpondra */
     xmlhttp.onreadystatechange=function()
     {
        //alert(xmlhttp.readyState + " readyState");
        if (xmlhttp.readyState == 4) /* 4 : Útat "complete" */
        {
           //alert(xmlhttp.responseText + " responseText");
           if (xmlhttp.status == 200) /* 200 : code HTTP pour OK */
           {
              if(xmlhttp.responseText=="okmail"){
                obj=document.getElementById('formemail');
                obj.email.disabled=false;
                obj.email.value="Email";
                alert("Inscription prise en compte");
              }else if(xmlhttp.responseText=="okforgotmail"){
                //ici
                ///$("#fancybox-inner").find("#emailforgot").attr("disabled","false");
                $("#fancybox-inner").find("#emailforgot").attr("disabled","");
                void($.fancybox.close());
              }else if(xmlhttp.responseText=="erreurforgotmail"){
                //ici
                
                $("#fancybox-inner").find("#emailforgot").attr("disabled","");
                $("#fancybox-inner").find("#errormsg").css("display","block");
              }else if(xmlhttp.responseText=="oklogin"){
                //window.location.reload();
                window.location.href=urlpro;
              }else if(xmlhttp.responseText=="errorlogin"){
                alert('Identifiants non valides');
                obj=document.getElementById('formlogin');
                obj.login.value='';
                obj.login.disabled=false;
                obj.mdp.value='';
                obj.mdp.disabled=false;
                obj.login.focus();
              }else{
                if(contact){
                  //alert(xmlhttp.responseText + " responseText");
                  contact=false;
                  $("#divformcontact").css("display","none");
                  $("#divfromcontactresult").html(xmlhttp.responseText);
                  $("#divfromcontactresult").css("display","block");
                  $(".returnsondage").css("display","block");
                }else{
                  tabresult=xmlhttp.responseText.split('|');
                  //remplacement des valeurs
                  if($('#resultval1')!=null){
                    $('#resultval1').find('.result').html(tabresult[0]+'%');
                    $('#resultval1').find('.fond').find('div').css('width',tabresult[0]*205/100);
                  }
                  if($('#resultval2')!=null){
                    $('#resultval2').find('.result').html(tabresult[1]+'%');
                    $('#resultval2').find('.fond').find('div').css('width',tabresult[1]*205/100);
                  }
                  if($('#resultval3')!=null){
                    $('#resultval3').find('.result').html(tabresult[2]+'%');
                    $('#resultval3').find('.fond').find('div').css('width',tabresult[2]*205/100);
                  }
                  if($('#resultval4')!=null){
                    $('#resultval4').find('.result').html(tabresult[3]+'%');
                    $('#resultval4').find('.fond').find('div').css('width',tabresult[3]*205/100);
                  }
                  if(tabresult[4]){
                    document.getElementById('sondageok').style.display='block';
                  }else{
                    document.getElementById('sondagepasok').style.display='block';
                  }
                  document.getElementById('sondagequestion').style.display='none';
                  document.getElementById('sondagereponse').style.display='block';
                  /*
                  if(xmlhttp.responseText=="erreurlogin"){
                    alert('Identifiant/Mot de passe non valide');
                  }
                  if(xmlhttp.responseText=="ok"){
                    window.location.reload();
                  }
                  if(xmlhttp.responseText=="erreurdeja"){
                    alert('Vous avez dÚjÓ participÚ au quizz');
                  }*/
                }
              }
           }
        }
     }
  }
  return xmlhttp;
}
function validateComment(obj){
  if(obj.commentaire.value==''){
    alert('Veuillez préciser votre commentaire');
    obj.commentaire.focus();
    return false;
  }
  return true;
}
function testlogin(obj,racine){
  if(obj.login.value==''||obj.login.value=='Nom de l\'utilisateur'){
    alert('Veuillez indiquer votre identifiant');
    obj.login.focus();
    return false;
  }
  if(obj.mdp.value==''||obj.mdp.value=='Mot de passe'){
    alert('Veuillez indiquer votre mot de passe');
    obj.mdp.focus();
    return false;
  }
  obj.login.disabled=true;
  obj.mdp.disabled=true;
  //alert("xmlhttp=1&login="+URLEncode(obj.login.value)+"&mdp="+URLEncode(obj.mdp.value))
  sendData(
		'POST',
		racine+'/tpl/testlogin.php',
		'xmlhttp=1&login='+URLEncode(obj.login.value)+"&mdp="+URLEncode(obj.mdp.value));
	return false;
}
function validateSondage(obj,racine){
  sondageval=obj.sondageval.value
  
  if(getCook("VOTE"+sondageval)=="1"){
    alert('Vous avez déja voté pour cette question');
  }else{
    setCook("VOTE"+sondageval,"1");
  
    //alert(racine+'/tpl/testlogin.php?xmlhttp=1&login='+obj.login.value+'&mdp='+obj.mdp.value+'&type='+indice)
    for(i=0;i<obj.sondage.length;i++){
      if(obj.sondage[i].checked){
        break;
      }
    }
    obj.valider.disabled=true;
    //alert(racine+'/tpl/savesondage.php')
    sendData(
		'POST',
		racine+'/tpl/savesondage.php',
		'xmlhttp=1&reponse='+(i+1)+"&content_id="+sondageval);
	}
  return false;
}
function setCook(nom,valeur) {
  var expDate = new Date()
  expDate.setTime(expDate.getTime() + (365 * 24 * 3600 * 1000))
  document.cookie = nom + "=" + escape(valeur)
      + ";expires=" + expDate.toGMTString()
}
function setCookDay(nom,valeur) {
  var expDate = new Date()
  expDate.setTime(expDate.getTime() + (24 * 3600 * 1000))
  document.cookie = nom + "=" + escape(valeur)
      + ";expires=" + expDate.toGMTString()
}
function getCook(nom) {
  deb = document.cookie.indexOf(nom + "=")
  if (deb >= 0) {
    deb += nom.length + 1
    fin = document.cookie.indexOf(";",deb)
    if (fin < 0) fin = document.cookie.length
    return unescape(document.cookie.substring(deb,fin))
  }
  return "";
}
function sendData(method, url, data){
  //alert("ici");
	data_cut = data.substring(10, data.length);
	//alert(data);
	//alert(data_cut);
	var xmlhttp = getHTTPObject(data_cut);
	//alert(xmlhttp)
	if (!xmlhttp)
	{
	return false;
	}
	
	if(method == "GET")
	{
	if(data == 'null')
	{
	xmlhttp.open("GET", url, true); //ouverture asynchrone
	}
	else
	{
	xmlhttp.open("GET", url+"?"+data, true);
	}
	xmlhttp.send(null);
	}
	else if(method == "POST")
	{
	xmlhttp.open("POST", url, true); //ouverture asynchrone
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.send(data);
	}
	return true;
}
function validateCustom(obj,racine){
    listentreprise="";
    for(i=0;i<obj.elements["entreprise_id[]"].length;i++){
      listentreprise+=obj.elements["entreprise_id[]"][i].value+",";
    }
    listcas="";
    for(i=0;i<obj.elements["cas_id[]"].length;i++){
      listcas+=obj.elements["cas_id[]"][i].value+",";
    }
    listtag="";
    for(i=0;i<obj.elements["tag_id[]"].length;i++){
      listtag+=obj.elements["tag_id[]"][i].value+",";
    }
    /*
    obj.reinitialiser.disabled=true;
    obj.valider.disabled=true;
    */
    for(i=0;i<obj.elements.length;i++){
      obj.elements[i].disabled=true
    }
    /*
    alert(listentreprise)
    alert(listtag)
    */
    //alert('xmlhttp=1&actif='+obj.actif.checked+'&retraite='+obj.retraite.checked+'&salarie='+obj.salarie.checked+'&maitrise='+obj.maitrise.checked+'&cadre='+obj.cadre.checked+'&listentreprise='+listentreprise+'&listtag='+listtag)
    sendData(
		'POST',
		racine+'/tpl/savecustom.php',
		'xmlhttp=1&actif='+((obj.actif.checked)?1:0)+'&retraite='+((obj.retraite.checked)?1:0)+'&salarie='+((obj.salarie.checked)?1:0)+'&maitrise='+((obj.maitrise.checked)?1:0)+'&cadre='+((obj.cadre.checked)?1:0)+'&listentreprise='+listentreprise+'&listtag='+listtag+'&listcas='+listcas);
    return false; 
}
function lauchsearch(obj){
  if(obj.query.value=='Rechercher'||obj.query.value==''){
    alert('Veuillez indiquer votre recherche');
    obj.query.focus();
    return false;
  }
  return true;
}
function estuneadressemail(monemail)
{
/*
flag=false;
	if(monemail!="")
	{
	longueur=monemail.substring(monemail.lastIndexOf("."),monemail.length).length
		if(monemail.indexOf("@")!=-1&&(longueur==3||longueur==4))
		{
			if(monemail.indexOf("@")<monemail.lastIndexOf(".")+1)
				flag= true;
		}
	}
return flag;
*/
var email = monemail;
var verif = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9-]{2,}[.][a-zA-Z]{2,4}$/
if (verif.exec(email) == null){
  return false;
}else{
  return true;
}
}
function news(obj,racine){
  if(!estuneadressemail(obj.email.value)){
    alert('Veuillez indiquer votre email');
    obj.email.focus();
    return false;
  }
  obj.email.disabled=true
   sendData(
		'POST',
		racine+'/tpl/saveemail.php',
		'xmlhttp=1&email='+obj.email.value);
  return false;
}
function validateCandidature(obj){
  if(obj.nom.value==''){
    alert('Veuillez indiquer votre nom');
    obj.nom.focus();
    return false;
  } 
  if(obj.prenom.value==''){
    alert('Veuillez indiquer votre prénom');
    obj.prenom.focus();
    return false;
  }
  if(obj.email.value==''){
    alert('Veuillez indiquer votre email');
    obj.email.focus();
    return false;
  }
  if(obj.fichier.value==''){
    alert('Veuillez indiquer votre CV');
    obj.fichier.focus();
    return false;
  }
  return true;
}
function validateJob(obj){
  if(obj.nom.value==''){
    alert('Veuillez indiquer votre nom');
    obj.nom.focus();
    return false;
  }
  if(obj.prenom.value==''){
    alert('Veuillez indiquer votre prénom');
    obj.prenom.focus();
    return false;
  }
  if(obj.email.value==''){
    alert('Veuillez indiquer votre email');
    obj.email.focus();
    return false;
  }
  if(obj.tel_pro.value==''){
    alert('Veuillez indiquer votre téléphone');
    obj.tel.focus();
    return false;
  }
  if(obj.poste.value==''){
    alert('Veuillez indiquer le poste');
    obj.poste.focus();
    return false;
  }
  if(obj.date_debut.value==''){
    alert('Veuillez indiquer la date de début');
    obj.date_debut.focus();
    return false;
  }
  if(obj.date_fin.value==''){
    alert('Veuillez indiquer la date de fin');
    obj.date_fin.focus();
    return false;
  }
  if(obj.entreprise_id.value==''){
    alert('Veuillez indiquer l\'entreprise');
    obj.entreprise_id.focus();
    return false;
  }
  if(obj.etablissement_id.value==''){
    alert('Veuillez indiquer l\établissement');
    obj.etablissement_id.focus();
    return false;
  }
  if(obj.classe_id.value==''){
    alert('Veuillez indiquer la plage M3E');
    obj.classe_id.focus();
    return false;
  }
  if(obj.niveau_id.value==''){
    alert('Veuillez indiquer le GF');
    obj.niveau_id.focus();
    return false;
  }
  if(obj.commune.value==''){
    alert('Veuillez indiquer la ville');
    obj.commune.focus();
    return false;
  }
  if(obj.description.value==''){
    alert('Veuillez indiquer la description');
    obj.description.focus();
    return false;
  }
  return true; 
}
var contact=false;
function validateContact(obj,racine){
  contact=true;
  sendData(
		'GET',
		racine+'/tpl/viewcontact.php',
		'xmlhttp=1&idterritoire='+obj.idterritoire.value+"&metiers_id="+obj.idregion.value);
		//alert('ici');
	return false;  
}
function retourcontact(){
    $("#divformcontact").css("display","block");
    $("#divfromcontactresult").css("display","none");
    $(".returnsondage").css("display","none");
  };
function validateMdp(obj,racine){
  if(obj.emailforgot.value==""){
    alert("Veuillez indiquer votre email")
    obj.emailforgot.focus();
    return false;
  }
  obj.emailforgot.disabled=true;
  //alert($("#fancybox-inner").find("#formforgotmail"))
  //alert($("#fancybox-inner").find("#formforgotmail").html())
 
  sendData(
		'POST',
		racine+'/tpl/sendmdp.php',
		'xmlhttp=1&email='+obj.emailforgot.value);

	return false;  
}
function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}
