/*
  * Viralyser - Affiliation SEO
  * http://code-seo.mp et Sylvain Deauré
  * Conserve les cookies existants: le premier gagne
  */

// Par défaut, le 1er arrivé reste. On n'écrase pas un cookie existant.

// Ne pas toucher en dessous
var cookie_aff_anchor = 'V';
var cookie_aff_name = 'Vir_aff';
var cookie_affpp_anchor = 'E';
var cookie_affpp_name = 'Vir_affpp';
var cookie_defaut_value = '1';

// CREATION DU COOKIE
function SetCookie (name, value)
	{
		var argv=SetCookie.arguments;
		var argc=SetCookie.arguments.length;
		var expires=(argc > 2) ? argv[2] : null;
		//var path=(argc > 3) ? argv[3] : null;
		var path="/";
		var domain=(argc > 4) ? argv[4] : null;
		var secure=(argc > 5) ? argv[5] : false;
		document.cookie=name+"="+escape(value)+
			((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
			((path==null) ? "" : ("; path="+path))+
			((domain==null) ? "" : ("; domain="+domain))+
			((secure==true) ? "; secure" : "");
	}
// LECTURE DU COOKIE ET VERIFICATION DE PRESENCE
function GetCookie (name)
	{
		var arg = name + "=";
		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 true;
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
		}
		return null;
	}

function GetCookieV(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
	{
	c_start=c_start + c_name.length+1;
	c_end=document.cookie.indexOf(";",c_start);
	if (c_end==-1) c_end=document.cookie.length;
	return unescape(document.cookie.substring(c_start,c_end));
	}
  }
return "";
}

function vredir(url)
{
		var pathname=location.pathname;
		var myDomain=pathname.substring(0,pathname.lastIndexOf('/')) +'/';
		var date_exp = new Date();
		date_exp.setTime(date_exp.getTime()+(30*1000));
		SetCookie("vredir",url,date_exp,myDomain);
		return true;
}


var cookie_aff = GetCookie(cookie_aff_name); // on demande le cookie
var cookie_affv = GetCookieV(cookie_aff_name);
try{
	 document.getElementById('vaff').value=cookie_affv;
}catch(e){
}

var cookie_affpp = GetCookie(cookie_affpp_name); // on demande le cookie


if (mode_test != '1') {

if(cookie_aff == null)
	{ //le cookie n'existe pas on le créé donc

		var pathname=location.pathname;
		var myDomain=pathname.substring(0,pathname.lastIndexOf('/')) +'/';
		var date_exp = new Date();
		date_exp.setTime(date_exp.getTime()+(cookie_expire*24*3600*1000));
		var hashtag = (self.document.location.hash.substring(1)); // LECTURE DU HASHTAG SANS LE #
		var url = self.document.location.href.split("#");
		url=url[0];
		//alert(hashtag);
		//PARSING
		//on verifie que c'est bien un hashtag pour l'affi
		if (hashtag.search(cookie_aff_anchor + "_") == -1)
			{
				//alert("ce n'est pas un # d'affiliation");

				//SetCookie(cookie_aff_name,cookie_defaut_value,date_exp,myDomain);
				//alert(cookie_defaut_value);
				// pas de hash
				aff=cookie_admin;
				if(aff != '')
					{
						SetCookie(cookie_aff_name,aff,date_exp,myDomain);
						//alert ('ad '+aff);
						if (window.location.href != url) {
							window.location.href=url;
						}
					}

			}
			else
			{
				var aff = hashtag.split("_");
				aff = (aff[1]);
				//alert (aff);
				if (aff == '') {
					// si aucun affilié, cookie admin
					aff=cookie_admin;
					//alert ('ad '+aff);
				}
				if(aff != '')
					{
						SetCookie(cookie_aff_name,aff,date_exp,myDomain);
						window.location.href=url;
					}

			}


	}//fin if(cookie_aff == null)


if(cookie_affpp == null) 	{ //le cookie n'existe pas on le créé donc
		var pathname=location.pathname;
		var myDomain=pathname.substring(0,pathname.lastIndexOf('/')) +'/';
		var date_exp = new Date();
		date_exp.setTime(date_exp.getTime()+(cookie_expire*24*3600*1000));
		var hashtag = (self.document.location.hash.substring(1)); // LECTURE DU HASHTAG SANS LE #
		var url = self.document.location.href.split("#");
		url=url[0];
		//alert(hashtag);
		//PARSING
		//on verifie que c'est bien un hashtag pour l'affi
		if (hashtag.search(cookie_affpp_anchor + "_") == -1)	{
			//alert("ce n'est pas un # d'affiliation");
		}	else {
			var aff = hashtag.split("_");
			aff = (aff[1]);
			if(aff != '') {
				SetCookie(cookie_affpp_name,aff,date_exp,myDomain);
				//window.location.href=url;
				affppemail=aff;
				// pour affichage si besoin
			}
		}
	}//fin if(cookie_affpp == null)

}
