//Position de la souris
var mousePos = { x:0 , y:0 };

  if (document.layers) 
	document.captureEvents(Event.MOUSEMOVE); 
	if (document.layers || document.all) 
	document.onmousemove = mouseMove; 
	if (document.addEventListener) 
	document.addEventListener('mousemove', mouseMove, true);

//Fenêtres et popups
var menuOpened = false;
var newWin = null;
var appletWin = null;
var printWin = null;

function closeWin(){
  if (newWin != null && !newWin.closed) {
    newWin.close();
  }
  if (appletWin != null && !appletWin.closed) {
    appletWin.close();
  }
  if (printWin != null && !printWin.closed) {
    printWin.close();
  }
}

function openPopUp(strURL,strType,strHeight,strWidth) {
  closeWin();
  var strOptions="";
  if (strType=="console") strOptions="toolbar,menubar,location,status,height="+strHeight+",width="+strWidth;
  if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
  if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
  newWin = window.open(strURL, 'newWin', strOptions);
  newWin.focus();
  return newWin;
} 

//Gestion des selects (IE)
function hideAllCombo() {
  displayAllCombo(false);
}

function showAllCombo() {
  displayAllCombo(true);
}

function displayAllCombo(value) {
  var disVal = 'block';
  if (!value) {
    disVal = 'none';  
  }

  var lin = document.getElementsByTagName('SELECT');
  for (var i=0;i<lin.length;i++) {
    var inp = lin[i];
    inp.style.display = disVal;
  }
}

//Gestion du onLoad sur <body>
function addToStart(fnc){  
  if(!window.listStart) window.listStart = new Array();  
  window.listStart.push(fnc);
}

function start(){
  var ls = window.listStart;
  if(ls){
    for(i=0; i<ls.length; i++){
      fnc = ls[i];
      if(typeof(fnc) == 'function'){
        fnc();
      } else {
        eval(fnc);
      }
    }
  }
}

//Affichage et masquage des menus
function showMenu(id) {
var d = document.getElementById('smenu'+id);
var e = document.getElementById('libmenu'+id);

	for (var i = 1; i<=15; i++) {
		/*if (document.getElementById('smenu'+i) && document.getElementById('libmenu'+i) && i!=id) {*/
    if (document.getElementById('smenu'+i) && document.getElementById('libmenu'+i)) {
			document.getElementById('smenu'+i).style.display='none';
			document.getElementById('libmenu'+i).style.color='#666666';
		}
    maskSelect(id,'off');
	}
	if (d && e) {
		d.style.display='block';
    maskSelect(id,'on');
    
		e.style.color='#003366';
	}
  
}

function getNbMenus() {
  var m = document.getElementById('navigation');
  var l = m.childNodes;
  for (var i=0;i<l.length;i++) {
    alert(l[i].nodeName);
    alert(l[i].name);
    alert(l[i].id);
    alert(l[i].className);
  }
}

//Affichage et masquage des onglets des menus de navigation
function showTab(id) {
var nav = document.getElementById('navigation');
var d = document.getElementById('nav'+id);
	for (var i = 1; i<=5; i++) {
		if (document.getElementById('nav'+i)) {
			document.getElementById('nav'+i).style.display='none';
		}
	}
	if (d && nav) {
		nav.className ='section'+id;
		d.style.display='';
	}
}

//Affichage et masquage des onglets d'une page
function showOnglet(id) {
	var c = document.getElementById('content'+id);
	var d = document.getElementById('onglet'+id);
	
	for (var i = 1; i<=5; i++) {
		if (document.getElementById('content'+i) && document.getElementById('onglet'+i)) {
			document.getElementById('content'+i).style.display='none';
			document.getElementById('onglet'+i).className='';
		}
	}
	if (c && d) {
		c.style.display='';
		d.className='selected';
	}
}

//Redirection javascript
function goTo(link) {
  alert('Bug Referer possible');
  window.location.replace(link);
}

function refresh() {
  window.location.reload();
}

