/***************************************************************************************
 * JavaScript functions used for the user modifications
 * 
 * @author André Masson
 ***************************************************************************************/

/**
 * Initialisation de l'objet de transfert d'objects "options" html
 */
var opt = new OptionTransfer("tousLesInterets", "mesInterets");
opt.setAutoSort(true);
opt.setDelimiter(",");
opt.setStaticOptionRegex("");
// Store list of options removed from left list into an input field
opt.saveRemovedLeftOptions("removedLeft");
// Store list of options removed from right list into an input field
opt.saveRemovedRightOptions("removedRight");
// Store list of options added to left list into an input field
opt.saveAddedLeftOptions("addedLeft");
// Store list of options radded to right list into an input field
opt.saveAddedRightOptions("addedRight");
// Store all options existing in the left list into an input field
opt.saveNewLeftOptions("newLeft");
// Store all options existing in the right list into an input field
opt.saveNewRightOptions("newRight");

// Liste des champs obligatoires à valider
var astrFields = new Array(5);
var astrFieldsDesc = new Array(5);
astrFields[0] = "usager_prenom";
astrFieldsDesc[0] = "votre prénom";
astrFields[1] = "usager_nom";
astrFieldsDesc[1] = "votre nom de famille";
astrFields[2] = "usager_motpasse";
astrFieldsDesc[2] = "votre mot de passe";
astrFields[3] = "usager_motpasse2";
astrFieldsDesc[3] = "votre mot de passe (une 2ième fois pour être validé par le système)";
astrFields[4] = "usager_courriel";
astrFieldsDesc[4] = "votre adresse de courriel";

/**
 * Initialize GUI on page load
 */
function onBodyLoad() {
	giveDefaultFocus();
	opt.init(document.forms[0]);
	resetDefaultWidths(document.forms[0].tousLesInterets, document.forms[0].mesInterets);
	refreshActions(document.forms[0].tousLesInterets, document.forms[0].right);
	refreshActions(document.forms[0].mesInterets, document.forms[0].left);
}

/**
 * Give default focus to field when page loaded
 */
function giveDefaultFocus() {
	var frm = document.forms[0];

	for (var i=0; i < astrFields.length; i++) {
		var fieldName = astrFields[i];
		var v = frm.elements[fieldName].value;
		if (isEmpty(v)) {
			if (frm.elements[fieldName].type == "text") {
				frm.elements[fieldName].select();
			}
			frm.elements[fieldName].focus();
			return;
		}
	}
}

/**
 * Form validation
 */
function validForm() {
	var frm = document.forms[0];

	// Validation des champs obligatoires
	if (!validFormGeneric(frm, astrFields, astrFieldsDesc)) {
		return false;
	}

	// validation des mots de passe
	var pwd1 = frm.elements["usager_motpasse"].value;
	var pwd2 = frm.elements["usager_motpasse2"].value;
	if (pwd1 != pwd2) {
		alert("Votre mot de passe est invalide, veuillez le resaisir à nouveau");
		frm.elements["usager_motpasse"].value = "";
		frm.elements["usager_motpasse2"].value = "";
		frm.elements["usager_motpasse"].focus();
		return false;
	}

	// validation courriel
	var email = frm.elements["usager_courriel"];
	if (!checkEmailAddress(email)) {
		alert("Votre mot de passe est invalide, veuillez le resaisir à nouveau");
		email.focus();
		email.select();
		return false;
	}

	// Validation ok
	return true;
}

/**
 * Form cleanup
 */
function resetForm() {
	if (!confirm("Réinitialiser toutes les informations ?")) {
		return;
	}
	var frm = document.forms[0];
	frm.reset();
	giveDefaultFocus();
}

