/*

	utiliy functions for the calendar.

*/
function abrowser() {
	var agent = navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion,10);
	this.minor = parseFloat(navigator.appVersion);
	this.ns  = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));
	this.ns2 = (this.ns && (this.major == 3));
	this.ns3 = (this.ns && (this.major == 3));
	this.ns4 = (this.ns && (this.major >= 4));
	this.ie   = (agent.indexOf("msie") != -1);
	this.ie3  = (this.ie && (this.major == 2));
	this.ie4  = (this.ie && (this.major >= 4));
	this.op3 = (agent.indexOf("opera") != -1);
	this.win9x = (this.ie && (agent.indexOf("windows 9") >= 0 || agent.indexOf("win 9") >= 0))	
	this.isSupportedbyWTReports = ((this.ns || this.ie) && this.major >= 4)
}

var theirbrowser = new abrowser()

// derive the word ax page from the toc
function ViewAsWord()
{
	var headExt, headDirExt
	var reportNameRoot,reportPathRoot
	var reportExt, reportname, wordPath

	reportname		= top.frames.toc.location.href
	headExt			= reportname.lastIndexOf(".")
	headDirExt		= reportname.lastIndexOf("/")
	headTypeExt		= reportname.lastIndexOf("_")
	reportNameRoot	= reportname.substring(headDirExt+1,headTypeExt)
	reportPathRoot	= reportname.substring(0,headDirExt+1)
	reportExt		= reportname.substring(headExt,reportname.length)

	wordPath = reportPathRoot + "ax" + reportNameRoot +  reportExt
	if (reportNameRoot.indexOf('i') != 0 )
	{
		top.frames.hiddenax.location.href = wordPath
	}
}
// derive the word ax page from the toc
function ViewAsExcel()
{
	var headExt, headDirExt
	var reportNameRoot,reportPathRoot
	var reportExt, reportname, wordPath

	reportname		= top.frames.toc.location.href
	headExt			= reportname.lastIndexOf(".")
	headDirExt		= reportname.lastIndexOf("/")
	headTypeExt		= reportname.lastIndexOf("_")
	reportNameRoot	= reportname.substring(headDirExt+1,headTypeExt)
	reportPathRoot	= reportname.substring(0,headDirExt+1)
	reportExt		= reportname.substring(headExt,reportname.length)

	wordPath = reportPathRoot + "axe" + reportNameRoot +  reportExt
	if (reportNameRoot.indexOf('i') != 0 )
	{
		top.frames.hiddenax.location.href = wordPath
	}
}

function getMonthAsString(month)
{
	var iMonth = parseInt(month,10);
	switch(iMonth)
	{
		case 0: return "00";
		case 1: return "01";
		case 2: return "02";
		case 3: return "03";
		case 4: return "04";
		case 5: return "05";
		case 6: return "06";
		case 7: return "07";
		case 8: return "08";
		case 9: return "09";
		case 10: return "10";	
		case 11: return "11";
	}
}

function LoadProfileStartPages()
{
	lastrep			= ""
	top.dem_chapter	= ""
	//show stats, but stay at old loc if it exists
	if (oldyear)
		BuildReportCalendar(oldyear, oldmonth, "", "", "" )
	else
		BuildReportCalendar(thelastyear, thelastmonth, "", "", "")		
	frames.toc.location.href  = tocPage
	frames.main.location.href = sumPage

}

function FindPrevYearWithReports(year){

	var pyi = Years.indexOf(year) - 4

	if (pyi >= 0){
		prevyear = Years.substring(pyi,(pyi+4))
	}
	else{
		prevyear = "";
	}	
	return (prevyear)
}

function FindNextYearWithReports(year){
	
	var nyi = Years.indexOf(year) + 4
	if (nyi <= (Years.length - 1)){
		nextyear = Years.substring(nyi,(nyi+4))
	}
	else{
		nextyear = ""
	}	
	return (nextyear)
}

function FindPrevMonthWithReports(year,month){

	var hitmonth = getMonthAsString(month)
	var prevmonth
	var pmi 
	var i = 0
	var l

	while( i <= (YearMonth[year].length -1) ) {

		if (YearMonth[year].substring(i,(i+2)) == hitmonth ){
			pmi = i
			i = YearMonth[year].length
		}
		else
			i += 2
	}
			
	pmi -= 2;

	if (pmi >= 0 ){
		
		prevmonth = YearMonth[year].substring(pmi,(pmi+2))
	}
	else{
		year = FindPrevYearWithReports(year)
		if (year != ""){
			l = YearMonth[year].length - 1
			prevmonth = YearMonth[year].substring(l-1,l+1)
		}
		else
		   	prevmonth = ""
	}
		
	PrevYear = year
	PrevMonth = prevmonth
}

