$(document).ready(function(e) {
	$('a.saveRecherche').bind('click', getComboRecherche);
	$('a.changeRecherche').bind('click', function() {
		$('#formulaireRecherche').submit();
	});
});

/**
 * Crée la recherche en base
 */
function ajaxCreerRecherche() 
{
	if (verifRecherche()) 
	{	
		var Form;
		
		// si l'on vient du formulaire espace perso
		if ($('#origineFormulaire').val() == "RECH")
		{
			Form = 'rechercheEmploi'

			// Cas d'une modification
			if ($('#idRecherche').val() != "")
			{
				var idRecherche = $('#idRecherche').val();
				var titre = $('#NewTitre').val();
			}
			else
			{
				var idRecherche = "-1";
				var titre = $('#NewTitre').val();
			}
				
			var contratCDI = $('#'+Form).find("input[name=contratCDI]").val();
			var contratCDD = $('#'+Form).find("input[name=contratCDD]").val();
			var formationAlternance = $('#'+Form).find("input[name=formationAlternance]").val();
			var formationApprentissage = $('#'+Form).find("input[name=formationApprentissage]").val();
			var formationContratPro = $('#'+Form).find("input[name=formationContratPro]").val();
			var formationNonAlternance = $('#'+Form).find("input[name=formationNonAlternance]").val();
			var labelPersonneHandicape = $('#'+Form).find("input[name=labelPersonneHandicape]").val();
			var labelJeuneDiplome = $('#'+Form).find("input[name=labelJeuneDiplome]").val();
			var labelDevelopDurable = $('#'+Form).find("input[name=labelDevelopDurable]").val();
			var labelMobileEtranger = $('#'+Form).find("input[name=labelMobileEtranger]").val();
			var dateDebutStage = $('#'+Form).find("input[name=dateDebutStage]").val();
			var dateFinStage = $('#'+Form).find("input[name=dateFinStage]").val();
			
			// On récupère la valeur du bouton radio check
			var formationMode = $('#'+Form).find("input[name=formationMode]:checked").val();
			var formationDistance = $('#'+Form).find("input[name=formationDistance]:checked").val();
			
			// On récupère la valeur d'un select
			var idDiplome = $('#'+Form).find("select[name=idDiplome]").val();			
						
			switch($('#'+Form).find("input[name=idUnivers]").val())
			{
				case '1':
					// Emploi
					formationMode='';
					formationNonAlternance='';
					formationDistance='';
					dateDebutStage = '';
					dateFinStage = '';
					break;
				case '2':
					// Formation
					contratCDI='';
					contratCDD='';
					labelJeuneDiplome='';
					labelDevelopDurable='';
					labelMobileEtranger='';
					dateDebutStage = '';
					dateFinStage = '';
					break;
				case '3':
					// Stage
					contratCDI = '';
					contratCDD = '';
					formationAlternance = '';
					formationApprentissage = '';
					formationContratPro = '';
					formationNonAlternance = '';
					labelPersonneHandicape = '';
					labelJeuneDiplome = '';
					labelDevelopDurable = '';
					labelMobileEtranger = '';
					break;
				case '4':
					// Ent & Comm
					contratCDI = '';
					contratCDD = '';
					formationAlternance = '';
					formationApprentissage = '';
					formationContratPro = '';
					formationNonAlternance = '';
					labelPersonneHandicape = '';
					labelJeuneDiplome = '';
					labelDevelopDurable = '';
					labelMobileEtranger = '';
					dateDebutStage = '';
					dateFinStage = '';
					break;
			}				
	
		} 
		else 
		{
			Form = 'formulaireRecherche'
			// titre
			if ($('#rechercheNewTitre').val() != "") {
				var titre = $('#rechercheNewTitre').val();
				var idRecherche = "-1";
			} else {
				var titre = $('#listeRecherche option:selected').html();
				var idRecherche = $('#listeRecherche').val();
			}
			// On récupère la valeur du champ
			var contratCDI = $('#'+Form).find("input[name=contratCDI]").val();
			var contratCDD = $('#'+Form).find("input[name=contratCDD]").val();
			var formationAlternance = $('#'+Form).find("input[name=formationAlternance]").val();
			var formationApprentissage = $('#'+Form).find("input[name=formationApprentissage]").val();
			var formationContratPro = $('#'+Form).find("input[name=formationContratPro]").val();
			var formationNonAlternance = $('#'+Form).find("input[name=formationNonAlternance]").val();
			var labelPersonneHandicape = $('#'+Form).find("input[name=labelPersonneHandicape]").val();
			var labelJeuneDiplome = $('#'+Form).find("input[name=labelJeuneDiplome]").val();
			var labelDevelopDurable = $('#'+Form).find("input[name=labelDevelopDurable]").val();
			var labelMobileEtranger = $('#'+Form).find("input[name=labelMobileEtranger]").val();
			var formationMode = $('#'+Form).find("input[name=formationMode]").val();
			var formationDistance = $('#'+Form).find("input[name=formationDistance]").val();
			// On récupère la valeur d'un input
			var idDiplome = $('#'+Form).find("input[name=idDiplome]").val();
			var dateDebutStage = '';
			var dateFinStage = '';
		}
		// alerte
		if ($('#rechercheAlerte')[0].checked) {
			var alerte = 1;
		} else {
			var alerte = 0;
		}
	
		/*alert('url à lancer : /ajax/ajaxRecherche.php?action=save&idRecherche='+idRecherche+'&titre ='+ titre+'&alerte='+ alerte+
'&idUnivers ='+ $('#'+Form).find("input[name=idUnivers]").val()+
'&idLieu ='+ $('#'+Form).find("input[name=villeTrouvee_id]").val()+
'&Quoi ='+ $('#'+Form).find("input[name=Quoi]").val()+
'&NbrKm ='+ $('#'+Form).find("input[name=NbrKm]").val()+
'&idFonction ='+ $('#'+Form).find("input[name=idFonction]").val()+
'&idDiplome ='+ idDiplome+
'&contratCDI ='+ $('#'+Form).find("input[name=contratCDI]").val()+
'&contratCDD ='+ $('#'+Form).find("input[name=contratCDD]").val()+
'&formationAlternance ='+ $('#'+Form).find("input[name=formationAlternance]").val()+
'&formationApprentissage ='+ $('#'+Form).find("input[name=formationApprentissage]").val()+
'&formationContratPro='+ $('#'+Form).find("input[name=formationContratPro]").val()+
'&formationMode ='+formationMode+
'&formationDistance ='+ formationDistance+
'&labelPersonneHandicape ='+ $('#'+Form).find("input[name=labelPersonneHandicape]").val()+
'&labelJeuneDiplome ='+ $('#'+Form).find("input[name=labelJeuneDiplome]").val()+
'&labelDevelopDurable ='+ $('#'+Form).find("input[name=labelDevelopDurable]").val()+
'&labelMobileEtranger='+ $('#'+Form).find("input[name=labelMobileEtranger]").val()+
'&dateDebutStage='+ $('#'+Form).find("input[name=dateDebutStage]").val()+
'&dateFinStage='+ $('#'+Form).find("input[name=dateFinStage]").val());*/

		$.get("/ajax/ajaxRecherche.php", {
			action : "save",
			idRecherche : idRecherche,
			titre : titre,
			alerte : alerte,
			idUnivers : $('#'+Form).find("input[name=idUnivers]").val(),
			idLieu : $('#'+Form).find("input[name=villeTrouvee_id]").val(),
			Quoi : $('#'+Form).find("input[name=Quoi]").val(),
			NbrKm : $('#'+Form).find("input[name=NbrKm]").val(),
			idFonction : $('#'+Form).find("input[name=idFonction]").val(),
			idDiplome : idDiplome,
			contratCDI : contratCDI,
			contratCDD : contratCDD,
			formationAlternance : formationAlternance,
			formationApprentissage : formationApprentissage,
			formationContratPro : formationContratPro,
			formationNonAlternance : formationNonAlternance,
			formationMode : formationMode,
			formationDistance : formationDistance,
			labelPersonneHandicape : labelPersonneHandicape,
			labelJeuneDiplome : labelJeuneDiplome,
			labelDevelopDurable : labelDevelopDurable,
			labelMobileEtranger : labelMobileEtranger,
			dateDebutStage : dateDebutStage,
			dateFinStage : dateFinStage
		}, function(data) {
			if (data != "") {
				alert("Un probleme est survenu lors de l'enregistrement de votre recherche \n\n"
						+ "Notez que votre libellé doit être unique.");
			} else {
				alert("votre recherche a bien été enregistrée");
				if ($('#origineFormulaire').val() == "RECH") {
					location.reload();
				}
				tb_remove();
			}
		});
	}
}

