//Adresse: http://www.renederhy.com/jslib/derhylib.js
function update_field(str, uri, backDiv) {
	var ajax = new derhy.myAjax() ;
	ajax.update(str, uri, backDiv) ;
}
function post_update_field(data, uri, backDiv) {
	var ajax = new derhy.myAjax() ;
	ajax.updatePost(data, uri, backDiv) ;
}
/* creation d'une classe spécifique Ajax : */ 
var derhy = {} ; //creation d'un objet derhy 
derhy.myAjax = function(){
	var xmlHttp= new Object() ;
	var div;
	
this.getText= function (str, uri, fonction_retour ){
		xmlHttp=getXHR() ;	
		if (xmlHttp==null){
			alert ("Browser does not support HTTP Request") ;
			return ;
		} 
		uri=uri+"?"+str ;
		uri=uri+"&ajax="+ Math.random() ;
		
		xmlHttp.open("GET",uri,true) ;
		xmlHttp.onreadystatechange= function(){ reponses(fonction_retour);}
		xmlHttp.send(null);
}
/***************************************************************/
	reponses= function(f) {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			if(xmlHttp.status==200){
				f(xmlHttp.responseText) ;
			} 
		} 
		return null ;
	}
/************************************************************/		
	this.update = function(str, uri, backDiv){ 
		div = backDiv;
		if (str.length==0){ 
			document.getElementById(backDiv).innerHTML="" ;
			return ;
		}	
		xmlHttp=getXHR() ;	
		if (xmlHttp==null){
			alert ("Browser does not support HTTP Request") ;
			return ;
		} 
		if(str !="") uri=uri+"?"+str +"&" ;
		uri=uri+"ajax="+ Math.random() ;
//alert( uri+"\nbackDiv:"+backDiv) ; 
		xmlHttp.open("GET",uri,true) ;
		xmlHttp.onreadystatechange= stateChanged;
		xmlHttp.send(null);	
} 
/************************************************************/		
	this.updatePost = function(DATA, uri, backDiv){ 
		div = backDiv;
		if (DATA.length==0){ 
			document.getElementById(backDiv).innerHTML="" ;
			return ;
		}	
		xmlHttp=getXHR() ;
			
		if (xmlHttp==null){
			alert ("Browser does not support HTTP Request") ;
			return ;
		} 
		DATA =DATA+"ajax="+ Math.random() ;
	      
		xmlHttp.open("DATA",uri,true) ;
		xmlHttp.setRequestHeader( "Content-type" , "application/x-www-form-urlencoded" );
		xmlHttp.onreadystatechange= stateChanged;
		xmlHttp.send(DATA);	
} 

/*****************************/
	stateChanged = function() { 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			document.getElementById(div).innerHTML=xmlHttp.responseText;
		} 
	
	// prévoir les erreurs ici.
	} 
/*****************************/
	getXHR =function(){
		var http;
		_msxml_progid= ['MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP'] ;
		try{ http = new XMLHttpRequest();			 }
		catch(e){
			for(var i=0; i<this._msxml_progid.length; ++i){ 
				try{
					http = new ActiveXObject(this._msxml_progid[i]);
					break;
				}catch(e){}
			}
		}
		finally{return http;}
	}
}// fin de classe
/***************************************************************************************************/
 function getWindowWidth(){
	var width =
		document.documentElement && document.documentElement.clientWidth ||
		document.body && document.body.clientWidth ||
		document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
		0;
	return width;
}
function getWindowHeight(){
    var height =
		document.documentElement && document.documentElement.clientHeight ||
		document.body && document.body.clientHeight ||
  		document.body && document.body.parentNode && document.body.parentNode.clientHeight ||
  		0;
  	return height;
}
function getDimensions(){
	 return {width: getWindowWidth  , height: getWindowHeight }  ;
}
/************************************************************/		
function centerDiv(div){
	var left = window.XMLHttpRequest == null ? document.documentElement.scrollLeft : 0;
	var top = window.XMLHttpRequest == null ? document.documentElement.scrollTop : 0;
	left = Math.max((left + (getWindowWidth() - div.offsetWidth) / 2), 0);
	top =  Math.max((top + (getWindowHeight() - div.offsetHeight) / 2), 0);
	div.style.left = left+ 'px';
	div.style.top = top + 'px';
	return {'left': left,'top': top};  
}

/*** fonction caml **************************/
function map( a, fn ){
	 var result = new Array();
	 for (var i = 0; i < a.length; i++){  result[i] = fn(a[i],i); }
	 return result ;
}
function do_list(a,fn){for (var i=0; i<a.length;i++){fn(a[i]);}}
function list_it(a,fn,s ){for (var i = 0; i < a.length ; i++){    s = fn( s, a[i]);  }  return s;}
//exemple:
//function sum( a ){ return list_it ( function( s, x ){return s + x;}, a );}
 var isIn= function(a,val){ return list_it(a, function(s,x){ return (x==val) || s ;}, false);}

function isList(a) {
    return a && typeof a == 'object' && a.constructor == Array;
}
/**************************************************/
function setQueryString(){
	var qs=[];
	do_list( document.forms, function(df){
	do_list (df.elements, function(e){ 
		if((typeof e.name != "undefined") && e.type !='radio'|| e.checked ){
			qs.push(map( [e.name,e.value], encodeURIComponent).join('='))
		}
		});
	});
	return qs.join('&') ;
}

/**************prise sur prototype **********/
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 toggle(div){
	($(div).style.display == "none")? $(div).style.display="block" :$(div).style.display="none" ;  
}
String.prototype.trim = function(){
	var s= this ;
	while( s.charAt(0)== ' '           ) s= (s.substring(1,s.length-1) ) ;
	while( s.charAt(s.length-1 ) ==' ' ) s = s.substring(0,(s.length-2) ) ;
	return s  ;
}
//exemple : alert( ">"+"  trim  ".trim()+"<" ) ;
 var  callLater= function(fn, interval) {
        var fn_no_send = function() {
            fn();
        };
}

function addEvent(obj, evType, fn , capt){
	if(obj.addEventListener){
		obj.addEventListener(evType,fn,capt);
		return true ; 
	}//Moz
	else if(obj.attachEvent) obj.attachEvent("on"+evType,fn) // IE5+
	else return false ;
}

