function asToAs(input, value, replace)
{
	if(input.value == value)
	{
		input.value = replace;
	}
}


function updateToEDB(ajx_page, ajx_param, ajx_redirect)
{
	var phpFile = site_url + 'ajx/' + ajx_page + '.ajx.php?' + ajx_param;
	
	new Ajax.Request(phpFile, { 
		onComplete: function()
		{
			/*document.location = ajx_redirect;*/
		}
	
	});
}


function popup_end()
{
	if($('popup_wait'))
	{
		if($('popup_wait').style.display != 'none')
		{
			popLbOnClose();
		}
	}
};


/**
 *
 * Fonction de validation de formulaire via son ID.
 */

function valid_form(form_id)
{
	frm = $(form_id);
	frm.submit();
}


/**
 *
 * Fonction de confirmation avec comme param�tre le message de confirmation a afficher, 
 * puis la destination de l'utilisateur apr�s avoir confirm�.
 */

function confirmation(msg, location)
{
	if(confirm(msg))
	{
		window.location = location;
	}
}


/**
 *
 * Fonction de confirmation avec comme param�tre le message de confirmation a afficher, 
 * puis la destination de l'utilisateur apr�s avoir confirm�.
 */

function confirmationForm(msg, form_id)
{
	if(confirm(msg))
	{
		FormRequired(form_id);
	}
}


/**
 *
 * Fonction d'affichage automatique d'un �l�ment DIV utilis� comme popup � titre informatif.
 * On utilise une fonction de prototype scriptaculous pour un effet d'apparition.
 */

function customAlert(msg)
{
	var error_text = $('error_text');
	var popup_text = $('popup_text');
	var dynamique_text = $('dynamique_text');
	
	if(error_text)
	{
	   error_text.innerHTML = msg;
	   Effect.Appear('error_cont', {duration: 0.3, queue: 'front'});
	}
	else if(popup_text)
	{
	   popup_text.innerHTML = msg;
	   Effect.Appear('popup_cont', {queue: 'front'});
	}
	else if(dynamique_text)
	{
	   Effect.Appear('dynamique_cont', {queue: 'front'});
	}
}


/**
 *
 * Fonction de limitation d'�criture pour les champs de formulaire.
 * La limitation ne laisse pas l'utilisateur �crire autre chose que des chiffres.
 */

function chiffres(event)
{
	// Compatibilit� IE / Firefox
	if(!event&&window.event)
	{
		event=window.event;
	}
	
	if(!event.which&&event.keyCode)
	{
		event.which=event.keyCode;
	}
	
	if(event.keyCode != 8 && event.keyCode != 9 && event.keyCode != 37 && event.keyCode != 39 && event.keyCode != 46)
	{
		// IE
		if(event.which < 48 || event.which > 57)
		{
			event.returnValue = false;
			event.cancelBubble = true;
			
			if(!event.keyCode)
			{
				event.preventDefault();
				event.stopPropagation();
			}
		}
	}
}


/**
 *
 * Fonction de limitation d'�criture pour les champs de formulaire.
 * La limitation ne laisse pas l'utilisateur �crire autre chose que des chiffres.
 */

function decimal(event)
{
	// Compatibilit� IE / Firefox
	if(!event&&window.event)
	{
		event=window.event;
	}
	
	if(!event.which&&event.keyCode)
	{
		event.which=event.keyCode;
	}
	
	if(event.keyCode != 8 && event.keyCode != 9 && event.keyCode != 37 && event.keyCode != 39 && event.keyCode != 46)
	{
		if(event.which != 44 && event.which != 46)
		{
			// IE
			if(event.which < 48 || event.which > 57)
			{
				event.returnValue = false;
				event.cancelBubble = true;
			
				if(!event.keyCode)
				{
					event.preventDefault();
					event.stopPropagation();
				}
			}
		}
		else
		{
			/** autoriser les points et les virgules **/
			var nbr = event.currentTarget;
			var nbr_v = $(nbr.id).value;
			
			var is_point = (nbr_v.indexOf(".") != -1);
			var is_virgule = (nbr_v.indexOf(",") != -1);
			var is_decimal = (is_point || is_virgule);
			
			/***/
			
			if(is_decimal)
			{
				event.returnValue = false;
				event.cancelBubble = true;
			
				if(!event.keyCode)
				{
					event.preventDefault();
					event.stopPropagation();
				}
			}
		}
	}
}