function FindNextMonthWithReports(year,month){

	var hitmonth = getMonthAsString(month)
	var nmi 
	var nextmonth
	var i  = 0

	while( i <= (YearMonth[year].length -1) ) {
		if (YearMonth[year].substring(i,(i+2)) == hitmonth ){
			nmi = i
			i = YearMonth[year].length
		}
		else
			i += 2
	}
	
	nmi += 2;
	if (nmi <= (YearMonth[year].length - 1)){
		nextmonth = YearMonth[year].substring(nmi,(nmi+2))
	}
	else{
		year = FindNextYearWithReports(year)
		if (year != "")
			nextmonth = YearMonth[year].substring(0,2)
		else
		   	nextmonth = ""
	}	
	NextYear = year
	NextMonth = nextmonth
}
function MonthOfTheYearByWeek(year,week){

	var month = JAN
	var theweek = WeekOfTheYear(year,month,1,0)
	
	while (theweek < week && month < DEC)
	{
		month++
		theweek = WeekOfTheYear(year,month,1,0)
		
	}
	
	if (theweek >= week)
		month = month -1
	return(month)
}
function MonthOfTheYearByQuarter(year,quarter){


	var amonth	 = JAN
	var squarter = -1

	switch( parseInt(quarter,10) )
	{
		case 1:	
			squarter = JAN
			break
		case 2:
			squarter = APR
			break
		case 3:
			squarter = JUL
			break
		case 4:
			squarter = OCT
			break

		default:

	}
	if ( squarter >= JAN )
	{
		// If the quarter has months already reported on we're done
		for (var amonth=squarter; amonth <= squarter + 2; amonth++)
		{
			if (YearMonth[year])
				if(ismonth(year,amonth))
					return
		}

		// since no dwm reports fell in this quarter add them all
		for (var amonth=squarter; amonth <= squarter + 2; amonth++)
		{
			incorporatemonth(year, getMonthAsString(amonth))
		}
	}
}


function WeekOfTheYear2(year,month,date,day){

	var monthDays = new montharr(31, 28, 31, 30, 31, 30, 31, 31, 30,31, 30, 31);
	var NewYearsDate = new Date
	var newYearsDay 
	var currentweeksunday = 0
	var nextweeksunday    = 0
	var nextmonth = 0
	var nDays
	var numsundays

	if (month == 0) 
		return (1)
	NewYearsDate.setDate(1)
	NewYearsDate.setMonth( nextmonth )
	if (year > 1999)
		NewYearsDate.setYear(year)
	else
		NewYearsDate.setYear(year - 1900)
	newYearsDay = NewYearsDate.getDay();
	nextweeksunday = (7 - newYearsDay) + 1
	numsundays = 2

	while( nextmonth < month  ){
		
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
			monthDays[1] = 29;
		nDays = monthDays[nextmonth];
		nextweeksunday += 7
		currentweeksunday = nextweeksunday
		nextweeksunday = (nextweeksunday % nDays)
		if (nextweeksunday == 0)
			nextweeksunday = nDays
		if (nextweeksunday < currentweeksunday){
			nextmonth++	
			if (nextmonth < month)
				numsundays++			
		}
		else
			numsundays++

	}

	return(numsundays)
}

function WeekOfTheYear(year,month,date,day){

	var monthDays = new montharr(31, 28, 31, 30, 31, 30, 31, 31, 30,31, 30, 31);
	var NewYearsDate = new Date
	var newYearsDay 
	var currentweekstart = 0
	var nextweekstart    = 0
	var nextmonth = 0
	var nDays
	var numstarts

	if (month == 0) 
		return (1)
	NewYearsDate.setDate(1)
	NewYearsDate.setMonth( nextmonth )
	if (year > 1999)
		NewYearsDate.setYear(year)
	else
		NewYearsDate.setYear(year - 1900)
	newYearsDay = NewYearsDate.getDay();
	var idays = weekstart - newYearsDay
	if (idays < 0)
	{
	 	nextweekstart  = 7 + idays + 1
	}
	else
	{
		nextweekstart = weekstart - newYearsDay + 1
	}
	numstarts = 2

	while( nextmonth < month  ){
		
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
			monthDays[1] = 29;
		nDays = monthDays[nextmonth];
		nextweekstart += 7
		currentweekstart = nextweekstart
		nextweekstart = (nextweekstart % nDays)
		if (nextweekstart == 0)
			nextweekstart = nDays
		if (nextweekstart < currentweekstart){
			nextmonth++	
			if (nextmonth < month)
				numstarts++			
		}
		else
			numstarts++

	}

	return(numstarts)
}
function ismonth(year,month){

	var smonth = getMonthAsString(month)
	if (YearMonth[year])
	{
		var i=0
		var mon = YearMonth[year].substring(i,(i+2))

		while( i <= (YearMonth[year].length -1) &&  parseInt(mon,10) <= parseInt(smonth,10)) 
		{
			if ( mon == smonth)
				return true
			i +=2
			mon = YearMonth[year].substring(i,(i+2))	
		}
	}
	return false
		
	
}

