// Global Functions and Objects
var aMonths = new Array();
aMonths[0] = "January";
aMonths[1] = "February";
aMonths[2] = "March";
aMonths[3] = "April";
aMonths[4] = "May";
aMonths[5] = "June";
aMonths[6] = "July";
aMonths[7] = "August";
aMonths[8] = "September";
aMonths[9] = "October";
aMonths[10]= "November";
aMonths[11]= "December";

var aDays		= new Array("Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday");

// COOKIES
//-------------------------------------------------------------------
var cookieExpDays = 100;
var cookieExpTime = new Date(); 
cookieExpTime.setTime(cookieExpTime.getTime() + (cookieExpDays*24*60*60*1000));

function getCookieVal (offset)
{
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}


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)
{  
	var argv    = setCookie.arguments;  
	var argc    = setCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : cookieExpTime;
	var path    = (argc > 3) ? argv[3] : null;  
	var domain  = (argc > 4) ? argv[4] : null;  
	var secure  = (argc > 5) ? argv[5] : false;  

	document.cookie = name + "=" + escape (value) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
}
 
// DOWNLOADS REGISTRATION
//-------------------------------------------------------------------
var aDownloads		= new Array();
function Download(dt,category,title,location,description,icon)
{
	this.date		= new Date(dt);
	this.category	= category;
	this.title		= title;
	this.location	= location;
	this.description= description;
	this.icon		= icon;
}

function getIcon(l)
{
	var ext = l.substr(l.length-3);
	switch(ext.toLowerCase())
	{
	case "pdf":		return "images/pdf.gif";
	case "wmv":		return "images/movie.gif";
	}
	return "";
}

function addDownload(dt,ca,ti,lo,de)
{
	aDownloads[aDownloads.length] = new Download(dt,ca,ti,lo,de,getIcon(lo));
}

function sortDownloads(o1,o2)
{
	if(o1.category < o2.category)
		return -1;
	if(o2.category < o1.category)
		return 1;
	if(o1.date < o2.date)
		return -1;
	if(o2.date < o1.date)
		return 1;
	if(o1.title < o2.title)
		return -1;
	if(o2.title < o1.title)
		return 1;
	return 0;
}

function sortDownloadsByTime(o1,o2)
{
	if(o1.date < o2.date)
		return 1;
	if(o2.date < o1.date)
		return -1;
	if(o1.category < o2.category)
		return -1;
	if(o1.title < o2.title)
		return -1;
	if(o2.title < o1.title)
		return 1;
	return 0;
}

// EVENT REGISTRATION
//-------------------------------------------------------------------
var aEvents		= new Array();
function Event(dt,title,location,description,type)
{
	this.date		= new Date(dt);
	this.title		= title;
	this.location	= location;
	this.description= description;
	this.type		= type;
}

function addEvent(dt,ti,lo,de)
{
	aEvents[aEvents.length] = new Event(dt,ti,lo,de,'ev');
}

function addTournament(dt,ti,lo,de)
{
	aEvents[aEvents.length] = new Event(dt,ti,lo,de,'to');
}

function addReview(dt,ti,lo,de)
{
	aEvents[aEvents.length] = new Event(dt,ti,lo,de,'re');
}

function addClinic(dt,ti,lo,de)
{
	aEvents[aEvents.length] = new Event(dt,ti,lo,de,'cl');
}

function sortEvents(o1,o2)
{
	if(o1.date < o2.date)
		return -1;
	if(o2.date < o1.date)
		return 1;
	if(o1.title < o2.title)
		return -1;
	if(o2.title < o1.title)
		return 1;
	return 0;
}

function sortEventsDecreasing(o1,o2)
{
	if(o1.date < o2.date)
		return 1;
	if(o2.date < o1.date)
		return -1;
	if(o1.title < o2.title)
		return 1;
	if(o2.title < o1.title)
		return -1;
	return 0;
}

// NEWS REGISTRATION
//-------------------------------------------------------------------
var aNews	= new Array();
var ck 		= getCookie("lv");
var ckd		= (ck == null ? null : new Date(ck));
var sn 		= getCookie("seqno");
sn			= (sn == null ? 0 : sn);