/**
 *
 * Fonction de d�placement de curseur, d�di�e aux champs de date d'anniversaire.
 * Nous v�rifions tous d'abord que le nombre de caract�res (chiffres) demand� est respect�, puis nous switchons vers le champ suivant.
 */

function AppFocus(input_id, maxlength, focus_id)
{
	var input_lenght = $(input_id).value.length;
	
	if(input_lenght == maxlength)
	{
		$(focus_id).focus();
	}
}

/**
 *
 * Fonction de control du jour de naissance saisie.
 * Nous v�rifions le chiffres fournie, qui devra �tre inf�rieur ou �gal � 31.
 * Nous v�rifions le nombre de caract�res saisie, qui devra �tre �gal � 2.
 */

function controlDay(input_id, maxlength, focus_id)
{
	var input_lenght = $(input_id).value.length;
	
	var input_value = $(input_id).value;
	var input_value = parseInt(input_value);
	
	var maxDay = 31;
	
	if(input_lenght >= 1)
	{
		if(input_lenght == 1 && input_value > 3)
		{
			
			$(input_id).style.background = '#e4e2e3';
			
			return false;
		}
		else if(input_value > maxDay)
		{
			
			$(input_id).style.background = '#e4e2e3';
			
			return false;
		}
		else if(input_lenght < maxlength)
		{
			
			$(input_id).style.background = '#e4e2e3';
			
			return false;
		}
		else
		{
			$(input_id).style.background = 'white';
			
			if(focus_id != '')
			{
				AppFocus(input_id, maxlength, focus_id);
			}
			
			return true;
		}
	}
	else if(maxlength > 0)
	{
		
		$(input_id).style.background = '#e4e2e3';
		
		return false;
	}
}


/**
 *
 * Fonction de control du mois de naissance saisie.
 * Nous allons controler que le chiffre saisie est inf�rieur ou �gal � 12.
 * Nous v�rifions le nombre de caract�res saisie, qui devra �tre �gal � 2.
 */

function controlMounth(input_id, maxlength, focus_id)
{
	var input_lenght = $(input_id).value.length;
	
	var input_value = $(input_id).value;
	var input_value = parseInt(input_value);
	
	var maxMounth = 12;
	
	
	if(input_lenght >= 1)
	{
		if(input_lenght == 1 && input_value > 1)
		{
			
			$(input_id).style.background = '#e4e2e3';
			
			return false;
		}
		else if(input_value > maxMounth)
		{
			
			$(input_id).style.background = '#e4e2e3';
			
			return false;
		}
		else if(input_lenght < maxlength)
		{
			
			$(input_id).style.background = '#e4e2e3';
			
			return false;
		}
		else
		{
			$(input_id).style.background = 'white';
			
			if(focus_id != '')
			{
				AppFocus(input_id, maxlength, focus_id);
			}
			
			return true;
		}
	}
	else if(maxlength > 0)
	{
		
		$(input_id).style.background = '#e4e2e3';
		
		return false;
	}
}


/**
 *
 * Fonction de control de l'ann�e de naissance saisie.
 * Nous v�rifions le chiffres fournie, qui devra �tre inf�rieur � la date limite pour que l'utilisateur est 18 ans.
 * Nous v�rifions le nombre de caract�res saisie, qui devra �tre �gal � 4.
 * Nous v�rifions la totalit� de la date saisie depuis le d�but et v�rifions l'�ge que cela indique.
 */

function controlYear(input_id, maxlength, date_min, date_id, focus_id)
{
	var input_lenght = $(input_id).value.length;
	
	var input_day = $('day_' + date_id).value;
	var input_mounth = $('mounth_' + date_id).value;
	var input_year = $('year_' + date_id).value;
	
	var date_selected = input_year + input_mounth + input_day;
	var date_selected = parseInt(date_selected);
	
	var date_min = parseInt(date_min);
	
	
	if(input_lenght >= 1)
	{
		if(date_selected > date_min)
		{
			
			$(input_id).style.background = '#e4e2e3';
			
			return false;
		}
		else if(input_lenght < maxlength)
		{
			
			$(input_id).style.background = '#e4e2e3';
			
			return false;
		}
		else
		{
			$(input_id).style.background = 'white';
			
			if(focus_id != '')
			{
				AppFocus(input_id, maxlength, focus_id);
			}
			
			return true;
		}
	}
	else if(maxlength > 0)
	{
		
		$(input_id).style.background = '#e4e2e3';
		
		return false;
	}
}


