/******************************************************************************
* dhtmllib.js                                                                 *
*                                                                             *
* Copyright 1999 by Mike Hall.                                                *
* Web address: http://members.aol.com/MHall75819                              *
* Email: MHall75819@aol.com                                                   *
* Last update: HJ 07/02/08                                                    *
*                                                                             *
* Provides basic functions for DHTML positioned elements which will work on   *
* both Netscape Communicator and Internet Explorer browsers (version 4.0 and  *
* up).                                                                        *
******************************************************************************/

// Determine browser.

var isMinNS4 = (document.layers) ? true : false;
var isMinIE4 = (document.all)    ? true : false;
var NS6 = (document.getElementById && !document.all) ? true : false;

//-----------------------------------------------------------------------------
// Layer visibility.
//-----------------------------------------------------------------------------

function hideLayer(layer) { // layer = objet calque (pas le nom !)

  if (isMinNS4)
    layer.visibility = "hide";
	if (isMinIE4 || NS6) {
		layer.style.visibility = "hidden";
		if (MoveAnim) {
			//Effect.SlideUp(layer);
		}
	}
}

function showLayer(layer) {

	if (isMinNS4)
		layer.visibility = "show";
	if (isMinIE4 || NS6) { // HJ 080125 - Effet scriptaculous
		layer.style.visibility = "visible";
		if (MoveAnim) {
			//Effect.SlideDown(layer, {duration:0.5, scaleMode:'contents'});
			Effect.BlindDown(layer, {duration:0.2});
		}
	}
}

function isVisible(layer) {

  if (isMinNS4 && layer.visibility == "show")
    return(true);
  if (isMinIE4 && layer.style.visibility == "visible")
    return(true);

  return(false);
}

//-----------------------------------------------------------------------------
// Layer positioning.
//-----------------------------------------------------------------------------

function moveLayerTo(layer, x, y) {
  if (isMinNS4)
    layer.moveTo(x, y);
  if (isMinIE4 || NS6) {
    layer.style.left = x+"px"; // HJ 070509 - Ajout +"px" sinon pb cause DTD dans Doctype
    layer.style.top  = y+"px";
  }
}

function moveLayerBy(layer, dx, dy) {

  if (isMinNS4)
    layer.moveBy(dx, dy);
  if (isMinIE4 || NS6) {
    layer.style.pixelLeft += dx;
    layer.style.pixelTop  += dy;
  }
}

function getzIndex(layer) {

  if (isMinNS4)
    return(layer.zIndex);
  if (isMinIE4 || NS6)
    return(layer.style.zIndex);

  return(-1);
}

function setzIndex(layer, z) {

  if (isMinNS4)
    layer.zIndex = z;
  if (isMinIE4 || NS6)
    layer.style.zIndex = z;
}

//-----------------------------------------------------------------------------
// Layer clipping.
//-----------------------------------------------------------------------------

function clipLayer(layer, clipleft, cliptop, clipright, clipbottom) {

  if (isMinNS4) {
    layer.clip.left   = clipleft;
    layer.clip.top    = cliptop;
    layer.clip.right  = clipright;
    layer.clip.bottom = clipbottom;
  }
  if (isMinIE4 || NS6)
    layer.style.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
}

//-----------------------------------------------------------------------------
// Layer scrolling.
//-----------------------------------------------------------------------------

function scrollLayerTo(layer, x, y, bound) {

  var dx = getClipLeft(layer) - x;
  var dy = getClipTop(layer) - y;

  scrollLayerBy(layer, -dx, -dy, bound);
}

function scrollLayerBy(layer, dx, dy, bound) {

  var cl = getClipLeft(layer);
  var ct = getClipTop(layer);
  var cr = getClipRight(layer);
  var cb = getClipBottom(layer);

  if (bound) {
    if (cl + dx < 0)

      dx = -cl;

    else if (cr + dx > getWidth(layer))
      dx = getWidth(layer) - cr;
    if (ct + dy < 0)

      dy = -ct;

    else if (cb + dy > getHeight(layer))
      dy = getHeight(layer) - cb;
  }

  clipLayer(layer, cl + dx, ct + dy, cr + dx, cb + dy);
  moveLayerBy(layer, -dx, -dy);
}

