/* Compatibilite */
function checkBrowser()
{	
	this.dom=document.getElementById?1:0;
	this.ver=navigator.userAgent;
	
	if (/Firefox[\/\s](\d+\.\d+)/.test(this.ver)) { //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
		this.ff=true;
	}
	else if(/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { //test for Opera/x.x or Opera x.x (ignoring remaining decimal places);	
		this.opera=true;
	}
	else {
		this.ie8=(this.ver.indexOf("MSIE 8")>-1 && this.dom)?1:0;
		this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom)?1:0;
		this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
		this.ie55=((this.ver.indexOf("MSIE 5.5")>-1 || this.ie6) && this.dom)?1:0;
		this.ie5=((this.ver.indexOf("MSIE 5")>-1 || this.ie5 || this.ie6) && this.dom)?1:0;
		this.ie4=(document.all && !this.dom)?1:0;
		this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
		this.ns4=(document.layers && !this.dom)?1:0;
		this.ie4plus=(this.ie8 || this.ie7 || this.ie6 || 	this.ie5 || this.ie4);
		this.ie5plus=(this.ie8 || this.ie7 || this.ie6 || this.ie5)
		this.ie6moins=(this.ie6 || this.ie5 || this.ie55 || this.ie4);
	}
	this.bw=(this.ie6 || this.ie7 || this.ie8 || this.ie5 || this.ie4 || this.ns4 || this.ns5 || this.ff || this.opera);	
	return this;
}
			
bw = new checkBrowser();
			
if (!document.getElementById) 
{
document.getElementById = getObjectById;
}

function getObjectById(ID) 
{
	var obj;
	if (bw.dom)
		return document.getElementById(ID);
	else if (bw.ie4)
		return document.all(ID);
	else if (bw.ns4)
		return eval('document.' + ID);
}

function getOpenerObjectById(ID) 
{
	var doc = window.opener.document;
	var obj;
	if (bw.dom)
		return doc.getElementById(ID);
	else if (bw.ie4)
		return doc.all(ID);
	else if (bw.ns4)
		return eval('doc.' + ID);
}

function getParentObjectById(ID) 
{
	var doc = window.parent.document;
	var obj;
	if (bw.dom)
		return doc.getElementById(ID);
	else if (bw.ie4)
		return doc.all(ID);
	else if (bw.ns4)
		return eval('doc.' + ID);
}

var mpositions = Array;

function mposition( e ){
	var ev = e || window.event;
	
	var positions = document.viewport.getScrollOffsets();
	
	mpositions[0] = ev.clientX + positions['left'];
	if(document.body && document.body.clientLeft) mpositions[0] -= document.body.clientLeft;
	mpositions[1] = ev.clientY + positions['top'];
	if(document.body && document.body.clientTop) mpositions[1] -= document.body.clientTop;
	//return true;
	var DocRef;    // Variable pour IE uniquement

  // L'événement est passée à la fonction
  // donc tous sauf IE
  if(e){                     // Dans ce cas on obtient directement la position dans la page
    Mouse_X = e.pageX;
    Mouse_Y = e.pageY;
  }
  else{                      // Dans ce cas on obtient la position relative à la fenêtre d'affichage
    Mouse_X = event.clientX;
    Mouse_Y = event.clientY;

    //-- Il faut traiter le CAS des DOCTYPE sous IE
    if( document.documentElement && document.documentElement.clientWidth) // Donc DOCTYPE
      DocRef = document.documentElement;   // Dans ce cas c'est documentElement qui est réfèrence
    else
      DocRef = document.body;                    // Dans ce cas c'est body qui est réfèrence

    //-- On rajoute la position liée aux ScrollBars
    Mouse_X += DocRef.scrollLeft;
    Mouse_Y += DocRef.scrollTop;
  }
  mpositions[0] = Mouse_X;
  mpositions[1] = Mouse_Y;
  
}
document.onmousemove = mposition;

