// JavaScript Document

var en_cours = false;

function ajouter_contact() {
	var obj = $('.ajouter_contact_structure').clone();
	var iId = 0;
	
	$('.ajouter_contact_structure_dupli').each( function (e) { iId++; });
	
	var strstr = obj.html() + '<table width="100%"><tr><td align="right"><a  style="color:#FF969B;" href="javascript:void(0)" onclick="delete_elc(\''+ iId +'\')">[delete]</a></td></tr></table>';
	
	obj.html(strstr);
	
	obj.attr('class', 'ajouter_contact_structure_dupli');
	obj.attr('id', 'elc_' + iId);
	obj.insertBefore("#lnk_add_contact");
}

function delete_elc(i) {
	$('#elc_' + i).remove();
}

function AjouterElement() {
	var obj = $('.FicheBPElement').clone();
	obj.attr('class', 'FicheBPElement_dupli');
	obj.insertBefore("#lnk_add_fiche");	
}

var nbEventsForm = 4;

function NextEventsForm(element) {
	for(i=1; i<=nbEventsForm; i++) {
		if ((element-1) == 1) {
			// étape obligatoire
			retour = valider_form(0);
			if (!retour) { return false; }
		}
		action = (i == element) ? 'show' : 'hide';
		
		eval("$('#Etape" + i + "')."+ action +"();");
	}
	// on remonte
	if (element > 1 && $('#img_step' + element).attr('src').match('_out')) {
		img_active = $('#img_step' + element).attr('src').substr(0, $('#img_step' + element).attr('src').length - 8) + '.jpg';
		$('#img_step' + element).attr('src', img_active);
	}
	setTimeout("remonte();", 80);
}

function remonte() {
	scrollTo(0, 0);
}

function charger_zone(lang) {
		if (lang != '') {
			if (en_cours) {
				if (confirm('Annuler la saisie en cours ??')) {
					en_cours = false;
					charger_zone(lang);
				}
			}
			else {
				$.ajax({
				   type: "POST",
				   url: "?exec=charger_parametre",
				   data: "lang_conf="+lang,
				   beforeSend : function() { $("#etatElement").empty(); $("#etatElement").append("wait...");   },
				   success: function(msg){
					    $("#etatElement").html(msg);
						//setTimeout("$('#etatElement').MultiFile();", 3000);
						$(function(){ // wait for document to load 
						 $('#z_workflow_files').MultiFile(); 
						});
						en_cours = true;
				   }
			   });
			}
		}
}

function sauver_workflow(lang) {
		id_workflow = $("#z_workflow").val();
		mots_workflow = $("#z_workflow_mots").val();
		nb_fiches_workflow = $("#z_workflow_nb_fiches").val();
		mots_query = $("#z_fiches_mots_query").val();
		mots_fiches_mots = '' ;
		var n = 0;
		// On va chercher les paramètres pour chaque fiche
		$(".getInfoFiche").each(
		function(){
				n++;
		});
		
		if (n > 0) {
			for(k=1; k<=n; k++) {
				mots_fiches_mots += $("#z_fiches_mots_" + k).val()+ '|';
			}
			mots_fiches_mots = mots_fiches_mots.substr(0, mots_fiches_mots.length-1);
			document.forms.upload_conf.submit();
		}
		
		if (id_workflow != '' && mots_workflow != '' && nb_fiches_workflow != '' && lang != '') {
			$.ajax({
			   type: "POST",
			   url: "?exec=save_workflow",
			   data: "id_workflow="+id_workflow+"&mots_query="+ mots_query +"&mots_workflow="+mots_workflow+"&nb_fiches_workflow="+nb_fiches_workflow+"&mots_fiches_mots="+mots_fiches_mots+"&lang_conf="+lang,
			   beforeSend : function() { $("#etatElement").empty(); $("#etatElement").append("wait...");   },
			   success: function(msg){
					$("#etatElement").html(msg);
					en_cours = false;
			   }
	  	   });	
		}
		
}

function ajouter_mots(element, cible) {
		selIdx = element.selectedIndex;
		newSel = element.options[selIdx].text;
		if (newSel != '') {
		str = "<a style='font-size:10px;' id='MOT_"+cible+"_"+element.value+"' href=\"javascript:supprimer_mot('"+cible+"', "+element.value+")\"><img src='"+_DIR_PLUGIN_WORKFLOW+"/img_pack/remove.png' align='absmiddle'>"+newSel+"<br/></a>";
		$("#ListChoices_" + cible).append(str);
		elvalue = ($("#"+cible).val() != '') ? ',' + element.value : element.value;
		newVal = $("#"+cible).val()  + elvalue;
		$("#"+cible).val(newVal);
		}
}

function supprimer_mot(cible, id_mot) {
	$("#MOT_"+ cible +"_"+id_mot).remove();	
	oldVal = ',' + $("#" + cible).val() + ',';
	mymot = ',' + id_mot + ',';
	newVal = oldVal.replace(mymot, '');
	newVal = newVal.substring(1, newVal.length);
	$("#" + cible).val(newVal);
}

function get_captcha() {
		$.ajax({
			   type: "POST",
			   url: "spip.php?page=captcha",
			   data: "tmp=validate",
			   success: function(msg){
				   captcha = msg;
			   }
	  	   });	
}

var captcha = "";

function valider_form(avec_captcha) {
	
	// Dans un premier temps vérifions le captcha
	// Sachant que c-1 dans les session AJX oblige
	block = false;
	get_captcha();
	captcha_img = captcha;
	//alert(captcha_img);
	$("#frm_inscription input[@type=text]").each(
		function(){
			if ($(this).val() == "" && $(this).attr('class') != 'non_obligatoire' && ($(this).attr('name')) != 'captcha') { 
				$(this).css('background', '#FFF');
				$(this).css('border-color', '#990000');
				block = true;
			}
			else {
				//eval($(this).attr('name')+"='"+$(this).val()+"';");
				// Gestion du captcha
				
				if (($(this).attr('name')) == 'captcha') {
					if ($(this).val() != captcha_img && avec_captcha==1) {
						$(this).css('background', '#FFF');
						$(this).css('border-color', '#990000');
						block = true;
					}
				}
				$(this).css('background', '#FFF');
				$(this).css('border-color', '#CCC');
			}
			
			
		}
	);
	
return (block == false);
}

function ResetRegions(element) {
	$.ajax({
			   type: "POST",
			   url: "spip.php?page=regions_jx",
			   data: "code=" + element,
			   beforeSend : function() { $("#se_regions").empty(); $("#se_regions").append("wait...");   },
			   success: function(msg){
				   $('#se_regions').html(msg);
			   }
	  	   });	
}