function incorporatemonth(year,month){

	var smonth = getMonthAsString(month)
	if (YearMonth[year])
	{
		var i=0
		var mon = YearMonth[year].substring(i,(i+2))

		while( i <= (YearMonth[year].length -1) &&  parseInt(mon,10) <= parseInt(smonth,10)) 
		{
			if ( mon == smonth)
				return
			i +=2
			mon = YearMonth[year].substring(i,(i+2))	
		}
		if (parseInt(mon,10) > parseInt(smonth,10))
			YearMonth[year] = YearMonth[year].substring(0,i) + smonth + YearMonth[year].substring(i,YearMonth[year].length)
		else
			YearMonth[year] += smonth
	}
	else
		YearMonth[year] = smonth
}


// Normalize the report types to months
// ------------------------------------


function ConvertWeeksToMonths(){

	var year, week, month, formatmonth = ""

	for (var pW in ReportWeek)
	{	
		year = pW.substring(0,4)
		week = pW.substring(6,pW.length)
		month = MonthOfTheYearByWeek(year,week)
		if (parseInt(month,10) < 10)
			formatmonth = "0" + month
		else
			formatmonth = month
		incorporatemonth(year,formatmonth )
	}

}
function ConvertMonthsToMonths(){

	var year, month, formatmonth = ""

	for (var pM in ReportMonth)
	{	
		year  = pM.substring(0,4)
		month = pM.substring(6,8)
		month = parseInt(month,10) - 1
		if (month < 10)
			formatmonth = "0" + month
		else
			formatmonth = month
		incorporatemonth(year,formatmonth )
	}

}
function ConvertQuartersToMonths(){

	var year, quarter, month, formatmonth = ""

	for (var pQ in ReportQuarter)
	{	
		year = pQ.substring(0,4)
		quarter = pQ.substring(6,pQ.length)
		MonthOfTheYearByQuarter(year,quarter)
	}

}
function ConvertYearsToMonths()
{
	var is = 0
	var ie = is + 4
	var syear

	if (Years)
	{
		while(ie <= Years.length)
		{
			syear = Years.substring(is,ie);
			// if no dwmq reports fall in this year add them all

			if (!YearMonth[syear])
				YearMonth[syear]= FULLYEAR
			is += 4
			ie = is + 4
		}
	}
	LoadMostRecentReport()


}
function montharr(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11){

	this[0] = m0
	this[1] = m1
	this[2] = m2
	this[3] = m3
	this[4] = m4
	this[5] = m5
	this[6] = m6
	this[7] = m7
	this[8] = m8
	this[9] = m9
	this[10] = m10
	this[11] = m11
}

function loadwtreport(reportname, reptype, value )
{
	top.currep	= "std"
	top.lastrep = ""
	var reportTOC
	var reportBody
	var headExt, headDirExt
	var reportNameRoot,reportPathRoot
	var reportExt

	headExt			= reportname.lastIndexOf(".")
	headDirExt		= reportname.lastIndexOf("/")
	reportNameRoot = reportname.substring(headDirExt+1,headExt)
	reportPathRoot = reportname.substring(0,headDirExt+1)
	reportExt      = reportname.substring(headExt,reportname.length)

	reportTOC  = reportPathRoot + reportNameRoot + "_t" +  reportExt
	reportBody = reportPathRoot + reportNameRoot + "_01_b" +  reportExt

	frames.toc.location.href  = reportTOC
	frames.main.location.href  = reportBody
	if (oldyear)
		BuildReportCalendar(oldyear, oldmonth, olddate, reptype, value )

}