function News(sn,dt,title,description,hilite)
{
	this.sequence	= sn;
	this.date		= new Date(dt);
	this.title		= title;
	this.description= description;
	this.hilite		= hilite;
}

function isNew(item)
{
	return item.sequence > sn;
}

function addNews(sn,dt,ti,de,hi)
{
	aNews[aNews.length] = new News(sn,dt,ti,de,(hi==1));
}

function sortNews(o1,o2)
{
	if(o1.date < o2.date)
		return 1;
	if(o2.date < o1.date)
		return -1;
	if(o1.title < o2.title)
		return 1;
	if(o2.title < o1.title)
		return -1;
	return 0;
}

// GLOBAL VARIABLES
//-------------------------------------------------------------------
var aNow  = new Date();
var aPrev = 0;
var sImage= "";
var sStyle= "";

// NEWS PAGE RENDERING
//-------------------------------------------------------------------
function hilite(it,on)
{
	var aRows=document.getElementsByName(it.id);
	for(var i=0; i<aRows.length; i++)
		aRows[i].style.backgroundColor = (on ? 'LemonChiffon' : '');
}

function renderPage()
{
	var nSeqNo = 0;
	aNews.sort(sortNews);

	document.write("<TABLE border=0 cellSpacing=0 Width='100%'>");
	for(var i=0; i<aNews.length; i++)
	{
		document.write('<TR id=ne_' + i + ' onmouseover=\'hilite(this,true);\' onmouseout=\'hilite(this,false);\'>');
		nSeqNo = (nSeqNo > aNews[i].sequence ? nSeqNo : aNews[i].sequence);
		document.write('<TD bgcolor="#8fbc8b" width="100%" valign="top" style="border-top: 1px solid silver">');
		document.write('<B>' + aNews[i].title + '</B></FONT>');
		document.write('</TD>');
		document.write('<TD bgcolor="#8fbc8b" nowrap align=right valign="top" style="border-top: 1px solid silver;">');
		document.write('<B>' + aDays[aNews[i].date.getDay()] + ' ' + aMonths[aNews[i].date.getMonth()] + ' ' + aNews[i].date.getDate() + ', ' + aNews[i].date.getYear() + '</B>');
		document.write('</TD>');
		document.write('</TR>');
		document.write('<TR id=ne_' + i + ' style="cursor:default;" onmouseover=\'hilite(this,true);\' onmouseout=\'hilite(this,false);\'>');
		document.write('<TD colSpan=2 style="border-left: 1px solid #8fbc8b; border-right: 1px solid #8fbc8b; border-bottom: 1px solid #8fbc8b" ' + (isNew(aNews[i]) ? 'bgColor="LemonChiffon"' : '') + '>');
		document.write(aNews[i].description);
		document.write('</TD>');
		document.write('</TR>');
		document.write('<TR><TD>&nbsp;</TD></TR>');
	}
	document.write("</TABLE>");
	document.write('You last visited this page ' + getCookie("lv"));
	setCookie("lv", aNow);
	setCookie("seqno", nSeqNo);
}

function renderNewsSummary()
{
	var nCount = 0;
	
	for(var i=0; i<aNews.length; i++)
		if(isNew(aNews[i]))
			nCount++;

	if(nCount > 0)
	{
		document.write("<TABLE border=0 Style='border-top: 2px solid darkred;border-left: 2px solid darkred;border-bottom: 2px solid darkred;border-right: 2px solid darkred' bgColor=lemonchiffon cellSpacing=0 Width='100%'>");
		document.write("<TR><TD align=center><A HREF='news.htm'><IMG Border=0 SRC='images/new.gif'></A></TD></TR>");
		document.write("<TR><TD align=center><A HREF='news.htm'>You haven't seen " + nCount + " of the " + aNews.length + " posted news</A></TD></TR>");
		if(ckd != null)
			document.write("<TR><TD align=center>You last visited this site on "  + aDays[ckd.getDay()] + ' ' + aMonths[ckd.getMonth()] + ' ' + ckd.getDate() + ', ' + ckd.getYear() + "</TD></TR>");
		document.write("</TABLE>");
	}
	setCookie("lv", aNow);
}
