// fonction de creation d'un objet "ajax" pour ie et firefox
function create_xhr_object(){
      var xhr_object=null;
      if(window.XMLHttpRequest){ // Firefox
         xhr_object = new XMLHttpRequest();
      }else if(window.ActiveXObject){ // Internet Explorer
         xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
      }else { // XMLHttpRequest non supporte par le navigateur
         alert("Ajax non supporte");
      }
      return xhr_object;
}

function PopulateFormFields(mode, id){
         var xhr_object=create_xhr_object();
         var url = "/ajax/populateForm.php?idRec="+id;

         xhr_object.onreadystatechange = function()
         {
             if(xhr_object.readyState == 4){
               if (xhr_object.responseText!=""){
                   myJsonReader(mode, xhr_object.responseText);
                   xhr_object=null;
               }
            }
         }
         xhr_object.open("GET", url, true);
         xhr_object.send(null);
    }

function myJsonReader(mode, myTable){

	switch (mode) {
	case 'CONSULT' :
		urlRss = GetData(myTable);
		switch (document.getElementById('idUnivers').value){
			case "1":
                $univers = "emploi";
                break;
            case "2":
                $univers = "formation";
                break;
            case "3":
                $univers = "entreprise-commerce";
                break;
            case "4":
                $univers = "stage";
                break;
		}
		document.rechercheEmploi.action='/'+$univers+'/liste.php';
		document.rechercheEmploi.submit();
		break;
	case 'RSS' :
		urlRss = GetData(myTable);
		switch (document.getElementById('idUnivers').value){
			case "1":
                $univers = "emploi";
                break;
            case "2":
                $univers = "formation";
                break;
            case "3":
                $univers = "entreprise-commerce";
                break;
            case "4":
                $univers = "stage";
                break;
		}

		document.rechercheEmploi.action='/'+$univers+'-rss/'+ urlRss;
		document.rechercheEmploi.submit();
		break;
	case 'MODIF' :
		urlRss = GetData(myTable);
		break;
	case 'CREER':
		// Remplissage des valeurs par d�faut
		// et les autres champs � vide
		document.rechercheEmploi.idRecherche.value = '';
		document.rechercheEmploi.rechercheAlerte.checked = 1;
		document.getElementById('idUnivers').value = 1;
		document.getElementById('radio_1').checked = 1;

	if (jQuery('#proximite_NbrKm')[0])
	{
		if ($('#NbrKm').val()>0)
		{
			$('#proximite_NbrKm').slider("moveTo", correspondancesKmIndice[$('#NbrKm').val()]);
		}
		else{

			$('#proximite_NbrKm').slider("moveTo", correspondancesKmIndice[20]);

		}
	}

		document.rechercheEmploi.NewTitre.value = '';
		document.rechercheEmploi.Quoi.value = '';
		document.rechercheEmploi.villeTrouvee_id.value = '';
		document.rechercheEmploi.villeTrouvee.value = '';
		document.rechercheEmploi.villeTrouvee_idParent.value = '';
		document.rechercheEmploi.villeTrouvee_longitude.value = '';
		document.rechercheEmploi.villeTrouvee_latitude.value = '';
		document.rechercheEmploi.Latitude.value = '';
		document.rechercheEmploi.Longitude.value = '';
		document.rechercheEmploi.NbrKm.value = 20;
		document.rechercheEmploi.idFonction.value = '';
		document.rechercheEmploi.idDiplome.value = '';
		document.rechercheEmploi.contratCDI.checked = 1;
		document.rechercheEmploi.contratCDD.checked = 1;
		document.rechercheEmploi.formationAlternance.checked = 1;
		document.rechercheEmploi.formationApprentissage.checked = 1;
		document.rechercheEmploi.formationContratPro.checked = 1;
		document.rechercheEmploi.formationNonAlternance.checked = 1;
		document.rechercheEmploi.contratCDI.value = 1;
		document.rechercheEmploi.contratCDD.value = 1;
		document.rechercheEmploi.formationAlternance.value = 1;
		document.rechercheEmploi.formationApprentissage.value = 1;
		document.rechercheEmploi.formationContratPro.value = 1;
		document.rechercheEmploi.formationNonAlternance.value = 1;
		document.getElementById('formationMode_3').checked = 1;
		document.getElementById('formationDistance_3').checked = 1;
		document.rechercheEmploi.labelPersonneHandicape.checked = 0;
		document.rechercheEmploi.labelJeuneDiplome.checked = 0;
		document.rechercheEmploi.labelDevelopDurable.checked = 0;
		document.rechercheEmploi.labelMobileEtranger.checked = 0;
		document.rechercheEmploi.dateDebutStage.value = '';
		document.rechercheEmploi.dateFinStage.value = '';
		break;
	}

}

