/****************************************************************/
/* PRIVATE FUNCTIONS											*/
/****************************************************************/


/*  Set a cookie. 												*/
/*  Name and value are mandatory and special characters 		*/
/*  require encoding.											*/
/*																*/
/*  If the path is not specified the path of the document		*/
/*  is supplied													*/
/*																*/
/*  The domain matches the end of the fully qualified 			*/
/*  domain name													*/
function Set_Cookie (name,value,expires,path,domain,secure) 
{
	var today 	= new Date();
	var expires = new Date();
	
	// one year from now
	expires.setTime(today.getTime() + 365*24*60*60*1000 );

	// set cookie properties
  	document.cookie = escape(name) + "=" + escape(value) +  					  					
	    ((expires) 	? "; expires=" + expires.toGMTString() : "") +
    	    ((path) 	? "; path=" + path : "") +
            ((domain) 	? "; domain=" + domain : "") +
            ((secure) 	? "; secure" : "");
            
} /* SetCookie */


/* Delete a cookie												*/
/* Just set to the past											*/
function Delete_Cookie (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";
  }
} /* DeleteCookie */


/* Read the cookie value given its name							*/
/*																*/
/* The document.cookie object is a list of values delimited by  */
/* cookiename_1=cookievalue_1;...;cookievalue_N=cookievalue_N   */
/*																*/
/* If the cookie is not found return null		      			*/
function Get_Cookie (name) 
{
  var arg = name + "=";
  var arglen = arg.length;
  var doclen = document.cookie.length;
  
  var i = 0;
  var j = 0;
  
  while (i < doclen) 
  {
  	
    j = i + arglen;
  
    // If the cookie named 'name' matches the cookie string	  
    if (document.cookie.substring(i, j) == arg)
    {    	  	
	return getCookieVal (j);
    }
    
	i = document.cookie.indexOf(" ", i) + 1;
	
    if (i == 0) 
		break; 
    
  }
  
  return null;
  
} /* GetCookie*/ 


/* Read the value of a cookie					*/
/* If it is the last cookie there is not a end delimiter	*/
/* otherwise the delimiter between cookies is ';'		*/
function getCookieVal (offset)
{
	
	var str;
	
	// Read the index of the cookie delimiter ';'
	str = document.cookie.indexOf(";", offset);
	
	// If search value is not found this is the last cookie
	if( str == -1 )
	{
		str = document.cookie.length;
	}
	
	// Read the cookie value
	return unescape(document.cookie.substring(offset, str));

} 

/* Returns the string after the "?" delimiter		*/
/* null if it does not match						*/
function extract_id( complete_string )
{
	
	var alen = complete_string.length;	
	var str = complete_string.indexOf("?f=", 0 );
			
	if( str == -1 )
	{
		return null;
	}
	else
	{
		return unescape(complete_string.substring( str + 3, alen ));
	}
		
} /* extract_id */

/* Return true if cookies are permitted, 			*/
/* false otherwise									*/
function Cookies_Enabled()
{
	var tmp;
	
	Set_Cookie("dummy", "dummy" );	
	tmp = Get_Cookie("dummy");	
	Delete_Cookie("dummy");	
	if ( tmp == null )
	{
		return false;
	}
	else
	{
		return true;
	}
}





/* Indicates if the user is identifiable			  			*/
var cookie_value = null;



/****************************************************************/
/* PUBLIC FUNCTIONS												*/
/****************************************************************/


/* If cookies are not enable check the id. 			  			*/
/* If id has been set return 'IDHAS_COOKIES_DISABLED" 			*/
/* otherwise return null.							  			*/
/* If cookies are enabled check the id.				  			*/
/* If id exists set the cookie,otherwise return null  			*/
/* First visit sets the cookie, then reset the time	  			*/
function Main_Cookie()
{
	var id_value;

	if ( Cookies_Enabled( ))
	{
		cookie_value = Get_Cookie("fid");
						
		if ( cookie_value == null )
		{											
			// First time: read the id
			id_value = extract_id( document.URL );
			
			// Process if the page has got an id 
			if ( id_value != null )
			{			
				Set_Cookie("fid", id_value );
				cookie_value = Get_Cookie("fid");						
			}
		}
		else
		{
			// Not first time: read value and set it again			
			cookie_value = Get_Cookie("fid");							
			Set_Cookie("fid", cookie_value );	
		}		
	}
	else
	{
		id_value = extract_id( document.URL );
		if ( id_value != null )
		{
			cookie_value = id_value + "HAS_COOKIES_DISABLED";
		}
				
	}	
}




/* Call the cgi script if the user is identifiable			*/
function Log_Cookie()
{
	var str;

	if ( cookie_value != null )
	{

	str = '<IMG SRC="http://www.telgas.net/cgi-bin/cookie_handler.cgi?COOKIE_VALUE=';
		str = str + cookie_value;
		str = str + '&URL=' + document.URL + '">';

		document.write(str); 		
	}

} /* Log_Cookie */





