//---------------------------------------
function bpf_stat_ecrire(action, Itemid) {

	if (!action || action.length == 0)
		action = 'contact';

	if (!Itemid) {
		Itemid = 0;
		var obj = $('BPF_id');
		if (obj)
			Itemid = obj.value;
	}

	// version mootools
	var url = bpf_url + "bpf_export.php";

	var param = {
		task : 'stat_ecrire',
		action : action,
		itemid : Itemid
	};
	var query = new Ajax(url, {
		method : 'post'
	});
	query.request(param);

	return;
}

// ---------------------------------
function fiche_dupliquer(Itemid, catId, admin, code_demande) {

	var url = bpf_url + "bpf_export.php?task=fiche_dupliquer&itemid=" + Itemid
			+ "&catId=" + catId + "&admin=" + admin + "&code_demande="
			+ code_demande;
	// var url=bpf_url +"bpf_export.php";

	/*
	 * // version prototype.js new Ajax.Request(url, { //parameters:
	 * {task:'fiche_dupliquer', itemid: Itemid,catId: catId}, onSuccess:
	 * function(reponse){ var result=reponse.responseText.evalJSON(true);
	 * //alert(result.href); if(result.erreur) alert(result.erreur);
	 * if(result.href){ //alert(result.href); location.href=result.href; } },
	 * onFailure: function(data){alert(data);} });
	 */

	// version mootools
	var query = new Ajax(url, {
		method : 'get',
		onComplete : function(reponse) {
			var result = Json.evaluate(reponse);
			if (result.erreur)
				alert(result.erreur);
			if (result.href) {
				location.href = result.href;
			}
		}
	});
	query.request();

}

// ---------------------------------
function fiche_publier(Itemid, catId) {

	/*
	 * var url=bpf_url +"bpf_export.php";
	 * 
	 * new Ajax.Request(url, { parameters: {task:'fiche_publier', itemid:
	 * Itemid,catId: catId}, onSuccess: function(reponse){ location.reload(); },
	 * onFailure: function(data){alert(data);} });
	 */

	// version mootools
	var url = bpf_url + "bpf_export.php";
	var param = {
		task : 'fiche_publier',
		itemid : Itemid,
		catId : catId
	};
	var query = new Ajax(url, {
		method : 'get',
		onComplete : function(reponse) {
			location.reload();

		},
		onFailure : function(data) {
			alert(data);
		}
	});
	query.request(param);

}

// ---------------------------------
function fiche_depublier(Itemid, catId) {

	/*
	 * //var url=bpf_url
	 * +"bpf_export.php?task=fiche_publier&itemid="+Itemid+"&catId="+catId; var
	 * url=bpf_url +"bpf_export.php";
	 * 
	 * new Ajax.Request(url, { parameters: {task:'fiche_depublier', itemid:
	 * Itemid,catId: catId}, onSuccess: function(reponse){ location.reload(); },
	 * onFailure: function(data){alert(data);} });
	 */
	// version mootools
	var url = bpf_url + "bpf_export.php";
	var param = {
		task : 'fiche_depublier',
		itemid : Itemid,
		catId : catId
	};
	var query = new Ajax(url, {
		method : 'get',
		onComplete : function(reponse) {
			location.reload();

		},
		onFailure : function(data) {
			alert(data);
		}
	});
	query.request(param);

}

// ---------------------------------------------------
// ET 23/04/2010
function objet_adresse(id_objet) {

	if (document.getElementById)
		return (document.getElementById(id_objet));
	if (document.all)
		return (document.all(id_objet));
	if (document.layers)
		return (document.layers[id_objet]);

	return null;
}

// ----------------------------------------------------------------------------
function combo_valeur_selectionner(obj, valeur) {
	var i;

	if (!obj || !obj.options)
		return;
	for (i = 0; i < obj.options.length; i++) {
		if (obj.options[i].value == valeur)
			obj.options[i].selected = true;
	}
}

