// JavaScript Document

function daysInMonth(iMonth, iYear)
 {
     return 32 - new Date(iYear, iMonth, 32).getDate();
 }
 
function printMonthDay(month,year,myday,myrow,mybox)
{
yearNum=year;
monthNumber=month-1;
row=myrow;
box=mybox;
day=myday;

numDaysInMonth=daysInMonth(monthNumber,yearNum);

start=0;
end=0;

if(row==1){start=1; end=7;}
if(row==2){start=8; end=14;}
if(row==3){start=15; end=21;}
if(row==4){start=22; end=28;}
if(row==5){start=29; end=35;}
if(row==6){start=36; end=42;}

stopit=0;

  for( var i=start; i<=end; i++)
  {
	if(stopit==0)
	  {
	  
	  num=i;
	  if(i>box){num=i-7;}
	  thisDate=new Date(yearNum, monthNumber, num);

	  weekDay=thisDate.getDay();
	  if(weekDay==0){weekDay=7;}
	  
	  if( (num>0) && (num<=numDaysInMonth) )
	  {
	  if(day==weekDay){stopit=1; }else{ num="";}
	  }
	  else{ num="";}
	}
  }

return num;
}

var emptyCells='#ccc';
var cellsWithNum='#fff';
var currentDayCell='#f7c92b';

var hoverCells='#f7c92c';
var clickedCells='#f7c92d';

function initializeTable(xCoords,yCoords)
{
//month=document.getElementById("hiddenMonth").value;
//year=document.getElementById("hiddenYear").value;

var target=document.getElementById('apDiv1' );
YAHOO.util.Dom.setX(target,xCoords); 
YAHOO.util.Dom.setY(target,yCoords);
target.style.visibility="visible";  
refreshCalendar();

var currentTime = new Date();
var currentDay=currentTime.getDate();
var weekDay=currentTime.getDay();
var currentMonth=currentTime.getMonth() + 1;
var currentYear=currentTime.getFullYear();


	for(var i=1; i<=42; i++)
	{	
		var dayID="otherDay"+i;

		if(document.getElementById(dayID).innerHTML==currentDay)
		{document.getElementById(dayID).style.backgroundColor=currentDayCell;
		}
		
	}

}

function initializeTableNoPos()
{
//month=document.getElementById("hiddenMonth").value;
//year=document.getElementById("hiddenYear").value;
refreshCalendar();

var currentTime = new Date();
var currentDay=currentTime.getDate();
var weekDay=currentTime.getDay();
var currentMonth=currentTime.getMonth() + 1;
var currentYear=currentTime.getFullYear();


	for(var i=1; i<=42; i++)
	{	
		var dayID="otherDay"+i;

		if(document.getElementById(dayID).innerHTML==currentDay)
		{document.getElementById(dayID).style.backgroundColor=currentDayCell;
		}
		
	}

}

function refreshCalendar()
{
month=document.getElementById("hiddenMonth").value;
year=document.getElementById("hiddenYear").value;

var count=0;
	for(var i=1; i<=42; i++)
	{	
	count++;
		var dayID="otherDay"+i;
		if((i>=1) && (i<=7)){row=1;}
		if((i>=8) && (i<=14)){row=2;}
		if((i>=15) && (i<=21)){row=3;}
		if((i>=22) && (i<=28)){row=4;}
		if((i>=29) && (i<=35)){row=5;}
		if((i>=36) && (i<=42)){row=6;}
		if(i==8){count=1;}if(i==15){count=1;}if(i==22){count=1;}if(i==29){count=1;}if(i==36){count=1;}
		
		document.getElementById(dayID).innerHTML=printMonthDay(month,year,count,row,i);
		number=printMonthDay(month,year,count,row,i);
		number=number+"";
		len=number.length
		if(len==0){ document.getElementById(dayID).style.backgroundColor=emptyCells;}
		else{document.getElementById(dayID).style.backgroundColor=cellsWithNum; }
	}
		
}

function addMonth()
{
month=document.getElementById("hiddenMonth").value;
year=document.getElementById("hiddenYear").value;

month=(parseInt(month)+1);

if(month>12){month=1; year=(parseInt(year)+1);}

document.getElementById("hiddenMonth").value=month;
document.getElementById("hiddenYear").value=year;

document.getElementById("showMonth").innerHTML=monthName(month)+" "+year;
//document.getElementById("showDate").innerHTML="";

clearHilighted();
refreshCalendar();

}

