//-- Obtenir les coordonnées de la souris
function GetMousePosition(e)
{
  var curX;
  var curY;
  //ie
	if(document.all){
		curX = event.clientX;
		curY = event.clientY;
	}
	//netscape 4
	if(document.layers){
		curX = e.pageX;
		curY = e.pageY;
	}
	//mozilla
	if(document.getElementById){
		curX = e.clientX;
		curY = e.clientY;
	}
	return [curX,curY];
}
// Rempalcer "a" par "b" dans "expr"
String.prototype['Remplacer'] = function(a,b){
    var v = this;
  	var i=0;
  	while (i!=-1) {
  		 i=v.indexOf(a,i);
  		 if (i>=0) {
  			 v=v.substring(0,i)+b+v.substring(i+a.length);
  			 i+=b.length;
  		 }
  	}
  	return v;
}
//-- Objet pour requetes ajax
//--- Methode = m, url = u, paramUrl = p
//--- Function CallBack = c
var xhr = function(m,u,p,c){
	    //--- Objet supporté par le navigateur
	    this.http = this.getHTTPObject();
	    //--- méthode d'envoi des données : get ou post
	    this.methode = m;
	    //--- url de la page serveur appelée
	    this.url = u;
	    //--- Paramètres à transmettre à la page serveur appelée => forme de passage de param classique
	    this.paramUrl = p;
	    //--- Fonction appelée pour traiter le résultat
	    this.callBack = c;
	    // Donnée passée à la fonction send de l'objet ajax
	    this.data = null;
	    //-- Envoi
	    this.Request();
};

xhr.prototype = {
//var xhr = {
	//--- Préparation des paramètres pour l'envoi
	setRequestInfo: function (){
	    if (this.methode == "POST"){
	    	  this.headers = ["Content-type", "application/x-www-form-urlencoded"];
	    	  this.data = this.paramUrl;
	    }
	    if (this.methode == "GET"){
	    	  this.data = null;
	    	  this.headers = null;
	    	  this.url = this.url + "?" + this.paramUrl;
	    }
	},
	//--- Obtenir l'objet xmlhttp du navigateur
	getHTTPObject: function (){
  	  var xmlhttp;
  	  /*@cc_on
  	  @if (@_jscript_version >= 5)
  	    try {
  	      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  	      } catch (e) {
  	      try {
  	        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  	        } catch (E) {
  	        xmlhttp = false;
  	        }
  	      }
  	  @else
  	  xmlhttp = false;
  	  @end @*/
  	  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  	    try {
  	      xmlhttp = new XMLHttpRequest();
  	    } catch (e) {
  	      xmlhttp = false;
  	    }
  	  }
  	  return xmlhttp;
	},
	//--- Envoyer la requete
	//--- Methode = m, url = u, paramUrl = p
	//--- Function CallBack = c
	Request: function (){ //m,u,p,c
      var obj = this;

  		//--- Définir les paramètres d'envoi
  		var http = this.getHTTPObject();
  		this.setRequestInfo();
  		//--- Definition de l'action au retour
  		http.onreadystatechange = function (){
  		      var result = null;
  	    		if (http.readyState == 4 && http.status == 200){
  	    		     if (obj.callBack == "") return false;
  	    		     //-- Quel type de réponse
                 //-- Default ou explicite => Texte
                 if (http.getResponseHeader("Content-Type") == "text/html" || http.getResponseHeader("Content-Type") == null){
                      result = http.responseText;
                 }//-- XML
                 else if (http.getResponseHeader("Content-Type") == "text/xml"){
                      result = http.responseXML;
                 }//-- JSON
                 else if (http.getResponseHeader("X-JSON") != null){
                      result = eval('('+http.getResponseHeader("X-JSON")+')');
                 }
                 //--------------------------
                 var cb = obj.getCallBack();
        			   if (cb){
         			   	    cb.apply(this,[result]);
         			   }else{
         			   	    alert("Erreur la fonction callback (obj.callBack) n'existe pas " + obj.callBack);
         			   }
  	    	  }
  		}
  		//--- Envoi
  		http.open(this.methode,this.url,true);
  		//--- Envoi des entetes
  		if (this.headers != null){
  			http.setRequestHeader(this.headers[0],this.headers[1]);
  		}
      http.send(this.data);
	},
	//-- Gestion des objets (2 niveaux) et/ou fonction single
	getCallBack: function(){
	
	    if (typeof this.callBack == 'function'){
          return this.callBack;
      }
	
	    var nb = this.getNumberOf(".",this.callBack);
      var tmp = "";
      switch (nb){
          case 0:
              tmp = "window[this.callBack]";
              break;
          case 1:
              var t = this.callBack.split(".");
              tmp = "window['"+t[0]+"']['"+t[1]+"']";
              break;
          case 2:
              var t = this.callBack.split(".");
              tmp = "window['"+t[0]+"']['"+t[1]+"']['"+t[2]+"']";
              break;
      }
      return eval(tmp);
  },
  //-- Occurences de s dans c
  getNumberOf: function(s,c){
      var n = 0;
      var len = c.length;
      var tmp = "";
      for (var i=1;i<=len;i++){
          tmp = c.charAt(i);
          if (tmp == s){
              n++;
          }      
      }      
      return n;
  }
	
}
