// Content-type: text/javascript; charset=iso-8859-1

//****************************************************************************//
// Valide une adresse email.
//****************************************************************************//
function isValidEmail(email) {
	if(email.search(/^[-a-zA-Z0-9._]*[-a-zA-Z0-9_]@[-a-zA-Z0-9_]+(\.[-a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/)!=0) {
		return false;
	} else {
		return true;
	}
}

//****************************************************************************//
// Centre un élément.
//****************************************************************************//
function centerThis(element) {
	var elementTop, elementLeft;

	// Dimensions de la fenêtre
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();

	// Dimensions de l'objet à centrer en tenant compte des paddings
	var elementHeight = $(element).height()
						+ myParseFloat($(element).css('padding-top'))
						+ myParseFloat($(element).css('padding-bottom'));
	var elementWidth = $(element).width()
						+ myParseFloat($(element).css('padding-left'))
						+ myParseFloat($(element).css('padding-right'));


	if (windowHeight <= elementHeight) {
		elementTop = $(window).scrollTop();
	} else {
		elementTop = ((windowHeight - elementHeight) / 2) + $(window).scrollTop();
	}

	if (windowWidth <= elementWidth) {
		elementLeft = $(window).scrollLeft();
	} else {
		elementLeft = ((windowWidth - elementWidth) / 2) + $(window).scrollLeft();
	}

	$(element).css({
		'top': elementTop,
		'left': elementLeft
	});
}

//****************************************************************************//
// Affiche une boite de message.
//****************************************************************************//
function showAlertBox (HTMLToDisplay, Width, Height, CloseLinkText) {
	// Suppression d'une éventuelle AlertBox déjà affichée
	$('#AlertBox').remove();

	// Affichage du "voile" en background
	showBG('AlertBox_BG', '#FFFFFF', '2000');

	// Insertion de l'AlertBox
	$('body').prepend('<div id="AlertBox">' +
						'<div id="AlertBox_load" style="color: #000000; list-style-type: none;"></div>' +
						'<div id="AlertBox_ctrl" style="text-align: center; font-family: Arial;"><a href="#" id="closeAlertBox">' + CloseLinkText + '</a></div>' +
					'</div>');
	// Stylage de l'AlertBox
	$('#AlertBox').css({	display: 'none', zIndex: '2001',
							position: 'absolute', top: '0', left: '0',
							width: Width + 'px',
							height: Height + 'px',
							backgroundColor: '#EBF7FD'
						});
	$('#AlertBox_load').css({	fontFamily: 'Arial', fontSize: '12px', padding: '30px' });
	// Bind des clics de fermeture
	$('#closeAlertBox').click(function () {
		$('#AlertBox_BG').remove();
		$('#AlertBox').remove();
		return false;
	});
/*
	$('#AlertBox_BG').click(function () {
		$('#AlertBox_BG').remove();
		$('#AlertBox').remove();
		return false;
	});
*/
	// Centrage de l'AlertBox
	centerThis('#AlertBox');
	// Chargement du HTML dans l'AlertBox
	$('#AlertBox #AlertBox_load').html(HTMLToDisplay);
	// FadeIn de l'AlertBox
	$('#AlertBox').fadeIn();
}

//****************************************************************************//
// Charge une URL dans une boite.
//****************************************************************************//
function showDisplayBox (URLToLoad, Width, Height, CloseLinkText, DisplayBoxClass) {
	// Suppression d'une éventuelle DisplayBox déjà affichée
	$('#DisplayBox').remove();

	// Affichage du "voile" en background
	showBG('DisplayBox_BG', '#FFFFFF', '1000');

	// Insertion de la DisplayBox
	var DisplayBoxHTML = '';
	DisplayBoxHTML += '<div id="DisplayBox"';
	if (DisplayBoxClass != '') DisplayBoxHTML += ' class="' + DisplayBoxClass + '"';
	DisplayBoxHTML += '>';
//	if (CloseLinkText != '') DisplayBoxHTML += '<div id="DisplayBox_ctrl"><a href="#" id="closeDisplayBox">' + CloseLinkText + '</a></div>';
	DisplayBoxHTML += '<div id="DisplayBox_ctrl"><a href="#" id="closeDisplayBox"><img src="images/btn_close_form.png" border="0" alt="Fermer"/></a></div>';
	DisplayBoxHTML += '<div id="DisplayBox_load"></div>';
	DisplayBoxHTML += '</div>';

	$('body').prepend(DisplayBoxHTML);
	// Stylage de la DisplayBox
	Height = Height - 80; // Un peu moche mais Buddy a mis du padding-top sur la DisplayBox...
	$('#DisplayBox').css({	display: 'none', zIndex: '1001',
							position: 'absolute', top: '0', left: '0',
							width: Width + 'px',
							height: Height + 'px'
						});
	// Bind des clics de fermeture
	$('#closeDisplayBox').click(function () {
		$('#DisplayBox_BG').remove();
		$('#DisplayBox').remove();
		return false;
	});
/*
	$('#DisplayBox_BG').click(function () {
		$('#DisplayBox_BG').remove();
		$('#DisplayBox').remove();
		return false;
	});
*/
	// Centrage de la DisplayBox
	centerThis('#DisplayBox');
	// Chargement de l'URL dans la DisplayBox
	$('#DisplayBox #DisplayBox_load').load(URLToLoad);
	// FadeIn de la DisplayBox
	$('#DisplayBox').fadeIn();
}

//****************************************************************************//
// Affiche un "voile" opacitï¿½ 0.8 en fond d'ï¿½cran.
//****************************************************************************//
function showBG (BG_Id, BG_Color, BG_zIndex) {
	// Suppression d'un éventuel "voile" déjà affiché
	$('#' + BG_Id).remove();
	// Insertion du "voile" en background
	$('body').prepend('<div id="' + BG_Id + '"></div>');
	// Stylage du background
	$('#' + BG_Id).css({	display: 'none', zIndex: BG_zIndex,
							position: 'absolute', top: '0', left: '0',
							width: $(document).width() + 'px',
							height: $(document).height() + 'px',
							backgroundColor: BG_Color,
							opacity: '0.8'
						});
	// FadeIn du background
	$('#' + BG_Id).fadeIn();
}


//****************************************************************************//
function myParseFloat(sNumber) {
	if (sNumber != undefined) {
		if (!isNaN(parseFloat(sNumber)))
			return parseFloat(sNumber);
		else {
			return (parseFloat((sNumber.toString()).substring(0, (sNumber.toString()).length - 2)));
		}
	}
}

//****************************************************************************//
/* Client-side access to querystring name=value pairs
	Version 1.3
	28 May 2008

	License (Simplified BSD):
	http://adamv.com/dev/javascript/qslicense.txt
*/
function QueryString(qs) { // optionally pass a querystring to parse
	this.params = {};

	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &

// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);

		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;

		this.params[name] = value;
	}
}

QueryString.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

QueryString.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}