//-----------------------------------------------------------------------------
// Layer background.
//-----------------------------------------------------------------------------

function setBgColor(layer, color) {

  if (isMinNS4)
    layer.bgColor = color;
  if (isMinIE4 || NS6)
    layer.style.backgroundColor = color;
}

function setBgImage(layer, src) {

  if (isMinNS4)
    layer.background.src = src;
  if (isMinIE4 || NS6)
    layer.style.backgroundImage = "url(" + src + ")";
}

// HJ 21/09/01
function writeText(layer, text, style) {
	if (document.getElementById) // IE5 et NS6
	{
		x = document.getElementById(layer);
		x.innerHTML = text;
	}
	else if (document.all) // IE 4
	{
		x = document.all[layer];
		x.innerHTML = text;
	}
	else if (document.layers) // NS4
	{
		x = document.layers[layer];
		text2 = '<P CLASS="' + style + '">' + text + '</P>'; // Mettre style perso ici
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}


//-----------------------------------------------------------------------------
// Layer properties.
//-----------------------------------------------------------------------------

function getLeft(layer) {

  if (isMinNS4)
    return(layer.left);
  if (isMinIE4 || NS6)
    return(layer.style.pixelLeft);
  return(-1);
}

function getTop(layer) {

  if (isMinNS4)
    return(layer.top);
  if (isMinIE4 || NS6)
    return(layer.style.pixelTop);
  return(-1);
}

function getRight(layer) {

  if (isMinNS4)
    return(layer.left + getWidth(layer));
  if (isMinIE4 || NS6)
    return(layer.style.pixelLeft + getWidth(layer));
  return(-1);
}

function getBottom(layer) {

  if (isMinNS4)
    return(layer.top + getHeight(layer));
  else if (isMinIE4 || NS6)
    return(layer.style.pixelTop + getHeight(layer));
  return(-1);
}

function getWidth(layer) {

  if (isMinNS4) {
    if (layer.document.width)
      return(layer.document.width);
    else
      return(layer.clip.right - layer.clip.left);
  }
  if (isMinIE4 || NS6) {
    if (layer.style.pixelWidth)
      return(layer.style.pixelWidth);
    else
      return(layer.clientWidth);
  }
  return(-1);
}

function getHeight(layer) {

  if (isMinNS4) {
    if (layer.document.height)
      return(layer.document.height);
    else
      return(layer.clip.bottom - layer.clip.top);
  }
  if (isMinIE4 || NS6) {
    if (false && layer.style.pixelHeight)
      return(layer.style.pixelHeight);
    else
      return(layer.clientHeight);
  }
  return(-1);
}

function getClipLeft(layer) {

  if (isMinNS4)
    return(layer.clip.left);
  if (isMinIE4 || NS6) {
    var str =  layer.style.clip;
    if (!str)
      return(0);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[3]);
  }
  return(-1);
}

function getClipTop(layer) {

  if (isMinNS4)
    return(layer.clip.top);
  if (isMinIE4 || NS6) {
    var str =  layer.style.clip;
    if (!str)
      return(0);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[0]);
  }
  return(-1);
}

function getClipRight(layer) {

  if (isMinNS4)
    return(layer.clip.right);
  if (isMinIE4 || NS6) {
    var str =  layer.style.clip;
    if (!str)
      return(layer.style.pixelWidth);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[1]);
  }
  return(-1);
}

function getClipBottom(layer) {

  if (isMinNS4)
    return(layer.clip.bottom);
  if (isMinIE4 || NS6) {
    var str =  layer.style.clip;
    if (!str)
      return(layer.style.pixelHeight);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[2]);
  }
  return(-1);
}

function getClipWidth(layer) {

  if (isMinNS4)
    return(layer.clip.width);
  if (isMinIE4 || NS6) {
    var str = layer.style.clip;
    if (!str)
      return(layer.style.pixelWidth);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[1] - clip[3]);
  }
  return(-1);
}

