
var gui_oldColor = '';
function row_over(row)
{
	var color = '#d9e8fb';
	gui_oldColor = row.style.backgroundColor;
	row.style.backgroundColor = color;
	row.style.backgroundImage='url(/ez/gentlemenModule/images/background/row-hover.gif)';
	row.style.backgroundRepeat = 'repeat-x';
}

function row_out(row)
{
	row.style.backgroundColor = gui_oldColor;
	row.style.backgroundImage='';
}

var gui_oldClassname = '';
function row_over_class(row)
{
	gui_oldClassname = row.className;
	row.className = 'row_over';
	//row.style.cursor = 'hand';
}
function row_out_class(row)
{
	row.className = gui_oldClassname;
	//row.style.cursor = '';
}


function GUI_select_check_all(select_elm)
{
	/* 
	* Sélectionne toutes les options d'un select multiple
	*/
	for(var i=0;i<select_elm.options.length;i++)
	{
		select_elm.options[i].checked = true;	
	}	
}

function GUI_select_check_all(oLink,selName) 
{
	/* 
	* Sélectionne/Déselectionne toutes les options d'un select multiple
	*/
	var opt
	var i = 0
	bWhich = (oLink.innerHTML == 'Sélectionner tout');
	while (opt = selName[i++]) opt.selected = bWhich;
	oLink.innerHTML = bWhich ? 'Désélectionner tout' : 'Sélectionner tout';
	return false;
}

function INPUT_upper(input)
{	
	input.value = input.value.toUpperCase();
	return true;
}

function printit()
{
	if (window.print)
	{
		window.print() ;  
	} else {
		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
	document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
		WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
	}
}

function GUI_copyToClipboard( inElement )
{
	/*
	* Copie le node ID vers le clipboard
	*/
	if( document.body.createControlRange )
	{
		ctrlRange = document.body.createControlRange();
		ctrlRange.add(document.all(inElement));
		ctrlRange.execCommand("Copy");
		alert('Le contenu a été copié dans le presse-papier.');
	}
	else
	{
		alert('Impossible de copier dans le presse papier avec ce navigateur. Utilisez Internet Explorer.');
	}
}

function GUI_ROW_click(row)
{
	if(row.style.backgroundColor=='')
		row.style.backgroundColor = '#FF9900';
	else
		row.style.backgroundColor = '';
}

function TABLE_addFooterToHeader()
{	
	TABLE_addFooterToHeader_byId('tableau1');	
}

function TABLE_addFooterToHeader_byId(id)
{	
	var thead = document.getElementById(id).tHead;
	var tfoot = document.getElementById(id).tFoot.rows[0];
	newtFoot = tfoot.cloneNode(true);
	thead.appendChild(newtFoot);	
}

function INPUT_telephone(input)
{
	/*
	* Formatte les N° de téléphone qui ne commencent pas par 00 (international)
	*/
	var out = '';
	string = input.value;

	if(string.length > 1 && string.substr(0,2) != '00')
	{
		digits = string.match(/\+?[0-9]/g);
		if(digits != null)
		{
			string = digits.join('');
			for(var i=0;i<string.length;i++)
			{
				out = out + string.substr(i,1);
				if( i>0 && (i%2)!=0 && i<(string.length-1))
				{
					out = out + '.';
				}
			}
		}
	}
	else
		out = string;
		
	input.value = out;
}

function GUI_cancelBubble(netEvent)
{
	if (document.all) {
		window.event.cancelBubble = true;
	} else {
		netEvent.cancelBubble = true;
	}
}

// LTrim(string) : Returns a copy of a string without leading spaces.
function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
   return rtrim(ltrim(str));
}

function $RF(el, radioGroup)
{
	/*
	* Fonction qui utilise prototype.js pour obtenir le valeur d'un bouton radio
	*/
	var res = Form.getInputs(el,'radio', radioGroup).find(function(re) {return re.checked;});
	if(res != null) {
		return $F(res);
	} else {
		return ""
	}
}

