﻿/*This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA*/

function checkEmail (email) {
	//checkEmail v0.4 by Romain Laurent

	//caractères autorisés
	charOk = ".-_abcdefghijklmnopqrstuvwxyz0123456789";
	//caractères mals places
	charPasOk = ".-_";
	//double caractères interdits
	doubleCharPasOk = ["..","--","__",".@","-@","_@","@.","@-","@_","-_","_-",".-","-.","._","_."];	

	//ecriture normalisée
	email = writeEmail(email);
	
	//longueur minimum
	if (!(email.length >= 5)) {
		return false;
	}
	//au moins un @
	if (email.indexOf("@") == -1) {
		return false;
	}
	//un et un seul @
	if (!(email.lastIndexOf("@") == email.indexOf("@"))) {
		return false;
	}
	//3° position à partir de la fin au minimum
	if (email.indexOf("@") > (email.length - 4)) {
		return false;
	}
	//pas de double caractères interdits
	for (i = 0 ; i < doubleCharPasOk.length ; i++) {
		if (!(email.indexOf(doubleCharPasOk[i]) == -1)) {
			return false;
		}
	}
	//dernier point après @
	if (!(email.lastIndexOf(".") > email.indexOf("@"))) {
		return false;
	}
	//pas de point ou tiret ou underscore au début
	if (!(charPasOk.indexOf(email.charAt(0)) == -1)) {
		return false;
	}
	//pas de point ou tiret ou underscore à la fin
	if (!(charPasOk.indexOf(email.charAt(email.length - 1)) == -1)) {
		return false;
	}
	//decoupe en sous chaine de la forme A@B.C
	a = email.substring(0 , email.indexOf("@"));
	b = email.substring((email.indexOf("@") + 1) , email.lastIndexOf("."));
	c = email.substring((email.lastIndexOf(".") + 1) , email.length);
	
	//vérification de la chaine A
	for (i = 0 ; i < a.length ; i++) {
		if (charOk.indexOf(a.charAt(i)) == -1) {
			return false;
		}
	}
	
	//vérification de la chaine B
	for (i = 0 ; i < b.length ; i++) {
		if (charOk.indexOf(b.charAt(i)) == -1) {
			return false;
		}
	}
	
	//vérification de la chaine C
	for (i = 0 ; i < c.length ; i++) {
		if (charOk.indexOf(c.charAt(i)) == -1) {
			return false;
		}
	}
	
	//syntaxe adresse email correcte
	return true;
}

function writeEmail (email) {
	//tout en minuscules
	email = email.toLowerCase();
	
	//pas d'accents
	email = email.replace(/ /g, "");
	email = email.replace(/é/g, "e");
	email = email.replace(/è/g, "e");
	email = email.replace(/ê/g, "e");
	email = email.replace(/ë/g, "e");
	email = email.replace(/à/g, "a");
	email = email.replace(/â/g, "a");
	email = email.replace(/î/g, "i");
	email = email.replace(/ï/g, "i");
	email = email.replace(/ù/g, "u");
	email = email.replace(/ô/g, "o");
	
	return email;
}