//Masquage du curseur d'attente
function displayPage() {
  document.getElementById('loadMask').style.display='none';
  //addToStart(showAllCombo);
}

//Affichage du curseur d'attente
function maskPage() {
  //Récupération de la taille de la fenêtre
  var d = document.getElementById('loadMask');
  var l;
  var h;
  
  if (document.body && d)
  {
    lwin = (document.body.clientWidth + document.body.scrollLeft);
    hwin = (document.body.clientHeight + document.body.scrollTop);
    h= document.body.scrollHeight;
    l= document.body.scrollWidth;
    
    if (hwin > h) {
      h = hwin;
    }
    
    if (lwin > l) {
      l = lwin;
    }
    
    d.style.display='';
    d.style.width=l;
    d.style.height=h;
    
  } else if (d) {
    d.style.display='';
  }
  //hideAllCombo();
  shutSelect();
}

//Désactivation de l'évènement onSubmit du formulaire
function shutOnSubmit(td) {
  if(navigator.appName!='Microsoft Internet Explorer') {
    td.form.setAttribute('onsubmit','');
  } else {
    td.form.onsubmit='';
  }
}

//Masquage des select de la page afin de résoudre le bug IE de superposition de DIV
function maskSelect(id,state) {
  //Si IE
  if (document.all && state == 'on') {
    
    //Récupération des select de la page
    list = document.getElementsByTagName("SELECT");
    
    var d = document.getElementById('smenu'+id);
    
    if (d) {
      var l = d.offsetWidth;
      var h = d.offsetHeight;
      var x = fDomOffset(d,'offsetLeft');
      var y = fDomOffset(d,'offsetTop');
    }
    
    for (var i=0;i<list.length;i++) {
      var e = list[i];
      
      //Si menu ouvert
      if (d) {
        var le = e.offsetWidth;
        var he = e.offsetHeight;
        var xe = fDomOffset(e,'offsetLeft');
        var ye = fDomOffset(e,'offsetTop');
        
        //Si select superposée avec menu
        if ( ((xe < x && (xe+le) > x) || (xe > x && xe < (x+l))) && 
             ((ye < y && (ye+he) > y) || (ye > y && ye < (y+h))) ) {
          
          e.style.visibility = 'hidden';
        }
      //Sinon menu fermé 
      } else {
          e.style.visibility = 'visible';
      }
    }
  } else if (document.all && state == 'off') {
    
    //Récupération des select de la page
    list = document.getElementsByTagName("SELECT");
    
    for (var i=0;i<list.length;i++) {
      var e = list[i];
      e.style.visibility = 'visible';
    }
  }
}

//Désactivation des select de la page lorsque maskPage est lancée
function shutSelect() {
  //Si IE
  if (document.all) {
  //Récupération des select de la page
    list = document.getElementsByTagName("SELECT");
    for (var i=0;i<list.length;i++) {
      var e = list[i];
      e.style.cursor = 'wait';
      e.onchange = '';
    }
  }
}

fDomOffset = function( oObj, sProp )
{ 
 // oObj : [Object] - Objet dont on désire le positionnement 
 // sProp : [String] - Propriété désirée : offsetLeft - offsetTop 
 var iVal = 0; 
 // On boucle tant que l'on n'est pas à la racine du document 
 while (oObj && oObj.tagName != 'BODY') { 
  eval('iVal += oObj.' + sProp + ';');
  oObj = oObj.offsetParent; 
 } 
 return iVal; 
}

function formatMoney(valeur) {
    // formate un chiffre avec 'decimal' chiffres après la virgule et un separateur 
    var decimal=2;
    var separateur=" ";
    var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; 
    var val=Math.floor(Math.abs(valeur));
    if ((decimal==0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;}
    var val_format=val+"";
    var nb=val_format.length;
    for (var i=1;i<4;i++) {
      if (val>=Math.pow(10,(3*i))) {
        val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i));
      }
    }
    if (decimal>0) {
      var decim=""; 
      for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";}
      deci=decim+deci.toString();
      if(deci=="00"){
      //val_format=val_format;
      //Obtenir 0.00 :
        val_format=val_format+"."+deci;
      }else{
        val_format=val_format+"."+deci;
      }
    }
    if (parseFloat(valeur)<0) {val_format="-"+val_format;}
    return val_format;
}