// ---------------------------------------------------
// ET 23/04/2010 nouvelle version
function BPF_recherche_exp_maj(nom, divid, cacher) {
	var divID = nom;
	var divIDTxt1 = 'sobiSearchTxt1';
	var divIDTxt2 = 'sobiSearchTxt2';

	var Pdiv = objet_adresse(divID);
	var PTxt1 = objet_adresse(divIDTxt1);
	var PTxt2 = objet_adresse(divIDTxt2);
	if (Pdiv) {
		var rech_exp_montrer = (Pdiv.className == 'recherche_sobi2_cachediv');
		if (rech_exp_montrer && !cacher) {
			Pdiv.className = '';
		} else {
			Pdiv.className = 'recherche_sobi2_cachediv';
			var obj = objet_adresse('field_objectif_general_formation');
			if (obj)
				combo_valeur_selectionner(obj, "all");
			var obj = objet_adresse('field_nom_organisme');
			if (obj)
				combo_valeur_selectionner(obj, "all");
			var obj = objet_adresse('field_code_public_vise');
			if (obj)
				combo_valeur_selectionner(obj, "all");

			var obj = objet_adresse('SobiCatSelected_0');
			if (obj)
				combo_valeur_selectionner(obj, "0");
			var obj = objet_adresse('SobiCatSelected_1');
			if (obj)
				combo_valeur_selectionner(obj, "0");
			var obj = objet_adresse('SobiCatSelected_2');
			if (obj)
				combo_valeur_selectionner(obj, "0");
			$_("sobiCid" + divid).value = 0;
			if ($("sobiCid"))
				$("sobiCid").value = 0;

		}
		if (PTxt1)
			PTxt1.className = (PTxt1.className == 'recherche_sobi2_cachediv') ? ''
					: 'recherche_sobi2_cachediv';
		if (PTxt2)
			PTxt2.className = (PTxt2.className == 'recherche_sobi2_cachediv') ? ''
					: 'recherche_sobi2_cachediv';
	}
}

// ---------------------------------------
function bpf_retour_recherche() {

	var url = bpf_url + "bpf_export.php";
	var param = {
		task : 'retour_recherche'
	};
	var query = new Ajax(url, {
		method : 'get',
		onComplete : function(reponse) {
			history.back();

		},
		onFailure : function(data) {
			alert(data);
		}
	});

	query.request(param);

}

// ---------------------------------------
// ET 22/12/2010
function BPF_recherche(mot, id_search) {
	var obj = document.getElementById("sobi2Search" + id_search);
	if (obj) {
		// alert("sobi2Search"+id_search + "->" + obj.value);
		obj.value = mot;
	}
	var obj = document.getElementById("field_departement");
	if (obj)
		combo_valeur_selectionner(obj, "all");

	// RAZ et cache de la recherche experte
	BPF_recherche_exp_maj('sobiSearchFormExtOpt_recherche_sobi2_', id_search, 1);

	var form = document.forms.ModsobiSearchFormContainer;
	var obj = document.getElementById("sobiSearchSubmitBt" + id_search);
	if (form && obj) {
		// alert("ModsobiSearchFormContainerBt"+id_search + " submit");
		obj.onclick();
		form.submit();
	}
}

