/* ////////////// COMMON ////////////// */

function getObj(name){
	if (document.getElementById){
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all){
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
}

function writeFlash (url,w,h) {
	document.write('<object type="application/x-shockwave-flash" data="'+url+'" width="'+w+'" height="'+h+'"><param name="movie" value="'+url+'" /><param name="wmode" value="transparent" /></object>');
}

function roll (obj) {
	obj = obj.getElementsByTagName("img")[0];
	extension = "." + obj.src.substring(obj.src.length-3,obj.src.length);
	state = (!Number(obj.src.charAt(obj.src.length-5))) ?1 :0;
	obj.src = obj.src.substring(0,obj.src.length-5) + state + extension;
}

function isMail(mail) {
	if(mail == null) return null;
	if(mail.match(/^.+@.+\..+$/)) return mail;
	else return null;
}

function returnError (input,msg) {
	alert(msg);
	input.focus();
}

/* //////////////////////////////////// */

/* ///////////// NAVIGATION ///////////// */

function initNavLeft () {
	navLeftData = [];
	navLeft = new nav(navLeftData);
		navLeft.addItem("1","/nos-engagements/index.asp","");
		navLeft.addItem("2","¤","");
			navLeft.addItem("2_1","/partager-avis-envies/contact.asp",["/partager-avis-envies/confirmation.asp"]);
			navLeft.addItem("2_2","/partager-avis-envies/temoignages.asp",["/partager-avis-envies/temoignages-theme.asp"]);
		navLeft.addItem("3","/etablissements-proches/index.asp","");
		navLeft.addItem("4","/espace-presse/index.asp","");
			navLeft.addItem("4_1","/espace-presse/telechargements.asp","");
			navLeft.addItem("4_2","/espace-presse/revue-presse.asp","");
			navLeft.addItem("4_3","/espace-presse/agenda.asp","");
			navLeft.addItem("4_4","/espace-presse/contact.asp","");
	navLeft.checkUrl();
}

function nav (store) {
	this.store = store;
	this.url = document.location.href;
	this.id = -1;
}

nav.prototype.addItem = function (id,url,extraUrl) {
	this.store[this.store.length] = {id:id,url:url,extraUrl:extraUrl};
}

nav.prototype.checkUrl = function () {
	for (var i=0;i<this.store.length&&this.id==-1;i++) {
		if (this.url.indexOf(this.store[i].url)!=-1) this.id = this.store[i].id;
		for (var j in this.store[i].extraUrl) if (this.url.indexOf(this.store[i].extraUrl[j])!=-1) this.id = this.store[i].id;
	}
	this.activeObject();
}

nav.prototype.activeObject = function () {
	
	this.idRoot = this.id.charAt(0);
	roll(new getObj("n"+this.idRoot).obj);
	
	if (this.id.length>1) {
		this.storeHeight = 0;
		roll(new getObj("sn"+this.id).obj);
		for (var i=0;i<this.store.length;i++) if (this.store[i].id.charAt(0)==this.idRoot&&this.store[i].id.length>1) this.storeHeight += new getObj("sn"+this.store[i].id).obj.offsetHeight;
		new getObj("n"+this.idRoot).obj.style.height = new getObj("n"+this.idRoot).obj.style.height + this.storeHeight + "px";
		new getObj("n"+this.idRoot+"_1").obj.style.visibility = "visible";
		new getObj("mask"+this.idRoot+"_1").obj.style.visibility = "visible";
	}
	
}

/* //////////////////////////////////// */

/* ////////// NOS ENGAGEMENTS ////////// */

function engagementsSlide (nb) {
	this.nb = nb;
	this.id = 0;
	this.oldId = -1;
	this.previous = new getObj("previous").obj;
	this.next = new getObj("next").obj;
	this.container = new getObj("engagementsInfos").obj;
	this.show();
}

engagementsSlide.prototype.precedent = function () {
	this.id--;
	this.show();
}

engagementsSlide.prototype.suivant = function () {
	this.id++;
	this.show();
}

engagementsSlide.prototype.show = function () {
	this.previous.style.display = (!this.id) ?"none" :"block";
	this.next.style.display = (this.id>=this.nb-1) ?"none" :"block";
	if (this.oldId!=-1) {
		this.container.getElementsByTagName("h5")[this.oldId].style.display = "none";
		this.container.getElementsByTagName("p")[this.oldId].style.display = "none";
	}
	this.container.getElementsByTagName("h5")[this.id].style.display = "block";
	this.container.getElementsByTagName("p")[this.id].style.display = "block";
	this.oldId = this.id;
}

/* //////////////////////////////////// */

/* ////////// CONTACT ////////// */

function envoyerMessage () {
	
	with (document.contact) {
		
		if (NOM.value=="") { returnError(NOM,"Veuillez saisir votre nom"); return false; }
		else if (PRENOM.value=="") { returnError(PRENOM,"Veuillez saisir votre prénom"); return false; }
		else if (EMAIL.value=="") { returnError(EMAIL,"Veuillez saisir votre e-mail"); return false; }
		else if (!isMail(EMAIL.value)) { returnError(EMAIL,"Veuillez saisir un e-mail valide"); return false; }
		else if (FK_ID_SALLE.value=="") { returnError(FK_ID_SALLE,"Veuillez saisir un type de message"); return false; }
		else if (SUJET.value=="") { returnError(SUJET,"Veuillez saisir un titre de message"); return false; }
		else if (MESSAGE.value=="") { returnError(MESSAGE,"Veuillez saisir un message"); return false; }
		else return true;
	}
	
}

/* ////////////// SONDAGE ////////////////////// */
function envoyerReponse () {
	
	with (document.reponse) {
		
		if (NOM.value=="") { returnError(NOM,"Veuillez saisir votre nom"); return false; }
		else if (EMAIL.value=="") { returnError(EMAIL,"Veuillez saisir votre e-mail"); return false; }
		else if (!isMail(EMAIL.value)) { returnError(EMAIL,"Veuillez saisir un e-mail valide"); return false; }
		else if (ETABLISSEMENT.value=="") { returnError(ETABLISSEMENT,"Veuillez sélectionner un établissement"); return false; }
		else if (DATE.value=="") { returnError(DATE,"Veuillez saisir une date"); return false; }
		else if (!isDate(DATE.value)) { returnError(DATE,"Veuillez saisir une date correcte (format : jj/mm/yyyy)"); return false; }
		else return true;
	}
	
}
function envoyerAvis () {
	
	with (document.contact) {
		
		if (NOM.value=="") { returnError(NOM,"Veuillez saisir votre nom"); return false; }
		else if (PRENOM.value=="") { returnError(PRENOM,"Veuillez saisir votre prénom"); return false; }
		else if (EMAIL.value=="") { returnError(EMAIL,"Veuillez saisir votre e-mail"); return false; }
		else if (!isMail(EMAIL.value)) { returnError(EMAIL,"Veuillez saisir un e-mail valide"); return false; }
		else if (DATE.value=="") { returnError(DATE,"Veuillez saisir une date"); return false; }
		else if (!isDate(DATE.value)) { returnError(DATE,"Veuillez saisir une date correcte (format : jj/mm/yyyy)"); return false; }
		else if (SUJET.value=="") { returnError(SUJET,"Veuillez saisir un titre de message"); return false; }
		else if (MESSAGE.value=="") { returnError(MESSAGE,"Veuillez saisir un message"); return false; }
		else return true;
	}
	
}

function isDate(d) {
	// Cette fonction permet de vérifier la validité d'une date au format jj/mm/aa ou jj/mm/aaaa
	// Par Romuald
	
	if (d == "") // si la variable est vide on retourne faux
	return false;
	
	e = new RegExp("^[0-9]{1,2}\/[0-9]{1,2}\/([0-9]{2}|[0-9]{4})$");
	
	if (!e.test(d)) // On teste l'expression régulière pour valider la forme de la date
	return false; // Si pas bon, retourne faux
	
	// On sépare la date en 3 variables pour vérification, parseInt() converti du texte en entier
	j = parseInt(d.split("/")[0], 10); // jour
	m = parseInt(d.split("/")[1], 10); // mois
	a = parseInt(d.split("/")[2], 10); // année
	
	// Si l'année n'est composée que de 2 chiffres on complète automatiquement
	if (a < 1000) {
	if (a < 89) a+=2000; // Si a < 89 alors on ajoute 2000 sinon on ajoute 1900
	else a+=1900;
	}
	
	// Définition du dernier jour de février
	// Année bissextile si annnée divisible par 4 et que ce n'est pas un siècle, ou bien si divisible par 400
	if (a%4 == 0 && a%100 !=0 || a%400 == 0) fev = 29;
	else fev = 28;
	
	// Nombre de jours pour chaque mois
	nbJours = new Array(31,fev,31,30,31,30,31,31,30,31,30,31);
	
	// Enfin, retourne vrai si le jour est bien entre 1 et le bon nombre de jours, idem pour les mois, sinon retourn faux
	return ( m >= 1 && m <=12 && j >= 1 && j <= nbJours[m-1] );
} 




/* ////////// ETABLISSEMENTS ////////// */

function voirEtablissement (val) {

	switch(val.id)
	{
		case "region" :
			with(document.formEtablissement.ville)
			{ options[0].selected=true; disabled = true; }
			with(document.formEtablissement.etablissement)
			{ options[0].selected=true; disabled = true; }
		case "ville" :
			with(document.formEtablissement.etablissement)
			{ options[0].selected=true; disabled = true; }
	}

	tmp = document.formEtablissement.etablissement;
	if (val=="") val = tmp.options[tmp.selectedIndex].value;
	if (val!="") document.formEtablissement.submit();
}

function gallery (objName,container,startId,isLoop,speed) {
	this.data = [];
	this.objName = objName;
	this.container = new getObj(container).obj;
	this.id = startId;
	this.isLoop = isLoop;
	this.speed = speed;
	this.intChange = "";
}

gallery.prototype.add = function (url) {
	this.data[this.data.length] = {url:url};
}

gallery.prototype.init = function () {
	this.nbImg = this.data.length;
	this.show(this.id);
	if (this.isLoop) this.loop();
}

gallery.prototype.loop = function () {
	this.intChange = setInterval(""+this.objName+".next()",this.speed);
}

gallery.prototype.next = function () {
	this.id = (this.id<this.nbImg-1) ?this.id+1 :0;
	this.change();
}

gallery.prototype.change = function () {
	this.container.src = this.data[this.id].url;
}

gallery.prototype.show = function (id) {
	this.container.src = this.data[id].url;
}

/* //////////////////////////////////// */

function voirMentionsLegales () {
	window.open("mentions-legales.htm","mentionsLegales","width=500,height=400,scrollbars=yes");
}