// datagrid.js

//Remplit le cadre Détail avec les données de l'item correspondant à la ligne survolée
function showDetail(idDatagrid, lineNumber) {
	// Get the object
	eval("var object = detailPropOf"+idDatagrid+"["+lineNumber+"];");
	// Update each property
	for (i in object) {
		// find the field tag.
		document.getElementsByName(i+"Of"+idDatagrid)[0].value=object[i];
	}
}

//Efface les données contenues dans le cadre Détail
function clearDetail(idDatagrid) {
	// Get the first object to check its properties
	eval("var object = detailPropOf"+idDatagrid+"[0];");
	// Update each property
	for (i in object) {
		// find the field tag.
		document.getElementsByName(i+"Of"+idDatagrid)[0].value="";
	}
}

// Harmonise la largeur de la datagrid avec la largeur de la barre d'actions de masse
function fixerTaille() {
	if ((document.getElementById("actionsTable")!=null) && (document.getElementById("paginationTable")!=null)) {
		if (document.getElementById("actionsTable").clientWidth<document.getElementById("DatagridTable").clientWidth) {
			document.getElementById("actionsTable").width=document.getElementById("DatagridTable").clientWidth;
			document.getElementById("paginationTable").width=document.getElementById("DatagridTable").clientWidth;
		}
		else {
			document.getElementById("DatagridTable").width=document.getElementById("actionsTable").clientWidth;
		}
	document.getElementById("actionsTable").style.visibility="visible";
	}
}

//Affiche un message de confirmation et lance l'action si l'utilisateur confirme
function confirmAction(strForm, dgComponentId, urlDesti, type, label, remoteAction, urlAction) {

	if (type=="UNITARY") {
		if (window.confirm(label))
			submit_act_unit(strForm, dgComponentId, urlDesti, remoteAction, urlAction);
	}else{
		if (window.confirm(label))
			submit_act_mass(strForm, dgComponentId, urlDesti, remoteAction, urlAction);
	}
}

//Affiche un message de confirmation et lance l'action si l'utilisateur confirme
function confirmAction(strForm, dgComponentId, urlDesti, type, label, remoteAction, urlAction, noCheckOfOneELementChecked) {

	if (type=="UNITARY") {
		if (window.confirm(label))
			submit_act_unit(strForm, dgComponentId, urlDesti, remoteAction, urlAction);
	}else{
		if (window.confirm(label))
			submit_act_mass(strForm, dgComponentId, urlDesti, remoteAction, urlAction, noCheckOfOneELementChecked);
	}
}


//Fait apparaître toutes les checkbox comme cochées
function cocher_tout(strForm, dgComponentId) {
	var formulaire = document.getElementById(strForm);
	if (eval("formulaire.cocheOf"+dgComponentId+".checked == true")) {
		if (eval("formulaire.casesOf"+dgComponentId+".length == null"))
			eval("formulaire.casesOf"+dgComponentId+".checked = true;");
		else
			for (i = 0; i < eval("formulaire.casesOf"+dgComponentId+".length"); i++) {
	     		eval("formulaire.casesOf"+dgComponentId+"["+i+"].checked = true;");
		    }
	}
	else {
		if (eval("formulaire.casesOf"+dgComponentId+".length == null"))
			eval("formulaire.casesOf"+dgComponentId+".checked = false;");
		else
			for (i = 0; i < eval("formulaire.casesOf"+dgComponentId+".length"); i++) {
				eval("formulaire.casesOf"+dgComponentId+"["+i+"].checked = false;");
		    }
	}
}

//Lance le tri sur une colonne dans un sens donné (croissant ou décroissant)
function trier_colonne(strForm, dgComponentId, ordre, nom, urlAction) {
	maj_formActionValue(strForm, urlAction);
	maj_componentIdOfCurrentAction(strForm, dgComponentId);
	maj_action(strForm, dgComponentId, "trier");
	maj_sortFieldName(strForm, dgComponentId, nom);
	if (ordre=="asc") {
		maj_sortMode(strForm, dgComponentId, "croissant");
	}
	else {
		if (ordre=="desc")
			maj_sortMode(strForm, dgComponentId,"decroissant");
	}
	document.getElementById(strForm).submit();
}

//Soumission d'une action unitaire
function submit_act_unit(strForm, dgComponentId, urlDesti, remoteAction, urlAction) {
	beginTask();

	maj_formActionValue(strForm, urlAction);
	maj_componentIdOfCurrentAction(strForm, dgComponentId);
	document.getElementsByName("remoteActionOf"+dgComponentId)[0].value=remoteAction;
	
	//On récupère le numéro du bean sur lequel on déclenche l'action unitaire
	var i = 4;
	var index = '';
	while(urlDesti.charAt(i)!= '_') {
	 //we take the index between the 2 '_' characters
	 index += urlDesti.charAt(i); //url_X_Y
	 i++;
	}
	//On met à jour les champs cachés et on submit
	maj_action(strForm, dgComponentId, "unitaryAction"+index);
	maj_destination(strForm, dgComponentId, document.getElementById(urlDesti+"Of"+dgComponentId).value);
	document.getElementById(strForm).submit();
	
}

