/*myscripts.js version 1.0*/

/*
/This function is used to check whether the month title shown at the top of the page
/corresponds to the current month as determined from the date
//It works by taking a parameter containing the month title string
//and searching for the first three letters of the month name (held in the m_names array)
//in the month title in turn. It will return the month number (0-11) or -1 if the month name 
//does not contain one of the abbreviated names
*/


function re_direct()
{
	target_base="http://www.calvaryprestatyn.org.uk"; //change this to put it in the calvary website
	//define an array of month ids

	month_id = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
            
	//define an array of year ids

	year_id = new Array("09","10","11","12","13");
	year_offset=2009;			
        
	//get the current month index and the current year index from the date
	//note that we need to subtract the year_offset from the
	//getFullYear to allow for the array starting at "09"

	now= new Date();
	month_no=now.getMonth();
	year_no=now.getFullYear() - year_offset;

	//alert(year_no);// debug
	
    	//now construct the url from the target_base
	//the subdirectory, the year_id indexed by year_no
	//and the month_id indexed by month_no
		        			
	target_href=target_base + "/calendar/";
	target_href=target_href + year_id[year_no] + month_id[month_no];
	target_href=target_href +".html";
        //document.write(document.lastModified);
        //alert("Redirecting: "+target_href);
                                
        window.location.href= target_href;
}


function check_month(m_str)
{

	m_names=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	//alert(m_str); //testing purposes
	m_number=-1;

	for(i=0; i<=11; i++)
	{
		if (m_str.search(m_names[i])>=0)
		{
		m_number=i;
		}
	}

	//alert(m_number); //testing purposes
  	return m_number; //0 to 11 or -1
}


/* 'loading_complete'
  This function is called when everything is loaded and
  it then reads the title of the page from the text box
  and compares the title with the current month
  If it is it determines the day of the month and highlights the appropiate
  cell  
*/


function loading_complete()
{
	//alert("Loading Complete");

/*get the current date in the variable now to identify the current month
  and then copy this date to first_of_month
  set the variable first_of_month to be the first day of the month
  using setDate and then use the getDay method to retrieve the
  number of the first day of the month and store it in first_day_of_month
*/ 

now=new Date();
//alert(now); //debug
first_of_month=now;
first_of_month.setDate(1);
first_day_of_month=first_of_month.getDay();

/*set up a table of row and cell names used to construct
  the html reference to the current day's cell. table_id is used
  to generate the first part of the reference which
  must be the same on all calendar pages 
*/

table_ID="Cal_";

cell_id = new Array(
	"R01C01", "R02C01", "R03C01", "R04C01", "R05C01", "R06C01", "R07C01",
	"R01C02", "R02C02", "R03C02", "R04C02", "R05C02", "R06C02", "R07C02",
	"R01C03", "R02C03", "R03C03", "R04C03", "R05C03", "R06C03", "R07C03",
	"R01C04", "R02C04", "R03C04", "R04C04", "R05C04", "R06C04", "R07C04",
	"R01C05", "R02C05", "R03C05", "R04C05", "R05C05", "R06C05", "R07C05",
	"R01C06", "R02C06", "R03C06", "R04C06", "R05C06", "R06C06", "R07C06"
	);

skip_column_one=7; //column one contains the labels for the days of the week

/* determine the current day number (1 to 31)
*/
today=new Date();
d=today.getDate();

/*now generate the cell reference from the table of id's (table_ID + cell_id)*/
cell_ref=table_ID+cell_id[d+skip_column_one+first_day_of_month-1];



//get the name of the month from the text box at the top of the page

mytext=document.getElementById("txt_month").innerHTML;

//evaluate the month number from the contents of text box 'txt_month'
m_number=check_month(mytext); //call to function defined earlier

//get current month from the real time date
m_id=today.getMonth();

//if the current month and the page match highlight the current date
if (m_id===m_number)
	{//alert("test condition true");
	document.getElementById(cell_ref).style.backgroundColor="#ffff80";
	}

//output today's date
str_today=today.toString(); 
document.getElementById("box_today").innerHTML=str_today.substring(0,24);// just the date and time parts


//output the latest update info
str_updated=document.lastModified;
//now change the date into uk ddmmyy format
str_updated_ukformat=str_updated.substring(3,5)+"/"
	+str_updated.substring(0,2)+"/"
	+str_updated.substring(8,10);
document.getElementById("box_update").innerHTML="Page last updated on: "+str_updated_ukformat;


//alert("End"); //debug



}


