$().toastmessage({sticky : true, position : 'center'});
//Fonction qui aide les boulets à comprendre pourquoi ils n'ont pas de shows en mode BTTF
function aidesaisie(){
	$('#bttfchoix').focus();
	$('#bttfoui').css("background-color","red").css("color","white");
}
//Fonction qui aide à la saisie : Duplique une heure dans tous les champs
function duplique_fin(nb){
	if($('#aide_fin').val() != '' && VerifDate($('#aide_fin').val(),'/') && $('#aide').val() != '' && VerifDate($('#aide').val(),'/')){
		var i = 1;
		while(i <= nb){
			if($('#DateDeb'+i).val() == '' && $('#DateFin'+i).val() == ''){
				$('#DateFin'+i).val($('#aide_fin').val());
				$('#DateDeb'+i).val($('#aide').val());
			}
		i++;
		}
	}
}
//Fonction qui fait apparaitre disparaitre les horaires de spectacles	    
function shiftOpacity_show(parc_id,liste) {
    max = liste.length;//Longueur maximum de la liste
	rapidite = 500;//Durée de l'effet
    if($('#indice_show'+parc_id).val() == max)
			$('#indice_show'+parc_id).val(0);//, on revient à 0
	
	rang = $('#rang_show'+parc_id).val();
    if(pair($('#indice_show'+parc_id).val()) == 1 && $('#indice_show'+parc_id).val() < max-1) {
        //disparition = De 100 à 0
		$('#parc'+parc_id+'_texte_show'+rang).fadeOut(rapidite);
		$('#parc'+parc_id+'_ligne_horaire'+rang).fadeOut(rapidite);		
		$("#indice_show"+parc_id).val(parseInt($("#indice_show"+parc_id).val())+1);
        setTimeout("shiftOpacity_show("+parc_id+",\""+liste+"\")",rapidite);
    } else if($('#indice_show'+parc_id).val() < max-1){
        //réapparition
		liste = liste.split(',');
        $("#indice_show"+parc_id).val(parseInt($("#indice_show"+parc_id).val())-1);
        //Et on affecte au innerHTML
		$('#parc'+parc_id+'_texte_show'+rang).html(liste[$('#indice_show'+parc_id).val()]);
        $("#indice_show"+parc_id).val(parseInt($("#indice_show"+parc_id).val())+1);
		if(parc_id == 1)
		{
			if(rang == 0)
				horaire0_show1.setInnerHTML("<span>"+liste[$('#indice_show'+parc_id).val()]+"</span>");
			else if(rang == 1)
				horaire1_show1.setInnerHTML("<span>"+liste[$('#indice_show'+parc_id).val()]+"</span>");
			else if (rang == 2)
				horaire2_show1.setInnerHTML("<span>"+liste[$('#indice_show'+parc_id).val()]+"</span>");
		}
		else
		{
			if(rang == 0)
				horaire0_show2.setInnerHTML("<span>"+liste[$('#indice_show'+parc_id).val()]+"</span>");
			else if(rang == 1)
				horaire1_show2.setInnerHTML("<span>"+liste[$('#indice_show'+parc_id).val()]+"</span>");
			else if (rang == 2)
				horaire2_show2.setInnerHTML("<span>"+liste[$('#indice_show'+parc_id).val()]+"</span>");		
		}
        $("#indice_show"+parc_id).val(parseInt($("#indice_show"+parc_id).val())+1);
		$('#parc'+parc_id+'_texte_show'+rang).fadeIn(rapidite);
		$('#parc'+parc_id+'_ligne_horaire'+rang).fadeIn(rapidite);
        if(rang < 2)
        	$("#rang_show"+parc_id).val(parseInt($("#rang_show"+parc_id).val())+1);
        else
        	$("#rang_show"+parc_id).val(0);
		
    }else
		$("#indice_show"+parc_id).val(0);
}  
//Fonction qui interdit les lettres
function interdire_lettres(e,i){
	keyCode = e.which ? e.which : e.keyCode;
	if((keyCode < 48 || keyCode > 57) && keyCode != 8 && keyCode != 37){
		return false;
	}
	
	//Forcage d'un ":" au bout de 2 caractères
	else if(keyCode != 8){
		if(i.value.length == 2){
				i.value += ':';
		}
	}
}
//Fonction qui vérifie la bonne saisie des paramètres du module
function verif_param(){
	if(VerifNombre($('#nb_saisie').val()) && VerifNombre($('#nb_horaire').val())){
		document.forms["dgi"].submit();
	}else
		alert('Merci de vérifier le paramètrage.');
		
}
//Fonction qui valide la bonne saisie de séances
function valid(nb,nb_seances){	
	var a = b = 1;
	var OK = true;
	var OK2 = false;
	$('#alerte_retour').fadeOut('fast');
	while(a <= nb || OK == false){
		if($("#touch"+a).val() == 1){
			if($('#spectacle_id'+a).val() != 0){
				if(VerifDate($('#DateDeb'+a).val(),'/') && VerifDate($('#DateFin'+a).val(),'/')){
					while(b <= nb_seances){
						if(document.getElementById('horairedeb'+a+'-'+b) && document.getElementById('horairefin'+a+'-'+b))
						{
							if($('#horairedeb'+a+'-'+b).val().length == 5)
							{
								if($('#horairefin'+a+'-'+b).val().length == 5)
								{
									if($("#cases"+a+"-"+b+" input:checked").length > 0) OK2 = true; 
									else {alert('Il manque une périodicité. Ligne n°'+a+'-'+b); OK = false; break;}
								}
								else
								{
									alert('Il faut un horaire de fin. Ligne n°'+a+'-'+b);
									OK = false;
									break;							
								}
								
							}
						}
						else if(document.getElementById('horaire'+a+'-'+b))
						{
							if($('#horaire'+a+'-'+b).val().length == 5)
							{
								if($("#cases"+a+"-"+b+" input:checked").length > 0) OK2 = true; 
								else {alert('Il manque une périodicité. Ligne n°'+a+'-'+b); OK = false; break;}
							}
						}
						if(b == nb_seances && OK2 == false){
							alert('Les séances n\'ont pas été correctement saisis. Ligne n°'+a);
							OK = false;
							break;
						}
						b++;
					}
				}else{
					alert('L \'enregistrement n\'est pas complet. Ligne n°'+a);
					OK = false;
					break;
				}
			}
		}
		a++;
		if(a == nb)
			break;
	}
	if(OK == true){
		document.forms["dgi"].submit();
	}	
}
function coche_tous(id,champ){
	var cases = $("#cases"+id).find(':checkbox');
	if(champ.checked) cases.attr('checked', true); 
	else cases.attr('checked', false);
}
function interroge_dispo() {
	var i = interroge_dispo.arguments[0];
	var j = interroge_dispo.arguments[1];
	var semaine = '';
	$('#cases'+i+'-'+j+' :checkbox:checked').each(function() {semaine += $(this).val();});
	var variables = 'mode=interroge_dispo&spectacle_id='+$('#spectacle_id'+i).val()+'&datedeb='+$('#DateDeb'+i).val()+'&datefin='+$('#DateFin'+i).val()+'&semaine='+semaine;
	if(interroge_dispo.arguments[2]) variables += '&seance_id='+interroge_dispo.arguments[2];
	if($('#DateDeb'+i).val() != '' && $('#DateFin'+i).val() != '' && ComparaisonDate($('#DateDeb'+i).val(),$('#DateFin'+i).val())){
		if(document.getElementById('horairedeb'+i+'-'+j) && document.getElementById('horairefin'+i+'-'+j)){
			if(ComparaisonHeure($('#horairedeb'+i+'-'+j).val(), $('#horairefin'+i+'-'+j).val())){
				if(semaine != ''){
					variables += '&horairedeb='+$('#horairedeb'+i+'-'+j).val()+'&horairefin='+$('#horairefin'+i+'-'+j).val();
					$.ajax(
						{
							type: "POST", url: "ajax_spectacle.php",	data : variables, dataType: "html",	timeout: 6000,
							success: function(data)
							{
								if(data == 2){
									$().toastmessage('showWarningToast','Un horaire existe déjà pour le spectacle à ces dates. Enregistrement '+i+' Horaire '+j);
									coche_tous(i+'-'+j,$('#cases'+i+'-'+j));
								}
							}
						}
					);
					touch(i);			
				}
			}else
				$().toastmessage('showWarningToast','Les horaires ne sont pas cohérents. Enregistrement '+i+' Horaire '+j);
		}else{
			if(semaine != ''){
				variables += '&horaire='+$('#horaire'+i+'-'+j).val();
				$.ajax(
					{
						type: "POST", url: "ajax_spectacle.php",	data : variables, dataType: "html",	timeout: 6000,
						success: function(data)
						{
							if(data == 2){
								$().toastmessage('showWarningToast','Un horaire existe déjà pour le spectacle à ces dates. Enregistrement '+i+' Horaire '+j);
								coche_tous(i+'-'+j,$('#cases'+i+'-'+j));
							}
						}
					}
				);
				touch(i);
			}
		}
	}else{
		$().toastmessage('showWarningToast','Les dates de représentation ne sont pas cohérentes. Enregistrement '+i);
		$('#DateDeb'+i).val(null);
		$('#DateFin'+i).val(null);
		coche_tous(i+'-'+j,$('#cases'+i+'-'+j));
	}
}
function change_interface(i,nb_horaire) {
	$.ajax(
		{
			// Define AJAX properties.
			type: "POST",
			url: "ajax_spectacle.php",
			data : 'mode=interface_saisie&i='+i+'&nb_horaire='+nb_horaire+'&spectacle_id='+$('#spectacle_id'+i).val(),
			dataType: "html",
			timeout: 6000,
			
			// Define the succss method.
			success: function(data)
			{
				$('#data_retour'+i).html(data);
				touch(i);
			}
		}
	);
}
function change_interface_gestion(i,nb_horaire) {
	var variables = 'mode=interface_gestion&i='+i+'&nb_horaire='+nb_horaire+'&spectacle_id='+$('#spectacle_id'+i).val()+'&representation_id='+$('#representation_id'+i).val();
	$.ajax(
		{
			// Define AJAX properties.
			type: "POST",
			url: "ajax_spectacle.php",
			data : variables,
			dataType: "html",
			timeout: 6000,
			
			// Define the succss method.
			success: function(data)
			{
				$('#data_retour'+i).html(data);
			}
		}
	);
}

