
/**
 * Initialise une variable globale pour l'objet Ajax
 */
var ajaxObj;
var ajaxObj2;

/**
 * Initialise une variable globale pour le div ciblé
 */
var divTarget;
var divTarget2;

/**
 * Initialisation de l'objet Ajax.
 *
 * @param bool $alt Objet Ajax alternatif (true/false)
 *
 * @uses ajaxObj
 */
function initAjax(alt)
{
	var obj;

	try { //Firefox, Opera 8.0+, Safari, IE7
		obj = new XMLHttpRequest();
	} catch(e) { //IE6 et moins
		try {
			obj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				obj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				alert("Votre navigateur n'est pas supporté");
				return false;
			}
		}
	}

	if(alt == true) {
		ajaxObj2 = obj;
	} else {
		ajaxObj = obj;
	}
}

/**
 * Lancement de l'animation "loading".
 *
 * @param bool $bln Si l'animation doit être activée ou désactivée (true/false)
 */
function ajaxAnim(bln)
{
	//Vérifie si le DIV existe
	if(document.getElementById('loadingBar') == null) {
		return false;
	}

	//Change l'animation
	document.getElementById('loadingBar').style.display = (bln == true) ? "" : "none";

	return true;
}

/**
 * Arrête le chargement de la demande page.
 *
 * @uses ajaxAnim()
 * @uses ajaxObj
 */
function stopLoading()
{
	//Annule la requête précédente si nécessaire
	if(ajaxObj && ajaxObj.readyState < 4) {
		ajaxObj = null;
	}
	ajaxAnim(false);
}

function loginCheck(email, pass)
{
	//Annule la requête précédente si nécessaire
	if(ajaxObj && ajaxObj.readyState < 4) ajaxObj.abord();

	//Init AJAX
	initAjax();
	ajaxAnim(true);

	//Action
	var action = (pass == null) ? "lost" : "login";

	//Effectue la requête
	ajaxObj.onreadystatechange = loginConfirm;
	ajaxObj.open("GET", "http://agrometeo.org/inc/user.inc.php?action="+action+"&email="+email+"&pass="+pass);
	ajaxObj.send(null);
}

function loginConfirm()
{
	if(ajaxObj.readyState == 4)
	{
		//Vérifie le code retourné
		if(ajaxObj.status == 200) {
			//Applique le contenu au DIV
			response = ajaxObj.responseText;

			if(response == "__login__") {
				window.location.href = 'http://agrometeo.org/index.php?page=reserved';
			} else if(response == "__email__") {
				alert("Un courriel vous a été envoyé pour confirmer la mise à jour de votre mot de passe");
			} else {
				alert(response);
			}
		} else {
			alert("Une erreur s'est produite, veuillez essayer à nouveau plus tard");
		}

		ajaxAnim(false);
	}
}

/**
 * Appel la nouvelle page via Ajax.
 *
 * @param string $pageName Nom de la page à appeler
 * @param string $params Paramètres 'GET' à ajouter à l'appel de la page
 *
 * @uses ajaxAnim()
 * @uses pageChange()
 * @uses ajaxObj
 */
function pageRefresh(pageName, params, target, pageName2, params2, target2)
{
	if(pageName != "") {
		if(document.getElementById(target) != null) {
			//Annule la requête précédente si nécessaire
			if(ajaxObj && ajaxObj.readyState < 4) ajaxObj.abord();

			//Init AJAX
			initAjax();
			ajaxAnim(true);

			//Si des paramètres GET à ajouter à l'appel de la page
			var paramGet = (params != null) ? params : "";

			//Cible du refresh
			divTarget = (target != null) ? target : "main";

			//Effectue la requête
			ajaxObj.onreadystatechange = pageChange;
			ajaxObj.open("GET", "http://agrometeo.org/inc/"+pageName+".inc.php"+paramGet);
			ajaxObj.send(null);
		} else {
			window.location.href = pageName;
		}
	}

	if(pageName2 != null) {
		if(document.getElementById(target2) != null) {
			//Annule la requête précédente si nécessaire
			if(ajaxObj2 && ajaxObj2.readyState < 4) ajaxObj2.abord();

			//Init AJAX
			initAjax(true);

			//Si des paramètres GET à ajouter à l'appel de la page
			var paramGet = (params2 != null) ? params2 : "";

			//Cible du refresh
			divTarget2 = (target2 != null) ? target2 : "main";

			//Effectue la requête
			ajaxObj2.onreadystatechange = function() { pageChange(true) };
			ajaxObj2.open("GET", "http://agrometeo.org/inc/"+pageName2+".inc.php"+paramGet);
			ajaxObj2.send(null);
		} else {
			window.location.href = pageName2;
		}
	}
}

/**
 * Affiche le contenu de la page obtenu dans le div principal.
 *
 * @uses ajaxAnim()
 * @uses ajaxObj
 */
function pageChange(alt)
{
	var obj = (alt == true) ? ajaxObj2 : ajaxObj;
	var div = (alt == true) ? divTarget2 : divTarget;

	if(obj.readyState == 4)
	{
		//Vérifie le code retourné
		if(obj.status == 200) {
			//Si commence par "<!DOCTYPE", affiche l'accueil
			if(obj.responseText.substr(0,9) == "<!DOCTYPE") {
				document.location.href = "http://agrometeo.org/";
				return;
			} else if(obj.responseText.substr(0,8) == "REDIRECT") {
				var redirect = obj.responseText.split(String.fromCharCode(29));
				window.location.href = redirect[1];
				return;
			}

			//Applique le contenu au DIV
			document.getElementById(div).innerHTML = obj.responseText;

			//Exécute les scripts JavaScript de la page
			var x = document.getElementById(div).getElementsByTagName("script");
			for(var i=0;i<x.length;i++)
			{
				if(x[i].src != "") { //Si un JS inclus
					//TODO
				} else { //Si une fonction à exécuter
					eval(x[i].text);
				}
			}
		} else {
			alert("Une erreur s'est produite, veuillez essayer à nouveau plus tard");
		}

		ajaxAnim(false);
	}
}

var nbrElements = null;
var mainSpaceHeight = null;
var mainSpaceWidth = null;
var headerHeight = null;
var menuWidth = null;

function initView()
{
	//Obtient la largeur et la hauteur de la fenêtre
	var bodyWidth = document.body.offsetWidth;
	menuWidth = document.getElementById('sidepanel').offsetWidth;
	var bodyHeight = document.body.offsetHeight;
	headerHeight = document.getElementById('header').offsetHeight + document.getElementById('banner').offsetHeight + document.getElementById('menu').offsetHeight + document.getElementById('footer').offsetHeight;
	document.getElementById('main').style.height = (bodyHeight - headerHeight)+"px";
	document.getElementById('sidepanel').style.height = (bodyHeight - headerHeight)+"px";
	document.getElementById('main').style.width = (bodyWidth - menuWidth)+"px";
	document.getElementById('main').style.left = menuWidth+"px";
}

