﻿

window.addEvent('domready', initForm);

function initForm ()
{
	// j'écoute l'évènement des boutons SUBMIT
	if ($("btnContact")) $("btnContact").addEvent ("click", checkForm);
	if ($("btnSubscribe")) $("btnSubscribe").addEvent ("click", checkForm);
	if ($("btnUnsubscribe")) $("btnUnsubscribe").addEvent ("click", checkForm);
	if ($("btnPostuler")) $("btnPostuler").addEvent ("click", checkForm);
}
function checkForm (e)
{
	// Hack IE
	if (e.target)
		target = e.target;
	else if (e.srcElement)
		target = e.srcElement;
	else if (window.event.srcElement)
		target = window.event.srcElement;
		
	if (target.id == "btnSubscribe") {form_id = "FSubscribe"; alert_id = "alertSubscribe";}
	else if (target.id == "btnUnsubscribe") {form_id = "FUnsubscribe"; alert_id = "alertUnsubscribe";}
	else if (target.id == "btnContact") {form_id = "FContact"; alert_id = "alert";}
	else if (target.id == "btnPostuler") {form_id = "FPostuler"; alert_id = "alert";}
	
	var aElements = $(form_id).elements; // les éléments du formulaire
	var bError    = false; // erreur ou pas
	
	// je réinitialise le style des labels
	var aLabels = $(form_id).getElements("label");
	
	for (var i = 0; i < aLabels.length; i++)
	{
		_setRightStyle (aLabels[i]);
	}
	// je parcours les éléments du formulaire
	for (var i = 0; i < aElements.length; i++)
	{
		var optional;
		if (aElements[i].optional)
		{
			optional = aElements[i].optional;
		}
		else if (aElements[i].attributes['optional'])
		{
			optional = aElements[i].attributes['optional'].value;
		}
		var classname  = aElements[i].className;
		
		// c'est un champs obligatoire
		if (optional == "false")
		{
			// si c'est un select
			if (classname == "dropdown")
			{
				// Hack IE
				var index = aElements[i].selectedIndex;
				if (index == undefined) index = aElements[i].getSelected()[0].value;
				
				// si aucun choix n'a été fait
				if (index == "0")
				{
					bError = true;
					var label = _getFormLabel (form_id, aElements[i].id);
					if (label) _setWrongStyle (label);
				}
			}
			else if (classname == "textfield" || classname == "textarea" || classname == "textfield-postuler") // sinon si c'est un champs texte
			{
				// si c'est l'email
				if (aElements[i].id == "email")
				{
					// je vérifie la validité de l'email
					if (!_checkMail (aElements[i].value ))
					{
						bError = true;
						var label = _getFormLabel (form_id, aElements[i].id);
						if (label) _setWrongStyle (label);
					}
				}
				else // sinon
				{
					// je vérifie que les champs de texte sont renseignés
					if (aElements[i].value == "")
					{
						bError = true;
						var label = _getFormLabel (form_id, aElements[i].id);
						if (label) _setWrongStyle (label);
					}
				}
			}
		}
		else // sinon
		{
			// si le champs est renseignés
			if (classname == "textfield" && aElements[i].value != "")
			{
				// si c'est le téléphone
				if (aElements[i].id == "telephone")
				{
					// je vérifie qu'il s'agit de chiffres
					if (!_isNumeric (aElements[i].value))
					{
						bError = true;
						var label = _getFormLabel (form_id, aElements[i].id);
						if (label) _setWrongStyle (label);
					}
				}
			}
		}
	}
	if (bError)
	{
		if ($(alert_id).getStyle('visibility') == "hidden") $(alert_id).setStyle('visibility', 'visible');
		if ($(alert_id).getStyle('display') == "none") $(alert_id).setStyle('display', 'block');
		if (lang == "fr")
			$(alert_id).innerHTML = "Attention, vous n'avez pas saisie les champs suivants"; 
		else
			$(alert_id).innerHTML = "You must fill in all fields"; 
		return false;
	}
	else
	{
		if ($(alert_id).getStyle('visibility') == "visible") $(alert_id).setStyle('visibility', 'hidden');
		if ($(alert_id).getStyle('display') == "block") $(alert_id).setStyle('display', 'none');
		$(alert_id).innerHTML = "&nbsp;"; 
		return true;
	}
}