// Utilisation de l'API dojo
// Telechargement : http://www.dojotoolkit.org/downloads
// API : http://api.dojotoolkit.org/
// dojo.query : http://docs.dojocampus.org/dojo/query
//dojo.require('dojo.string');
//dojo.require('dojo.dom');
//dojo.require('dojo.style');
//dojo.require('dojo.event');
//dojo.require('dojo.html');




// Au chargement de la page
$(document).ready(function(){

	$("img[class='tooltip']").tooltip({
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,
		fixPNG: true,
		showBody: " - ",
		extraClass: "pretty fancy"
	});
	$("td[class='tooltip']").tooltip({
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,
		fixPNG: true,
		showBody: " - ",
		extraClass: "pretty fancy"
	});
	$("span[class='tooltip']").tooltip({
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,
		fixPNG: true,
		showBody: " - ",
		extraClass: "pretty fancy"
	});
});

function evenementsClaviers(){
	/*if(document.event.ctrlKey){
		alert(0);
		switch(event.keyCode){
			case 78: //Ctrl+N: Annule ouverture dans une autre page
				return false;
				break;
		}// end if
	}
	else{
		alert(1);
	}// end if*/
}// end fonction evenementsclaviers

function afficherTraitementEnCours () {
	if(dojo.byId('div_menu_1')) {
		dojo.style(dojo.byId('div_menu_1'), 'display', 'none');
	}// fin if
	if(dojo.byId('div_menu_2')) {
		dojo.style(dojo.byId('div_menu_2'), 'display', 'none');
	}// fin if
	if(dojo.byId('div_cheminfer')) {
		dojo.style(dojo.byId('div_cheminfer'), 'display', 'none');
	}// fin if
	if(dojo.byId('div_contenu')) {
		dojo.style(dojo.byId('div_contenu'), 'display', 'none');
	}// fin if
	if(dojo.byId('div_progress_bar')) {
		dojo.style(dojo.byId('div_progress_bar'), 'display', 'block');
	}// fin if
}// fin function afficherTraitementEnCours

function resetFormulaire (str_nomFormulaire) {
	document.forms[str_nomFormulaire].reset();
}// fin function resetFormulaire

function validerFormulaire (str_nomFormulaire, str_target, str_action, str_messageConfirmation, str_input1, str_value1, str_input2, str_value2, str_input3, str_value3) {
	if (str_messageConfirmation == '' || confirm(str_messageConfirmation)) {
		if (str_target != '') {
			document.forms[str_nomFormulaire].target = str_target;
		}// fin if
		if (str_input1 != '') {
			document.forms[str_nomFormulaire].elements[str_input1].value = str_value1;
		}// fin if
		if (str_input2 != '') {
			document.forms[str_nomFormulaire].elements[str_input2].value = str_value2;
		}// fin if
		if (str_input3 != '') {
			document.forms[str_nomFormulaire].elements[str_input3].value = str_value3;
		}// fin if
		document.forms[str_nomFormulaire].action = (str_action == '' ? self.location.href : str_action);
		document.forms[str_nomFormulaire].submit();
		if (str_target == '') {
			afficherTraitementEnCours();
		}
	}// fin if
}// fin function validerFormulaire

function validerFormulaireListe(str_msgSelectionVide, str_nomFormulaire, str_target, str_action, str_messageConfirmation, str_input1, str_value1, str_input2, str_value2, str_input3, str_value3) {
	tab_checkbox = $("input:checkbox[name='line_select[]']");
	str_value2 = '';
	for(i = 0; i < tab_checkbox.length; i ++) {
		if(tab_checkbox[i].checked == true) {
			num_ref = tab_checkbox[i].value;
			str_value2 += (str_value2 == '' ? '' : '|') + num_ref;
		}// fin if
	}// fin for
	if(str_value2 == '') {
		alert(str_msgSelectionVide);
	}
	else {
		validerFormulaire (str_nomFormulaire, str_target, str_action, str_messageConfirmation, str_input1, str_value1, str_input2, str_value2, str_input3, str_value3);
	}// fin if
}// fin function validerFormulaireListe