function populate(nbr)
{
	//Cache la barre de défilement du cadre principal
	document.getElementById('main').style.overflow = (typeof(donothideoverflow) != "undefined") ? 'auto' : 'hidden';

	//Obtient la largeur et la hauteur de la fenêtre
	var bodyWidth = document.body.offsetWidth;
	var menuWidth = document.getElementById('sidepanel').offsetWidth;
	var bodyHeight = document.body.offsetHeight;
	var headerHeight = document.getElementById('header').offsetHeight + document.getElementById('banner').offsetHeight + document.getElementById('menu').offsetHeight + document.getElementById('footer').offsetHeight;
	mainSpaceWidth = bodyWidth - menuWidth;
	mainSpaceHeight = bodyHeight - headerHeight;
	nbrElements = nbr;

	if(nbr == 1) {
		//Dimensions
		var widthElem1 = width = (bodyWidth - menuWidth);
		var heightElem1 = height = (bodyHeight - headerHeight);

		//Redimensionne le produit
		if(document.getElementById('product-elem1')) {
			document.getElementById('product-elem1').style.height = height+'px';
			document.getElementById('product-elem1').style.width = width+'px';
		}

		//Cache les éléments non nécessaire
		//~ document.getElementById('product-separator1-2').style.display = "none";
		//~ document.getElementById('product-separator2-3').style.display = "none";
		//~ document.getElementById('product-separator2-4').style.display = "none";
		//~ document.getElementById('product-separator3-4').style.display = "none";
		//~ document.getElementById('product-elem2').style.display = "none";
		//~ document.getElementById('product-elem3').style.display = "none";
		//~ document.getElementById('product-elem4').style.display = "none";
	} else if(nbr == 2) {
		//Dimensions
		var widthElem1 = widthElem2 = width = (bodyWidth - menuWidth);
		var heightElem1 = heightElem2 = height = (((bodyHeight - headerHeight) / 2) - 2);

		//Redimensionne le produit
		document.getElementById('product-elem1').style.height = height+'px';
		document.getElementById('product-elem1').style.width = width+'px';
		document.getElementById('product-elem2').style.top = (height + 4)+'px';
		document.getElementById('product-elem2').style.left = '0px';
		document.getElementById('product-elem2').style.height = height+'px';
		document.getElementById('product-elem2').style.width = width+'px';
		document.getElementById('product-elem2').style.display = "";

		//Position separateur
		document.getElementById('product-separator1-2').style.top = height+'px';
		document.getElementById('product-separator1-2').style.left = '0px';
		document.getElementById('product-separator1-2').style.width = width+'px';
		document.getElementById('product-separator1-2').style.height = '4px';
		document.getElementById('product-separator1-2').style.display = "";
		document.getElementById('product-separator1-2').style.cursor = "n-resize";

		//Cache les éléments non nécessaire
		document.getElementById('product-separator2-3').style.display = "none";
		document.getElementById('product-separator2-4').style.display = "none";
		document.getElementById('product-separator3-4').style.display = "none";
		document.getElementById('product-elem3').style.display = "none";
		document.getElementById('product-elem4').style.display = "none";
	} else if(nbr == 3) {
		//Dimensions
		var widthElem1 = width1 = (bodyWidth - menuWidth);
		var widthElem2 = widthElem3 = width2 = (((bodyWidth - menuWidth) / 2) - 2);
		var heightElem1 = heightElem2 = heightElem3 = height = (((bodyHeight - headerHeight) / 2) - 2);

		//Redimensionne le produit
		document.getElementById('product-elem1').style.height = height+'px';
		document.getElementById('product-elem1').style.width = width1+'px';
		document.getElementById('product-elem2').style.top = (height + 4)+'px';
		document.getElementById('product-elem2').style.left = '0px';
		document.getElementById('product-elem2').style.height = height+'px';
		document.getElementById('product-elem2').style.width = width2+'px';
		document.getElementById('product-elem2').style.display = "";
		document.getElementById('product-elem3').style.top = (height + 4)+'px';
		document.getElementById('product-elem3').style.left = (width2 + 4)+'px';
		document.getElementById('product-elem3').style.height = height+'px';
		document.getElementById('product-elem3').style.width = width2+'px';
		document.getElementById('product-elem3').style.display = "";

		//Position separateur
		document.getElementById('product-separator1-2').style.top = height+'px';
		document.getElementById('product-separator1-2').style.left = '0px';
		document.getElementById('product-separator1-2').style.width = width1+'px';
		document.getElementById('product-separator1-2').style.height = '4px';
		document.getElementById('product-separator1-2').style.display = "";
		document.getElementById('product-separator1-2').style.cursor = "n-resize";
		document.getElementById('product-separator2-3').style.top = (height + 4)+'px';
		document.getElementById('product-separator2-3').style.left = width2+'px';
		document.getElementById('product-separator2-3').style.width = '4px';
		document.getElementById('product-separator2-3').style.height = height+'px';
		document.getElementById('product-separator2-3').style.display = "";
		document.getElementById('product-separator2-3').style.cursor = "e-resize";

		//Cache les éléments non nécessaire
		document.getElementById('product-separator2-4').style.display = "none";
		document.getElementById('product-separator3-4').style.display = "none";
		document.getElementById('product-elem4').style.display = "none";
	} else if(nbr == 4) {
		//Dimensions
		var widthElem1 = widthElem2 = widthElem3 = widthElem4 = width = (((bodyWidth - menuWidth) / 2) - 2);
		var heightElem1 = heightElem2 = heightElem3 = heightElem4 = height = (((bodyHeight - headerHeight) / 2) - 2);

		//Redimensionne le produit
		document.getElementById('product-elem1').style.top = '0px';
		document.getElementById('product-elem1').style.left = '0px';
		document.getElementById('product-elem1').style.height = height+'px';
		document.getElementById('product-elem1').style.width = width+'px';
		document.getElementById('product-elem1').style.display = "";
		document.getElementById('product-elem2').style.top = '0px';
		document.getElementById('product-elem2').style.left = (width + 4)+'px';
		document.getElementById('product-elem2').style.height = height+'px';
		document.getElementById('product-elem2').style.width = width+'px';
		document.getElementById('product-elem2').style.display = "";
		document.getElementById('product-elem3').style.top = (height + 4)+'px';
		document.getElementById('product-elem3').style.left = '0px';
		document.getElementById('product-elem3').style.height = height+'px';
		document.getElementById('product-elem3').style.width = width+'px';
		document.getElementById('product-elem3').style.display = "";
		document.getElementById('product-elem4').style.top = (height + 4)+'px';
		document.getElementById('product-elem4').style.left = (width + 4)+'px';
		document.getElementById('product-elem4').style.height = height+'px';
		document.getElementById('product-elem4').style.width = width+'px';
		document.getElementById('product-elem4').style.display = "";

		//Position separateur
		document.getElementById('product-separator1-2').style.top = '0px';
		document.getElementById('product-separator1-2').style.left = width+'px';
		document.getElementById('product-separator1-2').style.width = '4px';
		document.getElementById('product-separator1-2').style.height = height+'px';
		document.getElementById('product-separator1-2').style.display = "";
		document.getElementById('product-separator1-2').style.cursor = "e-resize";
		document.getElementById('product-separator2-3').style.top = height+'px';
		document.getElementById('product-separator2-3').style.left = '0px';
		document.getElementById('product-separator2-3').style.width = width+'px';
		document.getElementById('product-separator2-3').style.height = '4px';
		document.getElementById('product-separator2-3').style.display = "";
		document.getElementById('product-separator2-3').style.cursor = "n-resize";
		document.getElementById('product-separator2-4').style.top = height+'px';
		document.getElementById('product-separator2-4').style.left = (width + 4)+'px';
		document.getElementById('product-separator2-4').style.width = width+'px';
		document.getElementById('product-separator2-4').style.height = '4px';
		document.getElementById('product-separator2-4').style.display = "";
		document.getElementById('product-separator2-4').style.cursor = "n-resize";
		document.getElementById('product-separator3-4').style.top = (height + 4)+'px';
		document.getElementById('product-separator3-4').style.left = width+'px';
		document.getElementById('product-separator3-4').style.width = '4px';
		document.getElementById('product-separator3-4').style.height = height+'px';
		document.getElementById('product-separator3-4').style.display = "";
		document.getElementById('product-separator3-4').style.cursor = "e-resize";

	}

	//Navigateur
	var agent = navigator.userAgent.toLowerCase();

/*
	var elem1 = "";
	if(agent.indexOf("msie") != -1) {
		elem1 = "<object id=\"produit1\" classid=\"CLSID:2FAB7A45-2F53-47CE-9F4D-BA381F6BF609\" style='width: "+widthElem1+"px; height: "+heightElem1+"px;' standby=\"Chargement du produit ...\">"+
						"<param name=\"type\" value=\"text/wxscript\">"+
						"<param name=\"src\" value=\"http://feux.mesonet-quebec.org/wxscripts/generic.wxscript.php?id=3&amp;legende=true\">"+
						"<param name=\"legendAlign\" value=\"right\">"+
						"<param name=\"color\" value=\"#EDE6DAFF\">"+
						"<param name=\"caption\" value=\"Feux Actifs\">"+
						"<param name=\"captionAlign\" value=\"top\">"+
						"<param name=\"captionDateFormat\" value=\"%a, %I:%M %p %Z\">"+
						"<param name=\"captionProductIndex\" value=\"1\">"+
						"<param name=\"captionBackground\" value=\"#EDE6DAFF\">"+
						"<param name=\"captionFontSize\" value=\"9\">"+
					"</object>";
	} else {
		elem1 = "<embed style='width: "+widthElem1+"px; height: "+heightElem1+"px;' "+
				"id=\"produit1\" "+
				"legendAlign=\"right\" "+
				"caption=\"Feux Actifs\" captionAlign=\"top\" captionDateFormat=\"G&eactue;n&eactue;r&eactue; le %d %b %Y &agrave; %I:%M %p %Z\" captionProductIndex=\"1\" captionBackground=\"#EDE6DAFF\" captionFontSize=\"9\" "+
				"src=\"http://feux.mesonet-quebec.org/wxscripts/generic.wxscript.php?id=3&legende=true\" "+
			">";
	}
	document.getElementById('product-elem1').innerHTML = elem1;

	if(nbr > 1) {
		var elem2 = "";
		if(agent.indexOf("msie") != -1) {
			elem2 = "<object id=\"produit2\" classid=\"CLSID:2FAB7A45-2F53-47CE-9F4D-BA381F6BF609\" style='width: "+widthElem2+"px; height: "+heightElem2+"px;' standby=\"Chargement du produit ...\">"+
							"<param name=\"type\" value=\"text/wxscript\">"+
							"<param name=\"src\" value=\"http://feux.mesonet-quebec.org/wxscripts/generic.wxscript.php?id=3&amp;legende=true\">"+
							"<param name=\"legendAlign\" value=\"right\">"+
							"<param name=\"color\" value=\"#EDE6DAFF\">"+
							"<param name=\"caption\" value=\"Feux Actifs\">"+
							"<param name=\"captionAlign\" value=\"top\">"+
							"<param name=\"captionDateFormat\" value=\"%a, %I:%M %p %Z\">"+
							"<param name=\"captionProductIndex\" value=\"1\">"+
							"<param name=\"captionBackground\" value=\"#EDE6DAFF\">"+
							"<param name=\"captionFontSize\" value=\"9\">"+
						"</object>";
		} else {
			elem2 = "<embed style='width: "+widthElem2+"px; height: "+heightElem2+"px;' "+
								"id=\"produit2\" "+
								"legendAlign=\"right\" "+
								"caption=\"Feux Actifs\" captionAlign=\"top\" captionDateFormat=\"G&eactue;n&eactue;r&eactue; le %d %b %Y &agrave; %I:%M %p %Z\" captionProductIndex=\"1\" captionBackground=\"#EDE6DAFF\" captionFontSize=\"9\" "+
								"src=\"http://feux.mesonet-quebec.org/wxscripts/generic.wxscript.php?id=3&legende=true\" "+
							">";
		}
		document.getElementById('product-elem2').innerHTML = elem2;
	} else {
		document.getElementById('product-elem2').innerHTML = "";
	}

	if(nbr > 2) {
		var elem3 = "";
		if(agent.indexOf("msie") != -1) {
			elem3 = "<object id=\"produit3\" classid=\"CLSID:2FAB7A45-2F53-47CE-9F4D-BA381F6BF609\" style='width: "+widthElem3+"px; height: "+heightElem3+"px;' standby=\"Chargement du produit ...\">"+
							"<param name=\"type\" value=\"text/wxscript\">"+
							"<param name=\"src\" value=\"http://feux.mesonet-quebec.org/wxscripts/generic.wxscript.php?id=3&amp;legende=true\">"+
							"<param name=\"legendAlign\" value=\"right\">"+
							"<param name=\"color\" value=\"#EDE6DAFF\">"+
							"<param name=\"caption\" value=\"Feux Actifs\">"+
							"<param name=\"captionAlign\" value=\"top\">"+
							"<param name=\"captionDateFormat\" value=\"%a, %I:%M %p %Z\">"+
							"<param name=\"captionProductIndex\" value=\"1\">"+
							"<param name=\"captionBackground\" value=\"#EDE6DAFF\">"+
							"<param name=\"captionFontSize\" value=\"9\">"+
						"</object>";
		} else {
			elem3 = "<embed style='width: "+widthElem3+"px; height: "+heightElem3+"px;' "+
								"id=\"produit3\" "+
								"legendAlign=\"right\" "+
								"caption=\"Feux Actifs\" captionAlign=\"top\" captionDateFormat=\"G&eactue;n&eactue;r&eactue; le %d %b %Y &agrave; %I:%M %p %Z\" captionProductIndex=\"1\" captionBackground=\"#EDE6DAFF\" captionFontSize=\"9\" "+
								"src=\"http://feux.mesonet-quebec.org/wxscripts/generic.wxscript.php?id=3&legende=true\" "+
							">";
		}
		document.getElementById('product-elem3').innerHTML = elem3;
	} else {
		document.getElementById('product-elem3').innerHTML = "";
	}

	if(nbr > 3) {
		var elem4 = "";
		if(agent.indexOf("msie") != -1) {
			elem4 = "<object id=\"produit4\" classid=\"CLSID:2FAB7A45-2F53-47CE-9F4D-BA381F6BF609\" style='width: "+widthElem4+"px; height: "+heightElem4+"px;' standby=\"Chargement du produit ...\">"+
							"<param name=\"type\" value=\"text/wxscript\">"+
							"<param name=\"src\" value=\"http://feux.mesonet-quebec.org/wxscripts/generic.wxscript.php?id=3&amp;legende=true\">"+
							"<param name=\"legendAlign\" value=\"right\">"+
							"<param name=\"color\" value=\"#EDE6DAFF\">"+
							"<param name=\"caption\" value=\"Feux Actifs\">"+
							"<param name=\"captionAlign\" value=\"top\">"+
							"<param name=\"captionDateFormat\" value=\"%a, %I:%M %p %Z\">"+
							"<param name=\"captionProductIndex\" value=\"1\">"+
							"<param name=\"captionBackground\" value=\"#EDE6DAFF\">"+
							"<param name=\"captionFontSize\" value=\"9\">"+
						"</object>";
		} else {
			elem4 = "<embed style='width: "+widthElem4+"px; height: "+heightElem4+"px;' "+
								"id=\"produit4\" "+
								"legendAlign=\"right\" "+
								"caption=\"Feux Actifs\" captionAlign=\"top\" captionDateFormat=\"G&eactue;n&eactue;r&eactue; le %d %b %Y &agrave; %I:%M %p %Z\" captionProductIndex=\"1\" captionBackground=\"#EDE6DAFF\" captionFontSize=\"9\" "+
								"src=\"http://feux.mesonet-quebec.org/wxscripts/generic.wxscript.php?id=3&legende=true\" "+
							">";
		}
		document.getElementById('product-elem4').innerHTML = elem4;
	} else {
		document.getElementById('product-elem4').innerHTML = "";
	}
*/
}

