//-----------------------------------
// create a cookie
//-----------------------------------

function createCookie( name,            // id    of cookie
                       value,           // value of cookie
                       days  )          // expiry in days
{
  if ( days ) 
  {
    var date = new Date();

    date.setTime(date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );   // add ms 

    var expires = "; expires=" + date.toGMTString();
  }
  else 
  {
    var expires = "";
  }

  document.cookie = name + "=" + value + expires + "; path=/";
}


//-----------------------------------
// read a cookie
//-----------------------------------

function readCookie( name ) 
{
	var nameEQ  = name + "=";
	var ca      = document.cookie.split( ';' );

	for ( var i = 0; i < ca.length; i++ ) 
  {
		var c = ca [ i ];

		while ( c.charAt(0) == ' ' ) 
    {
      c = c.substring( 1, c.length );
    }

  	if ( c.indexOf( nameEQ ) == 0 ) 
    {  
      return c.substring( nameEQ.length, c.length );


    }
  }

	return null;
}


//-----------------------------------
// erase a cookie
//-----------------------------------

function eraseCookie( name ) 
{
  createCookie( name, "", -1 );
}


//-----------------------------------
// erase all cookies
//-----------------------------------

function eraseAllCookies() 
{
  createCookie( "visits",     "", -1 );
  createCookie( "last_visit", "", -1 );
}


//---------------------------------------------------------------------------------
// restore to factory settings: erase cookies and reset background color !!!OVEE check cmt
//---------------------------------------------------------------------------------

//function restoreFactoryBackground()
//{
//  // erase background color cookie
//  //------------------------------

//  eraseCookie ( "bkg_rgb" );          //!!!OVEE ??? nodig


//  // set background to factory default
//  //----------------------------------

//  document.CC3D.setNodeEventIn( 'SCR_INIT_BKG', 'sky_rgb', '0.2 0.3 0.4' );
//}


var bkg_rgb;

function readCookies()
{
  // read values from cookie
  //------------------------

  var visits  = readCookie( "visits"  );

//alert( 'visits: ' + visits );

  
  bkg_rgb = readCookie( "bkg_rgb" );


  if ( visits ) 
  {
    visits  = parseInt( visits ) + 1;
  } 
  else 
  {
    visits  = 1;
  }

  text = visits;

  document.getElementById( 'id_visits' ).innerHTML = text;

  createCookie( "visits", visits, 100 );    


  // date + time of last visit
  //--------------------------


  var date = new Date();

  date.getTime();
  s_date = date.toGMTString();              // get 'now' in GMT-string


  var s_last_visit = readCookie( "last_visit" );

  if ( s_last_visit )
  {
    text_last_visit = s_last_visit;               // last visit from cookie
  }
  else
  {
    text_last_visit = '--';
  }

  document.getElementById( 'id_last_visit' ).innerHTML = text_last_visit;

  createCookie( "last_visit", s_date, 100 );
}
