Object.prototype.extend = function(object) {
  for (var prop in object) {
    if (!(prop in this)) {
      this[prop] = object[prop];
    }
  }
}
// Ajouter au type Object une methode donnant l'etat de l'objet
Object.prototype.getState = function() {
  var s = "", value;
  for (var prop in this) {
    value = (typeof(this[prop]) == "function") ? "[function ...]" : this[prop];
    s += prop + " : " + value + "\n";
  }
  //s += "constructor=" + this.constructor;
  return s;
}

Log = {
  error: function(msg) {
    alert("Error : \n" + msg);
  }
}

/*-------------------------------------------------------*/
// Extension des objets DOMElement
// On stocke toutes les methodes dans un objet Element
if (!window.Element) {
  Element = new Object();
}

/** Renvoie le tableau des elements de type tagName enfants de element
 * Si tagName vaut *, renvoie tous les elements enfants */
Element.getChildElements = function(element, tagName) {
  var result = new Array();
  var name = tagName.toLowerCase();
  for (var i=0 ; i<element.childNodes.length ; i++) {
    var child = element.childNodes[i];
    if (child.nodeType == 1) { // C'est un element
      if (name == "*" || child.nodeName.toLowerCase() == name) {
        result.push(child);
      }
    }
  }
  return result;
}

/** Enleve les noeuds texte vides enfants de l'element */
Element.cleanWhiteSpace = function(element) {
  for (var i = 0; i < element.childNodes.length; i++) {
    var node = element.childNodes[i];
    if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) {
      element.removeChild(node);
    }
  }
}

/** Applique a element le style specifie.
 * style est un objet dont les proprietes sont des directives CSS
 * telles qu'utilisees en javascript (ex: fontStyle et non font-style)
 */
Element.setStyle = function(element, style) {
  for (directive in style) {
    element.style[directive] = style[directive];
  }
}

/** Deplace un noeud ou plusieurs d'un parent a un autre 
 * @param children noeud ou tableau de noeuds a deplacer
 * @newParent nouveau parent a donner au noeud
 */
Element.move = function(children, newParent) {
  if (!(children instanceof Array)) {
    children = [children];
  }
  for (var i=0 ; i<children.length ; i++) {
    child = children[i];
    newParent.appendChild(child.parentNode.removeChild(child));
  }
}

/** (inutilise)
 * Applique a l'element HTML element le style specifie
 * style est un objet dont les proprietes sont des directives CSS
 * telles qu'utilisees en javascript (ex: fontStyle et non font-style)
 * Si defaultStyle (optionnel) est present, applique ses directives
 * non presentes dans style.
 */
Element.setStyleOrDefaultStyle = function(element, style, defaultStyle) {
  // Appliquer le style
  for (directive in style) {
    element.style[directive] = style[directive];
  }
  if (defaultStyle) {
    // Appliquer les directives de defaultStyle absentes de style
    for (directive in defaultStyle) {
      element.style[directive] = element.style[directive] 
        || defaultStyle[directive];
    }
  }
}

/** Coordonnee left de l'element */
Element.getLeft = function(element) {
  var offsetLeft = 0;
  // On cumule les offset de tous les elements englobants
  while (element != null) {
    offsetLeft += element.offsetLeft;
    element = element.offsetParent;
  }
  return offsetLeft;
}

/** Coordonnee top de l'element */
Element.getTop = function(element) {
  var offsetTop = 0;
  // On cumule les offset de tous les elements englobants
  while (element != null) {
    offsetTop += element.offsetTop;
    element = element.offsetParent;
  }
  return offsetTop;
}


/*-------------------------------------------------------*/
/** Unicode */
Keys = {
  TAB:      9,
  ENTER:    13,
  ESCAPE:   27,
  PAGE_UP:   33,
  PAGE_DOWN:34,
  END:       35,
  HOME:      36,
  LEFT:     37,
  UP:       38,
  RIGHT:    39,
  DOWN:     40
};