function menuSwapButton(pos, name, action, lang)
{
	var over = (action == "over") ? "_over" : "";
	document.images['menu'+pos].src = "http://agrometeo.org/img/btn/"+lang+"/"+name+over+".jpg";

	if(pos > 1) {
		var prev = pos - 1;
		document.images['sep_'+prev+"-"+pos].src = "http://agrometeo.org/img/btn/separator"+((action == "over") ? "_right_over" : "")+".jpg";
	}

	var next = pos + 1;
	if(document.images['sep_'+pos+"-"+next] != null) {
		document.images['sep_'+pos+"-"+next].src = "http://agrometeo.org/img/btn/separator"+((action == "over") ? "_left_over" : "")+".jpg";
	} else if(document.images['sep_'+pos] != null) {
		document.images['sep_'+pos].src = "http://agrometeo.org/img/btn/separator_end"+((action == "over") ? "_over" : "")+".gif";
	}
}

var offX = 0;
var offY = 0;
var separator = null;
var movement = false;
document.onmouseup = Function('','resizing(null)');

function resizing(object)
{
	if(object === null) {
		movement = false;
		return false;
	}

	//Init vars
	offX = event.x - object.style.left.replace("px", "");
	offY = event.y - object.style.top.replace("px", "");
	separator = object;

	movement = true;
}

