function birthday(dy,mn)
{
//Get today's date and time at the user's computer

var now = new Date();

var mm = now.getMonth();
var dd = now.getDate();  //User's day of the month
var day = parseInt(dy);
var mon = parseInt(mn) - 1;

if((mm == mon)&&(dd == day))
{
//Birthday detected
var divopen = "<div style=\"position:absolute; left:19px; top:750px; width:124px; height:162px;\">";
var imgtag = "<img src=\"images/bxvism.bmp\" width=\"124\" height=\"161\" border=\"0\" id=\"pic_bxvi\" name=\"pic_bxvi\" title=\"\" alt=\"Papa\">";
var divclose = "</div>";
document.write(divopen + imgtag + divclose);
divopen = "<div class=\"regcent\" style=\"position:absolute; left:19px; top:920px; width:124px; height:30px;\"><span class=\"ar13\">";
divclose = "</span></div>";
document.write(divopen + "Pope Benedict\'s Birthday" + divclose);
}

}

function f()
{
var d=new Date(2011,10,3,5,30);
var d0 = new Date(2011,10,2,5,30);
var oneDay = 1000*3600*24;

var dms = d.getTime();
var dms0 = d0.getTime();
document.write(d.getTime() + " milliseconds since 1970/01/01 <br/>");
document.write(d0.getTime() + " milliseconds since 1970/01/01<br/>");
document.write((d - d0) + " milliseconds difference<br/>");
document.write( (d-d0)/oneDay + ' days difference');
}

function getDateDiff(dd,mm,yy,hh,mn,dd0,mm0,yy0,hh0,mn0)
{
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
dd=parseInt(dd); mm = parseInt(mm); yy = parseInt(yy); hh = parseInt(hh); mn = parseInt(hh); 
dd0=parseInt(dd0); mm0 = parseInt(mm0); yy0 = parseInt(yy0); hh0 = parseInt(hh0); mn0 = parseInt(hh0);

var date0 = months[mm0] + ' ' + dd0 + ',' + yy0 + hh0 + ':' + mn0 + ':' + '00' ;

var dte =new Date(date0);
var dms0 = dte.getTime();  // msec since 1st Jan 1970
 
//           alert(dd0 + ' ' + mm0 + ' ' + yy0 + ' | ' + hh0 + ' : ' + mn0 + ' | ' + dms0);
//Set 1 day in milliseconds
var one_day=1000*60*60*24;

//var dte = new Date(yy,mm,dd,hh,mn,0,0);
var dte = new Date(2011,10,19,18,0,0,0);
var dms = dte.getTime();  // msec since 1st Jan 1970 
//          alert(dd + ' ' + mm + ' ' + yy + ' | ' + hh + ' : ' + mn + ' | ' + dms);

var d = dms/one_day;
var d0 = dms0/one_day;

//          alert(d + '   ' + d0);
var dDiff = d - d0;
//          alert('Days since start of the week = ' + dDiff);
return dDiff;
}

