// Load other scripts
document.write("<SCRIPT language='JavaScript' type='text/javascript' src='/client/news.js'></SCRIPT>");
document.write("<SCRIPT language='JavaScript' type='text/javascript' src='/client/kb.js'></SCRIPT>");
document.write("<SCRIPT language='JavaScript' type='text/javascript' src='/client/activity.js'></SCRIPT>");
document.write("<SCRIPT language='JavaScript' type='text/javascript' src='/client/newsletter.js'></SCRIPT>");

// 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" : "");
}
 
// NEWSLETTERS REGISTRATION
//-------------------------------------------------------------------
var aNewsletters	= new Array();
function Newsletter(dt,title,location)
{
	this.date		= new Date(dt);
	this.title		= title;
	this.location	= location;
}

function addNewsletter(dt,ti,lo)
{
	aNewsletters[aNewsletters.length] = new Newsletter(dt,ti,lo);
}

function sortNewsletters(o1,o2)
{
	if(o1.date < o2.date)
		return 1;
	if(o1.date > o2.date)
		return -1;
	if(o1.title < o2.title)
		return -1;
	if(o1.title > o2.title)
		return 1;
	return 0;
}

function renderNewsletters()
{
	var sCurr = "";
	var sDate = "";

	aNewsletters.sort(sortNewsletters);

	document.write("<TABLE border=0 cellSpacing=0 Width='100%'>");
	document.write("<tr><td><ul>");
	for(var i=0; i<aNewsletters.length; i++)
	{
		sDate = aMonths[aNewsletters[i].date.getMonth()] + " " + aNewsletters[i].date.getYear();
		if(sDate != sCurr)
		{
			sCurr = sDate;
			document.write('</ul></td></tr>');
			document.write('<tr><td>&nbsp;</td></tr>');
			document.write('<tr><TD class=news id=ne_' + i + ' onmouseover=\'hilite(this,true);\' onmouseout=\'hilite(this,false);\'>');
			document.write('<a href="/pages/nl/' + aNewsletters[i].location + '"><H1>' + sCurr + '</H1></a>');
			document.write('<ul>');
		}

		document.write('<li>');
		document.write(aNewsletters[i].title);
		document.write('</li>');
	}
	document.write('</ul></td></tr>');
	document.write("</TABLE>");
}


// KNOWLEDGE BASE REGISTRATION
//-------------------------------------------------------------------
var aArticles		= new Array();
function Article(dt,title,subject,description,file)
{
	this.date		= new Date(dt);
	this.title		= title;
	this.subject	= subject;
	this.description= description;
	this.file		= file;
}

function addArticle(dt,ti,su,de,fi)
{
	aArticles[aArticles.length] = new Article(dt,ti,su,de,fi);
}

function sortArticlesByDate(o1,o2)
{
	if(o1.date < o2.date)
		return 1;
	if(o1.date > o2.date)
		return -1;
	if(o1.title < o2.title)
		return -1;
	if(o1.title > o2.title)
		return 1;
	if(o1.subject < o2.subject)
		return -1;
	if(o1.subject > o2.subject)
		return 1;
	return 0;
}

function sortArticles(o1,o2)
{
	if(o1.title < o2.title)
		return -1;
	if(o1.title > o2.title)
		return 1;
	if(o1.date < o2.date)
		return -1;
	if(o1.date > o2.date)
		return 1;
	if(o1.subject < o2.subject)
		return -1;
	if(o1.subject > o2.subject)
		return 1;
	return 0;
}

// ACTIVITY REGISTRATION
//-------------------------------------------------------------------
var aActivities		= new Array();
function Activity(dt,category,location,description,url)
{
	this.date		= new Date(dt);
	this.category	= category;
	this.location	= location;
	this.description= description;
	this.url		= url;
}

function addActivity(dt,ca,lo,de,u)
{
	aActivities[aActivities.length] = new Activity(dt,ca,lo,de,u);
}

function sortActivitiesByCategory(o1,o2)
{
	if(o1.category < o2.category)
		return -1;
	if(o1.category > o2.category)
		return 1;
	if(o1.location < o2.location)
		return -1;
	if(o1.location > o2.location)
		return 1;
	if(o1.date < o2.date)
		return 1;
	if(o2.date < o1.date)
		return -1;
	return 0;
}

