/* trouve le tag "parentTagName" parent de "element" */
function getParent(element, parentTagName) {
	if ( ! element )
		return null;
	else if ( element.nodeType == 1 && element.tagName.toLowerCase() == parentTagName.toLowerCase() )
		return element;
	else
		return getParent(element.parentNode, parentTagName);
}

/* ajoute une ligne */
function addLigneEIL(action, date_analyse, organ_cil, z_score, valeur_assign, ecart_type, corr_act) {

  var readonly = '';
  if (action == 'consulter')
    readonly = "readonly='readonly'";
    

  // Déclaration et initialisation d'une variable statique
  if ( typeof this.counter == 'undefined' ) this.counter = 0;
  ideil = this.counter;
  // Incrémentation de sa valeur
  this.counter++;
  $('maxindicedate').value = this.counter
  $('nbeils').value = parseInt ($F ('nbeils')) + 1;
  
	// 1. récuperer le node "TABLE" à manipuler
  table=document.getElementById("tableeil");
	
	// 2. on va manipuler le TBODY
	var tbody = table.tBodies[0];
	
	//tbody.rows.length
  iddate = "date"+ideil;
  idcor = "cor"+ideil;
  idorg = "org"+ideil;
  idscore = "score"+ideil;
  idval = "val"+ideil;
  idecart = "ecart"+ideil;
  trigger = "trigger"+ideil;
	
  line=tbody.insertRow(tbody.rows.length);
  
  cell=line.insertCell(0);
  cell.innerHTML="<input class='eil date' type='text' id="+iddate+" name="+iddate+" size='10'  maxlength='10' value='"+date_analyse+"' onblur='isDate(this)*requireValue(this)' title='Date de l&#39;analyse' "+readonly+" /><a id="+trigger+"><img class='date' src='../images/calendar.png' /></a>";
  
  cell=line.insertCell(1);
  cell.innerHTML="<input class='eil' type='text' id="+idorg+" name="+idorg+" size='20' maxlength='100' value='"+organ_cil+"' onblur='requireValue(this)*disallowDoubleQuote(this)' title='Organisateur des EIL' "+readonly+"/>";
  
  cell=line.insertCell(2);
  cell.innerHTML="<input class='eil' type='text' id="+idscore+" name="+idscore+" size='6' value='"+z_score+"' onblur='fixFloat(this)*requireValue(this)'  title='Z-Score' "+readonly+"/>";
  
  cell=line.insertCell(3);
  cell.innerHTML="<input class='eil' type='text' id="+idval+" name="+idval+" size='6' value='"+valeur_assign+"' onblur='fixFloat(this)*requireValue(this)'  title='Valeur assignée' "+readonly+" />";
  
  cell=line.insertCell(4);
  cell.innerHTML="<input class='eil' type='text' id="+idecart+" name="+idecart+" size='6' value='"+ecart_type+"' onblur='fixFloat(this)*requireValue(this)'  title='Ecart-type' "+readonly+" />";
  
  cell=line.insertCell(5);
  cell.innerHTML="<textarea class='eil' rows='1' id="+idcor+" name="+idcor+"  maxlength='65535' title='Corrections appliquées et actions mises en place si Z-Score>3' "+readonly+" onblur='disallowDoubleQuote(this)'>"+corr_act+"</textarea>";
  
  cell=line.insertCell(6);

  if (action != 'consulter')
  {
    cell.innerHTML="<a href='#' onclick='delLigneEIL(this); return false;'><img src='../images/supprimer.png' border='0' width='16' height='16' title=' Supprimer ' "+readonly+">";
    
    Calendar.setup({"ifFormat":"%d/%m/%Y",
      "daFormat":"%Y/%m/%d",
      "firstDay":1,
      "showsTime":false,
      "showOthers":false,
      "inputField":iddate,
      "button":trigger});
  }
    
	if ( document.all )
		line.style.display = "block"; // pour IE
	else
	{
		line.style.display = "table-row"; // pour Gecko
		line.style.verticalalign = "top"; // pour Gecko
	}
}