function getMassVenue(tst,dyTst,mnTst,yrTst,hrTst,minTst)
{

var nxt,sumMass,ch,tm,thm,thr,tmn,tmnow,d,svc,cmt,cmtS,ldr,hs,gf,fwdlook,ist;
var nxthr, nxtmn, tod, svcDay, srvceDay, srvceHr, srvceMn, srvceTime, srvceChurch;
var dd,mm,yy,hh,mn,dow,doy,isleap;
var ddBegin,mmBegin,yyBegin,hhBegin,mnBegin;
var wkdys = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
var edays = new Array(102,94,114,99,90,110,95,87,106,91,111,103);

var len = chtim.length;
var nxtt,nxtd,nxtch,nxtsvc;
var gotService = 0;

//Check whether we take today's date (or one entered by the user)

tst = parseInt(tst);

if(tst == 0)
{
//Get today's date and time at the user's computer
var now = new Date();

mm = now.getMonth();  //User's month  Jan = 0
dd = now.getDate();  //User's day of the month
dow = now.getDay();   //User's day of the week
yy = now.getFullYear(); //User's year
hh = now.getHours();
mn = now.getMinutes();
if(yy%4 == 0){isleap = 1} else {isleap =0}
doy = DayofYear(mm,dd,isleap);
}
else
{
//Retrieve date and time from the arguments of the calling routine
dd = parseInt(dyTst);
mm = parseInt(mnTst) - 1; //Month number (mnTst) has Jan=1, convert to a Jan=0 convention
yy = parseInt(yrTst);
hh = parseInt(hrTst);
mn = parseInt(minTst);

if(yy%4 == 0){isleap = 1} else {isleap =0}
doy = DayofYear(mm,dd,isleap);
//get day of the week (1st Jan 2004 was a Tuesday dayofweek = 4)
//Number of leapdays from 2004 until this year
var nlpdays = Math.floor((yy-2004-1)/4) + 1;
//Number of weekdays that new year advances relative to 2004
var nwkdys = yy - 2004 + nlpdays;
//Get new year weekday
var nyrwkday = nwkdys%7 + 4;
if(nyrwkday > 6){nyrwkday = nyrwkday -7};

dow = (doy - 1)%7 + nyrwkday;
if (dow > 6){dow = dow - 7}
//alert(dd + ' ' + mm + ' ' + yy + ' | ' + hh + ' : ' + mm + ' | ' + dow);
}
var mmd = mm+1;
//alert("Day of Year= " + doy + " Day of Week=" + dow + " Date= " + dd + "/" + mmd + "/" + yy)

// weekBegin is an array set in the script getMassXX.js
ddBegin = weekBegin[0];
mmBegin = weekBegin[1] - 1;
yyBegin = weekBegin[2];
hhBegin = weekBegin[3];
mnBegin = weekBegin[4];
//var mnBegind = mnBegin + 1;
var mmBegind = mmBegin + 1.0;
//alert('Start of Week = ' + ddBegin + '  ' + mmBegind + '  ' + yyBegin + ' | Time = ' + hhBegin + ':' + mnBegin);



var tt = hh + mm/60;
var ttBegin = hhBegin + mnBegin/60

var daydiff = getDateDiff(yy,mm,dd,hh,mn,yyBegin,mmBegin,ddBegin,hhBegin,mnBegin);
//alert('Time, date etc = ' + yy + ',' + mm + ',' + dd + ',' + tt + ' Begin time etc= ' + yyBegin + ',' + mmBegin + ',' + ddBegin +',' + ttBegin);

// If (dd,mn,yy,hr,mn) is equal to or less then (ddbegin,mmBegin,yyBegin,hhBegin,mnBegin) then point to first Saturday, ist = 0
// Else ignore first Saturday ist = 1

if((yy > yyBegin)||((yy == yyBegin)&&(mm > mmBegin))||((yy = yyBegin)&&(mm == mmBegin)&&(dd > ddBegin))||((yy == yyBegin)&&(mm == mmBegin)&&(dd == ddBegin)&&(tt > ttBegin))){ist = 1} else {ist = 0}


mmd = mm + 1;
//alert('Date and Time = ' + dd +'/' + mmd + '/' + yy + '  Time = ' + hh + ':' + mn + ' Day of Week = ' + dow + 'Day of Year = ' + doy + ' Start parameter ist = ' + ist); 
	
gf = edays[yy-2009] - 2;
hs = edays[yy-2009] - 1;
//if((doy == (edays[yy-2009]-2))&&(doy == (edays[yy-2009]-1)))
//if((doy == gf)||(doy == hs)){} else {}


//alert('Tomorrow = ' + tomorrow);
//alert("Today is " + dow);

//If ist = 0 and day of week = 6,or 5 then make dow = -1 or -2 as it is the days before week start


if(ist == 0){dow = dow - 7};

//If ist = 1 and dow = 0, then dow = 7 (beginning of the next week

//if((ist == 1)&&(dow == 0)){dow = 7};

//         alert('ist = ' + ist + '  dow = ' + dow);

tim = hh + mn/60;

for(var i=0; i<len; i++)
{
//Loop through the array 
  d = chtim[i][0];   //Service day 0=Sunday
  ch = chtim[i][1];  //Church name
	svc = chtim[i][2]; //Service
  thm = chtim[i][3];  //Service time in hours and minutes separated by a colon :
  cmt = chtim[i][4]; //Comment for the day
//alert("Time= " + tim + " ServiceDay= " + d + " Service= " + svc + " Service Time= " + thm + " Church= " + ch + " ist = " + ist);

//If ist == 0 then loop through array from 0 to len-1
//Else if ist == 1 ignore all servicedays where d <0 

if((ist == 0)||((ist == 1)&&(d >= 0)))
{
//If ServiceDay = -1, then Serviceday is actually Saturday
if(d == -1){svcDay = 6} else {svcDay = d}

//alert("ist = " + ist + " d= " + d + " ServiceDay = " + svcDay + " ServiceChurch = " + ch + " dow = " + dow + " tim = " + tim + " tm = " + tm);
  
	if(ch != "")
  {
  //there is a service today, but is the time right?
	//Find the time
	var ss = thm.split(':');
	thr = parseInt(ss[0]);
	if(ss.length > 1){tmn = parseInt(ss[1])} else {tmn = 0}
	tm = thr + tmn/60;
	//alert(tm + ' ' + thr + ':' + tmn + ' ServiceDay ' + d + ' Day of Week ' + dow);
  if(((tim < tm)&&(d == dow))||(d > dow))
    {
    nxtch = ch;
	  nxtd = d;
    nxtt = tm;
		nxtsvc = svc;
	  gotService = 1;
//alert("Service Found at: " + tim + " and DOW= " + dow+ "; Ch= " + nxtch + " Day= " + nxtd + " Time= " + nxtt + " ist = " + ist + " serviceday = " + d);
    }
  }
 
  if(gotService == 1){break}
}
} //end for loop


if(gotService == 1)
{

if(nxtch == "J"){srvceChurch = "St Joseph\'s"}
else if(nxtch == "A"){srvceChurch = "St Augustine\'s"}
else if(nxtch == "C"){srvceChurch = "the Convent of Mercy"}
else if(nxtch == "O"){srvceChurch = cmt}
else if(nxtch == "P"){srvceChurch = "Plymouth Cathedral"}
else if(nxtch == "T"){srvceChurch = "St Paul\'s, Westham"}
else{srvceChurch = "St Augustine\'s School"}

if(nxtt == 12){nxthr = 12; tod = " noon"} else if(nxtt == 24){nxthr = 12; tod = " midnight"} else if (nxtt < 12){nxthr = nxtt; tod = " am"} else if(nxtt >= 13){nxthr = nxtt-12; tod = " pm"} else {nxthr = nxtt; tod = " pm"}


srvceHr = Math.floor(nxthr);
if(tmn < 10){srvceMn = "0" + tmn} else {srvceMn = tmn};
//add the cfraction of the hour changed into minutes

if(nxthr > srvceHr)
{srvceTime = srvceHr + ":" + srvceMn + tod}
else {srvceTime = srvceHr + tod};
	
//alert("nxt day = " + nxtd + " Dayof Week = " + dow + " Day = " + wkdys[nxtd]);
var tmrrw = 0;
if(nxtd > dow)
{
	fwdlook = nxtd - dow;
	if((nxtd-dow) == 1)
	{
	tmrrw = doy +1;
	if(tmrrw > (365 + isleap)){tmrrw = 1}
	if(nxtd < 0 ){nxtd = nxtd + 7} 
	srvceDay = " tomorrow (" + wkdys[nxtd] + ")";
		}
	else
	{
	srvceDay = " on this coming " +wkdys[nxtd]
	}
}	
else
   {fwdlook = 0; srvceDay = " today"}

//alert('Tomorrow = ' + tmrrw + 'Day of week = ' + dow);
if(tmrrw != 0)
{
//day of week tomorrow
dow = dow + 1;
if(dow > 6){dow = dow - 7}
var saints = getSaintDay(tmrrw,isleap,dow);
var majorFeast = principalFeasts(yy,tmrrw,isleap);
var sName = trim(saints[0]);
var isfeast = majorFeast[0];
//alert('Tomorrow = ' + tmrrw + ' Saints day = ' + saints);
if(isfeast == 1)
{
var feast = majorFeast[1];
document.write("Tomorrow is " + feast);
}
else
{
if(sName != ""){document.write("Tomorrow is the feast of " + sName)}
}
}

if(nxtsvc != "Mass")
{
if(nxtsvc != "Service"){ldr = "<br>"} else {ldr = "<br>Our "}
} 
else {ldr = "<br>Our next "}
 
document.write(ldr + nxtsvc + " will be at " + srvceChurch +  srvceDay + " at " + srvceTime);

if(ch == "S")
{
 if(dow == 6)
 {cmtS = "(for the Children of the Parish and their Parents only)"}
 else
 {cmtS = "(for the schoolchildren, their parents and relatives and school staff only)"}
 if(cmt != ""){cmt = cmtS + '<br/>' + cmt} else {cmt = cmtS}
};

//Display comments on a future service only if we are less than 2 days away
if((cmt != "")&&(fwdlook < 2))
{
if(nxtch == "O"){cmt = "See Newsletter for details"}  
document.write("<br>" + cmt);
}

if(msgSeason != "")
{document.write('<br><br> <span class=\"ta14g\">' + msgSeason + '<\/span><br><span class=\"ta14g\">' + msgSeasonMasses + '<\/span>')}

} // end if (gotService == 1)


}