/* Listes à bascule */

//Déplacement d'un item
function selection_all_champs(champs,champs_affiche){
    for(i=0;i<champs.length;i++){
      champs.options[i].selected = true;
    }
    selection_champs(champs,champs_affiche);
    for(i=0;i<champs_affiche.length;i++){
      champs_affiche.options[i].selected = false;
    }
}

//Déplacement de tous les items
function selection_champs(champs,champs_affiche){
   //on récupère l'endroit sélectionner dans le select source
    selection = champs.selectedIndex;
    if(selection != -1){
        //on déselectionne tous les champs du select de destination où va être placé le(s) champ(s) selectionner
        while(champs_affiche.selectedIndex != -1){
            champs_affiche.options[champs_affiche.selectedIndex].selected = false;
        }

        while(champs.selectedIndex > -1){
            //on cherche la place de notre champ
            for(place=0;place<champs_affiche.length;place++){
                if(champs_affiche.options[place].text > champs.options[champs.selectedIndex].text){
                    break;
                }
            }
            //on décale tous les champs
            for(i=champs_affiche.length;i>place;i--){
                champs_affiche.options[i] = new Option(champs_affiche.options[(i-1)].text,champs_affiche.options[(i-1)].value);
            }

            //on insère le champ selectionner
            champs_affiche.options[place] = new Option(champs.options[champs.selectedIndex].text,champs.options[champs.selectedIndex].value);
            champs.options[champs.selectedIndex] = null;
            champs_affiche.options[place].selected = true;
        }

        if(champs.length > 0){
            if(selection >= champs.length ){
                selection = champs.length-1;
            }
            champs.options[selection].selected = true;
        }
         for(i=0;i<champs.length;i++){
        champs.options[i].selected = false;
    }
    }
}

function afficherAide() {
      var td = document.getElementById('aideText');
      if (td) {
          if(td.style.display != 'none'){
            td.style.display = 'none';
          }
          else {
             td.style.display = 'block';
          }
      }
}

//Ouverture de l'écran d'aide
function openAide(linkAide)
{
  var Nav = navigator.appName;
  var Ver = parseInt(navigator.appVersion);
  
  if ((Nav == "Netscape") && (Ver >= 4)){
  var newWindow = window.open(linkAide, 'Star', 'toolbar=no, menubar=no, status=yes, directories=no, resizable=yes, scrollbars=yes, location=no, width=800, height=600');
  newWindow.moveTo(0,0);
  newWindow.resizeTo(screen.availWidth,screen.availHeight);
  }
  else if((Nav == "Microsoft Internet Explorer") && (Ver >= 4))
  {
  propriete = "top=0,left=0,resizable=yes,";
  propriete += "toolbar=no, scrollbars=yes, menubar=no, location=no, status=yes";

  var newWindow = window.open(linkAide, 'Star', propriete);
  newWindow.moveTo(0,0);
  newWindow.resizeTo(screen.availWidth,screen.availHeight);
  }
  else
  {
  self.location=linkAide;
  }
  
}

//Affichage de la zone de filtre avancée
//Ne peut pas devnir générique car check si saisie par écran, pour gérer len rafraichissement
/*function displayFilters() {
  var td = document.getElementById('advancedfilters');
  var link = document.getElementById('switchfilterlink');
  if (td && td.style.display != 'none') {
    td.style.display = 'none';
    if (link) {
      link.value = 'Filtres avancés';
    }
  } else if (td) {
    td.style.display = 'block';
    if (link) {
      link.value = 'Filtres simples';
    }
  }
}*/


