// JavaScript Document
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
// insere du code html avant un element de la page..
function _InsertHtmlBefore( html, element ) {
    	if ( element.insertAdjacentHTML )	// IE
    	{
    		element.insertAdjacentHTML( 'beforeBegin', html ) ;
    	}
    	else								// Gecko
    	{
    		var oRange = document.createRange() ;
    		oRange.setStartBefore( element ) ;
    		var oFragment = oRange.createContextualFragment( html );
    		element.parentNode.insertBefore( oFragment, element ) ;
    	}
  }

// surcharge de la fonction getElementById pour simplifer le codage
function _Elem(id) {
  return document.getElementById(id);
}

// modifier simplement le style d'un id..
function _Style(id) {
  return _Elem(id).style ;
}

function activate() {
	for (var i = 0; i < arguments.length; ++i) {
		_Elem(arguments[i]).disabled = false;
	}
}

function desactivate() {
	for (var i = 0; i < arguments.length; ++i) {
		_Elem(arguments[i]).disabled = true;
	}
}

function Show() {
	for (var i = 0; i < arguments.length; ++i) {
		_Elem(arguments[i]).style.display = '';
		_Elem(arguments[i]).style.visibility = 'visible';
	}
}

function Hide() {
	for (var i = 0; i < arguments.length; ++i) {
		_Elem(arguments[i]).style.display = 'none';
		_Elem(arguments[i]).style.visibility = 'hidden';
	}
}

function showHide(id) {
	if (_Elem(id).style.display != 'none')
		Hide(id);
	else
		Show(id);
		
}

function bindDivs (id, idDst) {

		var sAgent = navigator.userAgent.toLowerCase() ;
       if (sAgent.indexOf("msie") == -1) {
          offsets = 1 ;
       } else {offsets = 0 }

       myTop = _Elem(idDst).offsetTop;
       myLeft = _Elem(idDst).offsetLeft;
       myHeight = Math.round(_Elem(idDst).clientHeight/10)*10;
       myWidth = Math.round(_Elem(idDst).clientWidth/10)*10;
       //showEditor(myTop, myLeft, myWidth, myHeight);
       _Elem(id).style.top = myTop+offsets ;
       _Elem(id).style.left = myLeft+offsets ;
       _Elem(id).style.width = myWidth ;
       _Elem(id).style.height = myHeight ;

       Show(id);
}

function ucFirst(str) {
   return str.substr(0,1).toUpperCase() + str.substr(1,str.length);
}

function swapWidth(dvObj) {
	newWidth = dvObj.style.width;
	if (newWidth == '90%') dvObj.style.width = "10%";
	else dvObj.style.width = "90%";
}

function swapIDContents(id1,id2) {
	tmpid = id2 ;
	tmpidc = _Elem(id2).innerHTML;

	// swap du contenu
	_Elem(id2).innerHTML = _Elem(id1).innerHTML;
	_Elem(id1).innerHTML = tmpidc ;

	// swap des id
	_Elem(id2).id = id1;
	_Elem(id1).id = id2;
}

function selectAddOption(sId, oText, oVal, where) {
	sTmp = _Elem(sId);
	nbOptions = sTmp.options.length;
	
	if (where == 'top') {
		for(i=nbOptions; i >= 1  ; i--) {
			sTmp.options[i] = new Option (sTmp.options[i-1].innerHTML, sTmp.options[i-1].value);
		}
		sTmp.options[0] = new Option(oText, oVal);
	} else {
		sTmp.options[nbOptions] = new Option(oText, oVal);
	}
}

function focusField(obj, def) {
	if (obj.value == def) obj.value = '';
	else if (obj.value == '') obj.value = def;
}

function fillSelect(destId, ary, val) {
 // vidage du select de destination
 _Elem(destId).length = 0;
 selectAddOption(destId, "Modèle.", 0, "top")
 for (i=0; i < ary[val].length; i++) {
   selectAddOption(destId,ary[val][i].lib, ary[val][i].id);
 }
}