function movement()
{
	if(separator.id == "product-separator1-2") {
		if(nbrElements == 2) {
			var newY = event.y - offY;
			var newY2 = (mainSpaceHeight - newY - 4);
			if(newY > 0 && mainSpaceHeight >= newY && newY2 > 0 && mainSpaceHeight >= newY2) {
				separator.style.top = newY+'px';

				document.getElementById('product-elem1').style.height = newY+'px';
				document.getElementById('produit1').style.height = newY+'px';

				document.getElementById('product-elem2').style.top = (newY + 4)+'px';
				document.getElementById('product-elem2').style.height = newY2+'px';
				document.getElementById('produit2').style.height = newY2+'px';
			}
		} else if(nbrElements == 3) {
			var newY = event.y - offY;
			var newY2 = (mainSpaceHeight - newY - 4);
			if(newY > 0 && mainSpaceHeight >= newY && newY2 > 0 && mainSpaceHeight >= newY2) {
				separator.style.top = newY+'px';

				document.getElementById('product-elem1').style.height = newY+'px';
				document.getElementById('produit1').style.height = newY+'px';

				document.getElementById('product-elem2').style.top = (newY + 4)+'px';
				document.getElementById('product-elem2').style.height = newY2+'px';
				document.getElementById('produit2').style.height = newY2+'px';

				document.getElementById('product-elem3').style.top = (newY + 4)+'px';
				document.getElementById('product-elem3').style.height = newY2+'px';
				document.getElementById('produit3').style.height = newY2+'px';

				document.getElementById('product-separator2-3').style.top = document.getElementById('product-elem3').style.top;
				document.getElementById('product-separator2-3').style.height = (parseInt(document.getElementById('product-elem1').style.height) > parseInt(document.getElementById('product-elem3').style.height)) ? document.getElementById('product-elem1').style.height : document.getElementById('product-elem3').style.height;
			}
		} else if(nbrElements == 4) {
			var newX = event.x - offX;
			var newX2 = (mainSpaceWidth - newX - 4);
			if(newX > 0 && mainSpaceWidth >= newX && newX2 > 0 && mainSpaceWidth >= newX2) {
				separator.style.left = newX+'px';

				document.getElementById('product-elem1').style.width = newX+'px';
				document.getElementById('produit1').style.width = newX+'px';

				document.getElementById('product-elem2').style.left = (newX + 4)+'px';
				document.getElementById('product-elem2').style.width = newX2+'px';
				document.getElementById('produit2').style.width = newX2+'px';

				document.getElementById('product-separator2-3').style.width = (parseInt(document.getElementById('product-elem1').style.width) > parseInt(document.getElementById('product-elem3').style.width)) ? document.getElementById('product-elem1').style.width : document.getElementById('product-elem3').style.width;
				document.getElementById('product-separator2-4').style.left = (parseInt(document.getElementById('product-elem2').style.left) < parseInt(document.getElementById('product-elem4').style.left)) ? document.getElementById('product-elem2').style.left : document.getElementById('product-elem4').style.left;
				document.getElementById('product-separator2-4').style.width = (parseInt(document.getElementById('product-elem2').style.width) > parseInt(document.getElementById('product-elem4').style.width)) ? document.getElementById('product-elem2').style.width : document.getElementById('product-elem4').style.width;
			}
		}
	} else if(separator.id == "product-separator2-3") {
		if(nbrElements == 3) {
			var newX = event.x - offX;
			var newX2 = (mainSpaceWidth - newX - 4);
			if(newX > 0 && mainSpaceWidth >= newX && newX2 > 0 && mainSpaceWidth >= newX2) {
				separator.style.left = newX+'px';

				document.getElementById('product-elem2').style.width = newX+'px';
				document.getElementById('produit2').style.width = newX+'px';

				document.getElementById('product-elem3').style.left = (newX + 4)+'px';
				document.getElementById('product-elem3').style.width = newX2+'px';
				document.getElementById('produit3').style.width = newX2+'px';
			}
		} else if(nbrElements == 4) {
			var newY = event.y - offY;
			var newY2 = (mainSpaceHeight - newY - 4);
			if(newY > 0 && mainSpaceHeight >= newY && newY2 > 0 && mainSpaceHeight >= newY2) {
				separator.style.top = newY+'px';

				document.getElementById('product-elem1').style.height = newY+'px';
				document.getElementById('produit1').style.height = newY+'px';

				document.getElementById('product-elem3').style.top = (newY + 4)+'px';
				document.getElementById('product-elem3').style.height = newY2+'px';
				document.getElementById('produit3').style.height = newY2+'px';

				document.getElementById('product-separator1-2').style.height = (parseInt(document.getElementById('product-elem1').style.height) > parseInt(document.getElementById('product-elem2').style.height)) ? document.getElementById('product-elem1').style.height : document.getElementById('product-elem2').style.height;
				document.getElementById('product-separator3-4').style.top = (parseInt(document.getElementById('product-elem3').style.top) < parseInt(document.getElementById('product-elem4').style.top)) ? document.getElementById('product-elem3').style.top : document.getElementById('product-elem4').style.top;
				document.getElementById('product-separator3-4').style.height = (parseInt(document.getElementById('product-elem3').style.height) > parseInt(document.getElementById('product-elem4').style.height)) ? document.getElementById('product-elem3').style.height : document.getElementById('product-elem4').style.height;
			}
		}
	} else if(separator.id == "product-separator2-4") {
		var newY = event.y - offY;
		var newY2 = (mainSpaceHeight - newY - 4);
		if(newY > 0 && mainSpaceHeight >= newY && newY2 > 0 && mainSpaceHeight >= newY2) {
			separator.style.top = newY+'px';

			document.getElementById('product-elem2').style.height = newY+'px';
			document.getElementById('produit2').style.height = newY+'px';

			document.getElementById('product-elem4').style.top = (newY + 4)+'px';
			document.getElementById('product-elem4').style.height = newY2+'px';
			document.getElementById('produit4').style.height = newY2+'px';

			document.getElementById('product-separator1-2').style.height = (parseInt(document.getElementById('product-elem1').style.height) > parseInt(document.getElementById('product-elem2').style.height)) ? document.getElementById('product-elem1').style.height : document.getElementById('product-elem2').style.height;
			document.getElementById('product-separator3-4').style.top = (parseInt(document.getElementById('product-elem3').style.top) < parseInt(document.getElementById('product-elem4').style.top)) ? document.getElementById('product-elem3').style.top : document.getElementById('product-elem4').style.top;
			document.getElementById('product-separator3-4').style.height = (parseInt(document.getElementById('product-elem3').style.height) > parseInt(document.getElementById('product-elem4').style.height)) ? document.getElementById('product-elem3').style.height : document.getElementById('product-elem4').style.height;
		}
	} else if(separator.id == "product-separator3-4") {
		var newX = event.x - offX;
		var newX2 = (mainSpaceWidth - newX - 4);
		if(newX > 0 && mainSpaceWidth >= newX && newX2 > 0 && mainSpaceWidth >= newX2) {
			separator.style.left = newX+'px';

			document.getElementById('product-elem3').style.width = newX+'px';
			document.getElementById('produit3').style.width = newX+'px';

			document.getElementById('product-elem4').style.left = (newX + 4)+'px';
			document.getElementById('product-elem4').style.width = newX2+'px';
			document.getElementById('produit4').style.width = newX2+'px';

			document.getElementById('product-separator2-3').style.width = (parseInt(document.getElementById('product-elem1').style.width) > parseInt(document.getElementById('product-elem3').style.width)) ? document.getElementById('product-elem1').style.width : document.getElementById('product-elem3').style.width;
			document.getElementById('product-separator2-4').style.left = (parseInt(document.getElementById('product-elem2').style.left) < parseInt(document.getElementById('product-elem4').style.left)) ? document.getElementById('product-elem2').style.left : document.getElementById('product-elem4').style.left;
			document.getElementById('product-separator2-4').style.width = (parseInt(document.getElementById('product-elem2').style.width) > parseInt(document.getElementById('product-elem4').style.width)) ? document.getElementById('product-elem2').style.width : document.getElementById('product-elem4').style.width;
		}
	}
}

