var cookie_expire = 100 ; //100 jours
var nameOfCookieRss = 'ctf';	
var nameOfCookieTab = 'tab';	
var nameOfCookieVersion = 'ver';	
var nameOfCookieColor = 'col';	

	/*
	These cookie functions are downloaded from 
	http://www.mach5.com/support/analyzer/manual/html/General/CookiesJavaScript.htm
	*/	
	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))) {
		   printError( "Get_Cookie *** Pas de cookie - name : " + name ) ;
		   return null;
	   }
	   if (start == -1) {
		   //printError( "Get_Cookie *** Start a -1 - name : " + name ) ;
		   return null; 
	   }
	   var end = document.cookie.indexOf(";",len); 
	   if (end == -1) end = document.cookie.length; 
	   
	   var retour = unescape(document.cookie.substring(len,end)) ;
	   var retour2 = retour.replace( /\./g , " .") ;
	   //printTrace( "Get_Cookie *** " + document.cookie  ) ;
	   printTrace( "Get_Cookie *** name = " + name + " - retour : " + retour2 ) ;
	   return retour ; 
	}

	// This function has been slightly modified
	function Set_Cookie(  name, value )
	{ 
		if( value != "" ) {
			
			var today = new Date();
			var expires_date = new Date( today.getTime() + (cookie_expire * 1000*60*60*24) );
			
			var cookieString = name + "=" + escape(value) + ";expires=" + expires_date.toGMTString() ; 
			
			var theValue = name + "=" + value ;
			var retour2 = theValue.replace( /\./g , " .") ;
			printTrace( "Set_Cookie *** cookieString : " + retour2 ) ;
			
			if( cookieString.length > 4000 ) {
				printError( "Set_Cookie *** Trop Long : " + cookieString.length ) ;
			} else {
				document.cookie = cookieString ; 
			}
		}
	} 

	function saveCookies()
	{
		if( !useCookiesToRememberRSSSources) return ;

		for(var numCat=0;numCat<numberOfCats;numCat++)
		{
			saveCookieOfATab( numCat ) ;
		}
	}

	function saveCookieOfABox( numBox )
	{
		saveCookieOfATab( tabs[numBox] )
	}
	
	function saveCookieOfATab( numCat )
	{
		if( !useCookiesToRememberRSSSources) return ;

		if( numCat < cookieCounter ) {
			numCookie = numCat ;
		} else {
			numCookie = cookieCounter ;
			cookieCounter ++
		}

		var strCookie = "" ;
		for(var no=0;no<numberOfColumns;no++)
		{
			var parentObj = document.getElementById('rssBoxesColumn' + numCat + '' + no);
			
			var items = parentObj.getElementsByTagName('DIV');
			if(items.length==0) continue;
			
			var item = items[0];
			
			var tmpItemArray = new Array();
			while(item){
				var boxNum = item.id.replace(/[^0-9]/g,'');
				tmpItemArray[tmpItemArray.length] = boxNum;
				item = item.nextSibling;			
			}
			
			var columnIndex = no;
			var tabIndex = numCat;
			
			for(var no2=tmpItemArray.length-1;no2>=0;no2--){
				var boxNum = tmpItemArray[no2];
				var nbRssItems = rssBoxesArray[boxNum]['nbRssItems'] ;
				
				for (var numItem = 0; numItem<nbRssItems ; numItem++) {
					var uniqueIdentifier = rssBoxesArray[boxNum]['uniqueIdentifier'][numItem];
					var isRead = allFeeds[uniqueIdentifier]['isRead'];
					strCookie = strCookie + uniqueIdentifier + '-' + isRead + '.' ;
					
					if( typeof(cookieRSSSources[uniqueIdentifier]) == 'undefined' ) {
						cookieRSSSources[uniqueIdentifier] = new Array() ;
						cookieRSSSources[uniqueIdentifier]['counter'] = numCookie ;
					}
					cookieRSSSources[uniqueIdentifier]['isRead'] = isRead ;
				}
			}
		}
		
		Set_Cookie(nameOfCookieRss + cookieRSSSources[uniqueIdentifier]['counter'], strCookie);
	}

	function getCookies()
	{
		cookieCounter = 0 ;
		var tmpArray = new Array();
		var cookieValue = Get_Cookie(nameOfCookieRss + cookieCounter);
		while(cookieValue && cookieValue!=''){
			var feedsOfTab = cookieValue.split('.'); // Box1.Box2...
			for(var no=0;no<feedsOfTab.length-1;no++){	// Looping through RSS items
				var items = feedsOfTab[no].split('-'); // uniqueIdentifier-isRead
	
				if(items.length>0){
					cookieRSSSources[items[0]] = new Array() ;
					if (items[1] == "true" ) cookieRSSSources[items[0]]['isRead']=true;
					else cookieRSSSources[items[0]]['isRead']=false;
					cookieRSSSources[items[0]]['counter']=cookieCounter;
				}
			}
			cookieCounter++;
			cookieValue = Get_Cookie(nameOfCookieRss + cookieCounter);
		}
	}

	function testCookiesVersion()
	{
		if( ctfVersion != Get_Cookie(nameOfCookieVersion)) {
			deleteCookie(nameOfCookieTab)
			deleteCookie(nameOfCookieVersion)
			deleteCookie(nameOfCookieColor)

			cookieCounter = 0 ;
			var cookieValue = Get_Cookie(nameOfCookieRss + cookieCounter);
			while(cookieValue && cookieValue!=''){
				deleteCookie(nameOfCookieRss + cookieCounter)
				cookieCounter++;
				cookieValue = Get_Cookie(nameOfCookieRss + cookieCounter);
			}
			Set_Cookie(nameOfCookieVersion, ctfVersion);
		}
	}
	

	function deleteCookie(name,path,domain) {
	   if (Get_Cookie(name)) document.cookie = name + "=" +
		  ( (path) ? ";path=" + path : "") +
		  ( (domain) ? ";domain=" + domain : "") +
		  ";expires=Thu, 01-Jan-70 00:00:01 GMT";
	}

