/*
--------------------
 Variables
--------------------
*/
projectPath = " path=/;";
pairSeparator = "|:|";
keyValueSeparator = ":-:";
isCGIProxy = top.location.href.search(/\.cgi\/[A-Z0-9]*\/https*\//) >= 0;



/*
---------------------
 Functions 
---------------------
*/

function getSIGALDomain()
{
	var result = "";
	result = (document.domain.split('.')[0] == 'www') ? document.domain.split('.').slice(1).join('.') : document.domain;
	if(parent.rootPath != null)
		result = parent.rootPath.split('/')[2];
	else if(top.rootPath != null)
		result = top.rootPath.split('/')[2];
	else if(top.opener != null && top.opener.parent.rootPath != null)
		result = top.opener.parent.rootPath.split('/')[2];
	return result;
}

/* getCookies()
   ======================================================
   Retrouve la structure des cookies avec leurs valeurs
   note: Les cookies sont en MINUSCULE
   @return : Aborescence des cookies (struct)
*/
function getCookies(str)
{	
	var ptrObj = new Array();
	var sep = ( str == null ) ? "=" : keyValueSeparator;
	var arr = ( str == null ) ? document.cookie.split(';') : str.split(pairSeparator);
	for( var i=0; i<arr.length; i++)
	{
		value = ((isCGIProxy)?unescape(arr[i]):arr[i]).split(sep);
		if( value.length > 1)
		{
			value[0] = ((isCGIProxy && value[0].indexOf(';') >= 0 )?value[0].split(';')[1]:unescape(value[0])).replace(/[^A-Za-z0-9_]/,'').toLowerCase();
			value[1] = unescape(value[1]);
			if( value[1].indexOf(keyValueSeparator) >= 0 )
			{
				ptrObj['_' + value[0]] = value[1];
				ptrObj[value[0]] = getCookies(value[1]);
			}
			else ptrObj[value[0]] = value[1];
		}
	}
	return ptrObj;
}


/* eraseAllCookies()
   ======================================================
   Efface tous les cookies en memoire.
*/
function eraseAllCookies()
{   
	var strDomaineName = getSIGALDomain();
	var boxNames = '[CFID][CFTOKEN][JSESSIONID][PERSISTENCE][TESTCOOKIE][PRINTONLY][SMSESSION][SMIDENTITY][ESSO_TRYNO]';
	var path = " path=/;";
	var now = new Date();
	var hier = new Date(now.getFullYear()-1,now.getMonth(),now.getDate(),0,0,0,0);
	var arrCook = document.cookie.split(';');
	var arrDom = new Array();
	var arrTmp = strDomaineName.split('.')

	arrDom[arrDom.length] = strDomaineName;
	if( arrTmp.length >= 3 )
		arrDom[arrDom.length] = arrTmp.slice(arrTmp.length-3).join('.');
	if( arrTmp.length >= 2 )
		arrDom[arrDom.length] = arrTmp.slice(arrTmp.length-2).join('.');

	for( var i=0; i<arrCook.length; i++)
	{
		var c_name = arrCook[i].split('=')[0].replace(/\s/g,'');
		var c_name_cpy = c_name;
		
		if( isCGIProxy && c_name.indexOf('COOKIE%3b') == 0 )
			c_name = c_name.split('%')[1].substring(2);
		
		if( c_name != '' && boxNames.indexOf('[' + c_name.toUpperCase() + ']') >= 0 )
		{
			if( c_name_cpy != c_name ) c_name = c_name_cpy;
			for( var j=0; j<arrDom.length; j++)
			{
				if( arrDom[j].split('.').length >= 2 )
					document.cookie = c_name + "=; domain=" + arrDom[j] + ';' + path  + " expires=" + hier.toGMTString();
				else
					document.cookie = c_name + "=;" + path  + " expires=" + hier.toGMTString();
			}
		}
	}
}

/* delCookie(_name)
   ======================================================
   Efface un cookie en memoire.
   @params: _name : nom du cookie (doit être en minuscule) (String)
*/
function delCookie(_name)
{   
	var strDomaineName = getSIGALDomain();
	var ici = new Date();
	var hier = new Date(ici.getFullYear()-1,ici.getMonth(),ici.getDate(),0,0,0,0);
	if( isCGIProxy ) _name = "COOKIE%3b" + _name.toUpperCase() + '%3b%2f%3b' + strDomaineName;
	document.cookie = _name + "=;" + projectPath  + " expires=" + hier.toGMTString();
}

/* setCookie(_name,_value[,_exp])
   ======================================================
   Créé un cookie en memoire.
   @param _name : nom du cookie (doit être en minuscule) (String)
   @param _value : valeur du cookie (String)
   @param _exp : date d'expiration (Date)
   note : si la valeur est vide, le cookie est effacee
          support les structures (ex: toto.a)
*/
function setCookie(_name,_value,_exp)
{
	var strDomaineName = getSIGALDomain();
	var arr = _name.split('.');
	
	// multidimentionnel
	if( arr.length > 1 )
	{
		cookies = getCookies();
		if( _value != '' ) _value = arr[1] + keyValueSeparator + _value;
		if( (ptr = cookies[arr[0]]) != null )
			for(i in ptr) if( i != arr[1] )
				_value += ((_value != '')?pairSeparator:'') + i + keyValueSeparator + ptr[i];
	}
	
	delCookie(arr[0]);
	if( _value != '' )
	{
		if( _exp != null && typeof _exp != 'object' && _exp == true )
		{
			ici = new Date();
			_exp = new Date(ici.getFullYear()+1,ici.getMonth(),ici.getDate(),0,0,0,0);
		}
		if( isCGIProxy ) arr[0] = "COOKIE%3b" + arr[0].toUpperCase() + '%3b%2f%3b' + strDomaineName;
	 	document.cookie = arr[0] + "=" + _value + ";" + projectPath + ((_exp != null ) ? " expires=" + _exp.toGMTString() : "");
	}
}

/* getUrlParams()
   ======================================================
   Retourne tous les parametres URL dans un object.
   @return : Structure des parametre urls
*/
function getUrlParams()
{
	params = new Array();
	if(location.search.indexOf('?') == 0)
	{
		arr = location.search.substring(1).split('&');
		for( i=0; i<arr.length; i++)
		{
			part = arr[i].split('=');
			if( part[0] != "" )
				params[part[0].toLowerCase()] = part.slice(1).join('=');
		}
	}
	return params;
}