/**
 * UTILISÉ POUR GARDER LA POSITION DE LA SOURIS (PRINCIPALEMENT POUR FIREFOX)
 */
var mouseX, mouseY;

/**
 * Retourne la position actuelle de la souris.
 */
function getMousePos(e)
{
	if(!e) {
		var e = window.event || window.Event;
	}

	if('undefined' != typeof e.pageX) {
		mouseX = e.pageX;
		mouseY = e.pageY;
	} else {
		mouseX = e.clientX + document.body.scrollLeft;
		mouseY = e.clientY + document.body.scrollTop;
	}

	//Ajuste la position à l'intérieur du DIV
	mouseX -= (menuWidth + 70);
	mouseY -= (headerHeight + 70);

	if(movement == true) {
		movement();
	}
}

//ASSIGNE ENSUITE LE GESTIONNAIRE DE SOURIS
document.onmousemove = getMousePos;

/**
 * Fonction pour placer le div près du curseur de la souris.
 *
 * @param string $divName Nom du DIV ciblé
 * @param string $forceState Forcé un état (show/hide)
 */
function swapDivVisibility(divName, forceState)
{
	var divObj = document.getElementById(divName);
	var state;

	if(forceState) {
		state = forceState;
	} else {
		state = (divObj.style.display == "") ? "hide" : "show";
	}

	if(state == "show") {
		divObj.style.display = '';
		divObj.style.top = (mouseY) + 'px';
		divObj.style.left = (mouseX) + 'px';
	} else {
		divObj.style.display = 'none';
	}
}

/**
 * Initialise le calendrier.
 *
 * @param string $time Heure
 * @param string $min Date minimum
 * @param string $max Date maximum
 */
function initCalendar(time, min, max, buddy, addtobuddy)
{
	//Si l'heure doit être utilisée
	calendarSetTime = time;

	//Affiche la ligne de l'heure si nécessaire
	if(time == true) {
		document.getElementById('divCalendrierTimePicker').style.display = '';
	} else {
		document.getElementById('divCalendrierTimePicker').style.display = 'none';
	}

	//Initialise la date minimale
	if(min != "null") {
		var date = min.split('-');
		minDate = new Date(date[0], (date[1] - 1), date[2]);
	} else {
		minDate = "null";
	}

	//Initialise la date maximale
	if(max != "null") {
		var date = max.split('-');
		maxDate = new Date(date[0], (date[1] - 1), date[2]);
	} else {
		maxDate = "null";
	}

	//Init buddy
	if(buddy != "null") {
		calBuddy = buddy;
		calAddtobuddy = addtobuddy;
	} else {
		calBuddy = calAddtobuddy = "null";
	}
}