/**
 *
 * Fonction de d�placement de curseur, d�di�e au calendrier lors de la s�lection de la date.
 */

function AppCalendar(cheat_id)
{
	$(cheat_id).focus();
}


/**
 *
 * Fonction de control de l'adresse email saisie.
 * Nous v�rifions la nomenclature de l'email, via une expression r�guli�re.
 */

function controlMail(input_id, empty_etat)
{
	var email_length = $(input_id).value.length;
	
	var email = $(input_id).value;
	var verif = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{2,}[.][a-zA-Z]{2,3}$/
	
	
	if(email_length >= 1)
	{
		if(empty_etat == false)
		{
			$(input_id).setAttribute('required', 'true');
		}
		
		/***/
		
		if (verif.exec(email) == null)
		{
			$(input_id).style.background = '#e4e2e3';
			
			return false;
		}
		else
		{
			$(input_id).style.background = 'white';
			
			return true;
		}
	}
	else if(empty_etat == true)
	{
		$(input_id).style.background = '#e4e2e3';
		
		return false;
	}
	else
	{
		if(empty_etat == false)
		{
			$(input_id).removeAttribute('required');
		}
		
		/***/
		
		$(input_id).style.background = 'white';
			
		return true;
	}
}


/**
 *
 * Fonction de control de l'adresse email de confirmation.
 * Nous v�rifions tout simplement qu'elle est �gal � l'email pr�c�dement saisie.
 */

function controlConfMail(conf_id, reel_id)
{
	var email_length = $(conf_id).value.length;
	
	var email = $(reel_id).value;
	var conf_e = $(conf_id).value;
	
	
	if(email_length >= 1)
	{
		if(email != conf_e)
		{
			$(conf_id).style.background = '#e4e2e3';
			
			return false;
		}
		else
		{
			$(conf_id).style.background = 'white';
			
			return true;
		}
	}
	else
	{
		$(conf_id).style.background = '#e4e2e3';
		
		return false;
	}
}


/**
 *
 * Fonction pour copier les valeurs d'un champ texte � un autre.
 */

function valueAs(copie_id, copieur_id)
{
	var valeur = $(copie_id).value;
	var as = $(copieur_id);
	
	as.value = valeur;
}



function controlLength(input_id, minLength)
{
	var input_length = $(input_id).value.length;
	
	if(input_length < minLength)
	{
		$(input_id).style.background = '#e4e2e3';
		
		return false;
	}
	else
	{
		$(input_id).style.background = 'white';
		
		return true;
	}
}


/**
 *
 * Fonction de chargement destin� au calendrier � son chargement.
 * Nous chargeons via une fonction AJAX prototype scriptaculous les �l�ments <select> du calendrier.
 * Ces �l�ments <select> sont utilis�s pour la s�lection du mois ou de l'ann�e d�sir�e.
 */

function chargeSelectCalendar(calendar_id, className, type, value)
{
	var pp = $(calendar_id);
	
	var phpFile = site_url + 'ajax/slc_year.ajx.php?' + type + '=' + value;
	if(type == 'm')
	{
		var value = parseInt(value) + 1;
		var phpFile = site_url + 'ajax/slc_mounth.ajx.php?' + type + '=' + value;
	}
	
	if(pp.getElementsByClassName(className)) 
	{
		input_dir = pp.getElementsByClassName(className);
		for(i=0; i < input_dir.length; i++)
		{
			var aj = new Ajax.Updater(input_dir[i].id, phpFile);
		}
	}
}