function validerFormulaireSauvegarder (str_nomFormulaire, str_target, str_action, str_messageConfirmation, str_input1, str_value1, str_input2, str_value2, str_input3, str_value3) {
	if (dojo.byId('indication_a_sauvegarder').innerHTML != '') {
		validerFormulaire (str_nomFormulaire, str_target, str_action, str_messageConfirmation, str_input1, str_value1, str_input2, str_value2, str_input3, str_value3);
	}// fin if
}// fin function validerFormulaire

function passerFormulaireASauvegarder (oThis) {
	if(dojo.byId('indication_a_sauvegarder').innerHTML == '') {
		// Etoile dans chemin de fer
		dojo.byId('indication_a_sauvegarder').innerHTML = '*';
		// Modifier etat du bouton sauvegarder
		dojo.query('a[name="sauvegarder_formulaire"] > span').forEach(function(node, index, nodeList){
			node.style.color = '#000000';
		});
	}// fin if
}// fin function passerFormulaireASauvegarder

function creerFichier(str_msgSelectionVide, str_nomFormulaire, str_action) {
	tab_checkbox = $("input:checkbox[name='line_select[]']");
	str_listeRef = '';
	for(i = 0; i < tab_checkbox.length; i ++) {
		if(tab_checkbox[i].checked == true) {
			num_ref = tab_checkbox[i].value;
			str_listeRef += (i == 0 ? '' : '|')+num_ref;
		}// fin if
	}// fin for
	if(str_listeRef == '') {
		alert(str_msgSelectionVide);
	}
	else {
		validerFormulaire (str_nomFormulaire, 'iframe_telechargement', str_action, '', '', '', '', '', '', '');
	}// fin if
}// fin function creerFichier

function supprimerElement(str_url, str_message) {
	if (confirm(str_message)) {
		window.location.replace(str_url);
	}// fin if
}// fin fonction supprimeElement

function afficherDetail(sNomFormulaire, sCheckbox, sDiv) {
	document.getElementById(sDiv).style.display = (eval(sNomFormulaire+'.'+sCheckbox).checked ? 'block' : 'none');
}// fin fonction afficherDetail

function viderDesactiveChamp(oThis, str_refChamp) {
	obj_champ = $("#" + str_refChamp);
	obj_ligneChamp = $("#ligne_" + str_refChamp);
	obj_champ.val('');
	if(oThis.checked) {
		obj_ligneChamp.css('display', '');
	}
	else {
		obj_ligneChamp.css('display', 'none');
	}// fin if
}// fin fonction viderDesactiveChamp

function compterNombreDeMots(str_nomChamp) {
	obj_textarea = dojo.byId(str_nomChamp);
	if (obj_textarea) {
		obj_spanNombreMots = dojo.byId(str_nomChamp + '_nbmots');
		obj_spanNombreMots.innerHTML = obj_textarea.value.length + obj_textarea.value.split('\n').length - 1;
	}// fin if
}// fin function compterNombreDeMots

function ligneSelection(oThis, strRefDiv) {
	if(oThis.className == 'style_list_ligne') {
		oThis.className = 'style_list_ligne style_list_ligne_selection';
	}
	else {
		oThis.className = 'style_list_ligne';
	}// fin if
}// fin function ligneSectect

