/**
 * Sur le chargement de la fenêtre de connexion,
 * on nomme la fenêtre de sorte à pouvoir être capable
 * de savoir si l'on se trouve dans le fenêtre principale
 * ou dans une autre.
 */
var mainWindowName = "EiMainWindow";
function onLoadConnexion() {
	window.name = mainWindowName;
}

/**
 * @return True si il la fenêtre est la fenêtre principale et false sinon.
 */
function isMainWindow(w) {
	if (w.name == mainWindowName) {
		return true;
	}
	return false;
}

/**
 * Sélectionne ou désélectionne une liste de checkbox.
 * @param element Checkbox permettant de sélectionner ou non la liste de checkbox.
 * @param Liste de checbox.
 */
function check(element, checkboxes) {
	var check = element.checked;
	if (!checkboxes) {
		return false;
	}
	if (!checkboxes.length) {
		checkboxes.checked = check;
	}
	for(var i=0; i<checkboxes.length; i++) {
		checkboxes[i].checked = check;
	}
}
/**
 * Sélectionne ou désélectionne une liste de checkbox dont la racine est identique.
 * @param element Checkbox permettant de sélectionner ou non la liste de checkbox.
 * @param L'objet formulaire.
 * @param la racine du nom des checkboxes à traiter
 */
function checkRacine(element, form, checkboxeNom) {
	
	var check = element.checked;
	var nomElem;
	
	for(var i=0; i<form.elements.length; i++) {
		nomElem = form.elements[i].name;
		nomElem = nomElem.substring(0,checkboxeNom.length)
		if(nomElem==checkboxeNom){
			form.elements[i].checked = check;
		}
	}
}

/**
 * Affecte une valeur à l'elément dispatch et poste de formulaire.
 * @param form Formulaire.
 * @param dispatchValue Valeur du dispatch.
 */
function dispatchAndSubmit(form, dispatchValue) {
	if (dispatchValue == "") {
		return false;
	}
	form.dispatch.value = dispatchValue;
	form.submit();
}

/**
 * Affiche un élement si il existe.
 * @param id Identifiant de l'élément.
 */
function showAnElement(id) {
	var elt = document.getElementById(id);
	if (elt) {
		elt.style.display = "";
	}
}

/**
 * Masque un élement si il existe.
 * @param id Identifiant de l'élément.
 */
function hideAnElement(id) {
	var elt = document.getElementById(id);
	if (elt) {
		elt.style.display = "none";
	}
}

/**
 * Masque les aides.
 */
function hideActionInfos(divId) {
	var infos = document.getElementById(divId);
	if (infos) {
		var divs = infos.getElementsByTagName("div");
		for (var i = 0; i < divs.length; i++) {
			var div = divs.item(i);
			div.style.display = "none";
		}
	}
}

function changeAction(action, divId) {
	hideActionInfos(divId);
	showAnElement(action.value);
}

/**
 * Ouvre une popup.
 * @param url Lien.
 * @param Nom de la fenêtre.
 * @param width Largeur.
 * @param height Hauteur.
 */
function openPopup(url, name, width, height) {
	if (screen.width < width) {
		width = screen.width - 50;
	}
	if (screen.height < height) {
		height = screen.height - 50;
	}
	var x = (screen.availWidth - width) / 2;
	var y = (screen.availHeight - height) / 2;
	var popup = window.open(url, name, 'scrollbars=1, directories=0, location=0, menubar=0, status=1, toolbar=0, resizable=1, width=' + width + ', height=' + height + ', top=' + y + ', left=' + x); 	
	popup.scrollbars = true;
	popup.focus();
	
	return false;
}

/**
 * Ouvre une popup pour les éditions.
 * @param url Lien.
 */
function openPopupEdition(url) {
	openPopup(url, 'EiEdition', 600, 400);
}

/**
 * Ouvre une popup pour la convocation de masse.
 * @param url Lien.
 * @param Nom de la fenêtre.
 * @param width Largeur.
 * @param height Hauteur.
 */