function GetData(myTable){
	// Remplissage du formulaire
	var myData = json_parse(myTable);
	var dist;
	var urlRss;

	document.rechercheEmploi.idRecherche.value = myData.idRecherche;
	document.rechercheEmploi.NewTitre.value = myData.Libelle;
	document.rechercheEmploi.rechercheAlerte.checked = myData.Alerte;

	// on check le radio bouton correspondant Univers
	$('#radio_'+myData.idUnivers).attr("checked", "checked");

	// on check le radio bouton correspondant Mode de formation
	$('#formationMode_'+myData.formationMode).attr("checked", "checked");

	// on check le radio bouton correspondant Formation distance
	$('#formationDistance_'+myData.formationDistance).attr("checked", "checked");

	document.getElementById('idUnivers').value = myData.idUnivers;
	$('#idUnivers').change();
	document.rechercheEmploi.Quoi.value = myData.Quoi;
	document.rechercheEmploi.villeTrouvee_id.value = myData.villeTrouvee_id;
	document.rechercheEmploi.villeTrouvee.value = '';
	document.rechercheEmploi.villeTrouvee_idParent.value = '';
	document.rechercheEmploi.villeTrouvee_longitude.value = '';
	document.rechercheEmploi.villeTrouvee_latitude.value = '';

	if(myData.villeTrouvee_id){
		document.rechercheEmploi.villeTrouvee.value = myData.villeTrouvee;
		document.rechercheEmploi.villeTrouvee_idParent.value = myData.villeTrouvee_idParent;
		document.rechercheEmploi.villeTrouvee_longitude.value = myData.villeTrouvee_longitude;
		document.rechercheEmploi.villeTrouvee_latitude.value = myData.villeTrouvee_latitude;
	}

	document.rechercheEmploi.Latitude.value = myData.Latitude;
	document.rechercheEmploi.Longitude.value = myData.Longitude;
	document.rechercheEmploi.NbrKm.value = myData.NbrKm;
	document.rechercheEmploi.idFonction.value = myData.idFonction;
	document.rechercheEmploi.idDiplome.value = myData.idDiplome;

	if (myData.contratCDI==true){
		document.rechercheEmploi.contratCDI.value=1;
	}
	if (myData.contratCDD==true){
		document.rechercheEmploi.contratCDD.value=1;
	}
	if (myData.formationAlternance==true){
		document.rechercheEmploi.formationAlternance.value=1;
	}
	if (myData.formationApprentissage==true){
		document.rechercheEmploi.formationApprentissage.value=1;
	}
	if (myData.formationContratPro==true){
		document.rechercheEmploi.formationContratPro.value=1;
	}
	if (myData.formationNonAlternance==true){
		document.rechercheEmploi.formationNonAlternance.value=1;
	}
	if (myData.contratCDI==true){
		document.rechercheEmploi.contratCDI.value=1;
	}
	if (myData.labelPersonneHandicape==true){
		document.rechercheEmploi.labelPersonneHandicape.value=1;
	}
	if (myData.labelJeuneDiplome==true){
		document.rechercheEmploi.labelJeuneDiplome.value=1;
	}
	if (myData.labelDevelopDurable==true){
		document.rechercheEmploi.labelDevelopDurable.value=1;
	}
	if (myData.labelMobileEtranger==true){
		document.rechercheEmploi.labelMobileEtranger.value=1;
	}
	document.rechercheEmploi.contratCDI.checked = myData.contratCDI;
	document.rechercheEmploi.contratCDD.checked = myData.contratCDD;
	document.rechercheEmploi.formationAlternance.checked = myData.formationAlternance;
	document.rechercheEmploi.formationApprentissage.checked = myData.formationApprentissage;
	document.rechercheEmploi.formationContratPro.checked = myData.formationContratPro;
	document.rechercheEmploi.formationNonAlternance.checked = myData.formationNonAlternance;
	document.rechercheEmploi.labelPersonneHandicape.checked = myData.labelPersonneHandicape;
	document.rechercheEmploi.labelJeuneDiplome.checked = myData.labelJeuneDiplome;
	document.rechercheEmploi.labelDevelopDurable.checked = myData.labelDevelopDurable;
	document.rechercheEmploi.labelMobileEtranger.checked = myData.labelMobileEtranger;

	document.rechercheEmploi.idFonctionEmploiStage.value = myData.idFonction;
	document.rechercheEmploi.idFonctionFormation.value = myData.idFonction;
	document.rechercheEmploi.idFonctionEntreprise.value = myData.idFonction;
	
    if(checkboxTypeFormation)
      checkboxTypeFormation(jQuery("#formationAlternance"));

	if (jQuery('#proximite_NbrKm')[0])
	{
		if ($('#NbrKm').val()>0)
		{
			$('#proximite_NbrKm').slider("moveTo", correspondancesKmIndice[$('#NbrKm').val()]);
		}
		else{

			$('#proximite_NbrKm').slider("moveTo", correspondancesKmIndice[20]);

		}
	}

	if(myData.NbrKm!='')
	{
		switch(myData.NbrKm){
			case 0 :
				dist = 0;
				break;
			case 5 :
				dist = 1;
				break;

			case 10 :
				dist = 2;
				break;

			case 20 :
				dist = 3;
				break;

			case 50 :
				dist = 4;
				break;

			case 'plus' :
				dist = 5;
				break;
		}

		document.rechercheEmploi.dateDebutStage.value = myData.dateDebutStage;
		document.rechercheEmploi.dateFinStage.value = myData.dateFinStage;
		
		return urlRss = myData.fonction_encode+'/'+myData.ville_encode+'/';
	}
}

function checkParam(id)
{
	if(document.getElementById(id).checked)
	{
		document.getElementById(id).value=1;
	}
	else
	{
		document.getElementById(id).value="";
	}

}