function addLigneFichier(nom) {

  var readonly = "readonly='readonly'";
    

  // Déclaration et initialisation d'une variable statique
  if ( typeof this.counter == 'undefined' ) this.counter = 0;
  idfichier = this.counter;
  // Incrémentation de sa valeur
  this.counter++;
  $('nbfichiersmax').value = this.counter
  $('nbfichiers').value = parseInt ($F ('nbfichiers')) + 1;
  
	// 1. récuperer le node "TABLE" à manipuler
  table=document.getElementById("tablefichier");
	
	// 2. on va manipuler le TBODY
	var tbody = table.tBodies[0];
	
  line=tbody.insertRow(tbody.rows.length);
  
  target = "target" + idfichier;
  userfile = "userfile" + idfichier;

  cell=line.insertCell(0);
  cell.innerHTML=
      "<div id="+target+"><input type='file' id="+userfile+" name="+userfile+"  size='70'  maxlength='50' onchange='fileChange (this, \"" + target+ "\")'>" +
      "<input name='targetid' id='targetid' type='hidden' value="+target+" />" +
      "<input name='userfileid' id='userfileid' type='hidden' value="+userfile+" />" +
      "<input name='idfichier' type='hidden' value="+idfichier+" />" +
      "<iframe name='target' style='display:none;' src='about:blank'></iframe></div>";
  
  cell=line.insertCell(1);
  cell.innerHTML="<a href='#' onclick='delLigneFichier(this); return false;'><img src='../images/supprimer.png' border='0' width='16' height='16' title=' Supprimer '"+readonly+">";
    
	if ( document.all )
		line.style.display = "block"; // pour IE
	else
	{
		line.style.display = "table-row"; // pour Gecko
		line.style.verticalalign = "top"; // pour Gecko
	}
}

/* supprimer une ligne */
function delLigneEIL(link) {
	// 1. récuperer le node "TABLE" à manipuler
	var td = link.parentNode;
	var table = getParent(td, 'TABLE');
	
	// 2. récuperer le TBODY
	var tbody = table.tBodies[0];
	
	// 3. Supprimer le TR
	tbody.removeChild(getParent(td, 'TR'));
	
  $('nbeils').value = $F('nbeils')-1;
}

function delLigneFichier(link) {
	// 1. récuperer le node "TABLE" à manipuler
	var td = link.parentNode;
	var table = getParent(td, 'TABLE');
	
	// 2. récuperer le TBODY
	var tbody = table.tBodies[0];
	
	// 3. Supprimer le TR
	tbody.removeChild(getParent(td, 'TR'));
	
  $('nbfichiers').value = $F('nbfichiers')-1;
}

/* initialise le script */
dtableInit = function () {
	var table = document.getElementsByTagName('TABLE');
	for ( var i = 0; i < table.length; i++ ) {
		// on récupère tous les tableaux dynamiques
		if ( table[i].className == 'eil_tabletwo' ) {
			
			var tbody = table[i].tBodies[0];
//			var newTr = tbody.rows[0].cloneNode(true);
			

			// on en ajoute une
//			tbody.appendChild(newTr);

			// on masque la première ligne du tbody (la ligne de reference)
//		  tbody.rows[0].style.display = 'none';
			
			
		}
	}
}

var ReponScroll = true;
ReponseonScroll = function (requete)
{
  ReponScroll = true;
}

onScroll = function (varSession, pos)
{ 
  if (ReponScroll)
  {
    ReponScroll = false;
    var laRequete = new Ajax.Request('/commun/save-scrollpos.php',{asynchronous:true,method: 'post',parameters: 'var='+varSession+'&pos='+pos,onComplete:ReponseonScroll});
  } 
}

initScrollPos = function (pos)
{
  $('innerb').scrollTop = pos;
}


