var now = new Date;
var tempDate = new Date;
var baseyear = now.getFullYear();

function psShowWin(theName, theFlags, theURL, thisURL)
{
 if (theURL)
 { psWindow = open(theURL,theName, psWinInfo[theName] );
 psWindow = open(theURL,theName, psWinInfo[theName] ); 
 }
 else 
 { if (psWindow)
 psWindow.close(); }
 if (thisURL) 
 location=thisURL; 
 if (theFlags & 1)
 { if (psWindow.focus)
 psWindow.focus();
 }
}
function psSetupWin(theName, winInfo)
{
 psWinInfo[theName] = winInfo;
}

var psWindow = null;
psWinInfo = new Array();

psSetupWin ("helpWin", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=450,height=250,screenX=10,screenY=10,left=10,top=10");

function resetLodge()
{
document.calc.lodge.selectedIndex=0
}

function makedate()
{
document.calc.date.options[0]=new Option("1st");
document.calc.date.options[1]=new Option("2nd");
document.calc.date.options[2]=new Option("3rd");
document.calc.date.options[3]=new Option("4th");
document.calc.date.options[4]=new Option("5th");
document.calc.date.options[5]=new Option("6th");
document.calc.date.options[6]=new Option("7th");
document.calc.date.options[7]=new Option("8th");
document.calc.date.options[8]=new Option("9th");
document.calc.date.options[9]=new Option("10th");
document.calc.date.options[10]=new Option("11th");
document.calc.date.options[11]=new Option("12th");
document.calc.date.options[12]=new Option("13th");
document.calc.date.options[13]=new Option("14th");
document.calc.date.options[14]=new Option("15th");
document.calc.date.options[15]=new Option("16th");
document.calc.date.options[16]=new Option("17th");
document.calc.date.options[17]=new Option("18th");
document.calc.date.options[18]=new Option("19th");
document.calc.date.options[19]=new Option("20th");
document.calc.date.options[20]=new Option("21st");
document.calc.date.options[21]=new Option("22nd");
document.calc.date.options[22]=new Option("23rd");
document.calc.date.options[23]=new Option("24th");
document.calc.date.options[24]=new Option("25th");
document.calc.date.options[25]=new Option("26th");
document.calc.date.options[26]=new Option("27th");
document.calc.date.options[27]=new Option("28th");
document.calc.date.options[28]=new Option("29th");
document.calc.date.options[29]=new Option("30th");
document.calc.date.options[30]=new Option("31st")
}

function makemonth()
{
document.calc.month.options[0]=new Option("January");
document.calc.month.options[1]=new Option("February");
document.calc.month.options[2]=new Option("March");
document.calc.month.options[3]=new Option("April");
document.calc.month.options[4]=new Option("May");
document.calc.month.options[5]=new Option("June");
document.calc.month.options[6]=new Option("July");
document.calc.month.options[7]=new Option("August");
document.calc.month.options[8]=new Option("September");
document.calc.month.options[9]=new Option("October");
document.calc.month.options[10]=new Option("November");
document.calc.month.options[11]=new Option("December")
}

function makeyear()
{
for (i=0 ; i<10 ; i++)
document.calc.year.options[i]=new Option(baseyear+i);
}

function makeweek()
{
document.calc.week.options[0]=new Option("first");
document.calc.week.options[1]=new Option("second");
document.calc.week.options[2]=new Option("third");
document.calc.week.options[3]=new Option("fourth");
document.calc.week.options[4]=new Option("fifth")
}

function makeday()
{
document.calc.day.options[0]=new Option("Sunday");
document.calc.day.options[1]=new Option("Monday");
document.calc.day.options[2]=new Option("Tuesday");
document.calc.day.options[3]=new Option("Wednesday");
document.calc.day.options[4]=new Option("Thursday");
document.calc.day.options[5]=new Option("Friday");
document.calc.day.options[6]=new Option("Saturday")
}


function remakeall()
{

document.calc.date.options[28].text="29th";
document.calc.date.options[29].text="30th";
document.calc.date.options[30].text="31st";

document.calc.month.options[1].text="February";
document.calc.month.options[3].text="April";
document.calc.month.options[5].text="June";
document.calc.month.options[8].text="September";
document.calc.month.options[10].text="November";

for (i=0 ; i<10 ; i++)
document.calc.year.options[i].text=(baseyear+i)+"";

document.calc.week.options[4].text="fifth"

document.calc.day.options[0].text="Sunday";
document.calc.day.options[1].text="Monday";
document.calc.day.options[2].text="Tuesday";
document.calc.day.options[3].text="Wednesday";
document.calc.day.options[4].text="Thursday";
document.calc.day.options[5].text="Friday";
document.calc.day.options[6].text="Saturday"
}

function leap(year)
{
return ((year%4 == 0) && ((year%400 == 0) || (year%100 != 0)))
}

function noFifthDay()
{
if (document.calc.date.selectedIndex>27) return (false);
tempDate.setDate(1);
tempDate.setMonth(document.calc.month.selectedIndex)
tempDate.setFullYear(document.calc.year.selectedIndex+baseyear);
tempDate.setDate((((7 - tempDate.getDay()) + 
 document.calc.day.selectedIndex) % 7) 
 + 29);
return (tempDate.getDate()<20)
}

function illegaldates()
{
if (document.calc.date.selectedIndex == 28 &&
 document.calc.month.selectedIndex == 1)
 {
 for (i=0 ; i<10 ; i++)
 if (!leap(i+baseyear))
 document.calc.year.options[i].text="--";
 }

theMonth = document.calc.month.selectedIndex

if (theMonth == 1 || theMonth == 3 || theMonth == 5 || 
 theMonth == 8 || theMonth == 10)

 document.calc.date.options[30].text="--";

if (theMonth == 1)
 {
 document.calc.date.options[29].text="--";
 if (!leap(document.calc.year.selectedIndex+baseyear))
 {
 document.calc.date.options[28].text="--"
 }
 }

theDate = document.calc.date.selectedIndex

if (theDate == 30)
 { document.calc.month.options[1].text="--";
 document.calc.month.options[3].text="--";
 document.calc.month.options[5].text="--";
 document.calc.month.options[8].text="--";
 document.calc.month.options[10].text="--" 
 }

if (theDate == 29)
 { document.calc.month.options[1].text="--";
 }

if ((theDate == 28) && 
 (!leap(document.calc.year.selectedIndex+baseyear)))
 { document.calc.month.options[1].text="--";
 }

if (document.calc.week.selectedIndex == 4)
 { for (i=0 ; i<7 ; i++)
 { theDate = new Date(document.calc.year.selectedIndex+baseyear,
 document.calc.month.selectedIndex,1);
 theDate.setDate((((7 - theDate.getDay()) + i) % 7) + 29);
 theMonth=theDate.getMonth()
 if (theMonth != document.calc.month.selectedIndex)
 {
 document.calc.day.options[i].text="--"
 } 
 }
 }
 
if (noFifthDay())
 document.calc.week.options[4].text="--";

}

function meetingdates()
{
if (document.calc.month.selectedIndex == 1 &&
 document.calc.week.selectedIndex == 1 &&
 document.calc.day.selectedIndex == 1)
 { 
 document.calc.lodge.selectedIndex=1
 }
else {if (document.calc.month.selectedIndex == 2 &&
 document.calc.week.selectedIndex == 2 &&
 document.calc.day.selectedIndex == 1)
 { 
 document.calc.lodge.selectedIndex=2
 }
else { if (document.calc.month.selectedIndex == 4 &&
 document.calc.week.selectedIndex == 2 &&
 document.calc.day.selectedIndex == 1)
 { 
 document.calc.lodge.selectedIndex=3
 }
else { if (document.calc.month.selectedIndex == 10 &&
 document.calc.week.selectedIndex == 2 &&
 document.calc.day.selectedIndex == 1)
 { 
 document.calc.lodge.selectedIndex=4
 } 

else { document.calc.lodge.selectedIndex=0 } } } }
}


function setTodaysCalculator()
{
document.calc.date.selectedIndex = (now.getDate() - 1);
document.calc.month.selectedIndex = (now.getMonth());
document.calc.year.selectedIndex = (now.getFullYear()-baseyear);
document.calc.week.selectedIndex = (Math.floor((now.getDate()-1)/7));
document.calc.day.selectedIndex = (now.getDay());
remakeall();
illegaldates();
meetingdates();
}

function todaysCalculator()
{
self.setTimeout("setTodaysCalculator()",1000)
}

function newDate()
{
resetLodge();
if (document.calc.date.options[document.calc.date.selectedIndex].text!="--")
 now.setDate(document.calc.date.selectedIndex+1);
setTodaysCalculator()
}

function newMonth()
{
resetLodge();
if (document.calc.month.options[document.calc.month.selectedIndex].text!="--")
 now.setMonth(document.calc.month.selectedIndex);
setTodaysCalculator()
}

function newYear()
{
resetLodge();
if (document.calc.year.options[document.calc.year.selectedIndex].text!="--")
 now.setFullYear(document.calc.year.selectedIndex+baseyear);
setTodaysCalculator()
}

function newWeek()
{
resetLodge();
if (document.calc.week.options[document.calc.week.selectedIndex].text!="--")
{ now.setDate(1);
 theDate = (((7 - now.getDay())
 + document.calc.day.selectedIndex) % 7) + 1 
 + (document.calc.week.selectedIndex * 7);
 now.setDate(theDate)
}
setTodaysCalculator()
}

function newDay()
{
resetLodge();
if (document.calc.day.options[document.calc.day.selectedIndex].text!="--")
{ now.setDate(1);
 theDate = (((7 - now.getDay())
 + document.calc.day.selectedIndex) % 7) + 1 
 + (document.calc.week.selectedIndex * 7);
 now.setDate(theDate)
}

setTodaysCalculator()
}

function newMeeting()
{
switch(document.calc.lodge.selectedIndex)
{
case 0:
break;

case 1: 
 now.setDate(1);
 now.setMonth(1);
 theDate = (((7 - now.getDay()) + 1) % 7) + 8;
 now.setDate(theDate);
 setTodaysCalculator()
break;

case 2: 
 now.setDate(1);
 now.setMonth(2);
 theDate = (((7 - now.getDay()) + 1) % 7) + 15;
 now.setDate(theDate);
 setTodaysCalculator()
break;

case 3: 
 now.setDate(1);
 now.setMonth(4);
 theDate = (((7 - now.getDay()) + 1) % 7) + 15;
 now.setDate(theDate);
 setTodaysCalculator()
break;

case 4: 
 now.setDate(1);
 now.setMonth(10);
 theDate = (((7 - now.getDay()) + 1) % 7) + 15;
 now.setDate(theDate);
 setTodaysCalculator()
break;

}

}


function drawcalc()
{
document.write(
'<FORM NAME="calc">',
'<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1" BGCOLOR="#104284" ALIGN="Center">',
'<TR ALIGN="center" VALIGN="middle">',
' <TD><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="4" BGCOLOR="#9CBDFF">',
'<TR ALIGN="center" VALIGN="middle">',
' <TD><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1" BGCOLOR="#104284">',
'<TR ALIGN="center" VALIGN="middle">',
' <TD><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" BGCOLOR="#5A84F7">',
'<TR ALIGN="right" VALIGN="middle">',
' <TD BGCOLOR="#9CBDFF"><B>Eastcote&nbsp;<BR>Lodge&nbsp;<BR>Meeting&nbsp;<BR>Date&nbsp;<BR>&nbsp;&nbsp;Calculator&nbsp;<\/B><\/TD>',
'<TD>',
'<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="2">',
'<TR ALIGN="left" VALIGN="middle">',
' <TD><BIG><B>The<\/B><\/BIG>&nbsp;',
'<SELECT NAME="date" SIZE="1" onChange="newDate()">',
'<OPTION>30th<\/SELECT>');
makedate();
document.write(
'&nbsp;<BIG><B>of<\/B><\/BIG>&nbsp;',
'<SELECT NAME="month" SIZE="1" onChange="newMonth()">',
'<OPTION>September<\/SELECT>');
makemonth();
document.write(
'&nbsp;',
'<SELECT NAME="year" SIZE="1" onChange="newYear()">',
'<OPTION>XXXX<\/SELECT>');
makeyear();
document.write(
' &nbsp;<BIG><B>is<\/B><\/BIG>&nbsp;',
'<\/TD>',
'<\/TR>',
'<TR ALIGN="right" VALIGN="middle">',
' <TD><BIG><B>the<\/B><\/BIG>',
' <SELECT NAME="week" SIZE="1" onChange="newWeek()">',
'<OPTION>second<\/SELECT>');
makeweek();
document.write(
'&nbsp;',
'<SELECT NAME="day" SIZE="1" onChange="newDay()">',
'<OPTION>Wednesday<\/SELECT>');
makeday();
document.write(
'<\/TD>',
'<\/TR>',
'<TR ALIGN="center" VALIGN="middle">',
' <TD>',
'<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="2" WIDTH="100%">',
'<TR>',
' <TD ALIGN="center" VALIGN="bottom">',
'<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="2" BGCOLOR="#9CBDFF">',
'<TR>',
' <TD ALIGN="center" VALIGN="middle"><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1" BGCOLOR="#104284">',
'<TR>',
' <TD ALIGN="center" VALIGN="middle"><SELECT NAME="lodge" SIZE="1" onChange="newMeeting()">',
'<OPTION>Eastcote Lodge Meeting Dates',
'<OPTION>&nbsp;second Monday of February',
'<OPTION>&nbsp;third Monday of March',
'<OPTION>&nbsp;third Monday of May',
'<OPTION>&nbsp;third Monday of November',
'<\/SELECT>',
'<\/TD><\/TR><\/TABLE>',
'<\/TD>',
'<\/TR><\/TABLE><\/TD>',
' <TD ALIGN="right" VALIGN="bottom" >',
' <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="2" BGCOLOR="#104284">',
'<TR>',
' <TD ALIGN="center" VALIGN="middle"><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="2" BGCOLOR="#9CBDFF">',
'<TR>',
' <TD ALIGN="center" VALIGN="middle"><INPUT TYPE="BUTTON" VALUE="help" onClick="psShowWin(\'helpWin\', 1, \'help.html\'); return false;">',
'<\/TD><\/TR><\/TABLE>',
'<\/TD><\/TR><\/TABLE>',
'<\/TD><\/TR><\/TABLE>',
'<\/TD><\/TR><\/TABLE>',
'<\/TD><\/TR><\/TABLE>',
'<\/TD><\/TR><\/TABLE>',
'<\/TD><\/TR><\/TABLE>',
'<\/TD><\/TR><\/TABLE>','<\/FORM>')
}
