/*	wcb.js	*/

/*	All cookie functions assume that cookies are set using set_cookie, and that
	the setting code does not use '; ' inside a field	*/

var expiry_date = new Date;
expiry_date.setFullYear( expiry_date.getFullYear() + 5 );
var visits = get_number( "Stops" );
visits++;

function set_cookie( name, value, expireDate, path, domain, isSecure )
{
	document.cookie = name + "=" + escape( value ) +
		(( expireDate ) ? "; expires=" + expireDate : "" ) +
		(( path ) ? "; path=" + path : "" ) +
		(( domain ) ? "; domain=" + domain : "" ) +
		(( isSecure ) ? "; secure" : "");
}

function delete_cookie( name )	{
	var expireDate = new Date;
	expireDate.setDate( expireDate.getDate() - 1 );                           
	var cookies = document.cookie.split('; ');
	for ( var i = 0; i < cookies.length; i++ )	{
		var cookieName = cookies[ i ].split('=')[ 0 ];
		if ( cookieName == name )	{
			document.cookie = cookieName + "=; expires=" + expireDate.toGMTString();
		}
	}
}

// get_name called with a number, from 0 up
function get_name( num )	{
	if ( document.cookie == "" )	{
		return "";
	}
	var cookies = document.cookie.split('; ');
	if ( cookies.length >= num )	{
		return cookies[ num ].split('=')[ 0 ];
	}
	return "";
}

function get_value( name )
{
	var cookies = document.cookie.split('; ');
	for ( var i = 0; i < cookies.length; i++ )	{
		var cookieName = cookies[ i ].split('=')[ 0 ];
		if ( cookieName == name )	{
			return unescape( cookies[ i ].split('=')[ 1 ] );
		} 
	}
	return "";
}

function get_number( name )
{
	var c = parseInt( get_value( name ));
	if ( isNaN( c ))	{
		return 0;
	}
	return c;
}

function total_cookies()	{
	if ( document.cookie == "" )	{
		return 0;
	}
	var cookies = document.cookie.split('; ');
	return cookies.length;
}

function not_first()
{
	set_cookie( "Stops", visits, expiry_date.toUTCString(), "/" );
	if ( visits > 1 )
		return true;
	else
		return false;
}

// handy tools

function popup( contents )
{
	wcb_helper = window.open( "", "E-mail", "width='256', height='192', resizable" );
//	wcb_helper.document.open( "text/html" );
	wcb_helper.document.writeln( contents );
	wcb_helper.focus();
}

function scramble( p6, p7 )
{
	var p1,p2,p3,p4,p5;
	p1 = '<a href="mai';
	p2 = p7;
	p3 = '">';
	p1 += 'lto:';
	p2 += '@';
	p5 = '</a>';
	p2 += 'webcostablanca.com';
	p4 = p6;

	document.write( p1 + p2 + p3 + p4 + p5 );
}

function set_bgcolour( eid, colour )
{
	var elem = document.getElementById( eid );
	elem.style.backgroundColor = colour;
}

/*	This function returns the day, month and year that the file it appears in was
	last modified. 'document.lastModified' is itself a string, but includes seconds,
	and varies in form from browser to browser.	*/

function date_modified()	{
	if ( !document.lastModified )
		return "2008";	// will need yearly updating
	var month_name = new Array( "January", "February", "March", "April", "May",
		"June", "July", "August", "September", "October", "November", "December" );
	var fdate = new Date( document.lastModified );
	var fday = fdate.getDate();
	var suffix = "th ";
	switch ( fday )	{
		case 1:
		case 21:
		case 31: 
			suffix = "st ";
			break;
		case 2:
		case 22:
			suffix = "nd ";
			break;
		case 3:
		case 23:
			suffix = "rd ";
			break;
	}
	var fmonth = month_name[ fdate.getMonth() ];
	var fyear = fdate.getFullYear();
	return fday + suffix + fmonth + " " + fyear;
}

function write_time()
{
	var day_name = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
	var month_name = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

	var cdate = new Date();
	var fday = cdate.getDate();
	var suffix = "th ";
	switch ( fday )	{
		case 1:
		case 21:
		case 31: 
			suffix = "st ";
			break;
		case 2:
		case 22:
			suffix = "nd ";
			break;
		case 3:
		case 23:
			suffix = "rd ";
			break;
	}
	var hour = cdate.getHours();
	if ( hour > 11 )	{
		hour -= 12;
		ap = "PM";
	}
	else  {
	 	ap = "AM";
	}
	if ( hour == 0 )	{
	 	hour = 12;
	}
	var minute = cdate.getMinutes();
	if ( minute < 10 )	{
		minute = "0" + minute;
	}
	document.write( day_name[ cdate.getDay() ] + ", " + fday + suffix + " " + month_name[ cdate.getMonth() ] );
	document.write( ": " + hour + ":" + minute + " " + ap );
}
