sErrColor = '#f4d7e2';
sValidColor = '#ffffff';
iTextMaxLength = 1000;

// Objet "Element de formulaire à vérifier"
// oItem : .item_id / .confirm_id / .mandatory / .type / .empty_msg / .invalid_msg / .differ_msg
// type : string | boolean | email | birth_year | login | password

function icFormItem (sItemId, sConfirmId, bMandatory, sType, sEmptyMsg, sInvalidMsg, sDifferMsg) {
	this.item_id     = sItemId;
	this.confirm_id  = sConfirmId;
	this.mandatory   = bMandatory;
	this.type        = sType;
	this.empty_msg   = sEmptyMsg;
	this.invalid_msg = sInvalidMsg;
	this.differ_msg  = sDifferMsg;
}

// Fonction de validation
// La liste des objets éléments est passée en paramètre

function FormValidation(oFormItem) {
	if(!cancelAction) {
		for (var item in oFormItem) {
			var oItem = oFormItem[item];
			var oElement = document.getElementById(oItem.item_id);
			// TYPE STRING
			if (oItem.type =='string'&&oElement) {
				if ((oItem.mandatory == 1)&&(oElement.value.length == 0)) {
					return riseError(oElement, null, oItem.empty_msg);
				}
				validate(oElement, null);
			}

			// TYPE BIRTH_YEAR
			if (oItem.type =='birth_year'&&oElement) {
				var today = new Date();
				if ((oItem.mandatory == 1)&&(oElement.value.length == 0)) {
					return riseError(oElement, null, oItem.empty_msg);
				}
				else if (isNaN(parseInt(oElement.value))) {
					return riseError(oElement, null, oItem.invalid_msg);
				}
				else if ((parseInt(oElement.value)<1880)||(parseInt(oElement.value)>today.getFullYear())) {
					return riseError(oElement, null, oItem.invalid_msg);
				}
				validate(oElement, null);
			}

			// TYPE LOGIN
			if (oItem.type =='login'&&oElement) {
				var regLogin = /^[a-zA-Z0-9_-]{3,20}$/i;
				if ((oItem.mandatory == 1)&&(oElement.value.length == 0)) {
					return riseError(oElement, null, oItem.empty_msg);
				}
				else if (!regLogin.test(oElement.value)){
					return riseError(oElement, null, oItem.invalid_msg);
				}
				validate(oElement, null);
			}

			// TYPE PASSWORD
			if (oItem.type =='password'&&oElement) {
				var oElementConfirm = document.getElementById(oItem.confirm_id);
				var regPassword = /^[a-zA-Z0-9_-]{4,20}$/i;
				if ((oItem.mandatory == 1)&&((oElement.value.length == 0)||(oElementConfirm.value.length == 0))) {
					return riseError(oElement, oElementConfirm, oItem.empty_msg);
				}
				else if (!regPassword.test(oElement.value)){
					return riseError(oElement, oElementConfirm, oItem.invalid_msg);
				}
				else if (oElement.value != oElementConfirm.value){
					return riseError(oElement, oElementConfirm, oItem.differ_msg);
				}
				validate(oElement, oElementConfirm);
			}

			// TYPE EMAIL
			if (oItem.type =='email'&&oElement) {
				var regEmail = /([0-9a-zA-Z]([-+.\w]*[0-9a-zA-Z_])*@(((([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})|(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))/;
				if ((oItem.mandatory == 1)&&(oElement.value.length == 0)) {
					return riseError(oElement, null, oItem.empty_msg);
				}
				else if ((oElement.value.length > 0)&&(!regEmail.test(oElement.value))){
					return riseError(oElement, null, oItem.invalid_msg);
				}
				validate(oElement, null);
			}

			// TYPE BOOLEAN
			if (oItem.type =='boolean'&&oElement) {
				if ((oItem.mandatory == 1)&&(!oElement.checked)) {
					return riseError(oElement, null, oItem.empty_msg);
				}
				validate(oElement, null);
			}

			// TYPE TEXT
			if (oItem.type =='text'&&oElement) {
				if ((oItem.mandatory == 1)&&(oElement.value.length == 0)) {
					return riseError(oElement, null, oItem.empty_msg);
				}
				else if (oElement.value.length > iTextMaxLength){
					return riseError(oElement, null, oItem.invalid_msg);
				}
				validate(oElement, null);
			}

			// TYPE PHONE
			if (oItem.type =='phone'&&oElement) {
				var regPhone = /^[0-9\.\+\-\(\) ]{10,30}$/g;
				if ((oItem.mandatory == 1)&&(oElement.value.length == 0)) {
					return riseError(oElement, null, oItem.empty_msg);
				}
				else if (!regPhone.test(oElement.value)){
					return riseError(oElement, null, oItem.invalid_msg);
				}
				validate(oElement, null);
			}

		}
	}
	else {
		return true;
	}
}

 function riseError (oItem,oConfirmItem,sMsg) {
	alert(sMsg);
	oItem.style.backgroundColor = sErrColor;
	if(oConfirmItem) oConfirmItem.style.backgroundColor = sErrColor;
	oItem.focus();

	return false;
}

 function validate (oItem, oConfirmItem) {
	oItem.style.backgroundColor = sValidColor;
	if(oConfirmItem) oConfirmItem.style.backgroundColor = sValidColor;

	return true;
}