function transfertVersListe(str_ref, str_sens, num_nbMaxElementDest) {
	obj_listeOrig = $("#" + str_ref + (str_sens == "droite" ? "_orig" : "_dest") + " div[class='style_list_ligne style_list_ligne_selection']");
	obj_contenuDest = $("#" + str_ref + (str_sens == "gauche" ? "_orig" : "_dest"));
	obj_listeDest = $("#" + str_ref + (str_sens == "gauche" ? "_orig" : "_dest") + " div");

	if(str_sens == "droite" && num_nbMaxElementDest != 0 && obj_listeDest.length >= num_nbMaxElementDest) {
		return;
	}// fin if

	obj_listeOrig.each(function() {
		obj_ligneSelectionne = $(this);
		obj_ligneSelectionne.removeClass('style_list_ligne_selection');
		obj_ligneDest = null;
		str_valeurOrig = ((obj_ligneSelectionne.attr('id')).split('__'))[0];
		if(obj_listeDest.length > 0) {
			str_valeurDest = 0;
			obj_listeDest.each(function() {
				obj_ligneDest = $(this);
				str_valeurDest = ((obj_ligneDest.attr('id')).split('__'))[0];
				if(obj_ligneSelectionne != null && str_valeurOrig < str_valeurDest) {
					obj_ligneSelectionne.insertBefore(obj_ligneDest);
					obj_ligneSelectionne = null;
				}// fin if
			});// fin each
			if(obj_ligneSelectionne != null && str_valeurOrig > str_valeurDest) {
				obj_contenuDest.append(obj_ligneSelectionne);
				obj_ligneSelectionne = null;
			}// fin if
		}
		else {
			obj_contenuDest.append(obj_ligneSelectionne);
		}// fin if
	});// fin each
	obj_listeDest = $("#" + str_ref + "_dest div");
	str_valeur = '';
	obj_listeDest.each(function() {
		obj_ligneDest = $(this);
		str_valeur += (str_valeur == '' ? '' : '|') + ((obj_ligneDest.attr('id')).split('__'))[0];
	});// fin each
	$("#" + str_ref).val(str_valeur);
	return;
}// function transfertVersListe

function afficheTypeChampEnFonctionDuFormat(oThis, str_refChamp) {
	obj_ligneChampSaisieSimple = $("#ligne_" + str_refChamp + 's');
	$("#" + str_refChamp + 's').val("");
	if(oThis.value == 'S') {
		obj_ligneChampSaisieSimple.css('display', '');
	}
	else {
		obj_ligneChampSaisieSimple.css('display', 'none');
	}// fin if
	obj_ligneChampSaisieMultilignes = $("#ligne_" + str_refChamp + 'm');
	$("#" + str_refChamp + 'm').val("");
	if(oThis.value == 'M') {
		obj_ligneChampSaisieMultilignes.css('display', '');
	}
	else {
		obj_ligneChampSaisieMultilignes.css('display', 'none');
	}// fin if
	obj_ligneChampSaisieDateSeule = $("#ligne_" + str_refChamp + 'd');
	$("#date_" + str_refChamp + 'd').val("");
	$("#" + str_refChamp + 'd').val("0000-00-00 00:00:00");
	if(oThis.value == 'D') {
		obj_ligneChampSaisieDateSeule.css('display', '');
	}
	else {
		obj_ligneChampSaisieDateSeule.css('display', 'none');
	}// fin if
	obj_ligneChampSaisieDateAvecHeure = $("#ligne_" + str_refChamp + 'h');
	//time_heure_admin_param_constante_valeur_h
	$("#date_" + str_refChamp + 'h').val("");
	$("#time_heure_" + str_refChamp + 'h').get(0).selectedIndex = 0;
	$("#time_minute_" + str_refChamp + 'h').get(0).selectedIndex = 0;
	$("#" + str_refChamp + 'h').val("0000-00-00 00:00:00");
	if(oThis.value == 'H') {
		obj_ligneChampSaisieDateAvecHeure.css('display', '');
	}
	else {
		obj_ligneChampSaisieDateAvecHeure.css('display', 'none');
	}// fin if
	obj_ligneChampSaisieFlag = $("#ligne_" + str_refChamp + 'g');
	$("#" + str_refChamp + 'g')[0].checked = false;
	if(oThis.value == 'G') {
		obj_ligneChampSaisieFlag.css('display', '');
	}
	else {
		obj_ligneChampSaisieFlag.css('display', 'none');
	}// fin if
	obj_ligneChampSaisieFichier = $("#ligne_" + str_refChamp + 'fichier');
	$("#" + str_refChamp + 'fichier').val("");
	if(oThis.value == 'F') {
		obj_ligneChampSaisieFichier.css('display', '');
		$("#ligne_" + str_refChamp + 'fichier_supprimer').css('display', '');
		$("#ligne_" + str_refChamp + 'fichier_telechargement').css('display', '');
	}
	else {
		obj_ligneChampSaisieFichier.css('display', 'none');
		$("#ligne_" + str_refChamp + 'fichier_supprimer').css('display', 'none');
		$("#ligne_" + str_refChamp + 'fichier_telechargement').css('display', 'none');
	}// fin if
}// fin function afficheTypeChampEnFonctionDuFormat