//Soumission d'une action de masse
function submit_act_mass(strForm, dgComponentId, url_desti, remoteAction, urlAction, noCheckOfOneElementChecked) {
	beginTask();
	
	maj_formActionValue(strForm, urlAction);
	maj_componentIdOfCurrentAction(strForm, dgComponentId);
	document.getElementsByName("remoteActionOf"+dgComponentId)[0].value=remoteAction;
	var formulaire = document.getElementById(strForm);

	var selected = false;
	if ( eval("formulaire.casesOf"+dgComponentId+" != null") ) {
		if ((eval("formulaire.casesOf"+dgComponentId+".length != null"))){
			selected = eval("!window.dgSelection"+dgComponentId+" || dgSelection"+dgComponentId+"!= -1;");
		} else if (eval("formulaire.casesOf"+dgComponentId+".checked==true")) {
			selected = true;
		}
	}
	
	if ( (selected==true) || (noCheckOfOneElementChecked==true) ) {
		maj_action(strForm, dgComponentId,"massAction");
		maj_destination(strForm, dgComponentId, url_desti);
		document.getElementById(strForm).submit();
	} else {
		var texte;
		if (navigator.browserLanguage=="fr") {
			texte = "Aucun item selectionné !";
		}else if (navigator.browserLanguage=="it") {
			texte = "Nessun articolo ha selezionato !";
		}else if (navigator.browserLanguage=="es") {
			texte = "Ningún artículo seleccionado !";
		} else if (navigator.browserLanguage=="de") {
		    texte = "Kein Einzelteil wählte !";
		} else {
			texte = "No item selected !";		
		}
		alert(texte);
	}

}

//Lance une action de pagination
function paginer(strForm, dgComponentId, sens, urlAction) {
	maj_formActionValue(strForm, urlAction);
	maj_componentIdOfCurrentAction(strForm, dgComponentId);
	maj_action(strForm, dgComponentId, "paginer");
	maj_destination(strForm, dgComponentId, sens);
	document.getElementById(strForm).submit();
}

//Fonctions de mise à jour des champs cachés
function maj_action(strForm, dgComponentId, pAction) {
	eval("document.getElementById(\""+strForm+"\").actionOf"+dgComponentId+".value= \""+pAction+"\";");
}

function maj_destination(strForm, dgComponentId, desti) {
	eval("document.getElementById(\""+strForm+"\").destinationOf"+dgComponentId+".value= \""+desti+"\";");
}

function maj_nomProp(strForm, dgComponentId, pNom) {
	eval("document.getElementById(\""+strForm+"\").nomPropOf"+dgComponentId+".value= \""+pNom+"\";");
}

function maj_sortFieldName(strForm, dgComponentId, fieldName) {
	eval("document.getElementById(\""+strForm+"\").sortFieldNameOf"+dgComponentId+".value= \""+fieldName+"\";");
}

function maj_sortMode(strForm, dgComponentId, mode) {
	eval("document.getElementById(\""+strForm+"\").sortModeOf"+dgComponentId+".value= \""+mode+"\";");
}

//Update all the fields named "componentIdOfCurrentAction", because the Datagridservlet use the componentIdOfCurrentAction[0].value to know who is the DG who call the sort or pagine action
function maj_componentIdOfCurrentAction(strForm, dgComponentId) {
	var forComponentIdOfCurrentActionInputHidden = document.getElementsByName("componentIdOfCurrentAction");
	for (i=0;i<forComponentIdOfCurrentActionInputHidden.length;i++) {
		forComponentIdOfCurrentActionInputHidden[i].value=dgComponentId;
	}
}

function maj_formActionValue(strForm, urlAction) {
	document.getElementById(strForm).action=urlAction;
}

/**
 * Méthode positionnant l'index de sélection courant dans le dataGrid
 */
function dgSel(dgComponentId, selectionId) {
	eval("dgSelection"+dgComponentId+"="+selectionId+";");
}

function initRowHighlighting() {
	if (!document.getElementsByTagName){ return; }
	var tables = document.getElementsByTagName('table');
	for(var i = 0; i < tables.length; i++){
		var table = tables[i];
		if(table.getAttribute('className') && table.getAttribute('className').indexOf('datagrid_table_content') > -1
		 || table.getAttribute('class') && table.getAttribute('class').indexOf('datagrid_table_content')> -1){
			//Make sure to use th tags for header row.
			attachRowMouseEvents(table.getElementsByTagName('tr'));
		}
	}
}

function rollOverEven(obj) {
	obj.setAttribute('oldClass', obj.className);
	obj.className = 'datagrid_heven';
}
function rollOverOdd(obj) { 
	obj.setAttribute('oldClass', obj.className);
	obj.className = 'datagrid_hodd';
}
function rollOut(obj) { 
	if (obj.getAttribute('oldClass'))
		obj.className = obj.getAttribute('oldClass');
}

function attachRowMouseEvents(rows){
	for(var i = 0; i < rows.length; i++){
		var row = rows[i];
		if(i%2 == 0){ 
			row.onmouseover = evenOver;
			this.className = 'datagrid_even';
		} else{ 
			row.onmouseover = oddOver;	
			this.className = 'datagrid_odd';
		}
		row.onmouseout = mouseOut
	}
}
function showTooltip(obj, width){
	var divTT=document.getElementById('ellipsisTooltip');
	divTT.onclick=function(){divTT.style.visibility='hidden'};
	divTT.onmouseout=function(){ hideTooltip();};
	divTT.x=getposOffset(obj, "left");
	divTT.y=getposOffset(obj, "top");
	divTT.style.left=divTT.x -1 + "px";
	divTT.style.top= divTT.y -1 + "px";
	divTT.innerHTML=obj.innerHTML;
	divTT.style.height= obj.offsetHeight ;
	
	if (divTT.offsetWidth > width)
		divTT.style.visibility='visible';
}
function hideTooltip(){
	var divTT=document.getElementById('ellipsisTooltip')
	divTT.style.visibility='hidden';
}