/**
 * Calendrier - Mois suivant.
 *
 * @uses updateCalendar()
 */
function nextMonth()
{
	month++;
	if(month > 11) {
		month = 0;
		year++;
	}
	updateCalendar();
}

/**
 * Calendrier - Mois pécédent.
 *
 * @uses updateCalendar()
 */
function prevMonth()
{
	month--;
	if(month < 0)
	{
		month = 11;
		year--;
	}
	updateCalendar();
}

/**
 * Calendrier - Année suivante.
 *
 * @uses updateCalendar()
 */
function nextYear()
{
	year++;
	updateCalendar();
}

/**
 * Calendrier - Année pécédent.
 *
 * @uses updateCalendar()
 */
function prevYear()
{
	year--;
	updateCalendar();
}

/**
 * Calendrier - Redessine le calendrier.
 */
function updateCalendar()
{
	var firstDay = new Date(year, month, 1).getDay();
	var lastDay = new Date(year, month + 1, 0).getDate();
	document.getElementById('calendarMonth').value = month;
	document.getElementById('calendarYear').value = year;

	var valid = true;
	var i = 0;
	var day = 1;
	var val = "";
	for(i = 0; i < 42; i++)
	{
		if(i >= firstDay && i < (lastDay + firstDay))
		{
			if(maxDate == "null" && minDate == "null") {
				document.getElementById('j'+i).className = (day == origDay && month == origMonth && year == origYear) ? 'calTabCellActive' : 'calTabCell';
			} else {
				var currentDate = new Date(year, (parseInt(month)), day);

				if(currentDate > maxDate && maxDate != "null") {
					document.getElementById('j'+i).className = 'calTabCellInactive';
				} else if(currentDate <= minDate && minDate != "null") {
					document.getElementById('j'+i).className = 'calTabCellInactive';
				} else {
					document.getElementById('j'+i).className = (day == origDay && month == origMonth && year == origYear) ? 'calTabCellActive' : 'calTabCell';
				}
			}
			val = day;
			day++;
		} else {
			document.getElementById('j'+i).className = 'calTabCell';
			val = "&nbsp;";
		}

		valid = writeCell("j"+i, val);
		if(valid == false) {
			break;
		}
	}
}

/**
 * Calendrier - Écris la date d'une cellule.
 *
 * @param string $cellName Nom de la cellule à écrire
 * @param string $cellValue Valeur à écrire dans la cellule
 */
function writeCell(cellName, cellValue)
{
	if(document.getElementById(cellName).innerHTML) {
		document.getElementById(cellName).innerHTML = cellValue;
		return true;
	} else {
		alert("");
		return false;
	}
}

/**
 * Calendrier - Mise à jour du mois.
 *
 * @uses updateCalendar()
 */
function updateMonth(val)
{
	month = parseInt(val);
	updateCalendar();
}

/**
 * Calendrier - Change le style de la cellule lorsque le curseur est au dessus.
 *
 * @param string $cellName Nom de la cellule ciblée
 */
function mouseOverCell(cellName)
{
	if(document.getElementById(cellName).className == 'calTabCellInactive') {
		return false;
	}

	var cellValue = "";

	if(document.getElementById(cellName).innerHTML) {
		cellValue = document.getElementById(cellName).innerHTML;
	} else {
		alert("");
		return;
	}

	if(cellValue > 0 && cellValue < 32) {
		if(document.getElementById(cellName).className == 'calTabCellActive') {
			active = true;
		}

		document.getElementById(cellName).className = 'calTabCellOver';
	}
}

/**
 * Calendrier - Change le style de la cellule lorsque le curseur la quitte.
 *
 * @param string $cellName Nom de la cellule ciblée
 */
function mouseOutCell(cellName)
{
	if(document.getElementById(cellName).className == 'calTabCellInactive') {
		return false;
	}

	if(active == true) {
		active = false;
		document.getElementById(cellName).className = 'calTabCellActive';
	} else {
		document.getElementById(cellName).className = 'calTabCell';
	}
}

/**
 * Calendrier - Ferme le calendrier et met à jour le champ de la date/heure.
 *
 * @param string $cellName Nom de la cellule sélectionnée
 *
 * @uses swapDivVisibility()
 */
function closeCalendar(cellName)
{
	if(document.getElementById(cellName).className == 'calTabCellInactive') {
		return false;
	}

	var cellValue = "";

	if(document.getElementById(cellName).innerHTML) {
		cellValue = document.getElementById(cellName).innerHTML;
	} else {
		alert("");
		return;
	}

	if(cellValue > 0 && cellValue < 32) {
		var mo = (month >= 9) ? month + 1 : "0"+(month+1);
		var dy = (cellValue > 9) ? cellValue : "0"+cellValue;
		var val = year+"-"+mo+"-"+dy;

		//Si l'heure doit être ajoutée
		if(calendarSetTime == true) {
			val += " "+validHour(document.getElementById('calendarTime').value);
		}

		//Exception pour IE6 et le bug des combobox qui apparaisse par dessus les DIV
		if(document.getElementById('calendarExceptionIE6') != null) {
			var cbos = document.getElementById('calendarExceptionIE6').value.split(";");
			for(var i = 0; i < cbos.length; i++)
			{
				if(document.getElementById(cbos[i]) != null) {
					document.getElementById(cbos[i]).style.display = '';
				}
			}
		}

		document.getElementById(fiName).value = val;
		swapDivVisibility('divCalendrier','hide');

		if(calBuddy != "null" && document.getElementById(calBuddy) != null) {
			var buddyDate = new Date(year, month, cellValue);
			buddyDate.setTime(buddyDate.getTime() + (calAddtobuddy * 1000));
			var buddyMonth = (buddyDate.getMonth() >= 9) ? buddyDate.getMonth() + 1 : "0"+(buddyDate.getMonth()+1);
			var buddyDay = (buddyDate.getDate() > 9) ? buddyDate.getDate() : "0"+buddyDate.getDate();
			var buddyYear = (buddyDate.getYear() < 1900) ? buddyDate.getYear() + 1900 : buddyDate.getYear();
			var valBuddy = buddyYear+"-"+buddyMonth+"-"+buddyDay;

			//Si l'heure doit être ajoutée
			if(calendarSetTime == true) {
				valBuddy += " "+validHour(document.getElementById('calendarTime').value);
			}

			document.getElementById(calBuddy).value = valBuddy;
		}
	}
}

/**
 * Calendrier - Affiche le calendrier et l'initialise.
 *
 * @param string $fi Nom du champ de la date
 *
 * @uses swapDivVisibility()
 * @uses updateCalendar()
 */