/*-------------------------------------------------------*/
/** DOM event */
if (!window.Event) {
  Event = new Object();
}

Event.event = function(event) {
  // W3C ou alors IE
  return (event || window.event);
}

Event.target = function(event) {
  return (event) ? event.target : window.event.srcElement ;
}

Event.preventDefault = function(event) {
  var event = event || window.event;
  if (event.preventDefault) { // W3C
    event.preventDefault();
  }
  else { // IE
    event.returnValue = false;
  }
}

Event.stopPropagation = function(event) {
  var event = event || window.event;
  if (event.stopPropagation) {
    event.stopPropagation();
  }
  else {
    event.cancelBubble = true;
  }
}

//la fameuse fonction dollar
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}
function toggle(obj) {
	var el = $(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

/**
 * tous les éléments du DOM (ou de l'arbo de node) de class searchClass
 * @param {Object} searchClass
 * @param {Object} node
 * @param {Object} tag
 */
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;
}

Array.prototype.contains = function (element) 
  {
          for (var i = 0; i < this.length; i++) 
       {
              if (this[i] == element) 
          {
                      return true;
              }
          }
          return false;
  };
  
function FieldJumper(idFirstField,idSecondField, idThirdField)
{
	if (!$(idFirstField) || !$(idSecondField)) {
	 alert('one of the fields doesn\'t exist in this document');
	 return;
	}
	
	this.firstfield=$(idFirstField);
	this.secondfield=$(idSecondField);
	this.thirdfield=$(idThirdField);
	this.propagate=true;
	if (this.firstfield.getAttribute("maxlength")) {
		this.firstmaxlength=this.firstfield.getAttribute("maxlength");
		}
		else {
		alert('the first field has no maxlength');
		return;
		}
	if (this.secondfield.getAttribute("maxlength")) {
		this.secondmaxlength=this.secondfield.getAttribute("maxlength");
		}
		else {
		alert('the second field has no maxlength');
		return;
		}
	this.setBehaviour();

}




FieldJumper.prototype.constructor=FieldJumper;

FieldJumper.prototype.extend({
		setBehaviour:function() {
		var current=this;
		this.firstfield.onfocus = function(e) {
			current.firstfield.select();
		}
		this.firstfield.onkeyup = function(e) {
			if (!e) var e = window.event;
			if (current.firstfield.value.length>=current.firstmaxlength
			&& e.KeyCode != 8
			&& e.keyCode != 16
			&& e.keyCode != 9)
			{
			current.propagate=false;
			current.secondfield.focus();
			current.secondfield.select();
			
			}
				
		}
		this.secondfield.onkeyup = function(e) {
			if (!e) var e = window.event;
			if ((current.secondfield.value.length>=current.secondmaxlength)
			&& e.KeyCode != 8
			&& e.keyCode != 16
			&& e.keyCode != 9)
			{
			current.thirdfield.focus();
			current.thirdfield.select();
			}
				
		}
	}
});

/** 
 * Returns array with x,y page scroll values.
 * Core code from - quirksmode.org
 */
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}




/** Returns array with page width, height and window width, height
* Core code from - quirksmode.org
* Edit for Firefox by pHaez
*/
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

/**
 * masque la div d'attente
 * @author gusau
 * 
 */
function hideActivity() {
	
	if ($('activity')) {
		$('activity').style.display='none';
	}	
}

function showActivity() {
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	if ($('activity')) {
		$('activity').style.position = 'absolute';
		$('activity').style.zIndex = 1500;
		$('activity').style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 128) / 2) + 'px');
		$('activity').style.left = (((arrayPageSize[0] - 20 - 128) / 2) + 'px');
		$('activity').style.display='block';
		//alert(arrayPageSize[0] + '-' + $('activity').style.left + '-' + $('activity').style.top);
	}	
}
  
 