// KGB outil de stat
var Kgbspy = function(sUrl){
	if(!sUrl){
		this.sUrl="kgb/kgbspy.php";
	}
	this.traceEvent=function(value){		
		this.sendAjax("POST",this.sUrl,"action=trace&value="+value);
	};	
	this.traceEventBd=function(value,param1,param2,param3,param4,param5){		
		this.sendAjax("POST",this.sUrl,"action=trace&value="+value+"&param1="+param1+"&param2="+param2+"&param3="+param3+"&param4="+param4+"&param5="+param5);
		
	};		
	this.countEvent=function(id,value){		
		this.sendAjax("POST",this.sUrl,"id="+id+"&action=count&value="+value);
	};	
	
	this.sendAjax= function(method, url, data){		
		var xmlhttp=this.getXMLHTTP();			
		if(method == "GET"){
			if(data == 'null'){
				xmlhttp.open("GET", url, true); 
			}else{
				xmlhttp.open("GET", url+"?"+data, true);
			}
			xmlhttp.send(null);
		} else if(method == "POST"){
			xmlhttp.open("POST", url, true); 
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xmlhttp.send(data);
		}
		return true;		
	};
	
	this.getXMLHTTP= function(){
   		var xhr = null;
	  	if(window.XMLHttpRequest) { 
        	xhr = new XMLHttpRequest();
   	 	} else if(window.ActiveXObject){ 
	 		try {
            	xhr = new ActiveXObject("Msxml2.XMLHTTP");
        	} catch(e) {
            	try {
                	xhr = new ActiveXObject("Microsoft.XMLHTTP");
            	}
            	catch(e1){
                	xhr = null;
				}
       	 	}
   		}
   		return xhr;
	};
	
	this.traceEventOnLoad= function(value){
		this.addLoadEvent(this.sendAjax("POST",	this.sUrl,"action=trace&value="+value));   	
	};
	
	this.traceEventOnLoadBd=function(value,param1,param2,param3,param4,param5){		
		this.addLoadEvent(this.sendAjax("POST",this.sUrl,"action=trace&value="+value+"&param1="+param1+"&param2="+param2+"&param3="+param3+"&param4="+param4+"&param5="+param5));
	};
	
	this.countEventOnLoad= function(id,value){
		this.addLoadEvent(this.sendAjax("POST",	this.sUrl,"id="+id+"&action=count&value="+value));   	
	};
	
	this.addLoadEvent= function(func){   
 		var oldonload = window.onload; 
    	if (typeof window.onload != 'function') { 
        	window.onload = func; 
   		} else { 
        	window.onload = function() 
    		{ 
            	oldonload(); 
            	func(); 	
        	} 
   		 } 
	}; 

	this.addunLoadEvent= function(func){   
 		var oldonunload = window.onunload; 
   		if (typeof window.onunload != 'function') { 
        	window.onunload = func; 
  	 	} else { 
        	window.onunload = function() 
    		{ 
           		oldonunload(); 
           		func(); 
	        } 
    	} 
	};
	
}