function changeOnglet (str_onglet) {
	// Ancien onglet actif
	str_ancienOnglet = document.getElementById("onglet_actif").value;
	// Traitement si on selectionnne un nouvel onglet
	if(str_ancienOnglet != str_onglet) {
		// Type de la page
		str_type = document.getElementById("_type").value;
		// On cache le detail de l'ancien onglet actif
		//$("#" + str_ancienOnglet + "_detail").css({display: "none"});
		document.getElementById(str_ancienOnglet + "_detail").style.display = "none";
		// On supprime la classe de l'ancien onglet actif
		//$("#" + str_ancienOnglet).removeClass("ongletselect");
		document.getElementById(str_ancienOnglet).className = "";
		// On affiche le detail de l'onglet selectionne
		//$("#" + str_onglet + "_detail").css({display: ""});
		document.getElementById(str_onglet + "_detail").style.display = "";
		// On ajoute la classe a l'onglet selectionne
		//$("#" + str_onglet).addClass("ongletselect");
		document.getElementById(str_onglet).className = "ongletselect";
		// Le champ cache prends la valeur de l'onglet selectionne
		//$("#onglet_actif").val(str_onglet);
		document.getElementById("onglet_actif").value = str_onglet;
		// MAJ de l'onglet selectionner pour le grader lors d'un F5
		$.ajax({
			type: "POST",
			async: false,
			url: "./page_" + str_type + ".php",
			data: "_action=maj_onglet&onglet_actif=" + str_onglet,
			success: function(){
			}// fin function
		});// fin appel ajax
	}// fin if
	document.getElementById(str_onglet).blur();
}// fin function changeOnglet

function checkAll(chr_OK) {
	tab_checkbox = $("input:checkbox[name='line_select[]']");
	for(i = 0; i < tab_checkbox.length; i ++) {
		tab_checkbox[i].checked = (chr_OK == 'O' ? true : false);
	}// fin for
}// fin function checkAll

function checkSelection(str_listeSelection) {
	tab_selection = $("#" + str_listeSelection).val().split('|');
	for(i = 0; i < tab_selection.length; i ++) {
		$("input:checkbox[id='line_select_" + tab_selection[i] + "']").each(function() {
			this.checked = true;
		});// fin each
	}// fin for
}// fin function checkSelection

function changerCalendrier(str_ref, str_annee, str_mois, str_dateSelectionnee) {
	$.ajax({
		type: 'GET',
		url: './html/calendrier.php',
		data: 'ref=' + str_ref + '&annee=' + str_annee + '&mois=' + str_mois + '&dateSelectionnee=' + str_dateSelectionnee + '&ajax=O',
		success: function(str_calendrierHTML){
			$('#calendrier_' + str_ref).html(str_calendrierHTML);
		}// fin function
	});// fin appel ajax
}// fin function changerCalendrier

function selectionDateDuCalendrier(str_ref, str_date) {
	$('#date_' + str_ref).val(str_date);
	tb_remove();
	$('#date_' + str_ref).trigger('onchange')
}// fin function selectionDateDuCalendrier