function convocationMasse(url, name, width, height) {
	if (screen.width < width) {
		width = screen.width - 50;
	}
	if (screen.height < height) {
		height = screen.height - 50;
	}
	var x = (screen.availWidth - width) / 2;
	var y = (screen.availHeight - height) / 2;
	var popup = window.open(url, name, 'scrollbars=1, directories=1, location=1, menubar=1, status=1, toolbar=1, resizable=1, width=' + width + ', height=' + height + ', top=' + y + ', left=' + x); 	
	popup.scrollbars = true;
	popup.focus();
	
	return false;
}

/**
 * Bouge vers le haut une sélection d'un tag <select>
 * @param select tag <select>
 */
function moveUp(select) {
	for(var i = 0; i < select.options.length; i++) {
		if (select.options[i].selected && select.options[i] != "" && select.options[i] != select.options[0]) {
			var tmpval = select.options[i].value;
			var tmpval2 = select.options[i].text;
			select.options[i].value = select.options[i-1].value;
			select.options[i].text = select.options[i-1].text;
			select.options[i].selected = "";
			select.options[i-1].value = tmpval;
			select.options[i-1].text = tmpval2;
			select.options[i-1].selected = "true";
		}
	}
}

/**
 * Bouge vers le haut une sélection d'un tag <select>
 * @param select tag <select>
 */
function moveDown(select) {
	for(var i = select.options.length - 1 ; i >= 0; i--) {
		if (select.options[i].selected && select.options[i] != "" && select.options[i+1] != select.options[select.options.length]) {
			var tmpval = select.options[i].value;
			var tmpval2 = select.options[i].text;
			select.options[i].value = select.options[i+1].value;
			select.options[i].text = select.options[i+1].text;
			select.options[i].selected = "";
			select.options[i+1].value = tmpval;
			select.options[i+1].text = tmpval2;
			select.options[i+1].selected = "true";
		}
	}
}

/**
 * Ouvre l'écran d'impression d'un tableau.
 * @param pha_iid Identifiant de la phase.
 */
function imprimerTableau(pha_iid, output) {
	openPopup("tableauImpression.do?dispatch=afficher&pha_iid=" + pha_iid + "&output=" + output, "TAB_PRINT", 550, 350);

}

function tableauEffectifs(epr_iid, pha_iid) {
	openPopup("tableauEffectif.do?dispatch=afficher&epr_iid=" + epr_iid + "&pha_iid=" + pha_iid, "TAB_EFF", 500, 350);
}

function affBoutonDeleg(typeDeleg) {
	
	var delegeAjouterBt = document.getElementById("delegeAjouterBt");
	var utilisateurJaRechercheBtAd = document.getElementById("utilisateurJaRechercheBtAd");
	var utilisateurJaRechercheBtJa = document.getElementById("utilisateurJaRechercheBtJa");
	var utilisateurDelegRechercheBtAd = document.getElementById("utilisateurDelegRechercheBtAd");
	var utilisateurDelegRechercheBtJa = document.getElementById("utilisateurDelegRechercheBtJa");
	
	var utilisateurJaRechercheBtJaDisplay = "none";
	var utilisateurJaRechercheBtAdDisplay = "none";
	var utilisateurDelegRechercheBtJaDisplay = "none";
	var utilisateurDelegRechercheBtAdDisplay = "none";
	var delegeAjouterBtDisplay = "none";
	
	if (typeDeleg == "JA") {
		utilisateurJaRechercheBtJaDisplay = "inline";
		utilisateurDelegRechercheBtJaDisplay = "inline";
	} else if (typeDeleg == "AD") {
		utilisateurJaRechercheBtAdDisplay = "inline";
		utilisateurDelegRechercheBtAdDisplay = "inline";
		delegeAjouterBtDisplay = "inline";
	}

	utilisateurJaRechercheBtJa.style.display = utilisateurJaRechercheBtJaDisplay;
	utilisateurJaRechercheBtAd.style.display = utilisateurJaRechercheBtAdDisplay;
	utilisateurDelegRechercheBtAd.style.display = utilisateurDelegRechercheBtAdDisplay;
	utilisateurDelegRechercheBtJa.style.display = utilisateurDelegRechercheBtJaDisplay;
	delegeAjouterBt.style.display = delegeAjouterBtDisplay;
}