// GLOBAL VARIABLES

// STATUS MESSAGES
var kDEFAULT_STATUS		= "The Cult of Sean";

// browser check
var browser 		= navigator.appName;
var version 		= navigator.appVersion;
var validBrowser	= false;

if (browser == "Microsoft Internet Explorer")
{
    if (version.indexOf("MSIE 5") != -1) { browser = "ie"; version = "5"; validBrowser = true; }
    if (version.indexOf("MSIE 4") != -1) { browser = "ie"; version = "4"; validBrowser = true; }
    if (version.indexOf("MSIE 3") != -1) { browser = "ie"; version = "3"; }
    else { browser = "ie";  version = "0"; }
}
else if (browser == "Netscape")
{
    if (parseInt(version) == 4) { browser = "nn"; version = "4"; validBrowser = true; }
    if (parseInt(version) == 3) { browser = "nn"; version = "3"; validBrowser = true; }
    if (parseInt(version) == 2) { browser = "nn"; version = "2"; }
    else { browser = "nn"; version = "0"; }
}

function highlightLink (what)
{
    if (document.all)
    {
        what.style.textDecoration   = "underline";
        what.style.color            = "Blue";
    }
}

function unhighlightLink (what)
{
    if (document.all)
    {
        what.style.textDecoration   = "none";
        what.style.color            = "#A02929";
    }
}

function loadImage (file)
{
	img 		= new Image();
	img.src 	= "gif/" + file;
	return img;
}

// Switches the image with the corresponding NAME Off
function switchOn (name)
{
    if (document.images)
	{
		document[name].src = imgOn[name].src;
		setStatus ("The Cult of Sean: " + document[name].alt);
	}
	
	return true;
}

// Switches the image with the corresponding NAME Off
function switchOff (name)
{
    if (document.images) { document[name].src = imgOff[name].src; }
	setStatus (kDEFAULT_STATUS);
	return true;
}

function setStatus (text)
{
    self.status = text;
    return true;
}


// NEWS FUNCTIONS

// open dependant window with news items; screenX/Y for NN4, top/left for MSIE4
function openNews ()
{
	if (validBrowser)
	{
		newsWin		= window.open ("/news/floater.php3", "newsfloater", "scrollbars,dependent,width=300,height=380,screenX=10,screenY=10,top=10,left=10")
	}
}

// close news window -- needs to be a function for some browsers
function closeNews ()
{
	if (validBrowser)
	{
		if (!newsWin.closed)
		{
        	newsWin.close()
        }
	}
}


// COOKIE FUNCTIONS

// Heinle's function for retrieving a cookie.
function getCookie (name)
{
	var cname = name + "=";               
  	var dc = document.cookie;             
  	if (dc.length > 0)
	{              
    	begin = dc.indexOf(cname);       
    	if (begin != -1)
		{           
      		begin += cname.length;       
      		end = dc.indexOf(";", begin);
      		if (end == -1) end = dc.length;
        	return unescape(dc.substring(begin, end));
    	} 
  	}
  	return null;
}

// An adaptation of Dorcht's function for setting a cookie.
function setCookie (name, value, expires, path, domain, secure)
{
  	document.cookie = name + "=" + escape(value)
		+ ((expires == null) ? "" : "; expires=" + expires.toGMTString())
		+ ((path == null) ? "" : "; path=" + path)
		+ ((domain == null) ? "" : "; domain=" + domain)
		+ ((secure == null) ? "" : "; secure");
}

// An adaptation of Dorcht's function for deleting a cookie.
function delCookie (name,path,domain)
{
	if (getCookie(name))
	{
    	document.cookie = name + "=" 
			+ ((path == null) ? "" : "; path=" + path)
			+ ((domain == null) ? "" : "; domain=" + domain)
			+ "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function formatDate (todaysDate)
{
	monthNames	= new Array ();
	monthNames[1]	= "January";
	monthNames[2]	= "February";
	monthNames[3]	= "March";
	monthNames[4]	= "April";
	monthNames[5]	= "May";
	monthNames[6]	= "June";
	monthNames[7]	= "July";
	monthNames[8]	= "August";
	monthNames[9]	= "September";
	monthNames[10]	= "October";
	monthNames[11]	= "November";
	monthNames[12]	= "December";
	
	var month		= monthNames[todaysDate.getMonth () + 1];
	var year		= y2k (todaysDate.getYear ());
	
	return todaysDate.getDate () + " " + month + " " + year;
}

function y2k (number) { return (number < 1000) ? number + 1900 : number; }

// ERROR CHECKING

function isBlank( text )
{
	text = chopExtraSpaces (text);
	return !(text.length > 0);
}

function isValidEmail( text )
{
	return( text.search (/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1 );
}

function isZero( num )
{
	return (num == 0);
}

function isInteger( num )
{
	return (num == parseInt (num));
}

function isPositive (num)
{
	return (num > 0);
}

function isPositiveInteger (num)
{
	return (isInteger (num) && isPositive (num));
}

function isWhole (num)
{
	return (isInteger (num) && (isPositive (num) || isZero (num)));
}

function exceedsMaxLength( value, maxLength )
{
	return value.length > maxLength;
}

// SUPPORT FUNCTIONS
function chopLeadingSpaces (text)
{
	return text.replace (/^\s+/, "");
}

function chopTrailingSpaces (text)
{
	return text.replace (/\s+$/, "");
}

function chopExtraSpaces (text)
{
	return chopTrailingSpaces (chopLeadingSpaces (text));
}

function chopAllSpaces (text)
{
	while (text.search (/\s/) >= 0) {text = text.replace (/\s/g, "");}
	return chopTrailingSpaces (chopLeadingSpaces (text));
}

function chopExtraLineBreaks (text)
{
	return text.replace (/(\n\s*){2,}/g, "\n");
}