function testDateSaisie(str_ref, str_messageErreur) {
	bln_dateSaisieValide = true;
	str_dateASauver = '';
	str_dateSaisieValeur = $('#date_' + str_ref).val();
	tab_dateCachee = $('#' + str_ref).val().split(' ');

	// Remplacement des - par des /
	str_dateSaisieValeur = str_dateSaisieValeur.replace(/-/g, '/');

	if(str_dateSaisieValeur == '') {
		str_dateAAfficher = '';
		str_dateASauver = '0000-00-00';
	}
	else if(str_dateSaisieValeur.split('/').length == 3) {
		$.ajax({
			type: 'POST',
			async: false,
			url: './include/ajax.php',
			data: 'fonction=testerDate&date=' + str_dateSaisieValeur,
			success: function(str_dateRetour){
				str_dateASauver = str_dateRetour;
			}// fin function
		});// fin appel ajax
		if(str_dateASauver == '0000-00-00') {
			str_dateSaisieValeur = '';
			bln_dateSaisieValide = false;
		}// fin if
	}
	else {
		bln_dateSaisieValide = false;
		str_dateASauver = '0000-00-00';
	}// fin if

	if(bln_dateSaisieValide) {
		$('#date_' + str_ref).val(str_dateSaisieValeur);
	}
	else {
		alert(str_messageErreur);
		$('#date_' + str_ref).focus();
	}// fin if
	$('#' + str_ref).val(str_dateASauver + ' ' + tab_dateCachee[1]);
}// fin function testDateSaisie

function metAJourHeure(str_ref) {
	str_valeurDateCachee = $('#' + str_ref).val();
	str_heure = $('#time_heure_' + str_ref).val();
	str_minute = $('#time_minute_' + str_ref).val();
	$('#' + str_ref).val(str_valeurDateCachee.substr(0, 11) + (str_heure < 10 ? '0' : '') + str_heure+':' + (str_minute < 10 ? '0' : '') + str_minute + ':00');
}// fin function metAJourHeure

function HelpInsertText(str_ref, str_tagDebut, str_textParDefaut, str_tagFin) {
	obj_textarea = document.getElementById(str_ref);
	if(obj_textarea.createTextRange()) {
		obj_textarea.focus(obj_textarea.caretPos);
		obj_textarea.caretPos = document.selection.createRange().duplicate();
		if(obj_textarea.caretPos.text.length > 0) {
			obj_textarea.caretPos.text = str_tagDebut + obj_textarea.caretPos.text + str_tagFin;
		}
		else {
			obj_textarea.caretPos.text = str_tagDebut + str_textParDefaut + str_tagFin;
		}// end if
	}
	else {
		obj_textarea.value += str_tagDebut + str_textParDefaut + str_tagFin;
	}// end if
}// end function HelpInsertText


function poseBaliseBBCode(str_ref, balise1, balise2, str_question1, str_question2) {
	try {
		var objet = dojo.byId(str_ref);
		var str_text = objet.value;
		if ($(document).selection) {
			objet.focus();
			sel = $(document).selection.createRange();
			sel.text = balise1 + sel.text + balise2;
		}
		else if(objet.selectionStart == objet.selectionEnd) {
			var str_url = '';
			var str_titre = '';
			if (balise1 == '[img]' && balise2 == '[/img]') {
				str_url = prompt(str_question1, str_url);
			}
			else if (balise1 == '[url]' && balise2 == '[/url]') {
				str_url = prompt(str_question1, str_url);
				str_titre = prompt(str_question2, str_titre);
			}
			else if (balise1 == '[mail]' && balise2 == '[/mail]') {
				str_url = prompt(str_question1, str_url);
				str_titre = prompt(str_question2, str_titre);
			}// fin if
			if(str_titre != '') {
				balise1 = balise1.substring(0, balise1.length - 1) + '=' + str_url + balise1.substring(balise1.length - 1, balise1.length);
				str_url = str_titre;
			}// fin if
			var textedebut = str_text.substring(0, objet.selectionStart);
			var textefin = str_text.substring(objet.selectionEnd, str_text.length);
			objet.value = textedebut + balise1 + str_url + balise2 + textefin;
		}
		else {
			var textedebut = str_text.substring(0, objet.selectionStart);
			var textefin = str_text.substring(objet.selectionEnd, str_text.length);
			var texteSelection = str_text.substring(objet.selectionStart, objet.selectionEnd);
			var str_url = texteSelection;
			var str_titre = '';
			if (balise1 == '[img]' && balise2 == '[/img]') {
				str_url = prompt(str_question1, str_url);
			}
			else if (balise1 == '[url]' && balise2 == '[/url]') {
				str_url = prompt(str_question1, str_url);
				str_titre = prompt(str_question2, str_titre);
			}
			else if (balise1 == '[mail]' && balise2 == '[/mail]') {
				str_url = prompt(str_question1, str_url);
				str_titre = prompt(str_question2, str_titre);
			}// fin if
			if(str_titre != '') {
				balise1 = balise1.substring(0, balise1.length - 1) + '=' + str_url + balise1.substring(balise1.length - 1, balise1.length);
				str_url = str_titre;
			}// fin if
			objet.value = textedebut + balise1 + str_url + balise2 + textefin;
		}// fin if
		preview(str_ref);
	}// fin try
	catch(e) {
		alert(e);
	}// fin catch
	return false;
}// end function poseBaliseBBCode