// -----------------------------------------
// ET 23/12/2010 sortie du code du champ "Nous contacter"
function contact_afficher(action, Itemid) {
	var html = '';
	var obj = document
			.getElementById('sobi2Details_field_contact_offre_nom_br');
	if (obj && obj.innerHTML.length)
		html += obj.innerHTML;
	obj = document
			.getElementById('sobi2Details_field_contact_offre_adresse1_br');
	if (obj && obj.innerHTML.length)
		html += obj.innerHTML;
	obj = document
			.getElementById('sobi2Details_field_contact_offre_adresse2_br');
	if (obj && obj.innerHTML.length)
		html += obj.innerHTML;
	obj = document
			.getElementById('sobi2Details_field_contact_offre_adresse3_br');
	if (obj && obj.innerHTML.length)
		html += obj.innerHTML;
	obj = document
			.getElementById('sobi2Details_field_contact_offre_adresse4_br');
	if (obj && obj.innerHTML.length)
		html += obj.innerHTML;
	obj = document
			.getElementById('sobi2Details_field_contact_offre_code_postal_br');
	if (obj && obj.innerHTML.length)
		html += obj.innerHTML;
	obj = document.getElementById('sobi2Details_field_contact_offre_ville_br');
	if (obj && obj.innerHTML.length)
		html += obj.innerHTML;
	obj = document
			.getElementById('sobi2Details_field_contact_offre_telfixe_br');
	if (obj && obj.innerHTML.length)
		html += obj.innerHTML;
	obj = document.getElementById('sobi2Details_field_contact_offre_fax_br');
	if (obj && obj.innerHTML.length)
		html += obj.innerHTML;
	obj = document
			.getElementById('sobi2Details_field_contact_offre_courriel_br');
	if (obj && obj.innerHTML.length)
		html += obj.innerHTML;
	obj = document.getElementById('sobi2Details_field_contact_offre_web_br');
	if (obj && obj.innerHTML.length)
		html += obj.innerHTML;

	html += '<br/><br/>';

	obj = document.getElementById('bpf_infos_div');
	if (obj)
		obj.innerHTML = html;

	bpf_stat_ecrire(action, Itemid);
}


// --------------------------------------------------
function bpf_formacode_ajouter(fieldname, itemid) {
	
	var obj=$(fieldname);
	if(!obj) return;
	var valeur=obj.value;
	
	var param="fieldname=" + fieldname +"&itemid=" + itemid + "&valeur=" +  valeur;
	fenetre_ouvrir(bpf_url + 'crci_bpf/bpf_formacode_gerer.php?'+param,
			'FORMACODEGERER', 1000, 700);
	// formacode_arborescence(fieldname);
}

//------------------------
function fenetre_ouvrir(src, name, w, h, param2) {
	var param = "height=" + h + ",width=" + w;
	if (param2 && param2.length > 0)
		param += "," + param2;
	else
		param += ",resizable=1,scrollbars=1";

	// pour debug
	// param +=",locationbar=yes,status=yes";
	var fenetre = window.open(src, name, param);
	if (fenetre && fenetre.focus)
		fenetre.focus();
}

//------------------------
function fenetre_fermer(){
if(window.opener)
	try	{
		window.opener.focus();
	}
	catch (e){
	}
window.close();
}

// ----------------------------------------
// ET 01/07/2011
function formacode_arborescence(fieldname) {

	if (!$('formacode_arborescence'))
		return;

	$('formacode_arborescence').innerHTML = "";

	documents_tree = new TafelTree('formacode_arborescence', formacode_arbo, {
		'generate' : true,
		'imgBase' : 'tafeltree/imgs/',
		'defaultImg' : 'page.gif',
		'defaultImgOpen' : 'folderopen.gif',
		'defaultImgClose' : 'folder.gif'
	});

}


//------------------------
function lien_html(url, img, texte, alt, classe, param, id){
	var t="";
	if (url && url.length>0){
		t+='<a ';
		if (id && id.length>0) t+=' id="' + id + '"';
		t+=' href="' + url;
		t+='">';
	}
	if (img.length>0){
		t+='<img src="' + img + '" border="0" style="vertical-align: middle;"';
		if (alt && alt.length>0){
		 	t+=' alt="' + alt + '"';
		 	t+=' title="' + alt + '"';
		}
		t+='>';

	}
	//if (classe.length=0) classe="texte8";
	if (typeof(texte)=='string' && texte.length>0){
		if (img.length>0) t+='&nbsp;';
		if (classe.length>0) t+='<span class="' + classe + '">';
		t+= texte;
		if (classe.length>0) t+='</span>';
	}
	if (url && url.length>0)
		t+="</a>";
	return(t);
}