function getClipHeight(layer) {

  if (isMinNS4)
    return(layer.clip.height);
  if (isMinIE4 || NS6) {
    var str =  layer.style.clip;
    if (!str)
      return(layer.style.pixelHeight);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[2] - clip[0]);
  }
  return(-1);
}

function getIEClipValues(str) {

  var clip = new Array();
  var i;

  // Parse out the clipping values for IE layers.

  i = str.indexOf("(");
  clip[0] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[1] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[2] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[3] = parseInt(str.substring(i + 1, str.length), 10);
  return(clip);
}

//-----------------------------------------------------------------------------
// Layer utilities.
//-----------------------------------------------------------------------------

function getLayer(name) { // Renvoie objet calque (pas le nom!)

  if (NS6)
    return document.getElementById(name);
  if (isMinNS4)
    return findLayer(name, document);
  if (isMinIE4 || NS6)
    return eval('document.all.' + name);

  return null;
}

function findLayer(name, doc) {

  var i, layer;

  for (i = 0; i < doc.layers.length; i++) {
    layer = doc.layers[i];
    if (layer.name == name)
      return layer;
    if (layer.document.layers.length > 0) {
      layer = findLayer(name, layer.document);
      if (layer != null)
        return layer;
    }
  }

  return null;
}

//-----------------------------------------------------------------------------
// Window and page properties.
//-----------------------------------------------------------------------------

function getWindowWidth() {

  if (isMinNS4)
    return(window.innerWidth);
  if (isMinIE4 || NS6)
    return(document.body.offsetWidth);
  return(-1);
}

function getWindowHeight() {

  if (isMinNS4)
    return(window.innerHeight);
  if (isMinIE4 || NS6)
    return(document.body.offsetHeight);
  return(-1);
}

function getPageWidth() {

  if (isMinNS4)
    return(document.width);
  if (isMinIE4 || NS6)
    return(document.body.scrollWidth);
  return(-1);
}

function getPageHeight() {

  if (isMinNS4)
    return(document.height);
  if (isMinIE4 || NS6)
    return(document.body.scrollHeight);
  return(-1);
}

function getPageScrollX() {

  if (isMinNS4)
    return(window.pageXOffset);
  if (isMinIE4 || NS6)
    return(document.body.scrollLeft);
  return(-1);
}

function getPageScrollY() {

  if (isMinNS4)
    return(window.pageYOffset);
  if (isMinIE4 || NS6)
    return(document.body.scrollTop);
  return(-1);
}

// HJ 050407
function CreateLayer(id,style,content,w,c,app) {
// Create a layer
// A adapter, voir DHTML_lib.js (from 'cm_divCreate' dans 'coolmenus4.js')
	if (NS6||IE5) {
		var div=document.createElement("DIV");
		div.className=style;
		div.id=id; 
		if(content) div.innerHTML=content;
		if(app) {app.appendChild(div); return div}
		if(w) document.body.appendChild(div);
		return div
	} else {
		var dstr='<div id="'+id+'" class="'+style+'"' 
		dstr+=">"+content;
		if(c) dstr+='</div>';
		if(w) document.write(dstr); else return dstr
	}
  return ""
}

function DeleteLayer(parent, child) { // HJ 070424
	var obj = document.getElementById(parent);
	var old = document.getElementById(child);
	
	obj.removeChild(old);
}

function getElementHeight(Elem) {
	var elem;
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	if (OP5) {
		xPos = elem.style.pixelHeight;
	} else {
		xPos = elem.offsetHeight;
	}
	return xPos;
}

function getElementWidth(Elem) {
	var elem;
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	if (OP5) {
		xPos = elem.style.pixelWidth;
	} else {
		xPos = elem.offsetWidth;
	}
	return xPos;
}

function getElementLeft(Elem) {
	var elem;
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	xPos = elem.offsetLeft;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	return xPos;
}


