
// begin: cookie_handling.js
//
// this template adds javascript to reading/writing cookies
//
// example: set a 2 year cookie named 'user' with value 'me':
// var expire_date = new Date();
// fix_date(expire_date);
// expire_date.setTime( expire_date.getTime() + 2 * 365 * 24 * 60 * 60 * 1000 );
// set_cookie('user','me',expire_date);

var cookie_caution = false;

function set_cookie(name,value,expires,path,domain,secure)
{
	// name - of cookie
	// value - of cookie
	// [expires] - expiration date of cookie (defaults to end of current session)
	//             milliseconds since Jan 1, 1970
	// [path] - path for which cookie is valid (defaults to path of caller)
	// [domain] - domain for which cookie is valid (defaults to domain of caller)
	// [secure] - boolean indicating if the cookie requires secure transmission

	// toGMTString() converts milliseconds since epoch to
	// format:  DAY, DD-MMM-YYYY HH:MM:SS GMT
	// example: Fri, 01-Feb-2007 22:15:00 GMT

	var curr_cookie = name + '=' + escape(value) +
		( expires ? '; expires=' + expires.toGMTString() : '' ) +
		( path    ? '; path='    + path : '' ) +
		( domain  ? '; domain='  + domain : '' ) +
		( secure  ? '; secure' : '' );
	if( !cookie_caution || (name + '=' + escape(value)).length <= 4000 )
	{
		document.cookie = curr_cookie;
	}
	else {
		if ( confirm('Cookie exceeds 4kB and will be cut!') )
		{
			document.cookie = curr_cookie;
		}
	}
}
function get_cookie(name)
{
	// return string containing value of specified cookie (else null)

	var prefix = name + '=';
	var cookie_start_index = document.cookie.indexOf(prefix);
	if (cookie_start_index == -1) { return null }
	var cookie_end_index = document.cookie.indexOf(';', cookie_start_index + prefix.length);
	if (cookie_end_index == -1) { cookie_end_index = document.cookie.length }
	cookie_start_index = cookie_start_index + prefix.length;

	return unescape( document.cookie.substring(cookie_start_index,cookie_end_index) )
}
function delete_cookie(name,path,domain)
{
	if ( get_cookie(name) )
	{
		document.cookie = name + '=' +
			( path   ? '; path='   + path   : '' ) +
			( domain ? '; domain=' + domain : '' ) +
			'; expires=Thu, 01-Jan-1970 00:00:01 GMT';
	}
}
function fix_date(date)
{
	// date - any instance of the Date object
	// this function formats the date for Mac computers
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0) { date.setTime(date.getTime() - skew) }
}
function get_time()
{
	// initialize time-related variables with current time settings
	var now = new Date();
	var hour = now.getHours();
	var minute = now.getMinutes();
	now = null;
	var ampm = '';
	// validate hour values and set value of ampm
	if (hour >= 12)
	{
		hour -= 12;
		ampm = 'PM';
	}
	else {
		ampm = 'AM';
		hour = (hour == 0) ? 12 : hour;
		// add zero digit to a one-digit minute
		if (minute < 10)
		{
			minute = '0' + minute // do not parse this number
		}
		// return time string
		return hour + ':' + minute + ' ' + ampm;
	}
}

// end: cookie_handling.js