//*** PARAMETERS and INITIALIZATION ***
var ii, jj;

var debug=false;
//debug=true;
var debugA=new Array();
for (jj=1; jj<9; jj++) debugA[jj]=666;

var tDate=new Date();
var msperd=1000*60*60*24;

var PARCal_start=[2008, 6, 1];
var PARCal_end=[2010, 9, 30];
var minWk=1;
var maxWk=GetTheWeek(PARCal_end[0],PARCal_end[1],PARCal_end[2]);
var	sessStart=0; 
var	sessTotal=2; 

var ii, jj;
var	actWk=new Array();

var actPARCal=new Array();
for (ii=minWk; ii<=maxWk; ii++) {
	actPARCal[ii]=new Array();
	for (jj=0; jj<7; jj++) actPARCal[ii][jj]='';
}
var cDateInfo=[0,0,0];
SetCurrentDateInfo();

//SpecifyPARCalMonRoutes();
SpecifyPARCalWedRoutes();
SpecifyPARCalThursdays();
SpecifyPARCalSpecialDays();

var wOne=[0,0,0];
var tOne=[0,0,0];
var wBeg, wEnd, wAdj;
var tBeg, tEnd;
var mStr=["","JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var dStr=["SUN","MON","TUE","WED","THU","FRI","SAT"];
var dD=[0,0,0,0,0];
var begHR=98;
var endHR=123;
var begYr=GetYearInfo(minWk*7-6);
var endYr=GetYearInfo(maxWk*7);
var begDST=new Array();
var tIOST=new Array();
var tIOSB=new Array();
var endDST=new Array();
var toolTop=[0,1,5,3,3,3,5,6,7];
var toolBot=[0,5,2,7,6,5,4,4,4];

var toolOff=[0,1,3,4,5,2,8,7,6];
var cts='<IMG SRC="sp.gif" WIDTH=9 HEIGHT=1>';	

// initialize wed night schedule = all TBDs


function SetCurrentDateInfo(){
	var cDate=new Date();
	cDateInfo[0]=cDate.getFullYear();
	cDateInfo[1]=cDate.getMonth();
	cDateInfo[2]=cDate.getDate();
}

function SetPARCalDate(d){
	tDate.setFullYear(PARCal_start[0],PARCal_start[1]-1,PARCal_start[2]);
	tDate.setDate(d);
}

function CalcDSTWeeks(){
	for (iYr=begYr;iYr<=endYr;iYr++) {
		begDST[iYr]=GetTheWeek(iYr,3,GetFirstSunday(iYr,3)+7);
		endDST[iYr]=GetTheWeek(iYr,11,GetFirstSunday(iYr,11));
	}
}

function GetFirstSunday(y,m) {
	var t1;
	t1=(GetTheDay(y,m,1)%7);
	t1=(t1<2)?t1+5:t1-2;
	return (7-t1);
}

function GetDateInfo(d){
	SetPARCalDate(d);
	return (tDate.getDate());
}

function GetMYStr(d){
	SetPARCalDate(d);
	return (' '+mStr[tDate.getMonth()+1]+' '+((tDate.getFullYear().toString()).substr(2)));
}

function GetYearInfo(d){
	SetPARCalDate(d);
	return(tDate.getFullYear());
}

function GetMonthInfo(d){
	SetPARCalDate(d);
	return(tDate.getMonth()+1);
}

function GetTheDay(y,m,d){
	var cDate=new Date();
	cDate.setFullYear(y,m-1,d);
	return Date2Day(cDate);
}

function GetTheWeek(y,m,d){
	return Day2Week(GetTheDay(y,m,d));
}

function Day2Week(d){
	return Math.floor((d+6)/7);
}

function Date2Day(dt){
	tDate.setFullYear(2008,5,1);
	return Math.ceil((dt.getTime()-tDate.getTime())/msperd);
}

function GetCurrentWeek(){
	var cDate=new Date();
	return Day2Week(Date2Day(cDate));
}

function IsToday(d){
	SetPARCalDate(d);
	return ((tDate.getFullYear()==cDateInfo[0]) && 
						(tDate.getMonth()==cDateInfo[1]) && 
						(tDate.getDate()==cDateInfo[2]));
}

function DoCalHeadHTML(){
	var i;
	AddDebugForm();
	document.write('<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 WIDTH=544>');
	document.write('  <TR valign=TOP>');
	document.write('    <TD WIDTH=250 align=LEFT><DIV CLASS="TOPIC" ID="W">Activities Calendar</DIV></TD>');
	document.write('    <TD WIDTH=294 STYLE="padding:0px" align=center>');
	for (i=1; i<13; i++) document.write('<DIV id="hd0_'+i+'" CLASS="hd" style="padding-right:8px;display:none">'+mStr[i]+'</DIV>')
	for (i=2008; i<2029; i++) document.write('<DIV id="hd1_'+i+'" CLASS="hd" style="padding-right:8px;display:none">'+i+'</DIV>');
	document.write('<DIV id="hd2_1" CLASS="hd" style="padding-right:8px;display:none">to</DIV>');		
	for (i=1; i<13; i++) document.write('<DIV id="hd3_'+i+'" CLASS="hd" style="padding-right:8px;display:none">'+mStr[i]+'</DIV>');
	for (i=2008; i<2029; i++) document.write('<DIV id="hd4_'+i+'" CLASS="hd" style="display:none">'+i+'</DIV>');
	document.write('</TD>');

	document.write('</TR></TABLE>');
	
}

function DoCalPostHTML() {
	document.write('    </DIV>');
	document.write('  </TD>');
	document.write('  <TD ROWSPAN=1 COLSPAN=1 BGCOLOR="#D5D5c2">');
	document.write('    <DIV><IMG STYLE="background-color:#171611" SRC="sp.gif" WIDTH=3 HEIGHT=32></DIV>');
	document.write('  </TD>');
	document.write('  <TD ROWSPAN=1 COLSPAN=3 STYLE="padding-left:0px" ALIGN="LEFT" BGCOLOR="#D5D5c2" WIDTH=200>');
	document.write('    <DIV><IMG SRC="sp.gif" WIDTH=1 HEIGHT=7></DIV>');
	DoCalCtrlHTML();
	document.write('  </TD>');
	document.write('</TR>');

	document.write('<TR VALIGN="TOP">');
	document.write('  <TD ROWSPAN=1 COLSPAN=1 BGCOLOR="#171611">');
	document.write('<DIV><IMG SRC="sp.gif" HEIGHT=1 WIDTH=1></DIV>');
	document.write('  </TD>');
	document.write('  <TD ROWSPAN=1 COLSPAN=7>');
	document.write('    <DIV STYLE="background-color:#D5D5c2;padding:0px;margin:0px;padding-left:9px;padding-right:9px">');
}

function ToolTop(n){
	var i=toolOff[n];
	document.write(cts+'<IMG ID="top'+i+'" class="cToolT" onclick="DoTool('+i+')" onmouseover="MOver('+i+')" onmouseout="MOut('+i+')" SRC="assets/images/ctrl/tool'+toolTop[i]+'.gif" WIDTH=26 HEIGHT=13>');
	tIOST[i]=document.getElementById("top"+i).style;
}

function ToolBot(n){
	var i=toolOff[n];
	document.write(cts+'<IMG ID="bot'+i+'" class="cToolB" onclick="DoTool('+i+')" onmouseover="MOver('+i+')" onmouseout="MOut('+i+')" SRC="assets/images/ctrl/tool'+toolBot[i]+'.gif" WIDTH=26 HEIGHT=13>');
	tIOSB[i]=document.getElementById("bot"+i).style;
}

function DoCalCtrlHTML(){
	document.write('<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 WIDTH=200>');
	document.write('  <TR valign=TOP>');
	document.write('  <TD WIDTH=158><IMG SRC="sp.gif" HEIGHT=1 WIDTH=158></TD>');
	document.write('  <TD WIDTH=42><IMG SRC="sp.gif" HEIGHT=1 WIDTH=42></TD>');
	document.write('  </TR>');
	document.write('  <TR valign=MIDDLE>');
	document.write('  <TD style="padding-left:2px" align=LEFT>');
		document.write('<DIV>');
		ToolTop(1);for (j=4; j>1; j--) ToolTop(j);
		document.write('</DIV>');	
		document.write('<DIV>');
		ToolBot(1);for (j=4; j>1; j--) ToolBot(j);
		document.write('</DIV>');
		document.write('<DIV STYLE="background-color:#DDDCCC"><IMG SRC="sp.gif" WIDTH=148 HEIGHT=4></DIV>');
		document.write('<DIV>');
		ToolTop(5);for (j=8; j>5; j--) ToolTop(j);
		document.write('</DIV>');
		document.write('<DIV>');
		ToolBot(5);for (j=8; j>5; j--) ToolBot(j);
		document.write('</DIV>');
	document.write('  </TD>');
	document.write('  <TD align=LEFT>');
		document.write('<IMG ID="resett" class="cReset" onclick="RTool()" onmouseover="ROver()" onmouseout="ROut()" SRC="assets/images/ctrl/tool8.gif" WIDTH=29 HEIGHT=21>');
	resettos=document.getElementById("resett").style;
	document.write('  </TD>');
	document.write('  </TR>');
	document.write('  </TABLE>');
}

function DoDayOfWeekHTML(loc){
	var iDay, cbmS, cbwS;
	cbmS=(loc=='top')?';border-bottom-width:10px':';border-top-width:9px';
	document.write('<div style="display:block"><table bgcolor="#ffffff" cellspacing=0 cellpadding=0 border=0 width=748><tr valign=top>');
	for (iDay=0; iDay<7; iDay++) {
		cbwS=(iDay<6)?'':';border-right-width:0px';
		document.write('<td align=center class="cCell" style="position:relative;width:106'+cbwS+cbmS+'"><div class="day">'+dStr[iDay]+'</div></td>');
	}
	document.write('</tr></table></div>');
}

function WriteAct(act){
	document.write('<div class="item" onmouseover="MOverI(this)" onmouseout="MOutI(this)" onclick="DoItemLink(this, &quot;'+act[3]+'&quot;)"><span style="color:993333">'+act[1]+'</span> '+act[2]+'</div>');
}

function WriteDST(w){
	var t1, t2;
	for (iYr=begYr;iYr<=endYr;iYr++) {
		if (w==begDST[iYr]) WriteAct(actList[14]);
		else if (w==endDST[iYr]) WriteAct(actList[15]);
	}
}

function WriteActDefault(d, w){
	var iAct;
	if ((d==3)||(d==4)) {
		document.write(actPARCal[w][d]);
		return;
	}
	if (d==1) {
		var mDST=DSTm(w);
		if (mDST==2)  iAct=13;
		else if (mDST==1) iAct=0;
		else iAct=12;
	}
	else if (d==2) iAct=1;
	else if (d==6) iAct=2;
	else if ((d==4) && (w>begHR) && (w<endHR)) iAct=16;
	else return;
	WriteAct(actList[iAct]);
}

function DSTm(w){
	var dy=w*7-5;
	var mo=GetMonthInfo(dy);
	var res;
	if ((mo<3) || (mo>11)) res=0;
	else if ((mo>3) && (mo<10)) res=2;
	else if (mo==10) res=1;
	else {
		var yr=GetYearInfo(dy);
		if (mo==3) res=(w<begDST[yr])?0:1;
		if (mo==11) res=(w<endDST[yr])?1:0;
	}
	return res;
}

function DoCalGridHTML(){
	var dayBase, iDate;
	var iAct, iDay, iWk, cbmS, cbwS, eomDays;
	var	actWeek, iActWk, iAct, actC, actDay, act;

	CalcDSTWeeks();
	DoDayOfWeekHTML('top');
	iActWk=0; 
	for (iWk=minWk; iWk<=maxWk; iWk++) {
		if (iWk==actWk[iActWk][0]) {
			actWeek=actWk[iActWk]; 
			actC=actWeek.length-1;
			iActWk++;
		}
		else actC=0;
		iAct=1; 			
		if (iAct>actC) actDay=7;
		else actDay=actWeek[iAct][0];
		document.write('<div id="w'+iWk+'" style="display:none"><table bgcolor="#ffffff" cellspacing=0 cellpadding=0 border=0 width=748><tr valign=top>');
		eomDays=(7-GetDateInfo(iWk*7+1));
		dayBase=iWk*7-6;
		for (iDay=0; iDay<7; iDay++){
			cbwS=(iDay<6)?'':';border-right-width:0px';
			cbmS=(eomDays<0)?'':((iDay>eomDays)?';border-top-width:9px':';border-bottom-width:10px');
			iDate=GetDateInfo(dayBase+iDay);
			document.write('<td class="cCell" style="position:relative;width:106;background-color:#ffffff'+cbwS+cbmS+'">');
			document.write('<div class="'+((IsToday(dayBase+iDay))?'tday':'day')+'">'+iDate+((iDate==1)?GetMYStr(dayBase+iDay):'')+'</div>');
			if (!iDay) WriteDST(iWk);
			if (actDay>iDay) WriteActDefault(iDay, iWk);
			else while (actDay==iDay) {
				if (actWeek[iAct].length>1) {
					if (actWeek[iAct].length>2) WriteAct(actWeek[iAct]);
					else WriteAct(actList[actWeek[iAct][1]]);
				}
				iAct++;
				if (iAct>actC) actDay=7;
				else actDay=actWeek[iAct][0];
			}
//		document.write('<div class="item">'+tYMD[0]+' '+tYMD[1]+' '+tYMD[2]+' '+tYMD[3]+'</div>');
			document.write('</td>');
		}
		document.write('</tr></table></div>');
	}
	DoDayOfWeekHTML('bottom');
}

function MOver(num) {
	if (TestTool(num)) {
		tIOST[num].backgroundColor=(wAdj && (num>5))?"#171611":"#ffffff";
		tIOSB[num].backgroundColor=(wAdj && (num<6))?"#171611":"#ffffff";
	} else {
		tIOST[num].backgroundColor="#171611";
		tIOSB[num].backgroundColor="#171611";
	}
}

function MOut(num) {
		tIOST[num].backgroundColor="#171611";
		tIOSB[num].backgroundColor="#171611";
}

function ROver() {
	resettos.backgroundColor="#ffffff";
}

function ROut() {
	resettos.backgroundColor="#171611";
}

function RTool() {
	SetDisplayWeeks(sessStart, sessTotal);
}


function DoItemLink(obj, loc){
	window.location.href=loc;
	obj.style.backgroundColor="#ffffff";
}

function MOverI(obj) {
	obj.style.backgroundColor="#ccffcc";
}

function MOutI(obj, bc) {
	obj.style.backgroundColor="#ffffff";
}

function ChangeMonth(num) {
	for (i=0; i<3; i++) tOne[i]=wOne[i];
	tOne[1]+=num;
	if (tOne[1]<1){
		tOne[1]+=12;
		tOne[0]--;
	}
	if (tOne[1]>12){
		tOne[1]-=12;
		tOne[0]++;
	}
	change=tBeg-(GetTheWeek(tOne[0],tOne[1],1)-tOne[2]);
	tBeg-=change; tEnd-=change;
	if ((tBeg<minWk) && (wBeg>minWk)) {
		tEnd-=(tBeg-minWk);
		tBeg=minWk;
		wAdj=1;
	}
	else if ((tEnd>maxWk) && (wEnd<maxWk)) {
		tBeg-=(tEnd-maxWk);
		tEnd=maxWk;
		wAdj=1;
	}
}

function SetWeeks(start, total) {
	sessStart=start; 
	sessTotal=total; 
	SetDisplayWeeks(start, total);
}

function SetDisplayWeeks(start, total) {
	tBeg=(start>minWk)?start:minWk;
	tEnd=tBeg+total-1;
	tEnd=(tEnd>maxWk)?maxWk:tEnd;
	DisplayWeeks();
}

function CalcWeeks(num) {
	tBeg=wBeg; tEnd=wEnd; wAdj=0;
	if (num==5) {
		tBeg--; tEnd--;
	} else if (num==6) {
		tBeg++; tEnd++;
	}	else if (num==1) tEnd--;
	else if (num==2) tEnd++;
 	else if (num==3) ChangeMonth(-12);
	else if (num==4) ChangeMonth(-1);
	else if (num==7) ChangeMonth(1);
	else if (num==8) ChangeMonth(12);
}

function TestTool(num) {
	CalcWeeks(num);
	return ((tBeg>=minWk) && (tEnd<=maxWk) && (tBeg<=tEnd));
}

function DoTool(num) {
	if (!TestTool(num)) return;
	
	CalcWeeks(num);
	DisplayWeeks();
	MOver(num);
}

function CalcWeekOne() {
	var wk=wBeg;	
	while (GetDateInfo(wk*7) > 7) wk++;
	wOne[0]=GetYearInfo(wk*7);
	wOne[1]=GetMonthInfo(wk*7);
	wOne[2]=wk-wBeg;
}

function DisplayWeeks() {
	var iWk;
	wBeg=tBeg;	wEnd=tEnd;
	DisplayDate();
	for (iWk=minWk; iWk<=maxWk; iWk++) {
		wkObj=document.getElementById('w'+iWk);
		if ((iWk<wBeg)||(iWk>wEnd)) wkObj.style.display="none";
		else wkObj.style.display="block";
	}
	CalcWeekOne();
}
													 
function DisplayDate() {
	var i, hdObj;
	for (i=0; i<5; i++) 
		if (hdObj=document.getElementById('hd'+i+'_'+dD[i]))
			hdObj.style.display="none";
	dD[0]=GetMonthInfo((wBeg*7)-6);
	dD[1]=GetYearInfo((wBeg*7)-6);
	dD[2]=1;	
	dD[3]=GetMonthInfo((wEnd)*7); 
	dD[4]=GetYearInfo((wEnd)*7); 
	if ((dD[3]==dD[0]) && (dD[4]==dD[1]))
		for (i=0; i<3; i++) dD[i]=0;
	else if	(dD[4]==dD[1]) dD[1]=0;
	for (i=0; i<5; i++) 
		if (hdObj=document.getElementById('hd'+i+'_'+dD[i]))
			hdObj.style.display="inline";
}

//***********************************************************************	
function CalDebug(name, value) {
	if (debug) eval("window.document.form1."+name+".value="+value);
}
function AddDebugForm() {
	if (debug) {
		document.write('<DIV><IMG SRC="sp.gif" WIDTH=2 HEIGHT=9></DIV>');
		document.write('<form name="form1">');
		document.write(' v1<input type="text" name="v1" size=18></input>');
		document.write(' v2<input type="text" name="v2" size=18></input>');
		document.write(' v3<input type="text" name="v3" size=18></input>');
		document.write(' v4<input type="text" name="v4" size=18></input><br>');
		document.write(' v5<input type="text" name="v5" size=18></input>');
		document.write(' v6<input type="text" name="v6" size=18></input>');
		document.write(' v7<input type="text" name="v7" size=18></input>');
		document.write(' v8<input type="text" name="v8" size=18></input>');
		document.write('</form>');
		document.write('<DIV><IMG SRC="sp.gif" WIDTH=2 HEIGHT=9></DIV>');
		debugA[8]=GetTheWeek(2008,6,1);
		CalDebug("v1",debugA[1]);
		CalDebug("v2",debugA[2]);
		CalDebug("v3",debugA[3]);
		CalDebug("v4",debugA[4]);
		CalDebug("v5",debugA[5]);
		CalDebug("v6",debugA[6]);
		CalDebug("v7",debugA[7]);
		CalDebug("v8",debugA[8]);
	}
}