//
// Based on sample code from "javascript.internet.com"
// original code generated on one static calendar with no weeks, months, 
// quarters, years or months
//
function BuildReportCalendar(theyear, themonth, thedate, ht, hd)
{

	var CC			= ""
	var CB			= ""
	var TOP_HTML	= ""
	var	BODY_HTML	= ""
	var PMCC		= ""
	var NMCC		= ""
	var MSCC		= ""
	var YSCC		= ""
	var MDESC		= ""
	var YDESC		= ""
	var bHomePage	= false
	var pMonth, nMonth, pyear, nyear, nyear, pDays, numweeksbefore, firstDay, startDay, testMe, nDays
	var pskipyear, nskipyear, formMonth, formQuarter
	var PostCalendarPadding, nextMonthday, prevMonthday

	if (thedate == "" && ht == "" && hd == "")
	{
		bHomePage = true
	}
	oldyear		= parseInt(theyear,10)
	oldmonth	= parseInt(themonth,10)
	hd			= parseInt(hd,10)
	if ( ! thedate )
	{
		thedate		= 0
	}
	olddate			= parseInt(thedate,10)
	var monthDays	= new montharr(31, 28, 31, 30, 31, 30, 31, 31, 30,31, 30, 31)
	var thisDay		= -1
	var numweeks	= 1
	var backyears	= 0
	var fordyears	= 0
	var today		= new Date()
	var year		= today.getYear()
	var	month		= today.getMonth()
	var	day			= today.getDate()
	
	// create integers
	theyear  = parseInt(theyear,10)
	themonth = parseInt(themonth,10)
	thedate  = parseInt(thedate,10)
		
	// find the next and previous reports
	FindPrevMonthWithReports(theyear,themonth)
	FindNextMonthWithReports(theyear,themonth)
	pyear	= PrevYear
	nyear	= NextYear
	pMonth	= PrevMonth
	nMonth	= NextMonth

	// used for calc of prevous/next "real" months
	pyearReal	= theyear
	nyearReal	= theyear
	pskipyear	= theyear - 1
	nskipyear	= theyear + 1
	pMonthReal	= themonth - 1
	nMonthReal	= themonth + 1
	if (pMonthReal == -1)
	{
		pMonthReal = 11
		pyearReal = theyear - 1
	}
	if (nMonthReal == 12)
	{
		nMonthReal = 0
		nyearReal = theyear + 1
	}
	// today's date:
	if (year <= 99)
	{
		 year  = year + 1900
	}
	month = today.getMonth()
	day	  = today.getDate()
	if (thedate)
	{
		today.setDate(thedate)
	}
	else
	{	
		today.setDate(1)
	}
	today.setMonth(themonth)
	if (theyear > 1999)
	{
		today.setYear(theyear)
	}
	else
	{
		today.setYear(theyear - 1900)
	}
	// set flag if viewable month contains today's date.
	if (theyear == year && themonth == month)
	{
		thisDay = day
	}
	// check for leap year (Feb days)
	if (((theyear % 4 == 0) && (theyear % 100 != 0)) || (theyear % 400 == 0))
	{
		monthDays[1] = 29
	}
	nDays		= monthDays[themonth]
	firstDay	= today
	firstDay.setDate(1);
	startDay	= firstDay.getDay()
	
	CC += "<center><table width='168'  border='0' cellpadding='1' cellspacing='0'>"
	CC += "<tr><td nowrap valign='center' align='left'>&nbsp;\n"

	MDESC =  monthNames.substring(themonth * 3,(themonth + 1) * 3) 
	YDESC =  theyear 

	// previous report link
	if (pMonth)
	{
		PMCC += "<a href=\"JavaScript:parent.BuildReportCalendar(" + pyear + "," + pMonth + "," + thedate + ")\" onMouseOver=\"status='" + PreviousMonth + "';return true;\" onMouseOut=\"status='';return true;\">"
			 + "<img hspace=0 vspace=0 valign=top border=0 src=\"images/prvrepnav.gif\">"
			 + "</a>"
	}
	// next report link
	if (nMonth)
	{
		NMCC += "<a href=\"JavaScript:parent.BuildReportCalendar(" + nyear + "," + nMonth + "," + thedate + ")\" onMouseOver=\"status='" + NextMonth + "';return true;\" onMouseOut=\"status='';return true;\">"
			 + "<img hspace=0 vspace=0 valign=top border=0 src=\"images/nxtrepnav.gif\" >"
			 + "</a>"
	}
	// fill combobox with month names for current year.
	MSCC	+= "<select width=4 name='selMonth' onChange=\"parent.BuildReportCalendar(X.selYear.options[X.selYear.selectedIndex].value,X.selMonth.options[X.selMonth.selectedIndex].value," + thedate + ")\" onMouseOver=\"status='" + selectMonthStatus + "';return true;\" onMouseOut=\"status='';return true;\">"
	intmonth = parseInt(themonth,10)

	for (i=0; i < YearMonth[theyear].length - 1; i+=2)
	{
		j = YearMonth[theyear].substring(i,i+2)
		j = parseInt(j,10)
		if ( j == intmonth )
		{
			MSCC += "<option selected value=" + j + ">" + monthNames.substring(j * 3,(j + 1) * 3) 
		}
		else
		{
			MSCC += "<option value=" + j + ">" + monthNames.substring(j * 3,(j + 1) * 3) 
		}
	}

	MSCC += "</select>"
	
	// fill combobox with years that contain reports
	YSCC += "<select width=4 name='selYear' onChange=\"parent.BuildReportCalendar(X.selYear.options[X.selYear.selectedIndex].value,parent.YearMonth[X.selYear.options[X.selYear.selectedIndex].value].substring(0,2)," + thedate + ")\" onMouseOver=\"status='" + selectYearStatus + "';return true;\" onMouseOut=\"status='';return true;\">"

	for (i=0; i < Years.length - 1; i+=4)
	{
		j = Years.substring(i,i+4)
		j = parseInt(j,10)
		if ( j == theyear)
		{
			YSCC += "<option selected value=" + j + ">" + j 
		}
		else
		{
			YSCC += "<option value=" +j + ">" + j 
		}
	}

	YSCC += "</select>"
	
	if (theirbrowser.win9x && theirbrowser.ie)
	{
		// no listboxes
		CC += PMCC + MDESC + "&nbsp;" + NMCC + YDESC + "&nbsp;&nbsp;"
	}
	else
	{
		CC += PMCC + MSCC + NMCC + YSCC
	}

	
	CC += "</td><td align='absmiddle' colspan='3' align='right'>"

	if (!bHomePage && bMsEnabled)
	{
		CC += "<a href=\"JavaScript:parent.LaunchOffice('word');\" onMouseOver=\"status='" + jsViewMSWordReport + "'; return true\" onMouseOut=\"status='';return true;\">"
		CC += "<img name='wordreport' alt=\"" + jsViewMSWordReport + "\" vspace='1' border=0 src=\"images/wordx.gif\">"
		CC += "</a>&nbsp;"
		CC += "<a href=\"JavaScript:parent.LaunchOffice('excel');\" onMouseOver=\"status='" + jsViewMSExcelReport + "'; return true\" onMouseOut=\"status='';return true;\">"
		CC += "<img name='excelreport' alt=\"" + jsViewMSExcelReport + "\" vspace='1' border=0 src=\"images/excelx.gif\">"
		CC += "</a>"
	}
	CC += "&nbsp;<a href='JavaScript:parent.LoadProfileStartPages();' onMouseOver=\"status='" + ProfileStats + "'; return true\" onMouseOut=\"status='';return true;\">"
	CC += "<img alt='" + ProfileStats + "' vspace='1' border=0 src='images/info.gif'>"
	CC += "</a>&nbsp;</td></tr>"
	CC += "<tr><td>&nbsp;</td><tr>"
	CC += "</table>"

	CB += "<center><table class='calendarbg' align='center' width='170' border='0' cellpadding='0' cellspacing='0'><tr><td width='100%'>"
	CB += "<table width='100%' border='0' cellpadding='1' cellspacing='1'>"
	CB += "<!-------------- weekday titles--------------->"
	CB += "<tr class='calendarheader'>"
	CB += "<td width='12%' align='center'>" + day0 + "</td>"
	CB += "<td width='12%' align='center'>" + day1 + "</td>"
	CB += "<td width='12%' align='center'>" + day2 + "</td>"
	CB += "<td width='12%' align='center'>" + day3 + "</td>"
	CB += "<td width='12%' align='center'>" + day4 + "</td>"
	CB += "<td width='12%' align='center'>" + day5 + "</td>"
	CB += "<td width='12%' align='center'>" + day6 + "</td>"
	CB += "<td width='*' align='center'>" + WeekColumnLabel + "</td>"
	CB += "</tr>"
	CB += "<!-- -----------------days of the month: (links if applicable)-------- -->\n"
	CB += "<tr class='calendardays'>"
	column		= 0
	numweeks	=1
	// check if the previous month was contained in a leap year.
	if (((pskipyear % 4 == 0) && (pskipyear % 100 != 0)) || (pskipyear % 400 == 0))
	{
		monthDays[1] = 29
	}
	pDays		= monthDays[pMonthReal]
	var idays	= (startDay-weekstart) 
	if (idays < 0)
	{
		idays = 7 + idays
	}

	for (i=0; i < idays; i++) 
	{
		if (i == 0 )
		{
			numweeks= numweeks - 1
		}
		prevMonthday = pDays - (idays - (i+1))
		CB += "<td class='calendarpadddays' width='12%' align='center'>" + prevMonthday + "</td>\n"
		column++
	}

	// fill the viewable month with current month dates/weeks and create links to reports
	numweeksbefore = WeekOfTheYear(theyear,themonth,i,0) 

	for (i=1; i<=nDays; i++) 
	{
		textonly = false
		if (ht == DAILY_REPORT && hd == i)
		{
			textonly = true
			CB += "<td bgcolor='#fdcb03' width='12%' align='center'>"
		}
		else
		{
			//thisDay == i ? color = "<b>" : color = ""
			CB += "<td width='12%' align='center'>"
		}
		CB += CheckForReport(DAILY_REPORT, theyear, 0,themonth, 0, i, textonly)
		CB += "</td>"
		column++
		if (column == 7) 
		{
			textonly = false
			if (ht == WEEKLY_REPORT && hd == (numweeks + numweeksbefore))
			{								
				textonly	= true
				CB			+= "<td bgcolor='#fdcb03' width='*' align='center'>"
			}
			else
			{
				CB			+= "<td class='calendarheader' width='*' align='center'>"
			}
			CB += CheckForReport(WEEKLY_REPORT, theyear, 0,themonth, (numweeks + numweeksbefore), 0, textonly)
			CB += "</td>"
			CB += "</tr>"
			CB += "<tr class='calendardays'>"
			numweeks += 1
			column = 0
		}
	}

	// fill the viewable month with over-lapping dates from next month add least week if required
	PostCalendarPadding = nDays % 7
	if (column != 0)
	{
		nextMonthday = 1

		while( column < 7  ) 
		{
			CB += "<td class='calendarpadddays' width='12%' align='center' >" + nextMonthday + "</td>"
			column +=1
			nextMonthday +=1
		}

		textonly = false
		if (ht == WEEKLY_REPORT && hd == (numweeks + numweeksbefore))
		{								
			textonly	= true
			CB			+= "<td bgcolor='#fdcb03'  width='*' align='CENTER'>"
		}
		else
		{
			CB			+= "<td class='calendarheader' width='*' align='CENTER'>"
		}
		CB +=CheckForReport(WEEKLY_REPORT, theyear, 0,themonth, (numweeks + numweeksbefore), 0, textonly)
		CB += "</td>"
	}

	CB += "</tr>\n"

	// create links for Y|Q|M reports

	CB += "<!-- -----------------year/quarterly/monthly (links if applicable)-------- -->"
	CB += "<tr class='calendarheader'>"
	textonly = false
	if (ht == YEARLY_REPORT && hd == theyear)
	{
		textonly	= true
		CB			+= "<td bgcolor='#fdcb03' colspan='3' ALIGN='CENTER'>"
	}
	else
	{
		CB		+= "<td colspan='3' align='CENTER'>"
	}
	
	CB += CheckForReport(YEARLY_REPORT, theyear, 0, 0, 0, 0, textonly)
	CB += "</td>"
	textonly = false
	if (ht == QUARTERLY_REPORT && hd == (thequarter + 1) )
	{
		textonly	= true
		CB			+= "<td bgcolor='#fdcb03' colspan='3' ALIGN='CENTER'>"
	}
	else
	{
		CB		+= "<td colspan='3' align='CENTER'>"
	}
	thequarter = Math.floor((themonth) / 3)
	
	CB += CheckForReport(QUARTERLY_REPORT, theyear, thequarter + 1, 0, 0, 0, textonly)
	CB += "</td>"
	textonly = false
	if (ht == MONTHLY_REPORT && hd == (themonth+1))
	{
		textonly	= true
		CB			+= "<td bgcolor='#fdcb03' colspan='2' align='CENTER'>"
	}
	else
	{
		CB += "<td colspan='2' align='CENTER'>"
	}
	CB += CheckForReport(MONTHLY_REPORT, theyear, 0, themonth, 0, 0, textonly)
	CB += "</td></tr>"
	CB += "</table>"
	CB += "</td><tr></table>"
	CC += "</center>"

	if (bfirst || theirbrowser.ns4)
	{
		if (theirbrowser.ie)
		{
			TOP_HTML	= START_CMD + CC + END_CMD
			BODY_HTML	= START_CALBODY + CB + END_CALBODY
		}
		else
		{
			TOP_HTML	= CC 
			BODY_HTML	= CB 
		}
		bfirst = false
		top.frames.month.document.open()
		top.frames.month.document.writeln(START_HTML + TOP_HTML + BODY_HTML + END_HTML)
		top.frames.month.document.close()
	}
	else
	{
		top.frames.month.document.all.calcommand.innerHTML	= CC
		top.frames.month.document.all.calbody.innerHTML		= CB
	}
}
function my_resize()
{
	if (theirbrowser.ns)
		top.location.reload(true)
}
function ShowLnk(lnk, color)
{
	if (theirbrowser.ns) return
	if(lnk)
	{
			lnk.style.color = color
			lnk.style.textDecoration = 'underline'
	}

}
function HideLnk(lnk, color)
{
	if (theirbrowser.ns) return
	if(lnk)
	{
			lnk.style.color = color
			lnk.style.textDecoration = 'none'
	}
}
function CheckForReport(reporttype, theyear, thequarter,themonth, theweek, thedate, textonly)
{
	var lookup, classname, reportname, statusMessage
	var formatYear    = theyear
	var formatQuarter = thequarter
	var formatMonth   = parseInt(themonth,10)  + 1
	var formatWeek    = parseInt(theweek,10)
	var formatDate    = parseInt(thedate,10)
	var formatDate    = parseInt(thedate,10)
	var ReadMonth     = themonth + 1

	if (thedate < 10)
		formatDate = "0"  + formatDate
	if (formatWeek < 10)
		formatWeek = "0"  + formatWeek
	if (formatMonth < 10)
		formatMonth = "0" + formatMonth
	if (formatQuarter < 10)
		formatQuarter = "0" + formatQuarter

	with ( frames.month.document )
	{
		if (reporttype == YEARLY_REPORT)
		{
			lookup			= theyear + ".y" + formatYear
			statusMessage	= formatYear
			reportname		= ReportYear[lookup]

			if (reportname)
			{
				var datatype = reportname.substring(reportname.length-4,reportname.length)
				if(datatype == "html")
				{
					classname="static" 
					if (textonly)
						classname="selstatic"
					HTM_LINK = "<a class='"+classname+"' href=\"JavaScript:parent.loadwtreport('" + reportname + "','" + reporttype + "','" + theyear + "')\" onMouseOver=\"status='" + reporttype + ":" + statusMessage + "';return true;\" onMouseOut=\"status='';return true;\">" + formatYear + "</a>"
				}
				else
				{
					classname="ondemand" 
					if (textonly)
						classname="selondemand"
					reportname	= reportname.substring(0,reportname.length-10)
					reportdesc	= reporttype + ":" + statusMessage
					HTM_LINK	= "<a class='"+classname+"' href=\"JavaScript:parent.SetObj('" + reportname + "','" + reportdesc + "','Yearly Report','" + theyear + "');\" onMouseOver=\"status='On demand " + reporttype + ":" + statusMessage + "';return true;\" onMouseOut=\"status='';return true;\">" + formatYear + "</a>"
				}
			}
			else
			{
				HTM_LINK = "<font color='#a1a1a1'>" + formatYear + "</font>"
			}
		}

		if (reporttype == QUARTERLY_REPORT)
		{
			lookup			= formatYear + ".q" + formatQuarter
			statusMessage	= QAbb + formatQuarter + ", " + formatYear
			reportname		= ReportQuarter[lookup]
			if (reportname)
			{
				var datatype = reportname.substring(reportname.length-4,reportname.length)
				if(datatype == "html")
				{
					classname="static" 
					if (textonly)
						classname="selstatic"
					HTM_LINK = "<a class='"+classname+"' href=\"JavaScript:parent.loadwtreport('" + reportname + "','" + reporttype + "','" + formatQuarter + "')\" onMouseOver=\"status='" + reporttype + ":" + statusMessage + "';return true;\" onMouseOut=\"status='';return true;\">" + QAbb + thequarter + "</a>"
				}
				else
				{
					classname="ondemand" 
					if (textonly)
						classname="selondemand"
					reportname	= reportname.substring(0,reportname.length-10)
					reportdesc	= reporttype + ":" + statusMessage
					HTM_LINK	= "<a class='"+classname+"' href=\"JavaScript:parent.SetObj('" + reportname + "','" + reportdesc + "','Quarterly Report','" + formatQuarter + "');\" onMouseOver=\"status='On demand " + reporttype + ":" + statusMessage + "';return true;\" onMouseOut=\"status='';return true;\">" + QAbb + thequarter + "</a>"
				}
			
			}
			else
			{
				HTM_LINK = "<font color='#a1a1a1'>" + QAbb + thequarter + "</font>"
			}
		}

		if (reporttype == MONTHLY_REPORT)
		{
			lookup			= formatYear + ".m" + formatMonth
			statusMessage	= ReadMonth + "/" + formatYear
			reportname		= ReportMonth[lookup]
			if (reportname)
			{
				var datatype = reportname.substring(reportname.length-4,reportname.length)
				if(datatype == "html")
				{
					classname="static" 
					if (textonly)
						classname="selstatic"
					HTM_LINK = "<a class='"+classname+"' href=\"JavaScript:parent.loadwtreport('" + reportname + "','" + reporttype + "','" + formatMonth + "')\" onMouseOver=\"status='" + reporttype + ":" + statusMessage + "';return true;\" onMouseOut=\"status='';return true;\">" + monthNames.substring(themonth * 3,(themonth + 1) * 3) + "</a>"
				}
				else
				{
					classname="ondemand" 
					if (textonly)
						classname="selondemand"
					reportname	= reportname.substring(0,reportname.length-10)
					reportdesc	= reporttype + ":" + statusMessage
					HTM_LINK	= "<a class='"+classname+"' href=\"JavaScript:parent.SetObj('" + reportname + "','" + reportdesc + "','Monthly Report','" + formatMonth + "');\" onMouseOver=\"status='On demand " + reporttype + ":" + statusMessage + "';return true;\" onMouseOut=\"status='';return true;\">" + monthNames.substring(themonth * 3,(themonth + 1) * 3) + "</a>"
				}
				
			}
			else
			{
				HTM_LINK =  "<font color='#a1a1a1'>" + monthNames.substring(themonth * 3,(themonth + 1) * 3) + "</font>"
			}
		}

		if (reporttype == WEEKLY_REPORT)
		{
			lookup			= formatYear + ".w" + formatWeek
			statusMessage	= " " + theweek + " " + formatYear
			reportname		= ReportWeek[lookup]

			if (reportname)
			{
				var datatype = reportname.substring(reportname.length-4,reportname.length)
				if(datatype == "html")
				{
					classname="static" 
					if (textonly)
						classname="selstatic"
					HTM_LINK = "<a class='"+classname+"' href=\"JavaScript:parent.loadwtreport('" + reportname + "','" + reporttype + "','" + formatWeek + "')\" onMouseOver=\"status='" + reporttype + ":" + statusMessage + "';return true;\" onMouseOut=\"status='';return true;\">" + theweek + "</a>"
				}
				else
				{
					classname="ondemand" 
					if (textonly)
						classname="selondemand"
					reportname	= reportname.substring(0,reportname.length-10)
					reportdesc	= reporttype + ":" + statusMessage
					HTM_LINK	= "<a class='"+classname+"' href=\"JavaScript:parent.SetObj('" + reportname + "','" + reportdesc + "','Weekly Report','" + formatWeek + "');\" onMouseOver=\"status='On demand " + reporttype + ":" + statusMessage + "';return true;\" onMouseOut=\"status='';return true;\">" + theweek + "</a>"
				}
			}
			else
			{
				HTM_LINK =  "<font color='#a1a1a1'>" + theweek + "</font>"
			}
		}


		if (reporttype == DAILY_REPORT)
		{
			var day			= false
			statusMessage	= ReadMonth + "/" + thedate + "/" + formatYear
			lookup			= formatYear + "" +  formatMonth
			if ( ReportDay[lookup] )
			{
				entiremonth = ReportDay[lookup]
				slink		= entiremonth.indexOf(formatDate + ".")
				if (slink > 0)
				{
					day = true
					slink = entiremonth.substring(slink,entiremonth.length)
				}
			}
			HTM_LINK	= ""
			var daylink = formatYear + "/m" + formatMonth + "/d" + formatDate + "/d"

			if (day)
			{	
				var elink		= slink.indexOf(",")
				var replink		= daylink + slink.substring(0,elink)
				var datatype	= replink.substring(replink.length-4,replink.length)
				if(datatype == "html")
				{
					classname="static" 
					if (textonly)
						classname="selstatic"
					HTM_LINK += "<a class='"+classname+"' href=\"JavaScript:parent.loadwtreport('" + replink + "','" + reporttype + "','" + thedate + "');\" onMouseOver=\"status='" + reporttype + ":" + statusMessage + "';return true;\" onMouseOut=\"status='';return true;\">" + thedate + "</a>"
				}
				else
				{
					classname="ondemand" 
					if (textonly)
						classname="selondemand"
					replink		= replink.substring(0,replink.length-10)
					reportdesc	= reporttype + ":" + statusMessage
					HTM_LINK	= "<a class='"+classname+"' href=\"JavaScript:parent.SetObj('" + replink + "','" + reportdesc + "','Daily Report','" + thedate + "');\" onMouseOver=\"status='On demand " + reporttype + ":" + statusMessage + "';return true;\" onMouseOut=\"status='';return true;\">" + thedate + "</a>"
				}
			}
			else
			{
				HTM_LINK += "<font color='#a1a1a1'>" + thedate + "</font>"
			}
			HTM_LINK += ""
		}

	}

	return HTM_LINK

}
function LoadMostRecentReport()
{	
	if (Years)
	{
		thelastyear		= Years.substring((Years.length-4), Years.length)
		possmonth		= YearMonth[thelastyear]
		thelastmonth	= possmonth.substring((possmonth.length-2), possmonth.length)		
		BuildReportCalendar(thelastyear, thelastmonth, "", "", "")		
		if( theirbrowser.ie4 )
		{
			with (frames.month.document)
			{
				links[links.length-1].click()
			}
		}
		else
		{
			/*
			with (frames.month.document)
			{
				var func = links[links.length-1].href
				eval(func)
			}
			*/
		}
	}
	else
	{
		with (frames.month.document)
		{
			open()
			var b = "<html>"
					+ "<title>Report Calendar</title>"
					+ "<head>"
					+ '<LI' + 'NK REL="styl' + 'esheet" TYPE="te' + 'xt/css" HREF="style_sheets/' + top.stylesheet + '">'
					+ "</head>"
					+ "<body bgcolor=#FFFFFF>"
					+ "<center><br><br><table width='100%' border='0' cellpadding='0' cellspacing='1'>"
					+ "<tr><td class='datatablenormalbold' nowrap align=center>" + noReports + "</td></tr>"
					+ "</table></center>"
			write(b)
			close()
			show_status()
		}
	}
}