//Affichage de tooltips pour les options contenues dans une liste à bascule
function displayTooltips(basculeLeftId, basculeRightId) {
  var td = document.getElementById(basculeLeftId);
  var te = document.getElementById(basculeRightId);
  
  if (td && td.options) {
    for (var i=0; i<td.options.length; i++) {
      td.options[i].title = td.options[i].innerHTML;
    }
  }
  
  if (te && te.options) {
    for (var i=0; i<te.options.length; i++) {
      te.options[i].title = te.options[i].innerHTML;
    }
  }
  
}

//Maxlength textarea
function ismaxlength(obj,txtlength){
  if (obj.getAttribute && obj.value.length>txtlength) {
    obj.value=obj.value.substring(0,txtlength);
  }
}

//Impression de la page
function printPage() { print(document); }

//Popups
function openPrintPopup(path) {
  //Mantis n°931
  //window.open(path, "popup", "width=950,height=700,resizable=yes,status=yes,toolbar=no,scrollbars=no");
  
  var Nav = navigator.appName;
  var Ver = parseInt(navigator.appVersion);
      
      if ((Nav == "Netscape") && (Ver >= 4)) {
        closeWin();
      
        printWin = window.open(path, 'printWin', 'toolbar=no, menubar=no, status=yes, directories=no, resizable=yes, scrollbars=yes, location=no, width=100%, height=100%');
        printWin.moveTo(50,50);
        printWin.resizeTo(screen.availWidth - 200,screen.availHeight - 200);
        printWin.focus();
      
      } else if((Nav == "Microsoft Internet Explorer") && (Ver >= 4)) {
        closeWin();
        
        var propriete;
        propriete = "top=50,left=50,resizable=yes,";
        propriete += "toolbar=no, scrollbars=yes, menubar=no, location=no, status=yes";
        printWin = window.open(path, 'printWin', propriete);
        printWin.resizeTo(screen.availWidth - 200,screen.availHeight - 200);
        printWin.focus();
    } else {
      window.open(path, "printWin", "resizable=yes,status=yes,toolbar=no,scrollbars=no");
    }
  
}

function openPasswordPopup(path) {
  //Mantis n°931
  window.open(path, "popup", "resizable=yes,status=yes,toolbar=no,scrollbars=no");
  //openPrintPopup(path); 
}

//Optenir la position absolue d'un tag HTML
function position(e) {
xPos = (navigator.appName.substring(0,3) == "Net") ? e.pageX : event.x+document.body.scrollLeft;
yPos = (navigator.appName.substring(0,3) == "Net") ? e.pageY : event.y+document.body.scrollTop;
var r = { x: xPos, y: yPos };
return r;
}

function mouseMove (evt) { 
		var x=0; var y=0;
		if (document.layers) {
			x=evt.x; y=evt.y;
		} 
		if (document.all) {
			x=event.clientX; y=event.clientY;
		} else {
			if (document.getElementById) {
				x=evt.clientX; y=evt.clientY; 
			}
		}
		var r = { x: x, y: y };
		/*r.x = (navigator.appName.substring(0,3) == "Net") ? r.x - document.body.scrollLeft: r.x;
		r.y = (navigator.appName.substring(0,3) == "Net") ? r.y - document.body.scrollTop: r.y;*/
		/*if (navigator.appName.substring(0,3) != "Net") {
		r = { x: x+document.body.scrollLeft, y: y+document.body.scrollTop };
		}*/
		mousePos = r;
	} 

function getAbsolutePos(el) {
	//alert('Calendar.getAbsolutePos Debut');
	var SL = 0, ST = 0;
	//alert('Avant var is_div = /^div$/i.test(el.tagName); '+el);
	var is_div = /^div$/i.test(el.tagName);
	//alert('Apres var is_div = /^div$/i.test(el.tagName); ');
	if (is_div && el.scrollLeft)
		SL = el.scrollLeft;
	if (is_div && el.scrollTop)
		ST = el.scrollTop;
	if (navigator.appName.substring(0,3) == "Net") {
		SL = 0;
		ST = 0;
	}
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent) {
		var tmp = this.getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	//alert('Calendar.getAbsolutePos Fin');
	return r;
};


