// for us with the King's Gate Church's Online Calendar

// Set the calendar Start Date
DayOfWeek = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
MonthsOfYear = new Array("Jan.","Feb.","Mar.","Apr.","May.","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec.");
MonthsInFull = new Array("January","February","March","April","May","June","July","August","September","October","November","December");


// Put in initial date remembering that day 1 = 1st and 0 month = january
var today = new Date()		
var first = new Date()
var datevar = new Date()		
var currentdayindex
var firstdayindex
var daysinmonth

// For testing future data
//today.setFullYear(2008);
//today.setMonth(9);
//today.setDate(27);

// First figure out the first day of the current month
first.setFullYear(today.getFullYear());
first.setMonth(today.getMonth());
first.setDate(1);
first.setHours(3);
first.setMinutes(0);
first.setSeconds(0);
first.setMilliseconds(0);

firstdayindex = first.getDay();

// Now calculate the number of days in the month
datevar.setTime(first.getTime());
datevar.setMonth(datevar.getMonth()+1);
datevar.setTime(datevar.getTime() - 86400000);
daysinmonth = datevar.getDate();

// Finally, set the first day as the first sunday before the first day of the month.
first.setTime(first.getTime() - (firstdayindex*86400000));
currentdayindex = Math.ceil((today.getTime() - first.getTime())/86400000);

// Initialise the calendar
function curDay() {
var dv = new Date()		
var i

// Put the month name at the top of the calendar
document.getElementById("MonthName").innerHTML = MonthsInFull[today.getMonth()]+" "+today.getFullYear();
dv.setTime(first.getTime());

//enter the dates into the calendar and whiten if not in this month
for (i=1;i<=42;i++){
document.getElementById("day"+i).innerHTML = dv.getDate(); 
if(i<=firstdayindex){document.getElementById("day"+i).style.color = "#fff";}
if(i>firstdayindex+daysinmonth){document.getElementById("day"+i).style.color = "#fff";}

dv.setTime(dv.getTime() + 86400000)
}

// Put current date on the top of the dayview
document.getElementById("day" + currentdayindex).style.background = "url(currentday.jpg) no-repeat";
document.getElementById("day" + currentdayindex).style.backgroundPosition = "top";
document.getElementById("day" + currentdayindex).style.color = "#fff";
chgDay(currentdayindex - 1);

}


function chgDay(daynumber) {
var suffix = "";
var newdate = new Date()
var daynum
var dayindex
var monthnum
var yearnum
var i

// Change to the correct Day & calculate the day of the month so as to get the correct suffix
newdate.setTime(first.getTime()+(86400000*daynumber));
daynum = newdate.getDate();
dayindex = newdate.getDay();
monthnum = newdate.getMonth();
yearnum = newdate.getFullYear();

if (daynum == 1 || daynum == 21 || daynum == 31){suffix = "st";}
else if (daynum == 2 || daynum == 22){suffix = "nd";}
else if (daynum == 3 || daynum == 23){suffix = "rd";}
else{suffix = "th";}

// Show this date at the top of the dayview section  
document.getElementById("dayname").innerHTML = DayOfWeek[dayindex]+", "+MonthsOfYear[monthnum]+" "+daynum+suffix+" "+yearnum;

// Now put the correct details in the dayview
// document.getElementById("daycontents0").innerHTML = DayContents[daynumber];
if(monthnum == 0){document.getElementById("daycontents0").innerHTML = January[daynum-1];}
else if(monthnum == 1){document.getElementById("daycontents0").innerHTML = February[daynum-1];}
else if(monthnum == 2){document.getElementById("daycontents0").innerHTML = March[daynum-1];}
else if(monthnum == 3){document.getElementById("daycontents0").innerHTML = April[daynum-1];}
else if(monthnum == 4){document.getElementById("daycontents0").innerHTML = May[daynum-1];}
else if(monthnum == 5){document.getElementById("daycontents0").innerHTML = June[daynum-1];}
else if(monthnum == 6){document.getElementById("daycontents0").innerHTML = July[daynum-1];}
else if(monthnum == 7){document.getElementById("daycontents0").innerHTML = August[daynum-1];}
else if(monthnum == 8){document.getElementById("daycontents0").innerHTML = September[daynum-1];}
else if(monthnum == 9){document.getElementById("daycontents0").innerHTML = October[daynum-1];}
else if(monthnum == 10){document.getElementById("daycontents0").innerHTML = November[daynum-1];}
else {document.getElementById("daycontents0").innerHTML = December[daynum-1];}
}