function GUI_show_error_flag(node)
{
	img = document.createElement('img');
	img.setAttribute('valign','absmiddle');
	img.style.marginLeft = '4px';
	img.src = '/ez/gentlemenModule/images/puces/ko.gif';
	target = node;
	// Insertion après
	target.parentNode.appendChild(img);	
	// Insertion avant
	//target.parentNode.insertBefore(img,target);	
	target.style.backgroundColor = '#c30c3e';
	target.style.color = '#FFFFFF';
}

Math.arrondi = function (n, p)
{ 
    return Math.round( n * Math.pow(10, p)) / Math.pow(10, p) ; 
};

function GUI_fixedHeaderTable(table, options )
{	
	table.style.display	=	"block"; // on reforce l'affichage du tableau, comme les autres tableaux sont des recopies de celui-là.
	/*
	options	=	{	table_width	:	800,
					table_height:	400,
					nbColFixed	:	2
				}					
	*/
	if ( ! options.table_width )
		options.table_width	=	900;
	if ( ! options.table_height )
		options.table_height=	400;
	if ( ! options.nbColFixed )
		options.nbColFixed	=	0;
		
	var param_table	=	{ 
							table_width		:	options.table_width,
							table_height	:	options.table_height,
							scrollBar_width	:	17,
							nbColFixed		:	options.nbColFixed
						};
	
	// on récupère le contenu total du tableau
	var tableClone1	=	table.cloneNode(true);	// => thead
	
	var tableClone2	=	table.cloneNode(true);	// => tbody
	
	if ( param_table.nbColFixed > 0 )
		var tableClone3	=	table.cloneNode(true);	// => colonnes figées
		
	var tableClone4	=	table.cloneNode(true);	// => footer
	
	// on construit le tableau qui contiendra uniquement le thead
	tableClone1.id	=	"tableau_thead";	
	tableClone1.setAttribute('onclick',"GUI_detectSortClick(event, $('"+table.id+"'));");
	tableClone1.addClassName('sorttableDerivate');
	tableClone1.removeClassName('sorttable');
	tableClone1.select('tbody').each(function(el){el.update('');});
	tableClone1.select('tfoot').each(function(el){el.update('');});
	
	// on construit le tableau qui contiendra uniquement le tbody ( sans les x premières colonnes )
	tableClone2.id	=	"tableau_tbody";	
	tableClone2.setAttribute('onclick',"");	
	tableClone2.addClassName('sorttableDerivate');
	tableClone2.removeClassName('sorttable');
	tableClone2.select('thead').each(function(el){el.update('');});
	tableClone2.select('tfoot').each(function(el){el.update('');});
	tableClone2.select('tbody').each(function(el){ 
			el.select('tr').each( function(tr) {
				tr.setAttribute('height','28px');
				listeTd	=	tr.select('td');
				for (z=0;z<param_table.nbColFixed;z++)
				{
					col	=	listeTd[z];
					col.remove();
				}
			}
		);
	});
	
	if ( param_table.nbColFixed > 0 )
	{
		// On construit Le tableau qui contiendra uniquement les x premières colonnes
		tableClone3.id	=	"tableau_firstCol";// 
		tableClone3.setAttribute('onclick',"");	
		tableClone3.addClassName('sorttableDerivate');
		tableClone3.removeClassName('sorttable');
		tableClone3.select('thead').each(function(el){el.update('');});
		tableClone3.select('tfoot').each(function(el){el.update('');});
		tableClone3.select('tbody').each(function(el){ 
				el.select('tr').each( function(tr) { 
					tr.setAttribute('height','28px');
					listeTd	=	tr.select('td');
					var colToAdd	=	new Array();
					for (z=0;z<param_table.nbColFixed;z++)
					{
						colToAdd[z]	=	listeTd[z];
					}
					tr.update(''); 
					for ( z=0;z<colToAdd.length;z++)
					{
						tr.appendChild(colToAdd[z]);
					}
				}
			)
		});;
	}
	
	// on construit le tableau qui contiendra uniquement le tfoot
	tableClone4.id	=	"tableau_tfoot";	
	tableClone4.setAttribute('onclick',"");
	tableClone4.addClassName('sorttableDerivate');
	tableClone4.removeClassName('sorttable');
	tableClone4.select('tbody').each(function(el){el.update('');});
	tableClone4.select('thead').each(function(el){el.update('');});
	
	
	var divContent			=	new Element('div');
	divContent.setAttribute('width',param_table.table_width+'px');
	divContent.setStyle('width:'+param_table.table_width+'px;position:relative;');
	
	if ( param_table.nbColFixed > 0 )
	{
		var divFirstColContent	=	new Element('div');
		divFirstColContent.id	=	"divFirstColContent";
		divFirstColContent.setStyle('border-left:1px solid #c8d5e7;position:relative;float:left;overflow:hidden;height:'+param_table.table_height+'px');
		divFirstColContent.appendChild(tableClone3);
	}
	
	var divFixedHeader		=	new Element('div');
	divFixedHeader.id		=	"divFixedHeader";
	divFixedHeader.setStyle('width:'+parseFloat(param_table.table_width-param_table.scrollBar_width)+'px;overflow:hidden');
	divFixedHeader.appendChild(tableClone1);
	
	var divTableContent		=	new Element('div');
	divTableContent.id		=	"divTableContent";
	divTableContent.setAttribute('onscroll','fnScroll()');
	if ( param_table.nbColFixed == 0)
		divTableContent.setStyle('border-left:1px solid #c8d5e7;overflow:scroll;height:'+(param_table.table_height+param_table.scrollBar_width)+'px;'); // width:'+table_width+'px;'
	else
		divTableContent.setStyle('overflow:scroll;height:'+(param_table.table_height+param_table.scrollBar_width)+'px;'); // width:'+table_width+'px;'
	
	divTableContent.appendChild(tableClone2);
	
	var divFixedFooter		=	new Element('div');
	divFixedFooter.id		=	"divFixedFooter";
	divFixedFooter.setStyle('width:'+parseFloat(param_table.table_width-param_table.scrollBar_width)+'px;overflow:hidden');
	divFixedFooter.appendChild(tableClone4);
		
	divContent.appendChild(divFixedHeader);
	if ( param_table.nbColFixed > 0 )
	{
		divContent.appendChild(divFirstColContent);
	}
	divContent.appendChild(divTableContent);
	divContent.appendChild(divFixedFooter);
		
	GUI_miseEnPageFixedHeader(table, divContent, param_table);
}