/* Liste à bascule */

function showDetailOpt(prov,dest) {
var c = document.getElementById(dest);
var d = document.getElementById(prov);
	for (i=0;i<d.length;i++) {
		var id = d.options[i].id.substring(3);
		if (document.getElementById('detail'+id)) {
			document.getElementById('detail'+id).style.display = 'none';
		}
	}
	for (i=0;i<c.length;i++) {
		var id = c.options[i].id.substring(3);
		if (document.getElementById('detail'+id)) {
			document.getElementById('detail'+id).style.display = 'block';
			document.getElementById('detailLabel'+id).innerHTML = c.options[i].value;
		}
	}
		

}

function ajouter(prov,dest){
var c = document.getElementById(prov);
var d = document.getElementById(dest);
	for (i=0;i<c.length;i++) {
		if (c.options[i].selected == true) {
			var sel = c.options[i];
			newsel = new Option(sel.value,sel.value,false,false);
			newsel.id = sel.id;
			d.options[d.length] = newsel;
		}	
	}
	
	for (i=0;i<c.length;i++) {
		if (c.options[i].selected == true) {
			c.options[i] = null;
			i=i-1;
		}	
	}
	if (prov.substring(9,11) == 'In') {
		showDetailOpt(prov,dest);
	} else {
		showDetailOpt(dest,prov);
	}
}

function ajouterTout(prov,dest){
var c = document.getElementById(prov);
var d = document.getElementById(dest);
	for (i=0;i<c.length;i++) {
			var sel = c.options[i];
			newsel = new Option(sel.value,sel.value,false,false);
			newsel.id=sel.id;
			d.options[d.length] = newsel;	
	}
	
	for (i=0;i<c.length;i++) {
			c.options[i] = null;
			i=i-1;
	}
	if (prov.substring(9,11) == 'In') {
		showDetailOpt(prov,dest);
	} else {
		showDetailOpt(dest,prov);
	}
	
}

function deselectionner(id) {
var c = document.getElementById(id);
	for (i=0;i<c.length;i++) {
		if (c.options[i].selected == true) {
			c.options[i].selected = false;
		}
	}
}

function showDetail() {
var c = document.getElementById('containerOut');
var selection = false;
var t = document.getElementById('labelElt');
var d = document.getElementById('detailElt');
if (t && d) {
	for (i=0;i<c.length;i++) {
		if (c.options[i].selected == true) {
			selection = true;
			t.innerHTML = c.options[i].value;
			d.style.display = 'block';
		}
	}
	if (selection == false) {
		d.style.display = 'none';
	}
}
}

//Anciennement liste.js
var rowIndex = 0;
var rowSel = null;

function refreshLine() {
    
    if (rowSel != null) {
      showLine(rowSel);	
		}	

}

function showLine(row) {
		var table;
		var index;				
		var i=1;
    
		if (row.parentNode) {
			// Get the parent table.
			table = row.parentNode.parentNode;
			
			// Get the row index.		
			for (index = 1; index < table.rows.length; index++) {
				/*if (row==table.rows[index]) {
					break;
				}*/
        table.rows[index].className = ('row'+i);
        if (table.rows[index] == row) {
          rowIndex = index;
          rowSel = row;
          alert(rowIndex);
        }
        i++
        if (i>2) {
          i=1;
        }
			}	
		 /*else {
			index = parseInt(row) - 1;
			table = document.getElementById(this.styleId);
			row = table.rows[index];
		}*/

  row.className = 'rowselected';
  }
}

function displayDiv(name) {
        
	var ie = document.getElementById('introExam');
	var ia = document.getElementById('introAnnuaire');
	if (name != '' && name == 'introExam' ) {
	    ia.style.display='none';
		ie.style.display='block';
	} else if (name != '' && name == 'introAnnuaire') {
	    ia.style.display='block';
	    ie.style.display='none';
	} else {
	    ia.style.display='block';
	    ie.style.display='none';
	} 
	   
}
