/* ################################################ */
/* SCRIPTS EN RAPPORT AVEC LES FONCTIONNALITES AJAX */
/* Auteur : Yann VOLEAU (Edenweb)                   */
/* ################################################ */



/************************************************//*
//  AJAX LISTING GENERAL
/************************************************/

//Affichage d'une liste de valeurs donnée
//name : le nom de la liste
//type : choix unqiue ou multiple dans la liste
//commentaire : paramètre d'options
function afficheListing(name,type,like){

	startPatienter();
	
	var options="";	
	
	
	var titre="Liste ";
	
	var width=420;
	var height=400;
	
	if (name=="diplome"){
		titre+="des certifications";
		width=600;
	}
	if (name=="niveau_sortie"){
		titre+="des niveaux de sortie";
		height=310;
	}
	if (name=="mesures"){
		titre+="des dispositifs de formation";
		width=640;
		height=380;
	}
	if (name=="bassin_emploi"){
		titre+="des bassins d'emploi";
		height=410;
	}
	
	if (name=="formacodes"){
		titre+="des domaines de formation";
		width=600;
	}
	
	if (name=="gd_domaine"){
		titre+="des grands domaines";
	}
	
	if (name=='diplome'){
		var ids_niveau_sortie = new Array();
		$("#form_recherche_avancee :input[name='niveau_sortie_id[]']").each(
			function(){
				ids_niveau_sortie.push($(this).val());
			}
		);
		if (ids_niveau_sortie.length>0)options+="&filtre="+ids_niveau_sortie;
	}
	
	if (name=='bassin_emploi'){
		var ids_depts = new Array();
		
		if (document.getElementById('geo_calvados').checked==true)
			ids_depts.push('14');
		if (document.getElementById('geo_manche').checked==true)
			ids_depts.push('50');
		if (document.getElementById('geo_orne').checked==true)
			ids_depts.push('61');
						
		if (ids_depts.length>0)options+="&filtre="+ids_depts;
	}
	
	
	
	var parameters = "type="+type+"&requete="+name+"&like="+escape(like)+options;
	
	
	getJsDialog(name,height,width,titre);
	
	
	
	$.ajax({
		   type: "POST",
		   url: "/ajax_listing",
		   data: parameters,
		   success: function(html){
				
				$("#listing_"+name).html(html);	
				
				if (name=="formacodes")chargeFormacodes(0);
								
				chargeListing(name);
				stopPatienter();
				
				$("#listing_"+name).dialog('open');
				
		   },
		   error:  function(request,text){
			   ajaxError(request,text);
		   }
		 });
	
}


function getJsDialog(table,diagHeight,diagWidth,titre){
	
	$("#listing_"+table).dialog({
		autoOpen: false ,
		buttons: { 
			"Fermer": function() { $(this).dialog("close"); }, 
			"Envoyer": function() { envoyerListing(table); }
		},
		dialogClass: "dialog_listing",
		height : diagHeight,
		width : diagWidth,
		minWidth : 300,
		minHeight : 150,
		resizable : false,
		title: titre
	});
	
}


function envoyerListing(table){
	
	startPatienter();
	
	var inputs=$("input[name='list_"+table+"[]']");
	var ids=new Array();
	
	for (var i=0 ; i < inputs.length ; i++){
		if (inputs[i].checked)ids.push(inputs[i].value);
	}
	
	if ($("#text_affinage_"+table).length){
		var hiddens=$("input[name='"+table+"_id[]']");
		for (var j=0 ; j < hiddens.length ; j++){
			var found=false;
			for (var i=0 ; i < inputs.length ; i++){
				if (hiddens[j].value == inputs[i].value) found = true;
			}
			if(found == false) ids.push(hiddens[j].value);
		}
	}
	
	if (ids.length > 0){
		affichageElements(ids,table);
	}else{
		supprAllValues(table);
	}
	$("#listing_"+table).dialog("close");
		
}



function affichageElements(ids,table){
	$.ajax({
	   type: "POST",
	   url: "/ajax",
	   data: "action=creationElementAffichage&name="+table+"&ids="+ids,
	   success: function(html){
			$("#selected_values_"+table).html(html);
			$(".btn_suppr_value").ToolTip({
				className: 'tooltips',
				position: 'mouse',
				delay: 200
			}); 
			$(".btn_suppr_all_values").ToolTip({
				className: 'tooltips_warning',
				position: 'mouse',
				delay: 200
			});
			calculReponses();
		},
	   error:  function(request,text){
			stopPatienter();
			ajaxError(request,text);
	   }
	 });
}

function supprValue(table,id){
	$("#div_value_"+table+"_"+id+" *").blur();
	$("#div_value_"+table+"_"+id).remove();
	if ($("#selected_values_"+table).children("div").length == 1){
		supprAllValues(table);
	}else{
		calculReponses();
	}
}


function supprAllValues(table){
	$("#selected_values_"+table+" *").blur();
	$("#selected_values_"+table).html("");
	calculReponses();

}


//fonction de rechargement du listing (cochage des éléments déjà ajoutés)
function chargeListing(table){
	
	var hiddens=$("input[name='"+table+"_id[]']");
	var inputs=$("input[name='list_"+table+"[]']");
	
	for (var i=0 ; i < inputs.length ; i++){
		for (var j=0 ; j < hiddens.length ; j++){
			if (inputs[i].value == hiddens[j].value) inputs[i].checked = true;
		}
	}
	
}




/************************************************/
/* */
/*  AJAX LISTING FORMACODES */
/* */
/************************************************/
//chargement de l'arborescence des formacodes
function afficheArboFormacodes(){
	chargeFormacodes('0');
}

