// Support routines for cookies, frames, hit counters
//

// Cookie info:  http://techpatterns.com/downloads/javascript_cookies.php

// Read and return cookie value
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

// write cookie with a timeout in days.  0 days implies a session cookie.
// example: putCookie('UURoanoke_CalendarSize','S',90,'/','uuroanoke.org','');
function putCookie(cookieName,value,expireDays,path,domain,secure)
{
  if(document.cookie != document.cookie)
    {index = document.cookie.indexOf(cookieName);}
  else
    { index = -1;}

  if (index == -1)
    {
    var expireDate=new Date();
    expireDate.setDate(expireDate.getDate()+expireDays);

    document.cookie=cookieName+"="+escape(value)+
    ( (expireDays) ? "; expires="+expireDate.toGMTString(): "" )+
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
    }
}

// write cookie with a timeout in minutes.  0 Minutes implies a session cookie.
// example: putCookieTo('UURoanoke_CalendarSize','S',10,'/','uuroanoke.org','');
function putCookieTo(cookieName,value,expireMinutes,path,domain,secure)
{
  if(document.cookie != document.cookie)
    {index = document.cookie.indexOf(cookieName);}
  else
    { index = -1;}

  if (index == -1)
    {
    var d=new Date();
    d.setTime(d.getTime()+(expireMinutes*60000));

    document.cookie=cookieName+"="+escape(value)+
    ( (expireMinutes) ? "; expires="+d.toGMTString(): "" )+
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
    }
}

// this deletes the cookie when called
// example: Delete_Cookie('UURoanoke_CalendarSize','/calendar/','');
function Delete_Cookie( name, path, domain ) {
 document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


// Display hit counter
function uucr_countr(pn, date)
{
  ValueEntered=Get_Cookie("UUCR_NoCount_cookie");
  document.write('<p> ');
  document.write('<a href="http://www.uuroanoke.org/counters.htm"><IMG SRC=');
  document.write('"http://www.uuroanoke.org/cgi-bin/');
  if (ValueEntered=="skip_me") {
    document.write('see_Count.pl?' + pn + '"');
  }
  else {
    document.write('UUCR_Count.pl?' + pn + '"');
  }
  document.write(' height=16 width=40></a>');
  document.write(' visitors since ' + date + '.');
  if (ValueEntered=="skip_me") {
    document.write('* ');
  }
}



