// JavaScript Document
function checkMail(email, msg)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) {
		return(true);
	} else {
		if (msg != "" && msg != null) {
			alert(msg);
		}
		return(false);
	}
}
// **********************************************
function showLayer(num) {
	var i;
	var layerBase = "ml";
	var layer;
	if (typeof(num) != "undefined" && num == 0) {
		if(layer = document.getElementById(layerBase+num)) {
			layer.style.visibility = 'visible';
		}
		return;
	}
	for(i=0; i<=10; i++) {
		if(layer = document.getElementById(layerBase+i)) {
			layer.style.visibility = (num == i ? 'visible' : 'hidden');
		}
	}
}
// **********************************************
function PPresetSearch() {
	var a_select = new Array("mp_s_service","mp_s_domaine","mp_s_period");
	for (var i=0; i<a_select.length; i++) {
		$(a_select[i]).options[0].selected = true;
	}
}
// **********************************************
function PPstep2check() {
	
	if (a_Sessions[0].getNumSelected() < a_Sessions.length) {
		// L'utilisateur à déselectionné une ou plusieurs sessions,
		// Il doit valider ces changements
		alert("Vous avez déselectionné des sessions.\n\nVeuillez valider vos changements avant de pouvoir passer à l'étape suivante.");
		return(false);
	}
	
	if (a_Sessions.length < 3) {
		if (conf("Il y a "+(3 - a_Sessions.length)+" plage(s) horaire(s) ne comportant aucun atelier.\n\nEtes-vous sûr de vouloir continuer avec ce programme ?")) {
			return(true);
		} else {
			return(false);
		}
	}
	return(true);
}
// **********************************************
function conf(message) {
	return(confirm(message));
}
// *********************************************

// **********************************************
// OBJET Session
function Session(element) {
	this.element =  element;
	this.element.oSession = this;
	
	this.id_session = parseInt(this.element.id.substr(4));
	
	this.initClassName = this.element.down().className;
	
	this.selected = false;
	this.over = false;
	
	this.radio = this.element.select('input.radio01')[0];
	this.group = parseInt(this.radio.name.substr(7));
	
	// lien infos
	var links = this.element.select('a.infosLink');
	if(typeof(links[0]) != "undefined") {
		links[0].oSession = this;
	}
	// this.element.select('a.infosLink')[0].oSession = this;
	this.element.select('div.infos01').each(function (elm) {
			elm.style.visibility='hidden';
			// elm.style.display='none';
	})
	
	this.initEvents();
	this.initState();
}
Session.prototype.initState = function() {
	this.setSelected(this.radio.checked);
}
Session.prototype.initEvents = function() {
	this.element.onmouseover = function() {
		this.oSession.over = true;
		this.oSession.refreshDisplay();
	}
	this.element.onmouseout = function() {
		this.oSession.over = false;
		this.oSession.refreshDisplay();
	}
	this.element.onclick = function() {
		if (!this.oSession.getSelected()) {
			// pas sélectionnée, on sélectionne
			this.oSession.setSelected(true);
		} else {
			// selectionné, on désélectioonne
			this.oSession.setSelected(false);
		}
	}
}
Session.prototype.refreshDisplay = function() {
	if (this.selected) {
		this.element.down().className = (this.over ? 'ses01o' : 'ses01s');
		this.element.down().className = 'ses01s';
	} else {
		this.element.down().className = this.initClassName;
		this.element.down().className = (this.over ? 'ses01o' : this.initClassName);
	}
}
Session.prototype.setSelected = function(selected) {
	this.selected = selected;
	this.radio.checked = selected;
	if (selected) {
		this.updateGroup();
	}
	this.refreshDisplay();
}
Session.prototype.getSelected = function() {
	return(this.selected);
}
Session.prototype.updateGroup = function() {
	for (var i=0; i<a_Sessions.length; i++) {
		if (a_Sessions[i].group == this.group && a_Sessions[i].id_session != this.id_session) {
			a_Sessions[i].setSelected(false);
		}
	}
}
Session.prototype.getNumSelected = function() {
	var num = 0;
	for (var i=0; i<a_Sessions.length; i++) {
		if (a_Sessions[i].getSelected()) {num++;}
	}
	return(num);
}
// **********************************************
function PPmoveBasket() {
	var minTop = 345;
	if($('panier')) {
		// elmViewportH = $('panier').viewportOffset().top + $('panier').getHeight();
		// elmViewportH = document.viewport.getDimensions().height - ($('panier').viewportOffset().top + $('panier').getHeight());
		
		// $('panier').style.marginTop = document.viewport.getScrollOffsets().top+'px';
		// $('panier').innerHTML = top;
		$('panier').style.top = (document.viewport.getScrollOffsets().top < minTop ? '' : document.viewport.getScrollOffsets().top+60+'px');
		// $('panier').down().innerHTML = $('panier').down().viewportOffset().top;
	}
}
// **********************************************
function PPinfosInitEvents() {
	var links = $$('a.infosLink');
	if (typeof(links) != "undefined") {
		links.each(function(a) {
			a.infosDiv = $('infos'+a.oSession.id_session);
			a.onmouseover = function() {
				/*
				var offsetH = document.viewport.getDimensions().height - (this.infosDiv.viewportOffset().top + this.infosDiv.getHeight());
				if(offsetH < 0) {
					this.infosDiv.style.marginTop = (offsetH-10)+'px';
				} else {
					this.infosDiv.style.marginTop = '';
				}
				*/
				// this.infosDiv.style.display  = '';
				this.infosDiv.style.visibility  = '';
			};
			a.onmouseout = function() {
				// this.infosDiv.style.display  = 'none';
				this.infosDiv.style.visibility  = 'hidden';
				// this.infosDiv.style.marginTop = '';
			}
		});
	}
}
// **********************************************
function initIELayerForm() {
	
	// $('ml1', 'ml2', 'ml3', 'ml4', 'ml5', 'ml6', 'ml7', 'ml8')
	$('ml1', 'ml2', 'ml3', 'ml4', 'ml5', 'ml6', 'ml7', 'ml8').each(function (layerDiv) {
			if (typeof(layerDiv) != 'undefined') {
				alert(layerDiv.tagName);
				layerDiv.select('a').each(function (menuLink) {
					menuLink.observe('mouseover', function(event){
						alert(Event.element(event));
					});
				});
			}
		});
	/*
	$('ml1', 'ml2', 'ml3', 'ml4', 'ml5', 'ml6', 'ml7', 'ml8').each(function(layerDiv) {
		layerDiv.observe('mouseover', function(event){
			// alert(Event.element(event).innerHTML);
			alert(Event.element(event));
		 });
	});
	*/
}
// **********************************************
function PPinit() {

	a_Sessions = new Array;
	a_sessions = $$('div.session');
	for (var i=0; i<a_sessions.length; i++) {
		a_Sessions.push(new Session(a_sessions[i]));
	}

	PPinfosInitEvents();
	// initIELayerForm();
	
}
// **********************************************
function isRegistrationClosed() {
	var date_limit = new Date(2008,3-1,6,20,0,0,0);
	var date_now = new Date();
	// alert("limit : "+date_limit.getTime()+"\nnow : "+date_now.getTime());
	return(date_now.getTime() > date_limit.getTime());
}
// **********************************************
Event.observe(window, 'load', PPinit);
Event.observe(window, 'scroll', PPmoveBasket);