function sortActivitiesByLocation(o1,o2)
{
	if(o1.location < o2.location)
		return -1;
	if(o1.location > o2.location)
		return 1;
	if(o1.category < o2.category)
		return -1;
	if(o1.category > o2.category)
		return 1;
	if(o1.date < o2.date)
		return 1;
	if(o2.date < o1.date)
		return -1;
	return 0;
}

function sortActivitiesByDate(o1,o2)
{
	if(o1.date < o2.date)
		return 1;
	if(o1.date > o2.date)
		return -1;
	if(o1.category < o2.category)
		return -1;
	if(o1.category > o2.category)
		return 1;
	if(o1.location < o2.location)
		return -1;
	if(o1.location > o2.location)
		return 1;
	return 0;
}


function sortActivitiesBySubject(o1,o2)
{
	if(o1.subject < o2.subject)
		return -1;
	if(o1.subject > o2.subject)
		return 1;
	if(o1.location < o2.location)
		return -1;
	if(o1.location > o2.location)
		return 1;
	if(o1.date < o2.date)
		return 1;
	if(o1.date > o2.date)
		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.sn < o2.sn)
		return -1;
	if(o1.sn > o2.sn)
		return 1;
	return 0;
}

function sortNewsByDate(o1,o2)
{
	if(o1.date > o2.date)
		return -1;
	if(o1.date < o2.date)
		return 1;
	if(o1.sn < o2.sn)
		return -1;
	if(o1.sn > o2.sn)
		return 1;
	return 0;
}


// GLOBAL VARIABLES
//-------------------------------------------------------------------
var aNow  = new Date();
var aPrev = 0;
var sImage= "";
var sStyle= "";
var hiliteClass = "";

// NEWS PAGE RENDERING
//-------------------------------------------------------------------
function hilite(it,on)
{
	var aRows=document.getElementsByName(it.id);
	for(var i=0; i<aRows.length; i++)
		if(on)
		{
			hiliteClass = aRows[i].className;
			aRows[i].className  = 'hilited';
		} else
			aRows[i].className  = hiliteClass;
}

function renderPage()
{
	var nSeqNo = 0;
	aNews.sort(sortNewsByDate);

	document.write("<TABLE border=0 cellSpacing=0 Width='100%'>");
	for(var i=0; i<aNews.length; i++)
	{
		document.write('<TR>');
		nSeqNo = (nSeqNo > aNews[i].sequence ? nSeqNo : aNews[i].sequence);
		document.write('<TD class=news id=ne_' + i + ' onmouseover=\'hilite(this,true);\' onmouseout=\'hilite(this,false);\'>');
		document.write('<TABLE width="100%" cellSpacing=0><TR><TD class=newsHeader width="100%">' + aNews[i].title + "</TD><TD class=newsHeader nowrap align=right>" + aDays[aNews[i].date.getDay()] + ' ' + aMonths[aNews[i].date.getMonth()] + ' ' + aNews[i].date.getDate() + ', ' + aNews[i].date.getYear() + '</td></tr>');
		document.write('<tr><td colspan=2><blockquote>' + aNews[i].description + '</blockquote></td></tr></table>');
		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);
}

function renderArticles()
{
	aArticles.sort(sortArticles);

	var sTitle = "";
	document.write("<TABLE border=0 cellSpacing=0 Width='100%'>");
	for(var i=0; i<aArticles.length; i++)
	{
		if(aArticles[i].title != sTitle)
		{
			sTitle = aArticles[i].title;
			document.write('<TR><TD><H1>' + sTitle + '</H1></TD></TR>');
		}
		document.write('<TR>');
		document.write('<TD class=news id=ne_' + i + ' onmouseover=\'hilite(this,true);\' onmouseout=\'hilite(this,false);\'>');
		document.write('<table width="100%" cellspacing=0><tr><td class=newsHeader width="100%">' + aArticles[i].subject + "</td><td class=newsHeader nowrap>" + aDays[aArticles[i].date.getDay()] + ' ' + aMonths[aArticles[i].date.getMonth()] + ' ' + aArticles[i].date.getDate() + ', ' + aArticles[i].date.getYear() + '</td></tr>');
		document.write('<tr><td colspan=2><blockquote><p>' + aArticles[i].description + '</p>');
		document.write('<a target=_abstract href="' + aArticles[i].file + '">Click here to view abstracts of literature</a></blockquote></td></tr></table>');
		document.write('</TD>');
		document.write('</TR>');
		document.write('<TR><TD>&nbsp;</TD></TR>');
	}
	document.write("</TABLE>");
}