// popups dhtml

// tableau permettant de stocker les informations susceptibles d'etres affichees sous forme de popup dhtml
// forme dpopups[popup_id][info_id] = [texte];		
var dpopups = Array;
var dpopup_entete = '<h2><div style="text-align: right;"><a href="javascript:;" onClick="hide_dpopup(this.parentNode.parentNode.parentNode.id, true);">X Fermer</a></div></h2>';
var current_dpopup = null;
var tw = null;

function dpopup(pid, id, position, wait, nohide) {
	
	if(wait > 0) {
		if(current_dpopup && current_dpopup != id) {
			hide_dpopup(pid, true);
		}
		if(!tw && !nohide) {
			tw = setTimeout('dpopup(\''+pid+'\', '+id+', \''+position+'\', 0)', wait);
		}		
		return true;
	}
	
	tw = null;
	var obj_id = pid+'_'+id;
	
	var obj = $(pid);
	
	if(obj_id == current_dpopup) {
		if(to) {
			window.clearTimeout(to);
			to = null;
		}
		if(obj && obj.style.visibility == 'visible') {
			return true;
		}
		return true;
	}
	else {
		if(obj) {
			hide_dpopup(pid, true);
		}
	}	
	// si le popup dhtml n'existe pas encore, on le cree, cela evite d'avoir a le declarer explicitement dans le code html de la page (ou permet de l'oublier...)
	if(!obj || (current_dpopup && obj_id != current_dpopup)) {
		obj = document.createElement('div');
		obj.id = pid;
		obj.className = 'dpopup';		
		document.body.appendChild(obj);
		obj.style.visibility = 'hidden';
		current_dpopup = null;
	}

	if(!nohide) obj.onmouseout = function() {hide_dpopup(pid, false);};
	else obj.onmouseout = null;

	if(dpopups[pid][id]) {
		// remplissage du popup
		obj.innerHTML = dpopup_entete+dpopups[pid][id];
		obj.style.height = obj.offsetHeight+'px';
		obj.onmouseover =  function() {dpopup(pid, id, position, 0, nohide);};
	}
	else {
		return false;
	}

	current_dpopup = obj_id;
	position_pop(obj, position);
	
	// enfin, on rend le popup visible
	obj.style.visibility = 'visible';
	if(tw && !nohide) {
		window.clearTimeout(tw);
		tw = null;
	}
	return true;
}

var info_opened = false;
function open_info(texte) {
	if(info_opened) return true;
	
	var obj = $('dinfo');
	if(!obj) {
		obj = document.createElement('div');
		obj.id = 'dinfo';
		document.body.appendChild(obj);
	}
	
	obj.innerHTML = texte;
	obj.style.top = mpositions[1]-30;
	obj.style.left = mpositions[0]+30;
	obj.style.visibility = 'visible';
	//obj.onmouseout = function() {close_info();};
	return true;
}

function close_info() {
	obj = $('dinfo');
	if(obj) {
		obj.style.visibility = 'hidden';
	}
	info_opened = false;
	return true;
}

function ie6_selects(display, idobj){
	
	selects = $$('select');
	
	selects.each( function(item) {
			item.style.display = display;
		}
	);
	
}

// ferme le popup, c-a-d  le rend simplement invisible
var to = null;
function hide_dpopup(pid, force) {
	if(to || force) {
		if(to) {
			window.clearTimeout(to);
		}
		
		if(bw.ie6moins) {
			ie6_selects('block', pid);
		}
		
		obj = $(pid);
		if(obj) {
			obj.parentNode.removeChild(obj);
		}
		to = false;
		current_dpopup = null;
	}
	else {
		to = window.setTimeout('hide_dpopup(\''+pid+'\', true)', 10);
	}
	return true;
}