function GUI_detectSortClick(event, table)
{
	if ( Prototype.Browser.IE )
	{
		msg	=	"Vous utilisez Internet Explorer™, il est possible que l'opération soit longue à s'exécuter (20 à 30 secondes).<br />";
		msg	=	msg + "Vous obtiendrez des résultats optimaux avec Google Chrome™ ou Mozilla Firefox™";
		msgBox.warn(msg,{life:15});
	}
	GUI_showLoading();
	e	=	Object.clone(event);
	tbl	=	table;
	setTimeout("sortColumnDerivateTable(e, tbl);",150);
}

fnScroll = function()
{
	adjustTop 	= 	parseFloat(document.body.scrollTop) || parseFloat(document.documentElement.scrollTop);
	adjustLeft	=	parseFloat(document.body.scrollLeft) || parseFloat(document.documentElement.scrollLeft);
	position	=	$('tableau_tbody').cumulativeScrollOffset();
	var top		=	0;
	var left	=	0;
	if ( adjustTop > 0 )
		top		=	parseFloat(position.top) - parseFloat(adjustTop);
	else
		top		=	parseFloat(position.top) ;
	
	if ( adjustLeft > 0 )
		left	=	parseFloat(position.left) - (parseFloat(adjustLeft));
	else
		left	=	parseFloat(position.left)  ;

	// déplacement des divs
	$('divFixedHeader').scrollTop		=	top;
	$('divFixedHeader').scrollLeft		=	left;
	
	$('divFirstColContent').scrollTop 	=	top;
	$('divFirstColContent').scrollLeft 	=	left;
	
	$('divFixedFooter').scrollTop		=	top;
	$('divFixedFooter').scrollLeft		=	left;
}