function showCalendar(fi)
{
	//Si déjà visible, cache le calendrier
	if(typeof(fiName) != "undefined") {
		if(fi == fiName && document.getElementById('divCalendrier').style.display == "")
		{
			swapDivVisibility('divCalendrier','hide');
			return false;
		}
	}

	//Exception pour IE6 et le bug des combobox qui apparaisse par dessus les DIV
	if(document.getElementById('calendarExceptionIE6') != null) {
		var cbos = document.getElementById('calendarExceptionIE6').value.split(";");
		for(var i = 0; i < cbos.length; i++)
		{
			if(document.getElementById(cbos[i]) != null) {
				document.getElementById(cbos[i]).style.display = 'none';
			}
		}
	}

	//Init form and field name
	fiName = fi;

	//Init original date
	var origDate = document.getElementById(fi).value;
	if(origDate != "")
	{
		var arrayDate = origDate.split(" ");

		//Décompose la date
		var array = arrayDate[0].split("-");
		origDay = array[2];
		origMonth = array[1] - 1;
		origYear = array[0];

		//Décompose l'heure
		if(arrayDate[1]) {
			var array = arrayDate[1].split(":");
			origHour = array[0];
			origMinute = array[1];
			origSeconde = array[2];
		} else {
			origHour = "00";
			origMinute = "00";
			origSeconde = "00";
		}
	}
	else
	{
		var today = new Date();
		origDay = today.getDate();
		origMonth = today.getMonth();
		origYear = today.getFullYear();
		origHour = today.getHours();
		origMinute = today.getMinutes();
		origSeconde = today.getSeconds();
	}

	//Init actual date
	day = origDay;
	month = origMonth;
	year = origYear;
	active = false;

	//Init actual hour
	if(calendarSetTime == true) {
		document.getElementById('calendarTime').value = origHour+":"+origMinute+":"+origSeconde;
	}

	//Show and init calendar (inverse will not always init cbo)
	swapDivVisibility('divCalendrier','show');
	setTimeout('updateCalendar()', 50);
}

/**
 * Valide l'heure entrée.
 *
 * @param string $time Heure à valider
 */
function validHour(time)
{
	//Si la chaîne est vide, retourne 00h00
	if(time == "") {
		return "00:00:00";
	}

	//Décompose l'heure
	var t = time.split(":");
	var heure = parseInt(t[0]);
	if(isNaN(heure)) {
		heure = "00";
	} else if(heure < 0 || heure > 23) {
		heure = "00";
	} else if(heure < 10) {
		heure = "0"+(heure);
	}
	var valeur = heure;

	//Vérifie les minutes
	if(typeof(t[1]) != "undefined") {
		var min = parseInt(t[1]);
		if(isNaN(min)) {
			min = "00";
		} else if(min < 0 || min > 60) {
			min = "00";
		} else if(min < 10) {
			min = "0"+(min);
		}
		valeur = valeur+":"+min;
	} else {
		return valeur+":00:00";
	}

	//Vérifie les secondes
	if(typeof(t[2]) != "undefined") {
		var sec = parseInt(t[2]);
		if(isNaN(sec)) {
			sec = "00";
		} else if(sec < 0 || sec > 60) {
			sec = "00";
		} else if(sec < 10) {
			sec = "0"+(sec);
		}
		return valeur+":"+sec;
	} else {
		return valeur+":00";
	}
}

/**
 * Sélection d'un option de la listbox avancée.
 *
 * @param string $divID ID de l'élément sélectionné
 * @param string $fieldname Nom du champ contenant l'index de la sélection
 * @param int $maxitems Nombre maximum d'item pouvant être sélectionné (-1 pour illimité)
 *
 * @uses ProgressiveShow()
 */
function lstBoxSelectOption(fieldname, line, maxitems)
{
	//Nombre d'options sélectionnées
	var lstSelectorNbrItems = lstBoxSelectOptionCount(fieldname, maxitems);
	var divID = fieldname+"_lstSelectorOpt["+line+"]";
	var mappingName = (document.getElementById(fieldname+"_mappingname") != null) ? document.getElementById(fieldname+"_mappingname").value : null;

	//Change l'état de l'option
	if(document.getElementById(divID).className == "lstBoxOption" || document.getElementById(divID).className == "lstBoxOptionAlt") {
		//Vérifie si le max a été atteint
		if(maxitems != -1 && lstSelectorNbrItems >= maxitems) {
			alert("Nombre maximum d'item déjà sélectionné ("+maxitems+")");
			return;
		}

		//Associe la nouvelle classe CSS
		document.getElementById(divID).className = (document.getElementById(divID).className == "lstBoxOption") ? "lstBoxOptionSelected" : "lstBoxOptionSelectedAlt";
		document.getElementById(fieldname+"_lstSelectorOptS["+line+"]").value = 1;

		//Ajoute de la station à la liste
		if(document.getElementById(mappingName+"_stationlist") != null) {
			var code = document.getElementById(fieldname+"_lstSelectorOptV["+line+"]").value;
			var fullname = document.getElementById(fieldname+"_lstSelectorOptN["+line+"]").value;

			if(document.getElementById(mappingName+"_stationlist_"+code) == null) {
				var newdiv = document.createElement('div');
				newdiv.id = mappingName+"_stationlist_"+code;
				newdiv.innerHTML = fullname;
				document.getElementById(mappingName+'_stationlist').appendChild(newdiv);
			} else {
				document.getElementById(mappingName+"_stationlist_"+code).style.display = '';
			}
		}
	} else { //Associe la classe CSS d'origine
		document.getElementById(divID).className = (document.getElementById(divID).className == "lstBoxOptionSelectedAlt") ? "lstBoxOption" : "lstBoxOptionAlt";
		document.getElementById(fieldname+"_lstSelectorOptS["+line+"]").value = 0;

		//Retire de la liste
		if(document.getElementById(mappingName+"_stationlist") != null) {
			document.getElementById(mappingName+"_stationlist_"+document.getElementById(fieldname+"_lstSelectorOptV["+line+"]").value).style.display = 'none';
		}
	}

	//Init vars
	var i = 0;
	var newOpt = new Array();
	var index = 0;
	var nbrElems = document.getElementById(fieldname+'_lstSelectorNbrItems').value;

	for(i = 0; i < nbrElems; i++)
	{
		//Si l'option est sélectionné, stock la valeur
		if(document.getElementById(fieldname+"_lstSelectorOptS["+i+"]").value == 1) {
			newOpt[index] = document.getElementById(fieldname+"_lstSelectorOptV["+i+"]").value;
			index++;
		}
	}

	//Attribue la nouvelle valeur
	document.getElementById(fieldname).value = newOpt.join(String.fromCharCode(29));
}

/**
 * Retire tous les éléments sélectionnés de la liste.
 *
 * @param string $fieldname Nom du champ en préfixe des éléments de la listBox
 *
 * @uses ProgressiveShow()
 */