function substractMonth ()
{
month=document.getElementById("hiddenMonth").value;
year=document.getElementById("hiddenYear").value;

month=(parseInt(month)-1);

if(month<1){month=12; year=(parseInt(year)-1);}
document.getElementById("hiddenMonth").value=month;
document.getElementById("hiddenYear").value=year;

document.getElementById("showMonth").innerHTML=monthName(month)+" "+year;	
//document.getElementById("showDate").innerHTML="";

clearHilighted();
refreshCalendar();

}

function clearHilighted()
{
	for(var i=1; i<42; i++)
	{
		var dayID="otherDay"+i;
		document.getElementById(dayID).style.backgroundColor=cellsWithNum;
	}
}






function hoverDay(id){
var dayID="otherDay"+id;
//alert(document.getElementById(dayID).style.backgroundColor );
//alert(document.getElementById(dayID).style.backgroundColor);
if(document.getElementById(dayID).style.backgroundColor !== emptyCells){document.getElementById(dayID).style.cursor="pointer";}
if(document.getElementById(dayID).style.backgroundColor === cellsWithNum){document.getElementById(dayID).style.backgroundColor=hoverCells;}
}

function hoverOut(id){
var dayID="otherDay"+id;

if(document.getElementById(dayID).style.backgroundColor === hoverCells){
document.getElementById(dayID).style.backgroundColor=cellsWithNum;}

}

function nameDay(weekDay)
{
var day_names = new Array ( );
day_names[0] = "Sunday";
day_names[1] = "Monday";
day_names[2] = "Tuesday";
day_names[3] = "Wednesday";
day_names[4] = "Thursday";
day_names[5] = "Friday";
day_names[6] = "Saturday";

return day_names[weekDay];

}

function monthName(monthNumber)
{
var month_names = new Array ( );
month_names[1] = "January";
month_names[2] = "February";
month_names[3] = "March";
month_names[4] = "April";
month_names[5] = "May";
month_names[6] = "June";
month_names[7] = "July";
month_names[8] = "August";
month_names[9] = "September";
month_names[10] = "October";
month_names[11] = "November";
month_names[12] = "December";

return month_names[monthNumber];

}


function loadSelectedMonth(selectedMonth)
{
document.getElementById("hiddenMonth").value=selectedMonth;	
year=document.getElementById("hiddenYear").value;
month=document.getElementById("hiddenMonth").value;

month=(parseInt(month));

document.getElementById("showMonth").innerHTML=monthName(month)+" "+year;
//document.getElementById("showDate").innerHTML="";

clearHilighted();
refreshCalendar();	
}

function loadSelectedYear(selectedYear)
{
document.getElementById("hiddenYear").value=selectedYear;	
year=document.getElementById("hiddenYear").value;
month=document.getElementById("hiddenMonth").value;

year=(parseInt(year));

document.getElementById("showMonth").innerHTML=monthName(month)+" "+year;
//document.getElementById("showDate").innerHTML="";

clearHilighted();
refreshCalendar();	
}


function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}


function loadInfo(myday,myrow,mybox)
 {
	 
var day=myday;
var row=myrow;
var box=mybox;
	
	var dayID="otherDay"+box;
	
	monthNumber=document.getElementById("hiddenMonth").value;
	yearNumber=document.getElementById("hiddenYear").value;
	
	 var dayNumber=printMonthDay(monthNumber,yearNumber,day,row,box);

	if(dayNumber!="")
	{
	document.getElementById("hiddenDay").value=dayNumber;
	clearHilighted();
	refreshCalendar();


	date=new Date(yearNumber, monthNumber, dayNumber);
	weekDay=date.getDay();

	document.getElementById(dayID).style.backgroundColor=clickedCells;
	var curMonthName=monthName(monthNumber+1);
	var curMonthInitials=curMonthName.charAt(0)+curMonthName.charAt(1)+curMonthName.charAt(2);

	//document.getElementById("showDate").innerHTML=nameDay(weekDay)+" "+dayNumber+" of "+curMonthInitials+" "+yearNumber;
	
	if(ReadCookie('favouriteDistrict')){criteria='&did='+ReadCookie('favouriteDistrict');}else{criteria='';}
	window.location.href='eventsbydate.php?d='+dayNumber+'&m='+(monthNumber+1)+'&y='+yearNumber+criteria;
	}

 }





<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//-->
