var browserDate = new Date();
FixCookieDate (browserDate); 
var expDate = browserDate;
expDate.setTime (expDate.getTime() + (7 * 24 * 60 * 60 * 1000)); // 7 days from now 
var lastVisit = GetCookie("day");
var thisVisit = browserDate.getDay();
SetCookie ("hour",browserDate.getHours(),null,"/");
SetCookie ("day",thisVisit,expDate,"/");

//window.onerror = logError;

function logError(msg, url, lno)
{	if (GetCookie("error")==null)
	{	SetCookie("error",msg+" - " + url+" - "+lno,null,"/");
		win2=window.open('','window2','toolbar=0,location=0,directories=0,scrollbars=1,status=0,menubar=0,resizable=1,width=400,height=250');
		win2.document.writeln('<B>Javascript Error:</B>');
		win2.document.writeln('<P><B>Error in file:</B> ' + url + '<BR>');
		win2.document.writeln('<B>Line number:</B> ' + lno + '<BR>');
		win2.document.writeln('<B>Message:</B> ' + msg + '<BR>');
		win2.document.writeln('<B>Possible Cause:</B> Incompatible browser<BR><BR>');
		win2.document.writeln('<P align="center"><B>The use of JavaScript will be curtailed</B><BR>');
		win2.document.writeln('<B>for the rest of your visit</B><BR>');
		win2.document.writeln('<B>and notice of this error will be included</B><BR>');
		win2.document.writeln('<B>in the visitor log.</B>');
		win2.document.writeln('<P><img src="cgi-bin/counter.pl?error">');
		win2.document.close();
	}
	return true;
}

var ref = window.location.href.indexOf("!!");
if ( ref < 0 )
{	if (GetCookie("ref")==null)
	{	SetCookie ("ref",document.referrer,null,"/"); }
}
else
{	SetCookie ("ref",window.location.search,null,"/");
	newPage = window.location.pathname;
	window.location.href  = newPage;
}

function welcome()
{	var entry = GetCookie("entry");
	var code = 0;
	if (entry == "backdoor")
	{	code = 1; }
	else
	{	if (lastVisit == null)
		{	code = 2;	}
		else
		{	if (lastVisit != thisVisit)
			{	code = 3;	}
		}	// else don't bother the repeat visitor - code=0
	}
	SetCookie ("msg",code,null,"/");
	htmlStr = ""
	+ cbObjectS("welcome",10,180,280)
	+ setWelcome(code);
	+ cbObjEnd;
	document.write(htmlStr);
	if (code == 0)
	{	cbHide("welcome"); }
}

function setWelcome(code)
{	count = 0;
	if (code>9)
	{	count = 1;
		code = code-10;
	}
	msgHead = "";
	msgCount = "<br>";
	msgTail = "";
	if (code==3)
	{	msgHead = "Welcome back to the <strong>Chorus Niagara</strong> web site.<br>"; }
	if (code==2)
	{	msgHead = "Welcome to the new <strong>Chorus Niagara</strong> web site.<br>";
		msgTail = "<br>";
	}
	if (count==1)
	{	if (code==1)
		{	msgCount = "The web site visitor count is "
			+ GetCookie("visitor");
		}
		else
		{	msgCount = "You are visitor number "
			+ GetCookie("visitor");
			msgTail = "<br>(since April, 2001)";
		}
	}
	htmlStr = ""
	+ "<div CLASS='welcome'><p class='note'>"
	+ msgHead
	+ msgCount
	+ msgTail
	+ "</p></div>";
	return htmlStr;
}

function addCount()
{	var code = 1+GetCookie("msg");
	cbInnerHTML("welcome",setWelcome(code));
	if (code != 11)
	{	cbFade("welcome", "10000"); }

}

function preload()
{	if (GetCookie("preload")!="done")
	{	count=0;
		if (location.host == "localhost")
		{	baseURL = "http://localhost/chorusniagara/"; }
		else
		{	baseURL = "http://www.chorusniagara.ca/"; }
		sources = new Array (
			"images/03-04/passionate.gif",
			"images/03-04/magnificent.gif",
			"images/03-04/inspiring.gif",
			"images/03-04/uplifting.gif",
			"images/03-04/unique.gif",
			"images/03-04/unique1.gif",
			"images/03-04/01.jpg",
			"images/03-04/02.jpg",
			"images/03-04/03.jpg",
			"images/03-04/04.jpg",
			"images/03-04/seasonbot.jpg",
			"portraits/chorus.jpg",
			"portraits/chorus2.jpg",
			"portraits/BobCooper.gif",
			"portraits/soloists/NilsBrown.jpg"
		);	
		images = new Array ();	
		for (i=0; i<sources.length; i+=1)
		{	images[i] = new Image();
			images[i].onload = loadCheck;
			images[i].src = baseURL + sources[i];
		}
	}
}

function loadCheck()
{   count++;
    if(count == sources.length-1) 
    {	SetCookie ("preload","done",null,"/"); }
}

function backdoor()
{	SetCookie ("entry","backdoor",null,"/"); }

function showSoloist(name, path) 
{	eval("popup=window.open('" + path + "resources/soloists.htm#" + name + "', 'poppage', 'toolbar=0,location=0,directories=0,scrollbars=1,status=0,menubar=0,resizable=1,width=700,height=250');");
	popup.focus();
}

function spotlight(href) 
{	eval("popup=window.open('" + href + "', 'poppage', 'toolbar=0,location=0,directories=0,scrollbars=1,status=0,menubar=0,resizable=1,width=700,height=250');");
	popup.focus();
}

function redirect(url) 
{	parent.window.opener.location=url;
	parent.window.opener.focus();
}

function getCookieVal (offset)
{	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function FixCookieDate (date)
{	var base = new Date(0);
	var skew = base.getTime(); // dawn of (Unix) time - should be 0
	if (skew > 0)  // Except on the Mac - ahead of its time
	date.setTime (date.getTime() - skew);
}

function GetCookie (name)
{	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) 
	{	var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
     		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
  	}
  	return null;
}

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


function DeleteCookie (name,path,domain)
{	if (GetCookie(name))
	{	document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
  	}
}