function activityLocation(ac)
{
	var sLocation = ac.location;
	if(ac.url.length > 0)
		sLocation+= "<BR>&nbsp;&nbsp;&nbsp;&nbsp;<font size=-4>Visit <A target=_partner href='" + ac.url + "'>this partner's web site</a></font>";

	return sLocation;
}

function renderActivities(criteria,tFrom,tTo)
{
	var sHTML		= "";
	var sCond1		= "";
	var sCond2		= "";
	var sVal1		= "";
	var sVal2		= "";
	var sVal3		= "";
	var sVal4		= "";
	var rowID		= 0;
	
	switch(criteria)
	{
	case "category":
		aActivities.sort(sortActivitiesByCategory);
		sCond1		= "aActivities[i].category";
		sCond2		= "activityLocation(aActivities[i])";
		sVal3		= "aDays[aActivities[i].date.getDay()] + ' ' + aMonths[aActivities[i].date.getMonth()] + ' ' + aActivities[i].date.getDate() + ', ' + aActivities[i].date.getYear()";
		sVal4		= "aActivities[i].description";
		break;
	case "location":
		aActivities.sort(sortActivitiesByLocation);
		sCond1		= "activityLocation(aActivities[i])";
		sCond2		= "aActivities[i].category";
		sVal3		= "aDays[aActivities[i].date.getDay()] + ' ' + aMonths[aActivities[i].date.getMonth()] + ' ' + aActivities[i].date.getDate() + ', ' + aActivities[i].date.getYear()";
		sVal4		= "aActivities[i].description";
		break;
	case "date":
		aActivities.sort(sortActivitiesByDate);
		sCond1		= "aDays[aActivities[i].date.getDay()] + ' ' + aMonths[aActivities[i].date.getMonth()] + ' ' + aActivities[i].date.getDate() + ', ' + aActivities[i].date.getYear()";
		sCond2		= "aActivities[i].category";
		sVal3		= "activityLocation(aActivities[i])";
		sVal4		= "aActivities[i].description";
		break;
	case "subject":
		aActivities.sort(sortActivitiesBySubject);
		sCond1		= "aActivities[i].description";
		sCond2		= "activityLocation(aActivities[i])";
		sVal3		= "aDays[aActivities[i].date.getDay()] + ' ' + aMonths[aActivities[i].date.getMonth()] + ' ' + aActivities[i].date.getDate() + ', ' + aActivities[i].date.getYear()";
		sVal4		= "aActivities[i].category";
		break;
	}

	sHTML += "<TABLE border=0 cellSpacing=0 Width='100%'>";
	
	for(var i=0; i<aActivities.length; i++)
	{
		if(tFrom)
			if(aActivities[i].date <= tFrom)
				continue;
			
		if(tTo)
			if(aActivities[i].date >= tTo)
				continue;
			
		if(eval(sCond1) != sVal1)
		{
			rowID = i;
			sVal1 = eval(sCond1);
			sVal2 = "";
			sHTML += "<TR valign=bottom><TD style='border-bottom: 1px solid LightSteelBlue;font-size: 16px;font-weight:bold;color: SteelBlue;'>" + sVal1 + "</TD><TD width=1 style='border-bottom:1px solid LightSteelBlue'><img onclick='toggleRow(this, \"_ac" + rowID + "\");' style='cursor:hand' src='images/scroll_up_active.gif'></TD></TR>";
		}
		if(eval(sCond2) != sVal2)
		{
			sVal2 = eval(sCond2);
			sHTML += "<TR id=_ac" + rowID + "><TD><H2>" + sVal2 + "</H2></TD></TR>";
		}
		sHTML += "<TR id=_ac" + rowID + ">";
		sHTML += "<TD class=news id=ac_" + i + " onmouseover='hilite(this,true);' onmouseout='hilite(this,false);'>";
		sHTML += "<UL><LI><b>" + eval(sVal3) + "</b><BR>" + eval(sVal4) + "</LI>";
		sHTML += "</TD></TR><TR id=_ac" + rowID + "><TD>&nbsp;</TD></TR>";
	}
	sHTML += "</TABLE>"
	
	return sHTML;
}

function toggleRow(img,id)
{
	aRows = document.getElementsByName(id);
	img.src = (aRows[0].style.display == "" ? "images/scroll_down_active.gif" : "images/scroll_up_active.gif");
	for(var i=0; i<aRows.length; i++)
		aRows[i].style.display = (aRows[i].style.display == "" ? "none" : "");
}