function lstBoxUnselectAll(fieldname) {
	//Désélectionne les éléments de la liste
	var elms = document.getElementById(fieldname+"_lstSelector").getElementsByTagName("div");
	for(var i = 0; i < elms.length; i++) {
		var elm = elms[i];
		if(elm.className == "lstBoxOptionSelected" || elm.className == "lstBoxOptionSelectedAlt") {
			document.getElementById(elm.id).className = (elm.className == "lstBoxOptionSelected") ? "lstBoxOption" : "lstBoxOptionAlt";
			var subElms = elm.getElementsByTagName("input");
			for(var j = 0; j < elms.length; j++) {
				var subElm = subElms[j];
				if(subElm != null && subElm.id.match("OptS")) {
					document.getElementById(subElm.id).value = 0;
				}
			}
		}
	}

	//Désélectionne les éléments de la carte
	var elms = document.getElementById("mapping_elements").getElementsByTagName("div");
	for(var i = 0; i < elms.length; i++) {
		var elm = elms[i];
		var bkimg = elm.style.backgroundImage;
		var regExp = new RegExp("(\"|')", "g");
		bkimg = bkimg.replace(regExp, "");

		if(bkimg.substr(4, (bkimg.length - 5)) == "http://agrometeo.org/img/icon_station_alt.gif") {
			document.getElementById(elm.id).style.backgroundImage = "url(\"http://agrometeo.org/img/icon_station.gif\")"
		}
	}

	//Vide les listes
	document.getElementById(fieldname).value = "";
	if(document.getElementById(fieldname+'_stationlist') != null) {
		document.getElementById(fieldname+'_stationlist').innerHTML = "";
	}
}

/**
 * Retourne le nombre d'option sélectionné.
 *
 * @param string $fieldname Nom du champ contenant l'index de la sélection
 *
 * @return int
 */
function lstBoxSelectOptionCount(fieldname, max)
{
	//Si le max est illimité, retourne -1
	if(max == -1) {
		return -1;
	}

	//Init vars
	var nbrItems = i = 0;
	var nbrElems = document.getElementById(fieldname+'_lstSelectorNbrItems').value;

	for(i = 0; i < nbrElems; i++)
	{
		//Si l'option est sélectionné
		if(document.getElementById(fieldname+"_lstSelectorOptS["+i+"]").value == 1) {
			if(max == 1) {
				document.getElementById(fieldname+"_lstSelectorOpt["+i+"]").className = (document.getElementById(fieldname+"_lstSelectorOpt["+i+"]").className == "lstBoxOptionSelectedAlt") ? "lstBoxOption" : "lstBoxOptionAlt";
				document.getElementById(fieldname+"_lstSelectorOptS["+i+"]").value = 0;
				break;
			} else {
				nbrItems++;
			}
		}
	}

	return nbrItems;
}

/**
 * Permet de modifier les paramètres affichés pour la sélection du produit.  Simulation du système d'onglet.
 *
 * @param string $suffix Suffixe des DIV à alterner
 * @param array $ids Tableau des ID des onglets à cacher
 */
function switchProductTab(suffix, ids)
{
	for(var i = 0; i < ids.length; i++)
	{
		document.getElementById('content_'+ids[i]).style.display = 'none';
		document.getElementById('tab_'+ids[i]).className='';
	}

	//~ if(document.getElementById('tab_map') != null) {
		//~ document.getElementById('content_map').style.display = 'none';
		//~ document.getElementById('tab_map').className='';
	//~ }
	//~ if(document.getElementById('tab_graph') != null) {
		//~ document.getElementById('content_graph').style.display = 'none';
		//~ document.getElementById('tab_graph').className='';
	//~ }
	//~ if(document.getElementById('tab_somm') != null) {
		//~ document.getElementById('content_somm').style.display = 'none';
		//~ document.getElementById('tab_somm').className='';
	//~ }
	//~ if(document.getElementById('tab_download') != null) {
		//~ document.getElementById('content_download').style.display = 'none';
		//~ document.getElementById('tab_download').className='';
	//~ }

	document.getElementById('content_'+suffix).style.display = '';
	document.getElementById('tab_'+suffix).className='selected';
}

/**
 * Carte sélectionnable - Affiche la région demandée.
 *
 * @param string $region Nom de la région à afficher;
 * @param bool $display Affiche ou cache la région (true/false)
 */
function showRegion(region, display)
{
	if(display == null) {
		document.getElementById('regionspace-'+region).style.display = 'none';
	} else if(display == false) {
		document.getElementById('regionspace-'+region).style.display = 'none';
	} else {
		document.getElementById('regionspace-'+region).style.display = '';
		document.getElementById('regionspace-'+region).style.top = currentMappingY;
		document.getElementById('regionspace-'+region).style.left = currentMappingX;
	}
}

/**
 * Carte sélectionnable - Affiche le nom de la station demandée.
 *
 * @param string $region Nom de la région en cours d'affichage;
 * @param string $name Code de la station à afficher;
 * @param string $fullname Nom de complet de la station;
 * @param float $lat Latitude;
 * @param float $lat Longitude;
 */
function showName(region, name, fullname, lat, lon)
{
	if(name == null) {
		document.getElementById('stationname-'+region).innerHTML = "";
		document.getElementById('stationname-'+region).style.display = 'none';
	} else {
		document.getElementById('stationname-'+region).innerHTML = (fullname != "") ? fullname : name;
		document.getElementById('stationname-'+region).style.display = '';
		document.getElementById('stationname-'+region).style.top = (lat - 12)+"px";
		document.getElementById('stationname-'+region).style.left = (lon + 16)+"px";
	}
}

/**
 * Sélectionne la station demandée.
 *
 * @param string $region Nom de la région en cours d'affichage;
 * @param string $code Code de la station sélectionnée;
 */
function stidSelection(region, code)
{
	if(document.getElementById(currentMapping+"_lstSelectorOptV[0]") != null) {
		var line = 0;
		var nbrElems = document.getElementById(currentMapping+"_lstSelectorNbrItems").value;
		for(i = 0; i < nbrElems; i++)
		{
			if(document.getElementById(currentMapping+"_lstSelectorOptV["+line+"]") == null) {
				break;
			}

			var value = document.getElementById(currentMapping+"_lstSelectorOptV["+line+"]").value;

			if(value == code) {
				if(currentMax != -1 && lstBoxSelectOptionCount(currentMapping) < currentMax && currentMax > 1) {
					document.getElementById(region+'_'+code).style.display = 'none';
				}
				lstBoxSelectOption(currentMapping, line, currentMax);
				if(currentMax == 1) {
					document.getElementById('regionspace-'+region).style.display = 'none';
				}
				break;
			}

			line++;
		}
		document.getElementById(region+"_"+code).style.backgroundImage = "url('http://agrometeo.org/img/icon_station"+((document.getElementById(region+"_"+code).style.backgroundImage.indexOf('alt') > 0) ? '' : '_alt')+".gif')";
	}
}

var currentMapping = null;
var currentMax = null;
var currentMappingX = currentMappingY = 0;

/**
 * Prépare la position du DIV de la région.
 *
 * @param string $elm Nom de l'élément HTML contenant la valeur de la station sélectionnée
 */
function prepareRegionPosition(elm, max, posX, posY)
{
	currentMappingY = (posY - 100) + 'px';
	currentMappingX = (posX - 200) + 'px';
	currentMapping = elm;
	currentMax = max;
}