function preview(str_ref) {
	var objet = dojo.byId(str_ref);
	var str_text = objet.value;
	var objetPreview = dojo.byId(str_ref + '_preview');

	if(objetPreview.style.display == "none") {
		return false;
	}// fin if

	var texte = str_text;

	texte = texte.replace(/>/g, '&gt;')
	texte = texte.replace(/</g, '&lt;');
	texte = texte.replace(/\r\n|\r|\n/g, '<br />');
	texte = texte.replace(/\[b]([^\[]*)\[\/b]/mig, '<span style="font-weight:bold;">$1</span>');
	texte = texte.replace(/\[u]([^\[]*)\[\/u]/mig, '<span style="text-decoration:underline;">$1</span>');
	texte = texte.replace(/\[i]([^\[]*)\[\/i]/mig, '<span style="font-style:italic;">$1</span>');
	texte = texte.replace(/\[s]([^\[]*)\[\/s]/mig, '<span style="text-decoration:line-through;">$1</span>');

	texte = texte.replace(/\[color=([^\[]*)\]/mig, '<span style=\'color:$1;\'>');
	texte = texte.replace(/\[bgcolor=([^\[]*)\]/mig, '<span style=\'background-color:$1;\'>');
	texte = texte.replace(/\[\/(color|bgcolor)\]/gi, '</span>');

	texte = texte.replace(/\[align=([^\[]*)\]/mig, '<div style=\'text-align:$1;\'>');
	texte = texte.replace(/\[\/align]/gi, '</div>');

	texte = texte.replace(/\[url]([^\]]*)\[\/url]/mig, '<a href="$1" target="_blank">$1</a>');
	texte = texte.replace(/\[url=([^\[]*)\]([^\]]*)\[\/url\]/mig, '<a href=\'$1\' target=\'_blank\'>$2</a>');
	texte = texte.replace(/\[mail]([^\[]*)\[\/mail]/mig, '<a href="mailto:$1" target="_blank">$1</a>');
	texte = texte.replace(/\[mail=([^\[]*)\]([^\]]*)\[\/mail\]/mig, '<a href=\'mailto:$1\' target=\'_blank\'>$2</a>');
	texte = texte.replace(/\[img]([^\]]*)\[\/img]/mig, '<img src="$1" border="0" />');

	texte = texte.replace(/\[list=([^\]]*)\]<br \/>\[\*\]/mig, '<ul type="$1"><li>');
	texte = texte.replace(/\[\*\]/mig, '</li><li>');
	texte = texte.replace(/<br \/>\[\/list]<br \/>/mig, '</li></ul>');

	objetPreview.innerHTML = texte;
	return texte;
}// end function preview

function preloadImages(tab_src) {
	var imgToLoad = new Array();
	for(var i = 0; i < tab_src.length; i++){
		imgToLoad[i] = new Image();
		imgToLoad[i].src = tab_src[i];
	}// end for
}// end function preloadImages
