/*
	Suivi des tags, version 1.1 (12/07/2007)
	Copyright: 2007, Direct-Assurance
*/

var xmlHttp; 
var requestURL = 'http://' + window.location.host + '/DA.WEB.IS.FluxstatAsynchrone.Web/Suivi.aspx?';  // adresse de la page qui tag

/* Varable de reconnaissance du navigateur */
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
//netscape, safari, mozilla behave the same??? 
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0; 

/*creation des parametres du flux*/
function initFlux(Flux)
{
	var strParamList= Flux.split('-');
	var strCookieList= LireCookie("AA");
	var str = "";
	var nbParams = strParamList.length;
	
	//si nbParams = 5, idSession et idVisiteur peuvent etre recuperes dans le cookie
	//si nbParams = 7, on transmet idSession et idVisiteur dans les parametres (le cookie peut etre detruit par le client)
	if (nbParams==5 || nbParams==7)
	{
		//le flux comment peut contenir plusieurs informations concatenees par '|'
		//on le remplace ce caractere par '-' pour avoir le bon format en base
		var fluxComment = strParamList[3].replace(/\|/g, "-");
		
		if (nbParams==7)
		{
			str = "Univers=" + strParamList[0] + "&Phase=" + strParamList[1] + "&flux=" + strParamList[2] + "&fluxComment="  + fluxComment + "&idActInterne=" + strParamList[4] + "&A2=" + strParamList[5] + "&A1=" + strParamList[6];
		}
		else
		{
			str = "" + strCookieList + "&Univers="  + strParamList[0] +  "&Phase="  + strParamList[1] +  "&flux="  + strParamList[2] +  "&fluxComment="  + fluxComment + "&idActInterne="  + strParamList[4];
		}
	}

	return str;
}

/*fonction a appeller pour tagguer un flux visiteur*/
function visiteur(Flux)
{
	var str="visite=false&" + initFlux(Flux);
	if (Flux.length > 0){
		envoie_flux(str);
	}
}
/*fonction a appeller pour tagguer un flux visite*/
function Visite(Flux)
{
	var str="visite=true&" + initFlux(Flux);
	if (Flux.length > 0){
		envoie_flux(str);
	}
}
/*permet de recupérer un coockie*/
function getCookieVal(offset)
{
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr)); 
}
/*permet de lire un coockie*/
function LireCookie(nom)
{
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen)
	{
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null; 
}
/*fonction d'initialisation et d'envoie de la requete Ajax*/
function envoie_flux(strParam){ 
	if (strParam.length > 0){ 
		//Append the name to search for to the requestURL 
		var url = requestURL + strParam; 
		//Create the xmlHttp object to use in the request 
		//stateChangeHandler will fire when the state has changed, i.e. data is received back 
		// This is non-blocking (asynchronous) 
		xmlHttp = GetXmlHttpObject(); 
		    
		//Send the xmlHttp get to the specified url 
		xmlHttp_Get(xmlHttp, url); 
	} 
} 

/*fonction d'envoie de requete Ajax en GET*/
function xmlHttp_Get(xmlhttp, url) { 
	xmlhttp.open('GET', url, true); 
	xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); 
	xmlHttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			// POUR DEBUG : alert(xmlhttp.responseText)
		}
	}
	xmlhttp.send(null); 
} 
/*initialisation de la l'objet d'envoie*/
function GetXmlHttpObject() { 
	var objXmlHttp = null;    //Holds the local xmlHTTP object instance 

	//Depending on the browser, try to create the xmlHttp object 
	if (is_ie){ 
		//The object to create depends on version of IE 
		//If it isn't ie5, then default to the Msxml2.XMLHTTP object 
		var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
		    
		//Attempt to create the object 
		try{ 
			objXmlHttp = new ActiveXObject(strObjName); 
			//objXmlHttp.onreadystatechange = handler; 
		} 
		catch(e){ 
		//Object creation errored 
			return; 
		} 
	} 
	else if (is_opera){ 
		//Opera has some issues with xmlHttp object functionality 
		//alert('Opera detected. The page may not behave as expected.'); 
		return; 
	} 
	else{ 
		// Mozilla | Netscape | Safari 
		objXmlHttp = new XMLHttpRequest(); 
	} 
		
	//Return the instantiated object 
	return objXmlHttp; 
} 		
