//CHURCH CALENDAR

//Various functions
//Moving Solemnities to nearest Sunday if they fall on Saturday or Monday
function getSaintDay(day,lp,dow)
{
//day = day of the year; sw = 0 pick out Saint(s) name(s); sw = 1 pick out type of feast
//dow = day of the week (Sunday = 0) 
//lp = 1 for a leap year, 0 otherwise
//feasts is an Array of length 366 with 2 elements; element [0][0] is the zeroth element
var colr,dy,sdy,styp,sclass,scol;
var feasts = new Array(
["",""],
["Blessed Virgin Mary Mother of God","Solemnity:","W"],
["St Basil the Great, St Gregory Nazianzen <br>(Bishops, Doctors of the Church)","Memorial:","W"],
["The Most Holy Name of Jesus	","Solemnity:","W"],
["","","W"],
["","","W"],
["","","W"],
["St Raymond of Penyafort (Priest)","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["St Aelred of Rievaulx (Religious)","","W"],
["St Hilary, (Bishop, Doctor of the Church)","","W"],
["","",""],
["","",""],
["","",""],
["St Antony (Abbot)","Memorial:","W"],
["","",""],
["St Wulstan (Bishop)","",""],
["","",""],
["St Agnes (Virgin, Martyr)","Memorial:","R"],
["St Vincent (Deacon, Martyr)","",""],
["","",""],
["St Francis de Sales (Bishop, Doctor of the Church)","Memorial:","W"],
["Conversion of St Paul","Feast:","W"],
["SS Timothy and Titus (Bishops)","	Memorial:	","W"],
["","",""],
["St Thomas Aquinas (Priest, Doctor of the Church)","Memorial:","W"],
["","",""],
["","",""],
["St John Bosco (Priest)","Memorial:","W"],
["","",""],
["The Presentation of the Lord","Feast:","W"],
["St Blaise, Bishop, Martyr; St Ansgar, Bishop","",""],
["","",""],
["St Agatha Virgin and Martyr","Memorial:	","R"],
["St Paul Miki and his Companions, Martyrs","Memorial:	","R"],
["","",""],
["St Jerome Emiliani, St Josephine Bakhita, Virgin","",""],
["St Teilo (Bishop)","",""],
["St Scholastica, Virgin","Memorial:","W"],
["Our Lady of Lourdes","",""],
["","",""],
["","",""],
["SS Cyril (Monk) and Methodius (Bishop)","Memorial:","W"],
["","",""],
["","",""],
["The Seven Founders of the Order of Servites","","W"],
["","",""],
["","",""],
["","",""],
["St Peter Damian (Bishop, Doctor of the Church)","","W"],
["The Chair of St Peter, Apostle","Feast:","W"],
["St Polycarp","	Memorial:	","W"],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["St David, Patron of Wales","Solemnity:","W"],
["","",""],
["","",""],
["St Casimir","",""],
["","",""],
["","",""],
["SS Perpetua and Felicity, Martyrs","Memorial:","R"],
["St John of God","",""],
["St Frances of Rome, Religious","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["St Patrick, Bishop","Feast:","W"],
["","",""],
["St Joseph, Husband of the Blessed Virgin Mary","Solemnity:","W"],
["","",""],
["","",""],
["","",""],
["St Toribius of Mogrovejo, Bishop","",""],
["","",""],
["Annunciation","Solemnity:","W"],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["St Francis of Paola  (Hermit)","",""],
["","",""],
["St Isidore (Bishop, Doctor of the Church)","",""],
["St Vincent Ferrer (Priest)	","",""],
["","",""],
["St John the Baptiste de la Salle (Priest)","Memorial:","W"],
["","",""],
["","",""],
["","",""],
["St Stanislaus (Bishop, Martyr)","Memorial:","R"],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["St Beuno, Abbot","",""],
["St Anselm (Bishop, Doctor of the Church)","",""],
["","",""],
["St George, Martyr (Patron of England)","Solemnity:","R"],
["St Adalbert (Bishop, Martyr)	","",""],
["St Mark (Evangelist)","Feast:","R"],
["","",""],
["","",""],
["St Peter Chanel (Priest, Martyr); St Louis de Montfort (Priest)","",""],
["St Catherine of Sienna (Virgin, Doctor of the Church)","Feast:","W"],
["St Pius V (Pope)","",""],
["St Joseph the Worker","",""],
["St Athanasius, Bishop, Doctor of the Church	","Memorial:","W"],
["SS  Philip and James, Apostles","Feast:","R"],
["The English Martyrs","Memorial:","R"],
["St Richard Reynolds","Memorial:","R"],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["SS Nereus and Achilleus, Martyrs, St Pancras, Martyr","",""],
["Our Lady of Fatima","",""],
["St Matthias","Feast:","R"],
["","",""],
["","",""],
["","",""],
["","",""],
["St Dunstan, Bishop","",""],
["St Bernadine of Sienna, Priest, Religious, Missionary","",""],
["SS Christopher of Magellanes and Companions, Martyrs","",""],
["St Rita of Cascia, Religious","",""],
["","",""],
["St Aldhelm of Sherbourne","Memorial:","W"],
["St Bede the Venerable","Memorial:","W"],
["St Philip Neri, Priest","Memorial:","W"],
["St Augustine of Canterbury, Bishop","Memorial:","W"],
["","",""],
["","",""],
["The Sacred Heart of Jesus","Solemnity:","W"],
["The Visitation of the Blessed Virgin Mary","Feast:","W"],
["St Justin, Martyr","",""],
["SS Marcellinus and Peter, Martyrs","",""],
["SS Charles Lwanga and Companions, Martyrs","Memorial:","R"],
["","",""],
["St Boniface, Bishop, Martyr, Patron of the Diocese","Solemnity:","R"],
["St Maria Goretti, Virgin, Martyr; St Norbert, Bishop","",""],
["St Columba, Abbot; St Ephrem of Syria Deacon, Doctor of the Church","",""],
["","",""],
["","",""],
["","",""],
["St Barnabas, Apostle","Memorial:","R"],
["The Immaculate Heart of Mary","Memorial:","W"],
["St Antony of Padua, Priest, Doctor of the Church; St Henry","Memorial:","W"],
["","",""],
["","",""],
["St Richard of Chichester, Bishop","",""],
["","",""],
["","",""],
["St Romuald, Abbot","",""],
["St Alban, Martyr; St Apollinaris, Bishop, Martyr","",""],
["St Aloysius Gonzaga, Religious","Memorial:","W"],
["St John Fisher, Bishop and St Thomas More, Martyrs","Feast:","R"],
["St Ethelreda (Audrey) Abbess","",""],
["The Birth of St John the Baptist","Solemnity:","W"],
["","",""],
["","",""],
["St Cyril of Alexandria, Bishop, Doctor of the Church","",""],
["St Irenaeus, Bishop, Martyr","Memorial:","R"],
["SS Peter and Paul, Apostles, Martyrs","Solemnity:","R"],
["The First Martyrs of the Church of Rome","",""],
["St Oliver Plunket, Bishop, Martyr","",""],
["","",""],
["St Thomas, Apostle","Feast:","R"],
["Blessed John Cornelius and Companions","Memorial:","R"],
["St Antony Zaccaria, Priest","",""],
["","",""],
["","",""],
["","",""],
["SS Augustine Zhao Rong (Priest) and Companions, Martyrs","",""],
["","",""],
["St Benedict, Patron of Europe","Feast:","W"],
["St John Jones, Priest, Martyr","",""],
["","",""],
["St Camillus de Lellis, Priest","",""],
["St Bonaventure, Bishop, Doctor of the Church; St Swithin, Bishop","Memorial:","W"],
["Our Lady of Mount Carmel","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["St Lawrence of Brindisi, Priest, Doctor of the Church","",""],
["St Mary Magdelene	","Memorial:","R"],
["St Bridget, Religious;  SS Philip Evans and John Lloyd, Priests, Martyrs","",""],
["St Sharbel Makhluf, Priest","",""],
["St James, Apostle","Feast:","R"],
["SS Joachim and Ann, parents of Mary","Memorial:","W"],
["","",""],
["","",""],
["St Martha","Memorial:","W"],
["St Peter Chrysologus","",""],
["St Ignatius Loyola, Priest","Memorial:","W"],
["St Alphonsus Liguori Bishop, Doctor of the Church","Memorial:","W"],
["St Eusebius of Vercelli, Bishop; St Peter Julian Eymard, Priest, Religious Fndr","",""],
["","",""],
["St John Vianney, Priest","Memorial:","W"],
["The Dedication of the Basilica of St Mary Major","",""],
["The Transfiguration of Our Lord","Feast:","W"],
["St Sixtus II, Pope, Martyr and Companions, Martyrs; St Cajetan, Priest","",""],
["St Dominic, Priest","Memorial:","W"],
["St Teresa Benedicta of the Cross, (Edith Stein), Virgin and Martyr","Feast:","R"],
["","",""],
["St Clare, Virgin","Memorial:","W"],
["St Jane Frances de Chantal","",""],
["SS Pontian Pope and Hippolytus, Martyrs","",""],
["St Maximilian Maria Kolbe, Priest, Martyr","Memorial:","R"],
["The Assumption of the Blessed Virgin Mary","Solemnity:","W"],
["St Stephen of Hungary","",""],
["","",""],
["","",""],
["St John Eudes","",""],
["St Bernard, Abbot, Doctor of the Church","Memorial:","W"],
["St Pius X, Pope","Memorial:","W"],
["The Queenship of the Virgin Mary","Memorial:","W"],
["St Rose of Lima, Virgin","",""],
["St Bartholomew","Feast:","R"],
["St Louis; St Joseph Calasanz, Priest","",""],
["Bl Dominic of the Mother of God, Priest<br>also St David Lewis, Martyr","",""],
["St Monica	","Memorial:","W"],
["St Augustine of Hippo, Bishop, Doctor of the Church","Memorial:","W"],
["The Beheading of St John the Baptist, Martyr","Memorial:","R"],
["SS Margaret Clithero, Anne Line, Margaret Ward, Martyrs","",""],
["St Aidan, Bishop, and the Saints of Lindisfarne","",""],
["","",""],
["","",""],
["St Gregory the Great, Bishop, Doctor of the Church","Memorial:","W"],
["St Cuthbert, Bishop","",""],
["","",""],
["","",""],
["","",""],
["The Birthday of the Blessed Virgin Mary","Feast:","W"],
["St Peter Claver, Priest	","",""],
["","",""],
["St Deiniol","",""],
["The Most Holy Name of Mary","",""],
["St John Chrysostom	","Memorial:","W"],
["The Triumph of the Holy Cross","Feast:","R"],
["Our Lady of Sorrows	","Memorial:","W"],
["SS Cornelius, Pope and Cyprian Bishop, Martyrs","	Memorial:","R"],
["St Robert Bellarmine, Bishop, Doctor of the Church","",""],
["","",""],
["St Januarius, Bishop; St Theodore of Canterbury, Bishop","",""],
["SS Andrew Kim Taegon, Priest, Martyr; Paul Cheong Hsang, Catechist, Martyr and their Companions, Martyrs","Memorial:","R"],
["","",""],
["The Dedication of the Cathedral","Feast:","W"],
["St Pius of Pietrelcina (Padre Pio)","Memorial:","W"],
["Our Lady of Walsingham","Memorial:","W"],
["","",""],
["SS Cosmas and Damian, Martyrs	","",""],
["St Vincent de Paul, Priest	","Memorial:	","W"],
["","",""],
["SS Michael, Gabriel and Raphael Angels	","Feast:","W"],
["St Jerome, Priest, Doctor of the Church	","Memorial:	","W"],
["St Teresa of the Child Jesus, Virgin, Doctor of the Church	","Memorial:","W"],
["The Guardian Angels","Memorial:","W"],
["","",""],
["St Francis of Assisi","Memorial:","W"],
["","",""],
["St Bruno	","",""],
["Our Lady of the Rosary	","Memorial:","W"],
["","",""],
["SS Denis, Bishop and Companions, Martyrs, St John Leonardi, Priest	","",""],
["St Paulinus of York, Bishop	","",""],
["","",""],
["","",""],
["St Edward the Confessor","",""],
["St Callistus I, Pope, Martyr","",""],
["St Teresa of Avila, Virgin, Doctor of the Church","Memorial:","W"],
["St Hedwig, Religious; St Margaret Mary Alacoque,Virgin; St Richard Gwyn Schoolmaster, Martyr","Memorial:","R"],
["St Ignatius of Antioch, Bishop, Martyr","",""],
["St Luke, Evangelist	","Feast:","R"],
["","",""],
["","",""],
["","",""],
["","",""],
["St John of Capistrano, Priest	","",""],
["St Antony Claret, Bishop","",""],
["The Six Welsh Martyrs and their Companions","Feast:","R"],
["","",""],
["","",""],
["Ss Simon and Jude, Apostles","Feast:","R"],
["","",""],
["","",""],
["","",""],
["All Saints"," Solemnity:","W"],
["All Souls","","B"],
["St Winefride, Virgin","","W"],
["St Charles Borromeo, Bishop","Memorial:","W"],
["","",""],
["St Illtud, Abbot","",""],
["St Willibrord, Bishop","",""],
["","",""],
["The Dedication of the Lateran Basilica","Feast:",""],
["St Leo the Great, Pope, Doctor of the Church","Memorial:","W"],
["St Martin of Tours, Bishop","Memorial:","W"],
["St Josephat, Bishop, Martyr","Memorial:","R"],
["","",""],
["St Dyfrig, Bishop","","W"],
["St Albert the Great, Bishop, Doctor of the Church","","W"],
["","",""],
["St Elizabeth of Hungary, Religious; St Hilda, Abbess; <br>St Hugh of Lincoln, Bishop","","W"],
["The Dedication of the Basilicas of Ss Peter and Paul","","W"],
["","",""],
["","",""],
["The Presentation of the Virgin Mary","Memorial:","W"],
["St Cecilia, Virgin, Martyr","Memorial:","R"],
["","",""],
["Ss Andrew Dung-Lac, Priest and Companions Martyrs	","Memorial:","R"],
["St Catherine of Alexandria	","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["St Andrew, Patron Saint of Scotland","Feast:","R"],
["","",""],
["","",""],
["St Francis Xavier, Priest	","Memorial:","W"],
["St John Damascene, Priest, Doctor of the Church	","",""],
["","",""],
["St Nicholas, Bishop	","",""],
["","",""],
["The Immaculate Conception of the Blessed Virgin Mary","Solemnity:","W"],
["St John Diego Cuahtlatoatzin","",""],
["St John Roberts, Priest, Martyr","",""],
["St Damasus I, Pope","",""],
["Our Lady of Guadalupe","",""],
["St Lucy, Virgin, Martyr","Memorial:","R"],
["St John of the Cross, Priest, Doctor of the Church","Memorial:","W"],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["","",""],
["St Peter Canisius, Priest, Doctor of the Church","Memorial:","W"],
["","",""],
["St John of Kanty, Priest","",""],
["","",""],
["The Nativity of Our Lord","Solemnity:","W"],
["St Stephen, First Martyr","Feast:","R"],
["St John, Apostle and Evangelist","Feast:	","W"],
["The Holy Innocents","Feast:","R"],
["St Thomas Becket, Bishop, Martyr","",""],
["","",""],
["St Sylvester I, Pope","",""]
);

dy = day;
//Test for Leap year

if (lp == 1)
{
	if (day != 60)
	{
	  if (day > 60)
	  {
	  //Knock back by 1 day to bring in line with array
	  dy = day - 1;
	  }
	//Else dy is unchanged
	}
	else
	{
	//Leap day itself - Feria
	dy = 0;
	}
}

//alert('Day of week = ' + dow + '  Day of Year = ' + dy);
//Special arrangements for Assumption (227), All Saints (305)
if(((dy==227)||(dy==305))&&((dow==6)||(dow==1))){sdy = ""; styp=""}
else if(((dy==226)||(dy==304))&&(dow==0)){sdy=trim(feasts[dy+1][0]);styp="Solemnity:"; colr=trim(feasts[dy+1][2])}
else
if(((dy==228)||(dy==306))&&(dow==0)){sdy=trim(feasts[dy-1][0]);styp="Solemnity:"; colr=trim(feasts[dy-1][2])}
else
if((dy==307)&&(dow==1)){sdy=trim(feasts[dy-1][0]); styp=""; colr="B"}
//All Souls falls on Monday if Nov 1st falls on Saturday
else
{
sdy = trim(feasts[dy][0]);  //Feast Day - Name
styp = trim(feasts[dy][1]); //Feast Type - solemnity, feast, memorial, commemoration
colr = trim(feasts[dy][2]); //Liturgical colour - White, Red, Black
}


if(styp == "Solemnity:"){sclass = 0}
else if(styp == "Feast:"){sclass = 1}
else if (styp == "Memorial:"){sclass = 2}
else if (styp == "")
{if(sdy != ""){styp = "Comm:"; sclass = 4} else {styp = "Feria"; sclass = 10}}

//alert('Day of Year = ' + day + '('+ dy + ')' + ' Day of week = ' + dow + ' Feast Day = ' + sdy + ' Feast Type = ' + styp + 'Feast Class = ' + sclass);


if(colr == "W"){scol = 1}
else if(colr == "R"){scol = 2}
else if(colr == "B"){scol = 6}
else {scol = 10}
//alert('sDay= ' + sdy + ' sType=' + styp + ' sClass=' + sclass + 'sColour=' + scol);
return[sdy,styp,sclass,scol];

}


function DayofYear(mth,dy,ilp)
//Returns the Day of the Year (Jan 1 = 1; Dec 31 = 365 or 366)
{
var d,xtr;
days = new Array(0,31,59,90,120,151,181,212,243,273,304,334);

// Set xtr = 0 or 1 if the month > Feb and if it's a leap Year
if (mth <= 1)
{
xtr = 0;
}
else
{
xtr = ilp;
}
d = days[mth] + dy + xtr;
return(d);
}

function trim(s){
var snew = s.replace(/^\s*(.*?)\s*$/,"$1");
//document.write('Here is the input string  \"' + s +'\"' + "<br>" + 'Here is the output string  \"' + snew + '\"');
return snew;
}


function GetMonth(dayno, ilp)
//Returns the Month, given the day of the year and whether it's a leap year
{
var xtr,mth;
var days = new Array(31,59,90,120,151,181,212,243,273,304,334,365);
// Set xtr = 0 or 1 if the month > Feb and if it's a leap Year
for (i = 0; i < 12; i++)
 {
        if (i <= 1)
        {
          xtr = 0;
        }
        else
        {
          xtr = ilp;
        }
        if (dayno <= days[i]+ xtr)
        {
         mth = i + 1;
         return (mth);
        }
 }
}



function GetDay(dayno, ilp)

{

var dy;

var days = new Array(0,31,59,90,120,151,181,212,243,273,304,334,365);

// Set xtr = 0 or 1 if the month > Feb and if it's a leap Year

for (i = 1; i < 13; i++)

 {

        if (i < 2)

        {

          xtr = 0;

        }

        else

        {

          xtr = ilp;

        }

        if (dayno <= days[i] + xtr)

        {

         dy = dayno - days[i-1] + xtr;

         return (dy);

        }

 }

}



function SundayOrdinaryTime(dyno,pdyno)

{

var wks;

var x = dyno - pdyno;

wks = Math.floor(x/7) + 1;

return (wks);

}



function GetSuffix(d)
{

//Set the ordinal for the date; i.e. st, nd, rd th


var last,suffix;

var suffixes = new Array("th","st","nd","rd");



if (d > 99)

{

d = d%100;

}



// 11th, 12th, 13th are special cases

if ((d==11)|| (d==12) || (d==13))
 {
   suffix = "th";
 }
 else
 {

//Find the last integer
   last = d%10;

   if (last < 4)

   {

     suffix = suffixes[last];

   }

   else

   {

    suffix = "th";

   }

}

return (suffix);
}


function getDiv(lft, tp, wd,ht)

{

divopen = "<div style = \"position:absolute; left:" + lft + "; top:" + tp + "; width:" + wd + "; height:" + ht + ";\">";

return (divopen);

}

function principalFeasts(yr,doy,isleap)
{
//doy is day of the year
var a,b,c,d,e,f,g,h,i,k,l,m,emon,eday,edayno;
a = yr % 19;
b = Math.floor(yr/100);
c = yr % 100;
d = Math.floor(b/4);
e = b % 4;
f = Math.floor((b+8)/25);
g = Math.floor((b-f+1)/3);
h = (19*a+ b - d - g + 15)%30;
i = Math.floor(c/4);
k = c % 4;
l = (32 + 2*e + 2*i - h - k) % 7
m = Math.floor((a + 11*h + 22*l)/451);
//This algorithm gives emon where Jan = 1
emon = Math.floor((h + l - 7*m + 114)/31);
eday = (h + l - 7*m + 114)% 31 + 1;

if (emon == 3)
 {edayno = 59 + eday + isleap}
else
 {edayno = 90 + eday + isleap}
 
if(doy == (edayno-46)){isfeast = 1; feast = "Ash Wednesday"}
else if (doy == (edayno-3)) {isfeast = 1; feast = "Maundy Thursday"}
else if (doy == (edayno-2)) {isfeast = 1; feast = "Good Friday"}
else if (doy == (edayno-1)) {isfeast = 1; feast = "Holy Saturday"}
else if (doy == edayno) {isfeast = 1; feast = "Easter Sunday"}
else if (doy == (edayno+42)) {isfeast = 1; feast = "Ascension"}
else if (doy == (edayno+49)) {isfeast = 1; feast = "Pentecost"}
else if (doy == (edayno+56)) {isfeast = 1; feast = "the feast of the Most Holy Trinity"}
else if (doy == (edayno+63)) {isfeast = 1; feast = "Corpus Christi"}
else {isfeast = 0; feast = ""}

//alert('Isfeast= ' + isfeast + ' Day of Year = ' + doy + ' Easter Day= ' + edayno + ' Feast = ' + feast);

return [isfeast,feast];
}

//Program Proper

function Holydays(tst,dyTst,mnTst,yrTst,hrTst,minTst,secTst)

{

var d,dayofweek,wkday,day,mon,yr,today,dow;

var a,b,c,d,e,f,g,h,i,k,l,m;  //Easter calculation

var nyrwkday,epiwkday,epidayno,ashdayno,mthudayno,gfridayno,hsatdayno,edayno;

var ascdayno,whitdayno,ccdayno,XKdayno,advdayno,xmswkday,xmasdayno,nyreve;

var ashday,ashmon,ashmnth,eday,emon,emnth,ascday,ascmon,ascmnth,whitday,whitmon,whitmnth,XKd,XKm,advd,advmon,advmth; //day, month (number) & month name
var sName,sType,scol;
var isleap,nlpdays,nwkdys;

var wknum,wkdaynum;
var movedSolemnity = 0;  //=1 to denote that Holyday is on the nearest or next Sunday
var stday; //Saints day (=either 'today' or the day of the moved Solemnity
var suffix,st,dayName,dayColour,dayClass,count,sName,sType,sClass,sColour;
var grAnts = new Array("O Sapientia", "O Adonai","O Radix Jesse", "O Clavis David", "O Oriens", "O Rex Gentium", "O Emmanuel");
var mnths =  new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

var wkdays = new Array("Sunday", "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

var timeofyear = new Array("Christmas","Ordinary Time","Lent","Easter","Ordinary Time (after Pentecost)","Advent","Christmastide");

var xmastide = new Array("Christmas Day", "St Stephen", "St John","the Holy Innocents", "St Thomas Becket","","St Sylvester");
var vcolr = new Array("_gld","_wht","_red","_ros","_prp","_grn","_blk","_nul");
//var colr = new Array("_wht","_red","_wht","_red","_red","_wht","_wht");
var xtdcol = new Array("_gld","_red","_wht","_red","_red","_wht","_wht");
var xtdcls = new Array(0,2,1,2,2,1,1);
var imgsrc, litcol;

var msgtp, msglft;

//set the point at which the information will be placed

dayName = "";

msgtp = -80;

msglft = -140;

//Check whether we take today's date (or one entered by the user)

tst = parseInt(tst);

if(tst == 0)
{
//Get today's date (day,month, year)

d = new Date();
hh = d.getHours();
mm = d.getMinutes();
ss = d.getSeconds();
dayofweek = d.getDay(); //Day of the week (Sun = 0)
day = d.getDate();
mon= d.getMonth(); //Month Jan = 0
yr = d.getFullYear();
if(yr%4 == 0){isleap = 1} else {isleap =0}
today = DayofYear(mon,day,isleap);
dow = today%7 + 1;
}
else
{
day = parseInt(dyTst);
mon = parseInt(mnTst) - 1;
yr = parseInt(yrTst);
hh = parseInt(hrTst);
mm = parseInt(minTst);
ss = parseInt(secTst);
//get day of the year 
if(yr%4 == 0){isleap = 1} else {isleap =0}
today = DayofYear(mon,day,isleap);
}

//Find the weekday at the start of the year
//Number of leap days from 2004 until this year (1 Jan 2004 was Thursday dayofweek=4)
nlpdays = Math.floor((yr - 2004 - 1)/4)+1;
//Number of weekdays that the new year advances relative to 2004
nwkdys = yr - 2004 + nlpdays;
nyrwkday = nwkdys%7 + 4;
if (nyrwkday > 6){nyrwkday = nyrwkday - 7;}

//find day of week if tst = 1

if(tst != 0)
{
dow = today%7;
dayofweek = (today - 1)%7 + nyrwkday;
if(dayofweek > 6) {dayofweek = dayofweek - 7}
}

//alert('New Year Wk day = ' + nyrwkday + '  Today = ' + today +'(' + dow + ')');

wkday = wkdays[dayofweek];


//Calculate Date of Epiphany 

//First get the Day of week for Epiphany at 6th January)
epiwkday = nyrwkday + 5;
if (epiwkday > 6)
{
epiwkday = epiwkday - 7;
}

//If Epiphany falls on M,Tu,W bring back solemnity to the previous Sunday
//Otherwise push it forward to the following Sunday

if(epiwkday <= 3){epidayno = 6 - epiwkday} else {epidayno = 6 + (7 - epiwkday)}
epiwkday = 0;  //Day of epiphany now set to Sunday
movedSolemnity = 1;
daynoSol = 6;
//alert('Epi week day = ' + epiwkday + ' Epiday number = ' + epidayno);

//Get day of week for Christmas Day
xmswkday = nyrwkday + 1 + isleap;
if (xmswkday > 6)
{xmswkday = xmswkday - 7}


//Calculate day of year for 1st Sunday in Advent
//Christmas day number is always day 359 + isleap
xmasdayno = 359 + isleap;
nyreve = xmasdayno + 6;

if (xmswkday == 0)
{
//Track back 5 full weeks (35 days
advdayno = xmasdayno - 35;
}
else
{
//Track back to previous Sunday 4th Sunday in Advent, then a further 3 weeks
advdayno = xmasdayno - xmswkday - 21;
}

advday = GetDay(advdayno,isleap);
advmon = GetMonth(advdayno,isleap);
advmnth = mnths[advmon - 1];

//document.write("Christmas Day = " + xmswkday + " Daynumber " + xmasdayno);
//document.write("<br>");
//suffix = GetSuffix(advday);
//document.write("First Sunday Advent = " + advday + suffix + " of " + advmnth + " (Daynumber = " + advdayno + ")");
//document.write("<br>");

//Christ the King  Xk
XKdayno = advdayno - 7;
XKd = GetDay(XKdayno,isleap);
XKm = GetMonth(XKdayno,isleap);

//document.write("Christ the King = " + XKd + " of " + XKm + " Daynumber = " + XKdayno);
//document.write("<br>");

//Now calculate Easter and Ash Wednesday


a = yr % 19;
b = Math.floor(yr/100);
c = yr % 100;
d = Math.floor(b/4);
e = b % 4;
f = Math.floor((b+8)/25);
g = Math.floor((b-f+1)/3);
h = (19*a+ b - d - g + 15)%30;
i = Math.floor(c/4);
k = c % 4;
l = (32 + 2*e + 2*i - h - k) % 7
m = Math.floor((a + 11*h + 22*l)/451);
//This algorithm gives emon where Jan = 1
emon = Math.floor((h + l - 7*m + 114)/31);
eday = (h + l - 7*m + 114)% 31 + 1;

if (emon == 3)
 {edayno = 59 + eday + isleap}
else
 {edayno = 90 + eday + isleap}

//Calculate Holy Saturday, Good Friday and Maundy Thursday
hsatdayno = edayno -1;
gfridayno = edayno - 2;
mthudayno = edayno - 3;

//Calculate Ash Wednesday 6 weeks + 4 days before Easter Sunday
ashdayno = edayno - 46;

ashday = GetDay(ashdayno,isleap);
ashmon = GetMonth(ashdayno,isleap);

suffix = GetSuffix(ashday);

//document.writeln("Ash Wednesday is " + ashday + suffix + " of " + ashmon);
//document.write("<br>");
//document.writeln("Ash Wednesday is day number " + ashdayno + " in " + yr);
//document.write("<br>");

//Calculate Ascension Thursday, Pentecost, Trinity, Corpus Christi
//Ascension translated to the following Sunday by order of the Bishops of E&W
ascdayno = edayno + 42;

ascday = GetDay(ascdayno,isleap);
ascmon = GetMonth(ascdayno,isleap);

whitdayno = edayno + 49;
whitday = GetDay(whitdayno,isleap);
whitmon = GetMonth(whitdayno,isleap);

ccdayno = edayno + 60;
ccday = GetDay(ccdayno,isleap);
ccmon = GetMonth(ccdayno,isleap);

//document.write("Ascension (" + ascdayno + ") is " + ascday + " of " + ascmon);
//document.write("<br>");
//document.write("Pentecost (" + whitdayno + ") is " + whitday + " of " + whitmon);
//document.write("<br>");
//document.write("Corpus Christi (" + ccdayno + ") is " + ccday + " of " + ccmon);
//document.write("<br>");



//Stages of the Liturgical Year

//New Years Day                       1
//Epiphany                            epidayno
//Ash Wednesday                       shdayno
//Easter                              edayno
//Pentecost                           whitdayno
//1st Sunday in Advent                advdayno
//Christmas Day                       xmasdayno
//New Years Eve                       nyreve
//Today's Date                        today



//Number of Weeks After New Year

//First Sunday after Epiphany
fsundayno = epidayno + 7; //13 - epiwkday;

//Sunday before Ash Wednesday
lsundayno = ashdayno - 3;

//Number of weeks before Lent
nwkprelent = Math.floor((lsundayno - fsundayno)/7) + 1;

//Number of Weeks between Easter and Christ the King
nwkposteast = Math.floor((XKdayno - edayno)/7)+1;

//Set up Array 'stages'

var stages = new Array(1,epidayno,ashdayno,edayno,whitdayno,advdayno,xmasdayno,nyreve+1);

//alert('Epiphany is on January ' + epidayno);

//Find where in the liturgical year we are today

for (count = 1; count < 8; count++)
{
        if (today < stages[count])
        {st = count - 1; break}
}

//document.write("Stage = " + st + " Today = " + today);
//document.write("<br>");
// Allow for the turn of the year.
if (st == 6)
{
next = 0;
}
else
{
next = st + 1;
}

//Calculate the week number since the previous stage and weekday
weeknum = Math.floor((today - stages[st])/7) + 1;
wkdy = (today - stages[st])%7;



if (st == 0) //Week between New Year and Epiphany
{
        imgsrc = "images/chasuble_wht.bmp";
		    if (today == 1)
        {
          dayName = "";
		  dayColour = 0;
		  dayClass = 1;
        }
        else if (wkdy == 0)
        {
          dayName = "Second Sunday of Christmas";
		  dayColour = 1;
		  dayClass = 1;
        }
        else
        {
          dayName = wkdays[dayofweek] + ", Weekday of Christmas";
		  dayColour = 1;
		  dayClass = 1;
        }
}

else if (st == 1) //Weeks between Epiphany and Lent
{
        if (today == epidayno)
        {
          dayName = "Epiphany";
		  		dayColour = 0;
		  		dayClass = 0;
		  		imgsrc = "images/chasuble_wht.bmp";
		    }
        else if (today < fsundayno)
        {
          dayName = wkdays[wkdy] + " after the Epiphany";
		  		dayColour = 1;
		  		dayClass = 1;
		  		imgsrc = "images/chasuble_wht.bmp";
		    }
        else if (today == fsundayno)
        {
          dayName = "Baptism of the Lord";
		  		dayColour = 0;
		  		dayClass = 0;
		  		imgsrc = "images/chasuble_wht.bmp";
		    }
        else
        {
       	wknum = Math.floor((today - fsundayno)/7) + 1;
        suffix = GetSuffix(wknum);
        wkdy = (today - fsundayno)%7;
        wkday = wkdays[wkdy];
        dayName = wkday + " of the " + wknum + suffix + " week in Ordinary Time";
				dayColour = 5;
				dayClass = 5;
				imgsrc = "images/chasuble_grn.bmp";
		    }
}
else if (st == 2) //Weeks between Lent and Easter
{
        imgsrc = "images/chasuble_prp.bmp";
		    if (today == ashdayno)
        {
        dayName = "Ash Wednesday";
        dayColour = 4;
		dayClass = 0;
		}
        else if (today <= ashdayno +3)
        {
        wkdy = (3+ today - ashdayno); //is this really necessary?
        wkday = wkdays[wkdy];
        dayName = wkday + " after Ash Wednesday";
		dayColour = 4;
		dayClass = 4;
        }
        else if (today == (edayno - 7))
        {
        dayName = "Passion Sunday (Palm Sunday)";
		dayColour = 2;
		dayClass = 0;
		imgsrc = "images/chasuble_red.bmp";
		}
        else if (today >= (edayno - 7))
        {
			if (today < (edayno - 3))
			{
			dayColour = 4;
			dayClass = 0;
			wkday = wkdays[today - (edayno - 7)];
			dayName = wkday + " of Holy Week";
			}
			else
			{
			hrofday = hh + mm/60 + ss/3600;
			wkdayno = today - (edayno - 3);
			var ptide = new Array("Maundy Thursday","Good Friday","Holy Saturday"); 
			var ptype = new Array(1,2,7);
			if((wkdayno < 2)||((wkdayno = 2)&&(hrofday < 21)))
			{
			dayName = ptide[wkdayno];
			dayColour = ptype[wkdayno];
			dayClass= 0}
			else
			{
			dayName = "Easter Vigil";
			dayColour = 1;
			dayClass = 0;
			}				
			}
		}
        else //all the other days in Lent
        {
        lentwk = Math.floor((today - (ashdayno + 4))/7) + 1;
		wkdayno = (today - (ashdayno + 4))%7;
          if (wkdayno == 0)
		  {
			dayName = lentwk + GetSuffix(lentwk) + " Sunday in Lent";
			dayClass = 1;
			if(lentwk == 4)
				{
				imgsrc = "images/chasuble_ros.bmp";
				dayColour = 3;
				dayName = "4th Sunday in Lent (Laetare Sunday)";
				}
            else
				{
				dayColour = 4;
				}
		  }
          else
          {
			dayName = wkdays[wkdayno] + " of the " + lentwk + GetSuffix(lentwk) + " week in Lent";
			dayClass = 1;
			dayColour = 4;
          }
        }
}

else if (st == 3) //Weeks after Easter
{
	imgsrc = "images/chasuble_wht.bmp";
    if (today == edayno)
    {
		dayName = "Easter Sunday";
		dayClass = 0;
		dayColour = 1;
		imgsrc = "images/chasuble_wht.bmp";
		}
	else if (today < edayno + 7)
	{
		wkdayno = (today - edayno);
		dayName = wkdays[wkdayno] + " in Easter Week";
		dayClass = 1;
		dayColour = 1;
	}
    else if (today == ascdayno)
	{
      dayName = "Ascension Day";
	    dayClass = 0;
	    dayColour = 1;
    }
    else
    {
        wknum = Math.floor((today - edayno)/7) + 1;
		wkdayno = (today - edayno)%7;
		dayClass = 1;
		dayColour = 1;
        if (wkdayno == 0)
          {
          dayClass = 0;
		  dayName = wknum + GetSuffix(wknum) + " Sunday of Easter";
          }
          else
          {
		  dayName = wkdays[wkdayno] + " of the " + wknum + GetSuffix(wknum) + " week of Eastertide";
          }
	}
}

else if (st == 4) //weeks in Ordinary time (after Pentecost)
{
    if (today == whitdayno)
    {
		dayName = "Pentecost (Whit Sunday)";
		dayClass = 0;
		dayColour = 2;
		imgsrc = "images/chasuble_red.bmp";
		   }
	else if (today == whitdayno + 7)
	{
        dayName = "Trinity Sunday";
		dayClass = 0;
		dayColour = 1;
		imgsrc = "images/chasuble_wht.bmp";
	}

    else if (today == ccdayno + 3)
    {
        dayName = "Solemnity: - the Body and Blood of Christ";
		dayClass = 0;
		dayColour = 1;
        imgsrc = "images/chasuble_wht.bmp"
		
	}

    else if (today == XKdayno) //Christ the King
    {
        imgsrc = "images/chasuble_wht.bmp";
		    dayName = "Christ the King (the last Sunday of the year)";
		dayClass = 0;
		dayColour = 1;
	}
	else
	{
        imgsrc = "images/chasuble_grn.bmp";
		
		wknum = Math.floor((today - advdayno)/7) + 35;
        wkdayno = (today - edayno)%7;

          if (wkdayno == 0)
          {
            dayName = wknum + GetSuffix(wknum) + " Sunday in Ordinary Time";
			dayClass = 1;
		    dayColour = 5;
          }
          else
          {
            dayName = wkdays[wkdayno] + " of the " + wknum + GetSuffix(wknum) + " week in Ordinary Time";
			dayClass = 1;
		    dayColour = 5;
          }
	}
}

else if (st == 5) //weeks of Advent
{
  var grant = "";
	var igrant = xmasdayno - today;
	if((igrant> 1)&(igrant <= 8))
	{
	//Set Greater Antiphon of the day
	grant = " : " + grAnts[8-igrant];
	}
	
	if (today == (advdayno + 14))
  {
	imgsrc = "images/chasuble_ros.bmp";
	dayName = "3rd Sunday in Advent (Gaudete Sunday)";
	dayClass = 0;
	dayColour = 3;
	
  }
  else
  {
	imgsrc = "images/chasuble_prp.bmp";
	dayClass = 1;
	dayColour = 4;
	
	wknum = Math.floor((today - advdayno)/7) + 1 ;
	wkdayno = (today - advdayno)%7;
		if (wkdayno == 0)
        {
          dayName = wknum + GetSuffix(wknum) + " Sunday in Advent" + grant;
        }
		else
        {
          dayName = wkdays[wkdayno] + " of the " + wknum + GetSuffix(wknum) + " week in Advent" + grant;
        }
  }
} 

else if (st == 6) //Christmastide
{
	xdays = today - xmasdayno;  //Number of days after Christmas Day (=0)
    //Determine the day of the week
    xwkday = xmswkday + xdays;
		if (xwkday > 6)
        {
          xwkday = xwkday - 7;
        }
        
	//Determine the liturgical colour and class of feast
    if (xwkday == 0) // i.e. Sunday
	{
      litcol = "_wht";
			dayClass = 0;
		  dayColour = 0;	
      if (xdays == 0)
	    {
      dayName = "Christmas Day";		  
		  }
      else
		  {
		  dayName = "The Most Holy Family";			
		  }
	}
    else
	{
        if (xdays == 0)
		{
			dayName = "Christmas Day";
			dayClass = 0;
			dayColour = 0;
		}
		else
		{
			litcol = "_wht";
			dayClass = 1;
			dayColour = 1;
			dayName = "The " + wkdays[xwkday] + " of the Octave of the Nativity";
		}
	}
    imgsrc = "images/chasuble" + litcol + ".bmp";
}

//alert('Liturgical colour= ' + imgsrc);

if(movedSolemnity == 1){stday = daynoSol} else {stday = today}  // ?????

var saints = getSaintDay(today,isleap,dayofweek);
sName = saints[0];  //Name of Saints day (Literal value)
sType = saints[1]; //Type of Saints Day Solemnity, Feast, Memorial, Comm
sClass = saints[2]; //Class of Saints Day 0,1,2,4,10
sColour = saints[3];  //Liturgical Colour  W=1,R=2,B=6

// dayName = the liturgical name for the day of the day of the year
// dayClass = the classification of the day according to the liturgical year (integer)
// sClass = the class of saints day 0,1,2,4
// the lower value prevails
// dayColour sets the liturgical colour (0,1,2,3,4,..7)
// sClass = the classification of the feast day (if any)
// sColour the liturgical colour if the feast day class  prevails over the day class 


document.write("Today is the " + day + GetSuffix(day) + " of " + mnths[mon] + " " + yr);

//alert('Feast Type = ' + dayColour + '  sType = ' + sType + 'sClass = ' + sClass + ' Feast Class=' + sClass);

if(dayClass == 0)
{
document.write(", " + dayName);
litcol = vcolr[dayColour];
imgsrc = "images/chasuble" + litcol + ".bmp";
}
else
{
//Either a Saint's Day or Ordinary
document.write(", " + dayName);
document.write("<br>");
document.write(sType +" " + sName);
if(dayClass < sClass){litcol = vcolr[dayColour]} else {litcol = vcolr[sColour]}
//alert('dayClass=' + dayClass + ' sClass=  ' + sClass);
imgsrc = "images/chasuble" + litcol + ".bmp";
}

if(st == 3)
{
//document.write("<br>" + "Christ is risen! - He is risen indeed!");
}


//Display the vestment colour

if(dayColour != 7)    //Holy Saturday (dayColour =7) No Mass at all
{
	divopen = getDiv(msglft,msgtp,48,64);
	divclose = " </div>";
	
	var temp = '<img src=\"' +imgsrc + '\" alt=\"Litcol\">';
	//alert(temp);
	document.write(divopen + temp + divclose);
}

if ((today >= XKdayno) && (today < advdayno))
{
	document.write("<br>");
	document.write("This is the last week of the liturgical year");
}
else
{
    newlityr = today - advdayno;
    if ((newlityr < 7) && (newlityr >= 0))
    {
        document.write("<br>");
        document.write("This is the first week of the new liturgical year");
    }
}

}