function controlEmpty(input)
{
	var input_id = input.id;
	var value = $(input_id).value;
	var input_type = input.getAttribute('type');
	
	
	if(input_type == 'checkbox')
	{		
		if(input.checked == true)
		{
			return true;
		}
		else
		{
			if(input.getAttribute('phrase'))
			{
				var msg = input.getAttribute('phrase');
				
				customAlert(msg);
			}
			
			return false;
		}
	}
	else
	{
		if(value == '')
		{
			$(input_id).style.background = '#e4e2e3';
			
			return false;
		}
		else
		{
			if(input.getAttribute('phrase'))
			{
				if($(input_id).value == input.getAttribute('phrase'))
				{
					
					$(input_id).style.background = '#e4e2e3';
					
					return false;
				}
				else
				{
					
					$(input_id).style.background = 'white';
					
					return true;
				}
			}
			else
			{
				
				$(input_id).style.background = 'white';
				
				return true;
			}
		}
	}
}


/**
 *
 */

function activeForm(elmts_id, disabled_value)
{
	var ss = $(elmts_id);
	
	
	var inputs = ss.getElementsByTagName('textarea');
	
	var i = 0;
	if(inputs)
	{
		while (i < inputs.length)
		{
			$(inputs[i].id).disabled = disabled_value;

		   i++;
		}
	}
	
	/***/
	
	var inputs = ss.getElementsByTagName('input');
	
	var i = 0;
	if(inputs)
	{
		while (i < inputs.length)
		{
			$(inputs[i].id).disabled = disabled_value;

		   i++;
		}
	}
	
	/***/
	
	var inputs = ss.getElementsByTagName('select');
	
	var i = 0;
	if(inputs)
	{
		while (i < inputs.length)
		{
			$(inputs[i].id).disabled = disabled_value;

		   i++;
		}
	}
	
	/***/
	/*
	if(disabled_value)
	{
		$(elmts_id).style.display = 'none';
	}
	else
	{
		$(elmts_id).style.display = 'block';
	}
	*/
}


/**
 *
 * Fonction de control total � la validation d'un formulaire.
 * Nous reprenons la totalit� des fonctions cit�es plus haut et ex�cutons un control sur chaque �l�ment voulu.
 */

