function toggle_check(id) {
	el = document.getElementById(id);
	if (el.checked)
		el.checked = 0;
	else
		el.checked = 1;
}

function focuss(id, word) {
	f = document.getElementById(id);
	if (f.value != word)
		return;
	f.value = '';
	return;
}

function blurr(id, word) {
	f = document.getElementById(id);
	if (f.value != word && f.value != '')
		return;
	f.value = word;
	return;
}

function verify_submit() {
	f = document.loginForm;
	if (f.entered_username.value.length == 0) {
		alert("Saisissez un pseudo");
		return;
	}
	if (f.entered_password.value.length == 0) {
		alert("Saisissez un mot de passe");
		return;
	}
	f.submit();
}

function ShowError (sText) {
	alert (sText);
	return false;
}

function EmailCheck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)

	if (str.indexOf(at)==-1){
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false;
	}

	if (str.indexOf(at,(lat+1))!=-1){
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1){
		return false;
	}
		
	if (str.indexOf(" ")!=-1){
		return false;
	}

	return true;		
}

function trim(str) {
	return str.replace(/^\s+|\s+$/g, '')
}

function checkfield (ob, len) {
	ob.value = trim (ob.value);
	if (ob.value.length < len) {
		ob.focus();ob.blur();
		ob.select();
		return false;
	} else {
		return true;
	}
}

function checkIsInt (ob, minVal, maxVal) {
	val = parseInt (ob.value);
	ob.value = val;
	if (isNaN (val)) {
		return false;
	} else {
		if (minVal > val || val > maxVal) {
			return false;
		}
		return true;
	}
}

function checkemail (str) {
	var filter=/^.+@.+\..{2,3}$/
	return (filter.test(str));
}	

function validate_register() {
	var f = document.forms.registerForm;

	if (!checkfield(f.p_login,2)) {
		return ShowError ("Saisissez votre pseudo.");
	}

	if (!checkfield(f.password,5)) {
		return ShowError ("Saisissez votre mot de passe.");
	}
	if (!checkfield(f.verification,5)) {
		return ShowError ("Saisissez la verification du votre mot de passe.");
	}
	if (f.password.value != f.verification.value) {
		return ShowError ("Les deux mot de passe ne sont pas identiques.");
	}

	if (!checkfield(f.a_Mail,5)) {
		return ShowError ("Saisissez votre adresse e-mail.");
	}
	if (!checkemail(f.a_Mail.value) ) {
		return ShowError ("Saisissez corectement votre adresse e-mail.");
	}

	if (f.a_Titre.selectedIndex == 0) {
		return ShowError ("Saisissez votre civilité.");
	}

	if (!checkfield(f.a_Nom, 2)) {
		return ShowError ("Saisissez votre nom.");
	}
	if (!checkfield(f.a_Prenom, 2)) {
		return ShowError ("Saisissez votre prenom.");
	}

	if (!checkfield(f.jour, 1)) {
		return ShowError ("Saisissez votre jour de naissance.");
	} else {
		if (!checkIsInt (f.jour, 1, 31)) {
			return ShowError ("Saisissez corectement votre jour de naissance.");
		}
	}
	Today = new Date();
	if (!checkfield(f.annee, 4)) {
		return ShowError ("Saisissez votre annee de naissance.");
	}
	else {
		var end = Today.getFullYear();
		var start = end - 100;
		if (!checkIsInt (f.annee, start, end))
			return ShowError ("L'annee de naissance n'est pas corectement saisie.\r\nLa valeur devrait être entre "+ start +" et "+end+".");
	}

	if (!checkfield(f.a_Adresse, 5)) {
		return ShowError ("Saisissez votre adresse.");
	}
	if (!checkfield(f.a_Cp,5)) {
		return ShowError ("Saisissez votre code postal.");
	}
	if (!checkfield(f.a_Ville, 2)) {
		return ShowError ("Saisissez votre ville.");
	}

	if (!checkfield(f.a_Code, 5)) {
		return ShowError ("Saisissez le code d'image.");
	}

	if (!f.reglement.checked)
		return ShowError ("Vous devez accepter les termes du reglement pour pouver vous inscrire.");

	return true;
}