var pop_and_blur_elmt_id;
function pop_and_blur_elmt(dom_elmt, position, callback) {
	if(bw.ie6moins) {
		ie6_selects('none', 'pop_blur');
	}
	
	pop_and_blur_elmt_id = dom_elmt.id;
	
	image = $('masque');
	if(!image) {
		image = document.createElement('img');
		image.id = 'masque';
		image.src = 'imgs/common/blur.gif';
		document.body.appendChild(image);
	}
	
	image.style.width = document.body.offsetWidth;
	image.style.height = document.body.offsetHeight;
	image.style.visibility = 'visible';
	
	obj = $('pop_blur');
	if(!obj) {
		obj = document.createElement('div');
		obj.id = 'pop_blur';
		obj.className = 'dpopup';
		obj.style.position = 'absolute';
		obj.style.visibility = 'hidden';
		document.body.appendChild(obj);
	}
	
	var link = document.createElement('a');
	link.href='javascript:void(0);';
	
	if(callback && callback != '') {
		eval('var code = function() {'+callback+'close_pop_and_blur();};');
		link.onclick = code;
	}
	else {
		link.onclick = function() {close_pop_and_blur();};
	}
	
	dom_elmt.style.display = 'block';
	
	link.innerHTML= 'Fermer';
	obj.appendChild(link);
	obj.appendChild(document.createElement('br'));
	obj.appendChild(dom_elmt);
	
	position_pop(obj, position);
	
	obj.style.visibility = 'visible';
	obj.style.display = dom_elmt.style.display = 'block';
	
	
	
	return true;
}

function lien_fermer_pop_and_blur() {
	return '<a href="javascript:void(0);" onClick="javascript:close_pop_and_blur(); return false;">Fermer</a><br>';
}

function pop_and_blur(html, position) {
	if(bw.ie6moins) {
		ie6_selects('none', 'pop_blur');
	}
	
	image = $('masque');
	if(!image) {
		image = document.createElement('img');
		image.id = 'masque';
		image.src = 'imgs/common/blur.gif';
		document.body.appendChild(image);
	}
	
	image.style.width = document.body.offsetWidth;
	image.style.height = document.body.offsetHeight;
	image.style.visibility = 'visible';

	obj = $('pop_blur');
	if(!obj) {
		obj = document.createElement('div');
		obj.id = 'pop_blur';
		obj.className = 'dpopup';
		obj.style.position = 'absolute';
		document.body.appendChild(obj);
		obj.style.visibility = 'hidden';
	}
	
	obj = $('pop_blur');
	if(obj.innerHTML == '') {
		obj.innerHTML += lien_fermer_pop_and_blur();
	}
	obj.innerHTML += html;
	position_pop(obj, position);
	obj.style.visibility = 'visible';
	
	return true;
}

function position_pop(obj, position) {
	
	var positions = document.viewport.getScrollOffsets();
	var dimensions = document.viewport.getDimensions();
	
	var obj_dimensions = $(obj.id).getDimensions();
	
	if(dimensions['width'] > 500 && obj_dimensions['width'] >= 476) {
		obj.style.width = 476+'px';
	}
	
	switch(position) {
		case 'souris' : pleft = (dimensions['width']- obj_dimensions['width']) / 2; ptop = (mpositions[1]-obj_dimensions['height']-10); break;
		case 'centre' : pleft = (dimensions['width'] - obj_dimensions['width']) / 2; ptop = mpositions[1]; break;
		case 'centre_total' : pleft = (dimensions['width'] - obj_dimensions['width']) / 2; ; ptop = positions['top'] + (dimensions['height'] - obj_dimensions['height']) / 3; break;
		case 'top': pleft = (dimensions['width'] - obj_dimensions['width']) / 2; ptop = positions['top']+5; break; 
		default: pleft = (dimensions['width'] - obj_dimensions['width']) /2; ptop = mpositions[1]; break;
	}
	
	if(ptop < 150) ptop = 150;
	
	obj.style.top = ptop+'px';
	obj.style.left = pleft+'px';
	obj.style.height = obj_dimensions['height']+'px';
	
	var max_height = dimensions['height'] - (ptop-positions['top']+15);
	
	// ajustement de la hauteur du popup
	if(parseInt(obj.style.height) > max_height) {
		obj.style.height = max_height+'px';
	}
	
	if(obj_dimensions['height'] < parseInt(obj.style.height)) {
		obj.style.height = (obj_dimensions['height']+2)+'px';
	}
}