function FormRequired(form_id)
{
	var msg = '';
	
	var cp_id = '';
	var ville_id = '';
	var region_id = '';
	
	var mdp_id = '';
	var reel_id = '';
	var date_min = '';
	var submit = true;
	var ss = $(form_id);
	var captchabox = true;
	var captchabox_id = '';
	
	
	var inputs = ss.getElementsByTagName('textarea');
	//var msg = 'Tous les champs color�s doivent �tre mentionn�.';
	
	var i = 0;
	if(inputs)
	{
		while (i < inputs.length)
		{
			if(inputs[i].getAttribute('required'))
			{
				if(controlEmpty(inputs[i]) == false)
				{
					var submit = false;
					
					if(inputs[i].getAttribute('error') != '')
					{
						msg += inputs[i].getAttribute('error') +"\r\n";
					}
				}
			}

		   i++;
		}
	}
	
	/***/
	
	var inputs = ss.getElementsByTagName('input');
	//var msg = 'Tous les champs color�s doivent �tre mentionn�.';
	
	var i = 0;
	if(inputs)
	{
		var radio_name = '';
		var radio_submit = false;
		
		while (i < inputs.length)
		{
			input_type = inputs[i].getAttribute('type');
			
			if(input_type == 'radio' && radio_name != '' && radio_name != inputs[i].getAttribute('name') && (i + 1) < inputs.length)
			{
				if(radio_submit == false)
				{
					var submit = false;
				}
				
				var radio_submit = false;
			}
			
			if(input_type == 'radio' && radio_name == inputs[i].getAttribute('name'))
			{
				if(inputs[i].checked == true)
				{
					var radio_submit = true;
				}
			}
			
			if(input_type == 'radio')
			{
				var radio_name = inputs[i].getAttribute('name');
				
				if(inputs[i].checked == true)
				{
					var radio_submit = true;
				}
			}
			
			if(input_type == 'radio' && radio_name != '' && radio_name == inputs[i].getAttribute('name') && (i + 1) == inputs.length)
			{
				if(radio_submit == false)
				{
					var submit = false;
				}
				
				var radio_submit = false;
			}
			
			/***/
			
			if(inputs[i].getAttribute('required') && $(inputs[i].id).disabled == false)
			{
				if(controlEmpty(inputs[i]) == false)
				{
					var submit = false;
					
					if(inputs[i].getAttribute('error') != '')
					{
						msg += inputs[i].getAttribute('error') +"\r\n";
					}
				}
				else
				{
					if(inputs[i].getAttribute('formatday'))
					{
						var mounth_id = String($(inputs[i].id).readAttribute('id')).replace('day', 'mounth');
						
						if(controlDay(inputs[i].id, 2, '') == false)
						{
							var submit = false;
						}
					}
					
					if(inputs[i].getAttribute('formatmounth'))
					{				
						if(controlMounth(inputs[i].id, 2, '') == false)
						{
							var submit = false;
						}
					}
					
					if(inputs[i].getAttribute('formatyear'))
					{
						var date_id = String($(inputs[i].id).readAttribute('id')).replace('year_', '');
						
						if(controlYear(inputs[i].id, 4, date_min, date_id, inputs[i].id, '') == false)
						{
							var submit = false;
					
							msg += 'La date de naissance est incorrect.'+"\r\n";
						}
					}
					
					if(inputs[i].getAttribute('formatheure'))
					{
						if(controlHeure(inputs[i].id, 2, '') == false)
						{
							var submit = false;
						}
					}
					
					if(inputs[i].getAttribute('formatminute'))
					{
						if(controlMinute(inputs[i].id, 2, '') == false)
						{
							var submit = false;
						}
					}
					
					/***/
					
					if(inputs[i].getAttribute('code_postal'))
					{
						var cp_id = inputs[i].id;
					}
					
					/***/
					
					if(inputs[i].getAttribute('telephone'))
					{
						var tel_id = inputs[i].id;
						
						if(controlTel(tel_id, 'telephone', true) == false)
						{
							var submit = false;
						}
					}
					
					/***/
					
					if(inputs[i].getAttribute('mobile'))
					{
						var mobile_id = inputs[i].id;
						
						if(controlTel(mobile_id, 'mobile', true) == false)
						{
							var submit = false;
						}
					}
					
					/***/
					
					if(inputs[i].getAttribute('email'))
					{
						var reel_id = inputs[i].id;
						
						if(controlMail(reel_id, true) == false)
						{
							var submit = false;
						}
					}
					
					if(inputs[i].getAttribute('conf_email'))
					{
						if(controlConfMail(inputs[i].id, reel_id) == false)
						{
							var submit = false;
						}
					}
					
					if(inputs[i].getAttribute('mdp'))
					{
						var mdp_id = inputs[i].id;
						
						if(controlLength(mdp_id, 6) == false)
						{
							var submit = false;
						}
					}
					
					if(inputs[i].getAttribute('conf_mdp') && mdp_id != '')
					{
						if(controlConfMDP(inputs[i].id, mdp_id) == false)
						{
							var submit = false;
						}
					}
					
					if(inputs[i].getAttribute('secure'))
					{
						if(controlCaptcha(inputs[i].id) == false)
						{
							var submit = false;
						}
					}
					
					if(inputs[i].getAttribute('captchabox'))
					{
						var captchabox = false;
						var captchabox_id = inputs[i].id;
					}
				}
			}

		   i++;
		}
	}
	
	/***/
	
	var inputs = ss.getElementsByTagName('select');
	//var msg = 'Tous les champs color�s doivent �tre mentionn�.';
	
	var i = 0;
	if(inputs)
	{
		while (i < inputs.length)
		{			
			if(inputs[i].getAttribute('required') && $(inputs[i].id).disabled == false)
			{
				if(controlEmpty(inputs[i]) == false)
				{
					var submit = false;
				}
				else
				{
					if(inputs[i].getAttribute('select_city'))
					{
						var ville_id = inputs[i].id;
					}
					
					if(inputs[i].getAttribute('select_region'))
					{
						var region_id = inputs[i].id;
					}
					
					if(region_id != '' && ville_id != '' && cp_id != '')
					{
						if(selectVille(cp_id, ville_id, region_id, true) == false)
						{
							var submit = false;
						}
					}
				}
			}

		   i++;
		}
	}
	
	
	if(!submit)
	{
		//customAlert(msg);
		alert(msg);
	}
	else
	{
		if(!captchabox)
		{
			//gm_ShowMap(form_id, captchabox_id);
		}
		else
		{
			ss.submit();
		}
	}
}