/**
 * Récupère les titres des recherches et complète la combobox
 */
function getComboRecherche() {
	$.getJSON("/ajax/ajaxRecherche.php", {
		action : "getlibelle"
	}, function(data) {
		var listeRecherche;
		$('#listeRecherche').empty();

		$.each(data.recherches, function(i, recherche) {
			monOption = document.createElement('option');
			$(monOption).attr("value", recherche.RCH_id);
			$(monOption).html(recherche.RCH_libelle);
			$('#listeRecherche').append($(monOption));
		});
	});
}

/**
 * Ajoute la balise input pour saisir le nouveau titre
 */
function makeInputRecherche() {
	if ($('#listeRecherche').val() == "-1") {
		if (!$('#rechercheNewTitre')[0]) {
			monInput = document.createElement('input');
			$(monInput).attr("type", "text");
			$(monInput).attr("id", "rechercheNewTitre");
			$('#listeRecherche').before($(monInput));
		}
	} else {
		if ($('#rechercheNewTitre')[0]) {
			$('#rechercheNewTitre').remove();
		}
	}
}

/**
 * Vérifie que le titre de la recherche est valide
 */
function verifRecherche() {
	retour = true;
	if (($('#listeRecherche').val() == "-1" && $('#rechercheNewTitre').val() == "")
			|| $('#listeRecherche').val() == "") {
		alert("veuillez choisir un titre.");
		retour = false;
	}
	return retour;
}