function close_pop_and_blur() {
	var obj = $('pop_blur');
	
	if(bw.ie6moins) {
		ie6_selects('block', obj.id);
	}
	
	if(obj) {
		if(pop_and_blur_elmt_id) {
			savobj = $('save_'+pop_and_blur_elmt_id);
			if(!savobj) {
				savobj = document.createElement('div');
				savobj.id = 'save_'+pop_and_blur_elmt_id;
				savobj.style.display = 'none';
				savobj.style.height = 0;
				document.body.appendChild(savobj);
			}
			savobj.appendChild($(pop_and_blur_elmt_id));
		}
		obj.parentNode.removeChild(obj);
	}
	var masque = $('masque');
	if(masque) {
		masque.style.visibility = 'hidden';
	}
	return true;
}

/* POPUPS */
var multi;
var width = screen.width;
if(width < 1024) multi = 0.75;
else multi = 1;

function popup(url, nom, width, height) {
	var w = width * multi;
	var h = height * multi;
	var neo=window.open(url, nom, "left=0,top=0, width="+w+"px,height="+h+"px,resizable=no,toolbar=no,scrollbars=yes");
	if(neo.focus){neo.focus();}
}

var spacer = '<img src="imgs/spacer.gif" width=30px />';


////////////////////////////////////////////////////////////////////////////////
// objet XmlHttpRequest compatible avec tous les navigateurs.
////////////////////////////////////////////////////////////////////////////////
var xhr = null;
function getXhr(){
	if(window.XMLHttpRequest) { // Firefox et autres	    
	   xhr = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){ // Internet Explorer 
	   try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
	}
	else { // XMLHttpRequest non support? par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	   xhr = false; 
	} 
}

// divers utiles
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

String.prototype.trim = function()
{
    return this.replace(/(?:^\s+|\s+$)/g, "");
}

// Fonction de "http://www.pbdr.com/vbtips/asp/JavaNumberValid.htm"
function is_numeric( strString )
//  check for valid numeric strings	
{
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	
		//  test strString consists of valid characters listed above
		for (i = 0; i < strString.length && blnResult == true; i++)
		{
			strChar = strString.charAt(i);
			
			if (strValidChars.indexOf(strChar) == -1)
			{
				blnResult = false;
			}
		}

		return blnResult;
}

function DateInt()
{
	var now = new Date();

		var h = now.getHours();
	        var m = now.getMinutes();
       		var s = now.getSeconds();
				
	return ( h * 60 * 60 ) + ( m * 60 ) + s;
}

function sleep( intTime )
{
	if( !is_numeric( intTime ) )
	{
		return;  // Arret de l'éxecution
	}
		
	intTime = ( intTime / 1000 );
	var Exp = DateInt() + intTime; // TimeOut
		
		while( Exp > DateInt() )
		{
			;
		}
}

function removeEmptyChildren(node) {
	if(!node || !node.childNodes.length) return false;
	
	for(i = 0; i < node.childNodes.length; i++) {
		if(node.childNodes[i].nodeValue && node.childNodes[i].nodeValue.replace(/^\s+|\s+$/g, '').length == 0) {
			node.removeChild(node.childNodes[i]);
		}
	}
	return node;
}

function tinyDefaultClasses(element_id, html, body) {
	html = html.replace(/<p>/gi, '<p class="texteutil">');
	//html = html.replace(/<br \/>/gi, '</p><p></p><p>');
	return html;
}