//rechargement de l'arborescence des formacodes
function rechargeArboFormacode(){
	document.getElementById('childs_code_0').innerHTML = '';
	document.getElementById('champ_affine_formacode').value="";
	chargeFormacodes('0');
}

//vérification des conditions à remplir pour faire une recherche sur les formacodes
function checkAffinageFormacode(input){
	var nb_mini=3;
	if(input.value.indexOf('%')!=-1)nb_mini+=1;
	if (input.value.length<nb_mini){
		alert('Votre recherche doit porter sur au moins 3 caractères');
	}else{
		chargeRechercheFormacodes(input.value);
	}
}


//chargement de l'arborescence soujacente à un code_pere
function chargeFormacodes(code_pere){
	
	startPatienter();
	
	$.ajax({
	   type: "POST",
	   url: "/ajax",
	   data: "action=chargeFormacode&id="+code_pere,
	   success: function(html){

			if ($("#childs_code_"+code_pere).children().length==0){
				
				$("#childs_code_"+code_pere).html(html);
			
				if ($("#img_"+code_pere).length) $("#img_"+code_pere).attr("src","/img/bt_moins.gif");
				
			}else{
				if ($("#childs_code_"+code_pere).length){
									
					if(document.getElementById('childs_code_'+code_pere).style.display=='block'){
						document.getElementById('childs_code_'+code_pere).style.display='none';
						if (document.getElementById('img_'+code_pere))document.getElementById('img_'+code_pere).setAttribute("src","/img/bt_plus.gif");
					}else{
						document.getElementById('childs_code_'+code_pere).style.display='block';
						if (document.getElementById('img_'+code_pere))document.getElementById('img_'+code_pere).setAttribute("src","/img/bt_moins.gif");
					}
				}
			}
			chargeListing('formacodes');
			
			stopPatienter();
			
	   },
	   error:  function(request,text){
		   ajaxError(request,text);
		   stopPatienter();
	   }
	});	
	
}


function calculReponsesFullText(mode){
	
	startPatienter();
	
	var text = $("#input_full_text").val();
	var ou = $("#input_full_text_ou").val();
	
	if (mode=='tous'){
		while (text.indexOf('"')!=-1)
			text = text.replace('"','');
		$("#input_full_text").val(text);
	}
	
	if (mode=='exacte'){
		while (text.indexOf('"')!=-1)
			text = text.replace('"','');
		text='"'+text+'"';
		$("#input_full_text").val(text);
	}
	
	$('#full_text').val(text);
	$('#full_text_ou').val(ou);
		
	$.ajax({
	   type: "POST",
	   url: "/ajax",
	   data: "action=calculReponsesFullText&text="+escape(text)+"&ou="+escape(ou),
	   success: function(html){
			$('#nb_resultats').html(html);
			if ($('#form_recherche_avancee').length==0 )
				$('#bloc_corps').html("");
			$('.ac_results').css('display','none');
			stopPatienter();
			goBlink();	
		},
	   error:  function(request,text){
			stopPatienter();
			ajaxError(request,text);
	   }
	 });
		
}

function calculReponses(){
	startPatienter();
		
	var ids_diplome = new Array();
	var ids_formacode = new Array();
	var ids_gd_domaine = new Array();
	var ids_niveau_sortie = new Array();
	var ids_bassin_emploi = new Array();
	var ids_mesures = new Array();
	var foad = false;
	var geo_calvados = false;
	var geo_manche = false;
	var geo_orne = false;
	var texte="";
	var type_form="";
	
	
	$(":input[name='niveau_sortie_id[]']").each(function(){
			ids_niveau_sortie.push($(this).val()); });
	$(":input[name='diplome_id[]']").each( function(){
			ids_diplome.push($(this).val()); });
	$(":input[name='formacodes_id[]']").each(function(){
		ids_formacode.push($(this).val()); });
	$(":input[name='gd_domaine_id[]']").each(function(){
		ids_gd_domaine.push($(this).val()); });
	$(":input[name='bassin_emploi_id[]']").each(function(){
			ids_bassin_emploi.push($(this).val()); });
	$(":input[name='mesures_id[]']").each(function(){
			ids_mesures.push($(this).val()); });
	
	
	if ( $(":input[name='geo_calvados']:checked").val() != null ) geo_calvados = true;
	if ( $(":input[name='geo_manche']:checked").val() != null ) geo_manche = true;
	if ( $(":input[name='geo_orne']:checked").val() != null ) geo_orne = true;
	texte =  $(":input[name='full_text']").val();
	type_form = $(":input[name='type_formation']:checked").val();
	foad =  $(":input[name='foad']:checked").val();
	
	$.ajax({
	   type: "POST",
	   url: "/ajax",
	   data: 	"action=calculReponses"+ 
				"&full_text="+ escape(texte) + 
				"&type_form="+ type_form + 
				"&ids_diplome=" + ids_diplome + 
				"&ids_formacode=" + ids_formacode + 
				"&ids_gd_domaine=" + ids_gd_domaine + 
				"&ids_niveau_sortie="+ids_niveau_sortie+ 
				"&foad="+foad+ 
				"&geo_calvados="+geo_calvados+ 
				"&geo_manche="+geo_manche+ 
				"&geo_orne="+geo_orne+ 
				"&ids_bassin_emploi="+ids_bassin_emploi+ 
				"&ids_mesures="+ids_mesures,
	   success: function(html){
			$('#nb_resultats').html(html);
			//razRechFullText();
			$('.ac_results').css('display','none');
			stopPatienter();
			goBlink();	
		},
	   error:  function(request,text){
			stopPatienter();
			ajaxError(request,text);
	   }
	 });
	
		
}

function goBlink(){
	
	blink('nb_resultats',4,400);
	
}


function ajaxError(request,text){
	alert("Erreur d'appel des données : "+text);
}

