// -------------------------------------------------------------------------------------------------------
//
// Filename: Cookie.js
//
// Dependencies: utilities.js
//
// -------------------------------------------------------------------------------------------------------

// -------------------------------------------------------------------------------------------------------
// Cookie Format
// -------------------------------------------------------------------------------------------------------
// Name		|	Description
// -------------------------------------------------------------------------------------------------------
// lastvisit	|	Milliseconds to represent last visit date
// subscribed	|	Boolean to represent whether registered for mailer
// news##	|	Boolean to represent whether specified news story has been read
// -------------------------------------------------------------------------------------------------------

// -------------------------------------------------------------------------------------------------------
// displaycookiecontents
// -------------------------------------------------------------------------------------------------------
function displaycookiecontents()
{

  alert ( document.cookie ) ;

}

// -------------------------------------------------------------------------------------------------------
// displaynewtoyoumessage
// -------------------------------------------------------------------------------------------------------
function displaynewtoyoumessage ( newsstoryid )
{

  var viewedflag = false ;

  // Find out if the story has been read by this user
  viewedflag = getnewsstoryread ( newsstoryid ) ;

  if ( viewedflag == false )
  {

    document.write ( "<img src='../images/new.gif' width='28' height='11' alt='New To You'/>" ) ;

  }

} // End displaynewtoyoumessage

// -------------------------------------------------------------------------------------------------------
// checkmailersubscription
// -------------------------------------------------------------------------------------------------------
function checkmailersubscription ()
{

  var registeredFlag = false ;

  // Find out if the user has registered for the mailer
  registeredFlag = eval(cookieVal('subscribed' ) ) ;

  if ( registeredFlag == false )
  {

    opennewwindow ( "misc/subscribe.htm", "register", 670, 320, 'noscroll' ) ;

  }

} // End checkmailersubscription

// -------------------------------------------------------------------------------------------------------
// setlastvisitdate
// -------------------------------------------------------------------------------------------------------
function setlastvisitdate ()
{

  var todaysDate 	= new Date ;
  var expiryDate	= new Date ;
  var milliseconds 	= 0 ;

  // Get the number of milliseconds since 01\01\1970
  milliseconds = todaysDate.getTime() ;

  // Set the expiry date for 6 months
  expiryDate.setMonth ( expiryDate.getMonth() + 6 ) ;

  // Now place this information into the cookie
  document.cookie = "lastvisit=" + milliseconds +"; expires=" + expiryDate.toGMTString() ;

} // End setlastvisitdate

// -------------------------------------------------------------------------------------------------------
// logsubscriptiontomailer
// -------------------------------------------------------------------------------------------------------
function logsubscriptiontomailer ()
{

  var expiryDate	= new Date ;

  // Set the expiry date for 6 months
  expiryDate.setMonth ( expiryDate.getMonth() + 6 ) ;

  // Now place this information into the cookie
  document.cookie = "subscribed=" + true + "; expires=" + expiryDate.toGMTString() ;

} // End logsubscriptiontomailer

// -------------------------------------------------------------------------------------------------------
// setnewsstoryread
// -------------------------------------------------------------------------------------------------------
function setnewsstoryread ( newsstoryid )
{

  var expiryDate	= new Date ;

  // Set the expiry date for 6 months
  expiryDate.setMonth ( expiryDate.getMonth() + 6 ) ;

  // Now place this information into the cookie
  document.cookie = newsstoryid + "=" + true +"; expires=" + expiryDate.toGMTString() ;

} // End setnewsstoryread

// -------------------------------------------------------------------------------------------------------
// getnewsstoryread
// -------------------------------------------------------------------------------------------------------
function getnewsstoryread ( newsstoryid )
{

  var viewedFlag = false ;

  // Find out if the user has viewed this news story
  viewedFlag = eval(cookieVal( newsstoryid ) ) ;

  return viewedFlag ;

} // End getnewsstoryread

// -------------------------------------------------------------------------------------------------------
// cookieVal
// -------------------------------------------------------------------------------------------------------
function cookieVal ( cookieName )
{

  var thisCookie 	= document.cookie.split ("; ") ;
  var count 		= 0 ;

  for ( count = 0; count < thisCookie.length; count++ )
  {

    if ( cookieName == thisCookie[count].split("=")[0])
    {

      return thisCookie[count].split("=")[1] ;

    }

  }

  return 0 ;

} // End cookieVal