function GUI_showLoading() 
{
	// Création d'une div de chargement
	var zIdx = 1;
	if ( ! loadingDiv )
	{
		loadingDiv  = new Element("div",
			{
				id:'loadingDiv', 
				style:"text-align:center;vertical-align:middle;background-color:#000000;color:white;font-weight:bold;position:absolute;top:0;left:0;display:none;"
			}
		);
		$(document.body).insert(loadingDiv);
		loadingDiv.setOpacity(0.5);
		// on recupere les dimensions du viewport
		var vpDim = document.viewport.getDimensions();
		// ... les dimensions du body
		var bodyDim = $(document.body).getDimensions();
		// On garde le max 
		var protectDim = {
		width: Math.max(vpDim.width, bodyDim.width), 
		height: Math.max(vpDim.height, bodyDim.height)
		};
		// qui definissent la taille de notre div de protection
		loadingDiv.setStyle(
			{
				width:protectDim.width + "px",
				height: protectDim.height + "px",
				zIndex:zIdx
			}
		);
		loadingDiv.innerHTML	=	'<img style="padding:25px;position:absolute;top:200px;left:45%;" width="75px" src="'+ROOT_URL+'images/ajax-loader.gif" />'
									+'<br /><p style="position:absolute;top:300px;left:44%;font-size:16px">Chargement en cours</p>';
		// on l'affiche
	}
	loadingDiv.show();	
}

function GUI_hideLoading()
{
	loadingDiv.hide();
}

