﻿function Querystring(qs) { 
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') 

	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}
function TRgetE(id){
	if(document.getElementById){
			return document.getElementById(id);
		}else if(document.all){
			return document.all[id];
		}else{
			return false;
	}
}
function Delete_Cookie(name, path, domain) {
if (Get_Cookie(name)) document.cookie = name + '=' +
((path) ? ';path=' + path : '') +
((domain) ? ';domain=' + domain : '') +
';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
function Get_Cookie(name){
	
var start = document.cookie.indexOf(name + '=');
var len = start + name.length + 1;
if ((!start) &&
(name != document.cookie.substring(0, name.length)))
{
return null;
}
if (start == -1) return null;
var end = document.cookie.indexOf(';', len);
if (end == -1) end = document.cookie.length;
return unescape( document.cookie.substring(len, end));
}

function Set_Cookie(name, value, expires, path, domain, secure) 
{
var today = new Date();
today.setTime(today.getTime());

if (expires){
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + '=' +escape( value ) +
( ( expires ) ? ';expires=' + expires_date.toGMTString() : '' ) + 
( ( path ) ? ';path=' + path : '' ) + 
( ( domain ) ? ';domain=' + domain : '' ) +
( ( secure ) ? ';secure' : '' );
}

function genPost(formulaire){
	//Quant on post un formulaire il recharge la page, donc mon script n'a pas de temps
	//a stocker le post dans la base de donnée. Il dois d'abord créer la cookie post = 'oui'
	//puis en load de la page il verifie si c est oui, stock la cookie dans la DB et il met non pour le prochain on load
	Delete_Cookie('post', '/', '');
	Set_Cookie('post', 'oui', 1, '/', window.location.host, false);
	
	//Si le nom du formulaire est spécifié il stock son nom dans le cookie 'form'
	//Si non il stock inconnu
	if (!(formulaire=='')){
		Delete_Cookie('form', '/', '');
		Set_Cookie('form', formulaire, 1, '/', window.location.host, false);
	}else{
		Delete_Cookie('form', '/', '');
		Set_Cookie('form', 'inconnu', 1, '/', window.location.host, false);
	}
}
function genClick(){
	TRgetE('trackpro').src = 'http://tracking.trackpro.ch/trackproClick.asp'+window.location.search;
}
function genCookie(){
//Générateur de cookie
//********************
		var qs = new Querystring();
		//Creation des cookies de 1 jour	
		if (!(Get_Cookie('idm') == 'non')){
			Set_Cookie('idm', 'oui', 1, '/', window.location.host, false);
		}
		//Creation des cookies temporaires	
		if (Get_Cookie('idm') == 'oui'){
			var IDUnique = escape(qs.get('IDUnique'));
			Set_Cookie('idunique', IDUnique, 1, '/', window.location.host, false);
			Set_Cookie('qs', window.location.search, 1, '/', window.location.host, false);		
		}
	if (!(Get_Cookie('qs') == 'null')){
		Delete_Cookie('idm', '/', '');
		Set_Cookie('idm', 'non', 1, '/', window.location.host, false);
	}
}

function loadTracker(){
	if (!(Get_Cookie('idm') == 'oui')){
		genClick();	
	}
	genCookie();
	//Generer le post si post dans la cookie = oui
	if (Get_Cookie('post') == 'oui'){
		var QS = Get_Cookie('qs');
		TRgetE('trackpro').src = 'http://tracking.trackpro.ch/trackproPost.asp'+QS+'&form='+Get_Cookie('form');;
		Delete_Cookie('post', '/', '');
		Set_Cookie('post', 'non', 1, '/', window.location.host, false);
	}
	
	//Si IDUnique dans le QS change. Donc le meme client a clické sur une autre campagne
	var qs = new Querystring();
	var IDUnique = new String(qs.get('IDUnique'));
	if (IDUnique == 'null'){
		IDUnique = escape(qs.get('IDUnique'));
	}
	if ((!(IDUnique == Get_Cookie('idunique'))) && (IDUnique.length == 8)){
		Delete_Cookie('idunique', '/', '');
		Delete_Cookie('qs', '/', '');
		var IDUnique = escape(qs.get('IDUnique'));
		Set_Cookie('idunique', IDUnique, 1, '/', window.location.host, false);
		Set_Cookie('qs', window.location.search, 1, '/', window.location.host, false);
	}
}

function addLoadEvent(func) { 
  var oldonload = window.onload; 
  if (typeof window.onload != 'function') { 
	window.onload = func; 
  } else { 
	window.onload = function() { 
	  if (oldonload) { 
		oldonload(); 
	} 
	  func(); 
	} 
  } 
} 

addLoadEvent(loadTracker);