/***************************************************
 Fonction permettant la cr?ation des infobulles.
***************************************************/
function creationInfoBulle(ctner, ctxt, txt){
	new YAHOO.widget.Tooltip("tt1"+ctxt, { context:ctxt, text:txt} );
};


/***************************************************
 Fonction permettant la validation du formulaire
 choixRegion.
***************************************************/
function validerChoixRegion(){
	document.forms.choixRegion.submit();
	YAHOO.example.container.dlg.show();
};

/***************************************************
 Fonction permettant la validation du formulaire
 choixVille.
***************************************************/
function validerChoixVille(){
	document.forms.choixVille.submit();
	YAHOO.example.container.dlg.show();
};

/***************************************************
 Fonction v?rifiant la validit? du mail de la BAI.
***************************************************/
function verifMail(a){
	if(	document.form_idee.mail.disabled==false){
		testm = false ;
		for (var j=1 ; j<(a.length) ; j++) {
			if (a.charAt(j)=='@') {
				if (j<(a.length-4)){
					for (var k=j ; k<(a.length-2) ; k++) {
						if (a.charAt(k)=='.') testm = true;
					}
				}
			}
		}
		
		if (testm==false) alert('Votre adresse e-mail est incorrecte.');
		
		else alert('Votre adresse e-mail semble correcte.');
		return testm ;
	}
	else{
		return true;
	}
};

/********************************************************************
 Fonction qui d?grise le champ mail de la BAI si il est coch?.
*********************************************************************/
function grise_selected(){	
	if(	document.form_idee.mail.disabled==false){
	document.form_idee.mail.disabled=true;
	}
	else{
		document.form_idee.mail.disabled=false;
	}
	/*var obj = document.getElementById("opt"+id);
	var objType = document.getElementById("type"+id);
	if(objType.value=="checkbox" || objType.value=="radio" || objType.value=="select") {
		obj.disabled=false;
		obj.value="Saisissez ici les diff?rentes options s?par?es par le symbole ';'.";
		obj.select();
		obj.focus();
	}
	else{
		obj.disabled=true;
		obj.value=" ";
	}*/
};


/********************************************************************
 Fonction qui affiche ou cache une ligne d'un tableau HTML.
*********************************************************************/
function affichecacheTR(nomId){	
	if(document.getElementById(nomId)==null){
	return false;
	}
	else{
		if(document.getElementById(nomId).style.display=="none" || document.getElementById(nomId).style.display=="")
		{
		 if(navigator.appName == "Microsoft Internet Explorer") document.getElementById(nomId).style.display="inline";
		 else document.getElementById(nomId).style.display="table-row";
		}
		else document.getElementById(nomId).style.display="none";
	}
	return true;
};


/***************************************************************************
 Retourne vrai si le paramètre est nul ou vide ou rempli de champ vide.
****************************************************************************/
function estVide(texte){	
	if(texte==null){
	return true;
	}
	else{
		if(trim(texte)=="") return true;
	}
	return false;
};

function trim(s) {
    return s.replace(/^\s+/, '').replace(/\s+$/, '');
}


/***************************************************************************
 Modifie l'état du tableau (avec croix) de la page administration.
****************************************************************************/
function modifierCroixCaseAdmin(type, nom){	
	if(type == "OUI") {
		document.getElementById(nom).value = "true";
		document.getElementById("TDOUI"+nom).className = "tdOUItrue";
		document.getElementById("TDNON"+nom).className = "tdNONfalse";
	} else {
		document.getElementById(nom).value = "false";
		document.getElementById("TDOUI"+nom).className = "tdOUIfalse";
		document.getElementById("TDNON"+nom).className = "tdNONtrue";
	}
	
};

/***************************************************************************
 Modifie l'état du tableau (avec croix) du QCM1.
****************************************************************************/
function modifierCroixQCM1(question, choix){	
	document.getElementById("QCM1"+question).value = choix;
	for (var i = 1; i < 6; i++) {
		if(i == choix) {
			document.getElementById("TD"+question+"."+i).className = "QCM1CroixAvec"+i;
		}
		else document.getElementById("TD"+question+"."+i).className = "QCM1CroixSans"+i;
	}	
};


/***************************************************************************
 Permet d'ouvrir une popup centrée sur l'écran.
****************************************************************************/
function popupcentree(page,largeur,hauteur,options) {     
	var top=(screen.height-hauteur)/2;     
	var left=(screen.width-largeur)/2;     
	window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options); 
} 

// Pour exportation PDF CDT...