// Fonction de mise en page du nouveau tableau
function GUI_miseEnPageFixedHeader(table, divContent, param_table) // tableThead, tableTbody, tableFirstCol)
{
	var sizeOfColToAdd	=	new Array();
	var styleCol		=	new Array();
	var firstTrduTbody	=	table.select('tbody')[0].select('tr')[0];
	// On récupère la taille des premières colonnes qui devront être fixées
	var	listeTd		=	firstTrduTbody.select('td');			
	for ( z=0;z<param_table.nbColFixed;z++)
	{
		sizeOfColToAdd[z]	=	listeTd[z].getWidth();//(	sizeOfColToAdd[z]	< listeTd[0].getWidth() ) ? listeTd[0].getWidth() : sizeOfColToAdd[z];
		styleCol[z]			=	'width:'+sizeOfColToAdd[z]+'px';
	}

	
	// On vide le contenu de l'élément parent
	upperElement	=	table.up();
	upperElement.update('');
	
	// On masque le tableau originel qui reste présent pour recevoir / construire les nouveaux tableaux triés
	table.style.display	=	'none';
	
	// on ajoute le tableau masqué
	upperElement.appendChild(table);
	// On ajoute le div qui contient le tableau fixé
	upperElement.appendChild(divContent);

	var theads		=	$('tableau_thead').select('thead');
	var thead		=	theads[0];
	
	var tbodies		=	$('tableau_tbody').select('tbody');
	var tbody		=	tbodies[0];
	
	var tfoots		=	$('tableau_tfoot').select('tfoot');
	var tfoot		=	tfoots[0];
	
	var wdth		=	0;
		
	var tbody_td_width	=	new Array();
	var thead_td_width	=	new Array();
		
	var i 			=	0;
	var TB_nbCols	=	0;//tbody.cells.length;
	var TB_nbRows	=	tbody.rows.length;
	
	var TH_nbRows	=	thead.rows.length;
	
	var newTr	=	new Element('tr');
	TB_nbCols	=	tbody.select('tr')[0].select('td').length;
	tbody.select('tr')[0].select('td').each( function (td) {
		if ( ! tbody_td_width[i] )
			tbody_td_width[i]	=	td.getWidth();
		if ( tbody_td_width[i] < td.getWidth() )
			tbody_td_width[i]	=	td.getWidth();
		i	=	i+1;
	});
	
	i	=	0;
	nb_tr	=	thead.select('tr').length;
	listeTrDuThead	=	thead.select('tr');
	listeTrDuThead[nb_tr-1].select('td').each(function (td) {
		if ( ! thead_td_width[i] )
				thead_td_width[i]	=	td.getWidth();
		if ( thead_td_width[i] < td.getWidth() )
			thead_td_width[i]	=	td.getWidth();
		i	=	i+1;
		});
	// ajustement des colonnes 
	var tr_th	=	new Element('tr');	// ligne qu'on ajoutera au thead pour ajuster les tailles de colonnes
	var tr_tb	=	new Element('tr');	// ligne qu'on ajoutera au tbody pour ajuster les tailles de colonnes
	var tr_tf	=	new Element('tr');	// ligne qu'on ajoutera au tfoot pour ajuster les tailles de colonnes
	
	// on installe les x premières colonnes de l'entête et du footer
	var sizeTotal 	=	0;
	for ( z=0;z<param_table.nbColFixed;z++)
	{
		var elDiv_temp	=	new Element('div');
		var td_temp		=	new Element('td');
		var elDiv_temp2	=	new Element('div');
		var td_temp2	=	new Element('td');
		elDiv_temp.setStyle('height:0px;width:'+sizeOfColToAdd[z]+'px');
		td_temp.appendChild(elDiv_temp);
		elDiv_temp2.setStyle('height:0px;width:'+sizeOfColToAdd[z]+'px');
		td_temp2.appendChild(elDiv_temp2);
		tr_th.appendChild(td_temp);
		tr_tf.appendChild(td_temp2);
		sizeTotal	=	sizeTotal + +sizeOfColToAdd[z];
	}

	// puis on mets en page toutes les autres
	for ( i_col = 0; i_col < TB_nbCols ; i_col++ )
	{
		var td_1	=	new Element('td');
		var td_2	=	new Element('td');
		var td_3	=	new Element('td');
		var elDiv_1	=	new Element('div');
		var elDiv_2	=	new Element('div');
		var elDiv_3	=	new Element('div');
		if ( thead_td_width[i_col+param_table.nbColFixed] < tbody_td_width[i_col] )
		{
			elDiv_1.setStyle('height:0px;width:'+(parseFloat(tbody_td_width[i_col]))+'px');
			elDiv_2.setStyle('height:0px;width:'+(parseFloat(tbody_td_width[i_col]))+'px');
			elDiv_3.setStyle('height:0px;width:'+(parseFloat(tbody_td_width[i_col]))+'px');
		}
		else
		{
			elDiv_1.setStyle('height:0px;width:'+(parseFloat(thead_td_width[i_col+param_table.nbColFixed]))+'px');
			elDiv_2.setStyle('height:0px;width:'+(parseFloat(thead_td_width[i_col+param_table.nbColFixed]))+'px');
			elDiv_3.setStyle('height:0px;width:'+(parseFloat(thead_td_width[i_col+param_table.nbColFixed]))+'px');
		}
		td_1.appendChild(elDiv_1);
		td_2.appendChild(elDiv_2);
		td_3.appendChild(elDiv_3);
		tr_th.appendChild(td_1);
		tr_tb.appendChild(td_2);
		tr_tf.appendChild(td_3);
	}
	
	thead.appendChild(tr_th);
	tbody.appendChild(tr_tb);
	tfoot.appendChild(tr_tf);
	
	if ( param_table.nbColFixed > 0 )
	{
		$('tableau_firstCol').select('tbody').each(function(tb){
			tb.select('tr').each(function(tr){ 
				listeTd			=	tr.select('td');
				
				for ( z=0;z<param_table.nbColFixed;z++)
				{
					listeTd[z].setAttribute('width',sizeOfColToAdd[z]+'px');
				}
			});
			var newTr	=	new Element('tr');
			for ( z=0;z<param_table.nbColFixed;z++)
			{
				var td	=	new Element('td');
				var divv=	new Element('div');
				divv.setAttribute('width',sizeOfColToAdd[z]+'px');
				td.appendChild(divv);
			}
			newTr.appendChild(td);
			tb.appendChild(newTr);
		});
	}
	GUI_hideLoading();
}