function getElementTop(Elem) {
	if(document.getElementById) {	
		var elem = document.getElementById(Elem);
	} else if (document.all) {
		var elem = document.all[Elem];
	}
	yPos = elem.offsetTop;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return yPos;
}

function getDivsRegExp(theREstr) {
// HJ 061012
// Renvoie une collection des divs dont l'id correspond a l'expression reguliere passee en parametre
	var a = document.getElementsByTagName('div');
	var len = a.length;
	var out = new Array();
	theRE = new RegExp(theREstr);
	for (var i = 0; i < len; i++)
	{
		if (theRE.test(a[i].id))
			out.push(a[i]);
	}
	return out;
}

function getEltsRegExp(eltType,theREstr)
// HJ 061012
// Renvoie une collection des elements dont l'id correspond a l'expression reguliere passee en parametre
// [Ref] UMP (devis)
{
	var a = document.getElementsByTagName(eltType);
	var len = a.length;
	var out = new Array();
	theRE = new RegExp(theREstr);
	for (var i = 0; i < len; i++)
	{
		if (theRE.test(a[i].id))
			out.push(a[i]);
	}
	return out;
}

var G_oPopUpSelector; // Objet global
function clsPopUpSelector(parentid, title, prompt, tabchoices, buttons, onChosen) {
	this.id = "popMain"; // Un seul par page !
	this.parentid = parentid; // =div sous lequel positionner le popup
	this.title = title;
	this.prompt = prompt;
	this.hasCloseBox = true;
	this.optArray = tabchoices;
	this.onChosen = onChosen; // Fonction a lancer qd choix fait
	this.result = 0;
};
clsPopUpSelector.prototype.Draw = function() {
	var divB, divT, divC, divS;
	
	if (document.getElementById(this.id)) {
		var div=document.getElementById(this.id);
	} else {
		var div=document.createElement("div");
		//div.className="popMain";
		div.id=this.id;
		div.style.display="block";
		div.style.position="absolute";
		div.style.zIndex=99;
	
		// Barre de boutons :
		divB=document.createElement("div");
		//divB.className="popBox";
		divB.id="popBox";
		divB.innerHTML="<a href='javascript:G_oPopUpSelector.Hide();' class='popButton'>X</a>";
		div.appendChild(divB);
		// Barre de titre :
		divT=document.createElement("div");
		divT.id="popTitle";
		div.appendChild(divT);
		// Que si style indefini :
	// 	div.style.width="380px";
	// 	div.style.color="#CC0000";
	// 	div.style.background="#FFFFE6";
	// 	div.style.padding="2px";
	// 	div.style.border="1px solid black";
		//div.style.font="8pt 'MS Comic Sans','Arial',sans-serif";
	
		// Contenu :
		divC=document.createElement("div");
		divC.id="popContent";
		div.appendChild(divC);

		// Barre de status (optionelle) :
		divS=document.createElement("div");
		divS.id="popStatus";
		divS.innerHTML="";
		div.appendChild(divS);
		
		document.body.appendChild(div);
	}
	div.style.top=getElementTop(this.parentid)+getElementHeight(this.parentid);
	div.style.left=getElementLeft(this.parentid);
//alert(div.style.top);
	divT=document.getElementById("popTitle");
	divT.innerHTML=this.title;

	divC=document.getElementById("popContent");
	theCont="<ol>";
	for (var i=0;i<this.optArray.length;i++) {
		theCont+="<li><a href='javascript:G_oPopUpSelector.Validate("+this.optArray[i][0]+");'>"+this.optArray[i][1]+"</a></li>"
	}
	theCont+="</ol>"
	//theCont+="<a href='javascript:G_oPopUpSelector.Hide();' class='popupsel'>Annuler</a>";
	divC.innerHTML="<p>"+this.prompt+"</p>"+theCont;
	div.style.display="block";
}
clsPopUpSelector.prototype.Hide = function() {
	var div=document.getElementById(this.id);
	div.style.display="none";
}
clsPopUpSelector.prototype.Validate = function(id) {
	this.result = id;
	this.Hide();
	eval(this.onChosen);
}


