REGEX_AUTO_FIELD = /^[^_]+(_(Req|Mail|Pass|Int))+$/;
REGEX_BLANK = /^\s*$/;
REGEX_TYPED_FIELD = /_(Mail|Pass|Int)$/;

MSG_BLANK =  ' est vide.';
MSG_MAIL = ' n\'est pas une adresse mail valide.';
MSG_PASSWORD = ' est incorrect ou l\'identifiant est vide.';
MSG_NOT_INTEGER = ' n\'est pas un entier.';

function addListener(element, baseName, handler) {
	if (element.addEventListener)
		element.addEventListener(baseName, handler, false);
	else if (element.attachEvent)
		element.attachEvent('on'+baseName,handler);
}

function addFormChecks() {
	var forms = document.forms;
	for (var index = 0; index < forms.length; ++index) {
		var form = forms.item(index);
		addListener(form, 'submit', checkForm);
	}
}
function checkForm(e) {
	e = e || window.event;
	var form = e.target || e.srcElement;
	var errors = '';
	var faulty = null;
	for (var index = 0; index < form.elements.length; ++index) {
		var field = form.elements.item(index);
		if (!field.id.match(REGEX_AUTO_FIELD))
			continue;
		var value = field.value;
		// Champ requis ?
		if (field.id.match(/_Req/) && value.match(REGEX_BLANK)) {
			errors += field.title + MSG_BLANK + '\n';
			faulty = faulty || field;
			continue;
		}
		// Champ typé ?
		var match = field.id.match(REGEX_TYPED_FIELD);
		if (match) {
			var type = match[1];
			var error = checkTypedField(value, type, field);
			if (error) {
				errors += field.title + error + '\n';
				faulty = faulty || field;
			}
		}
	}

	if (!faulty)
		return;
	stopEvent(e);
	alert(errors);
	faulty.focus();
}

function checkTypedField(value, type, field) {
	var temp = MSG_MAIL;
	if (type == 'Mail') {
		for (var i=1 ; i<(value.length) ; i++) {
			if (value.charAt(i)=='@') {
				if (i<(value.length-4)){
					for (var j=i ; j<(value.length-2) ; j++) {
						if (value.charAt(j)=='.') {
							return false;
						}
					}
				}
			}
		}
		if (value.match(REGEX_BLANK)) {
			return false;
		}
		return temp;
	}
	if (type == 'Pass') {
		var id1 = field.id;
		var id2 = id1;
		id2 = id2.replace('_Pass','');
		id2 = id2 + "2";
		if ((document.getElementById(id1).value != document.getElementById(id2).value && (!value.match(REGEX_BLANK) || !document.getElementById(id2).value.match(REGEX_BLANK))) || (document.getElementById(id1).value == document.getElementById(id2).value && document.getElementById('textLogin').value.match(REGEX_BLANK) && (!value.match(REGEX_BLANK) || !document.getElementById(id2).value.match(REGEX_BLANK))) || (!document.getElementById('textLogin').value.match(REGEX_BLANK) && (value.match(REGEX_BLANK) || document.getElementById(id2).value.match(REGEX_BLANK)))) {
			return MSG_PASSWORD;
		}	
	}
	if (type == 'Int') {
		if (!value.match(REGEX_BLANK)) {
			try {
				val = parseInt(value, 10);
				if (String(val) != value)
					throw val;
			} catch (e) {
				return MSG_NOT_INTEGER	
			}
		}
	}
}

function stopEvent(e) {
	var e = e || window.event;
	if (e.preventDefault) {
	   e.preventDefault();  // FF
	}
	e.returnValue = false;  // IE
}

function decorateLabels() {
	var labels = document.getElementsByTagName('label');
	for (var index = 0; index < labels.length; ++index) {
		var label = labels[index];
		if (label.htmlFor) {
			var elt = document.getElementById(label.htmlFor);
			if (!elt)
				continue;
			if (elt.id.match(/Req/))
				label.className += ' required';
		}
	}
}
addListener(window, 'load', addFormChecks);
addListener(window, 'load', decorateLabels);

function ajouter() {
	for (var i=2;i<=2;i++) {
		if (document.getElementById("fichier"+i).style.visibility == '') {
			document.getElementById("fichier"+i).style.visibility = 'visible';
			document.getElementById("fichier"+i).style.position = 'relative';
			if (i==2) {
				document.getElementById("ajouter").style.visibility = 'hidden';
				document.getElementById("ajouter").style.position = 'absolute';
			}
			return;
		}
	}
}

function changeObjet(choix) {
	if (choix == 2) {
		document.location = "/demande-de-devis.html"
	}
	else if (choix == 3) {
		document.getElementById('statut').className = '';
		document.getElementById('poste').className = '';
	} else {
		document.getElementById('statut').className = 'hidden';
		document.getElementById('poste').className = 'hidden';
	}
}