if(location.pathname.indexOf("Identification") < 0 && location.pathname.indexOf("Accueil") < 0 && location.pathname.indexOf("Communication") < 0
&& location.pathname.indexOf("AjoutActualite") < 0 && location.pathname.indexOf("ModifActualite") < 0 && location.pathname.indexOf("ConnexionENT") < 0 
&& location.pathname.indexOf("AutresServices") < 0 && location.pathname.indexOf("Etablissement") < 0 && location.pathname.indexOf("Mediatheque") < 0 
&& location.pathname.indexOf("EDT") < 0 && location.pathname.indexOf("VieScolaire") < 0 && location.pathname.indexOf("CahierDeTexte") < 0 
&& location.pathname.indexOf("Bureautique") < 0 && location.pathname.indexOf("GestionDesMoyens") < 0 && location.pathname.indexOf("Administration") < 0 
&& location.pathname.indexOf("Absences") < 0 && location.pathname.indexOf("Stat") < 0 && location.pathname.indexOf("Contrat") < 0 
&& location.pathname.indexOf("SaisieDesNotes") < 0 && location.pathname.indexOf("Situation") < 0 && location.pathname.indexOf("Repartition") < 0 
&& location.pathname.indexOf("Evolution") < 0 && location.pathname.indexOf("Releve") < 0 && location.pathname.indexOf("Bulletin") < 0 
&& location.pathname.indexOf("Cantine") < 0 && location.pathname.indexOf("Trombinoscope") < 0 && location.pathname.indexOf("Assistance") < 0 
&& location.pathname.indexOf("ModuleCentral") < 0 && location.pathname.indexOf("ConseilDeClasse") < 0 && location.pathname.indexOf("PartiePublique") 
&& location.pathname.indexOf("Actualites") < 0 && location.pathname.indexOf("PartiePublique") < 0) {
	Event.observe(window, 'load', allpdfencours);
}

function allpdfencours(){
	if ( recuperationsession('AllPDFenCours') == "true" ) {
		if ( parseInt(recuperationsession('PDFenCours')) != -1 ) {
			setTimeout("allpdfencours();", 1000);
		}else {
			genererAllPDF(recuperationsession('IndexClasse'));
		}
	}
};

function recuperationsession(data) {
	var donnees = 'attribut='+data;
	var retour;
	new Ajax.Request(
			'/LaureatsNet/RecuperationSession',
			{
				method: 'post',
				asynchronous: false,
				postBody: donnees,
				onLoading: function() {
					},
				onSuccess: function(transport) { 
						retour = transport.responseText;
					},
				onFailure: function() { 
						alert('Requête échouée sessioooooon.');

					}
			}
		);
	return retour;
};

//function dateFR() {
//	var date = new Date();
//	var dateFR;
//	if (date.getDate() <= 9 )
//		dateFR = "0"+date.getDate();
//	else
//		dateFR = date.getDate();
//	dateFR += "/";
//	if ((date.getMonth()+1) <= 9 )
//		dateFR += "0"+(date.getMonth()+1);
//	else
//		dateFR += (date.getMonth()+1);
//	dateFR += "/";
//	dateFR += date.getFullYear();
//	
//	return dateFR;
//};


function genererAllPDF(indexClasse){
	griser(true);
	var classe;
	var index = parseInt(indexClasse);
		
		new Ajax.Request(
				'/LaureatsNet/ImpressionAllCDT',
				{
					method: 'post',
					asynchronous: true, 
					postBody: 'IndexClasse='+index,
					onLoading: function() {
						if ( $("BoutonGenererCDT") != null ) {
							$("BoutonGenererCDT").update("<input id=\"annulerCDT\" type=\"button\" value=\"Annuler la génération des CDT\" onclick=\"annuler();\" >\n");
							$("genererZIP").disabled = true;
						}
					},
					onSuccess: function(transport) { 
						if (transport.responseText == "Annuler") {
							if ( $("BoutonGenererCDT") != null ) {
								$("BoutonGenererCDT").update("<input id=\"genererCDT\" type=\"button\" value=\"Générer tous les CDT\" onclick=\"if (window.confirm('La génération de tous les cahiers de textes peut prendre du temps ( 1 min environ pour un cahiers de texte volumineux ).\\nVoulez-vous continuez ?')) return genererAllPDF('0');\" >\n<input id=\"genererCDT\" type=\"button\" value=\"Reprendre la génération des CDT\" onclick=\"genererAllPDF('"+index+"');\" >\n");
								$("genererZIP").disabled = false;
								griser(false);
							}
						}else {
							new Ajax.Request(
									'/LaureatsNet/ImpressionCDT',
									{
										method: 'post',
										asynchronous: true, 
										postBody: transport.responseText,
										onLoading: function() {
											classe = recuperationsession('Classe');
											if ( $("telecharger"+classe) != null )
												$("telecharger"+classe).update("<img src=\"icons/ajax-loader.gif\">");
											},
										onSuccess: function() { 
												if ( $("telecharger"+classe) != null ) {
												var date = dateFR();
													$("telecharger"+classe).update("<input type=\"button\" value=\"Télécharger\" onclick=\"if (!this.isContentEditable) return !window.open('RecuperationPDF?vv=v&Classe="+classe+"','blank');\">\n");
													$("date"+classe).update(date);
												}
												index++;
												if ( index != parseInt(recuperationsession('NbClasses')) )
													setTimeout("genererAllPDF('"+index+"');",1000);
												else {
													if ( $("BoutonGenererCDT") != null ) {
														griser(false);
														$("BoutonGenererCDT").update("<input id=\"genererCDT\" type=\"button\" value=\"Générer tous les CDT\" onclick=\"genererAllPDF('0');\" >\n");
														$("genererZIP").disabled = false;
													}
												}
												
											},
										onFailure: function() { 
												alert('Requête échouée PDF.');
						
											}
									}
								);
							}
						},
					onFailure: function() { 
							alert('Requête échouée AllPDF.');
						}
				}
			);
};				



