function Initialise(PageId)
{
	document.getElementById(PageId).className="activelink";
  ProcessContacts();
  TrapAnchorClick();
  Nifty("div#VerseOfTheDay","VerseOfTheDay");
  LogPageHit(PageId);
}

function SetSrc(oDiv)
{
	var imgs = document.getElementById(oDiv).getElementsByTagName("img");
  for(i = 0; i < imgs.length; i++)
    {
	    if((imgs[i].src == ""))
	    {
		    // Gallery images have their src stored in the name attribute
		    imgs[i].src = imgs[i].name
      }
    }
}

function FullImage(ThisImage)
{
	ThisImage = ThisImage.replace("/Thumbnails/","/FullSize/");
	// Remove the img unique identifier that was added by the DelayLoads routine
	ThisImage = ThisImage.substring(0, ThisImage.lastIndexOf(imgUniquePrefix));
  popup = window.open('ImagePopup.html?i=' + ThisImage,'Fullsize','toolbar=no,location=no,directories=no,menuBar=no,scrollbars=yes,status=yes,resizable=no');
  popup.document.focus;
}

function ProcessContacts()
{
  tag = document.getElementsByClassName("Phone");
  for(i in tag)
  {
    tag[i].title = "Telephone number";
  }

  tag = document.getElementsByClassName("Email");
  for(i in tag)
  {
    tag[i].title = "Email address";
  }
}

function xlat(ext, dom, acc)
{
  document.write("<a href='mai" + "lto:" + acc + "&#64;" + dom + ext + "' class='Email'> Email</a>");
}

document.getElementsByClassName = function(cl)
{
  var retnode = [];
  var myclass = new RegExp('\\b *'+cl+' *\\b');
  var elem = this.getElementsByTagName('*');
  for (var i = 0; i < elem.length; i++)
  {
    var classes = elem[i].className;
    if (myclass.test(classes)) retnode.push(elem[i]);
  }
  return retnode;
};

function TrapAnchorClick()
{
  anc = document.getElementsByTagName("a");
  for(i in anc)
  {
    Ext = GetFileExtension(anc[i].href).toLowerCase();
    switch(Ext)
    {
      case 'html' :
      case 'htm' :
      case 'shtml' :
      case 'asp' :
      case 'php' :
      DocType = "webpage";
      break;

      case 'ppt' :
      DocType = "PowerPoint file";
      break;

      case 'doc' :
      DocType = "Word file";
      break;

      case 'xls' :
      DocType = "Excel file";
      break;

      case 'pdf' :
      DocType = "PDF file";
      break;

      case 'swf' :
      DocType = "Flash file";
      break;

      case 'dcr' :
      DocType = "Shockwave file";
      break;

      case 'jnlp' :
      case 'jar' :
      DocType = "Java file";
      break;

      case 'vbs' :
      case 'js' :
      DocType = "script file";
      break;

      case 'zip' :
      DocType = "Zip file";
      break;

      case 'bmp' :
      case 'gif' :
      case 'jpg' :
      case 'jpeg' :
      DocType = 'image file';
      break;

      case 'wav' :
      DocType = 'sound file';
      break;

      case 'avi' :
      case 'mov' :
      case 'wmv' :
      case 'mpg' :
      DocType = 'movie file';
      break;

      default :
      DocType = "page";
    }
    switch (anc[i].className)
    {
      case 'ToggleClosed' :
        anc[i].title = "Show details";
        break;

      case 'Offsite' :
        anc[i].className = "Offsite " + Ext;
        anc[i].title = "Off-site " + DocType;
        break;

      case 'Onsite' :
        anc[i].className = "Onsite " + Ext;
        anc[i].title = "On-site " + DocType;
    }
  }
}

function GetFileExtension(url)
{
  if (!url) {return "";}
  dot = url.lastIndexOf(".");
  if ( dot == -1 ) {return "";}
  return url.substr(dot + 1, url.length);
}

function AnchorClick()
{
  BigWindow();
}

function SizeAndPosition()
{
  window.moveTo(0, 0);
  window.resizeTo(screen.availWidth * 0.99, screen.availHeight * 0.98);
}

function ToggleDiv(ID,ThisAnc)
{
  oDiv = document.getElementById(ID);
  switch(oDiv.style.display)
  {
  case '' :
    //oDiv.style.display = 'block';
    $("div#" + ID).show("slow");
    writeSessionCookie("div"+ID, "open");
    ThisAnc.className="ToggleOpen";
    ThisAnc.title="Hide details";
    break;
  case 'none' :
    //oDiv.style.display = 'block';
    $("div#" + ID).show("slow");
    writeSessionCookie("div"+ID, "open");
    ThisAnc.className="ToggleOpen";
    ThisAnc.title="Hide details";
    break;
  case 'block' :
    //oDiv.style.display = 'none';
    $("div#" + ID).hide("slow");
    writeSessionCookie("div"+ID, "closed");
    ThisAnc.className="ToggleClosed";
    ThisAnc.title="Show details";
    break;
  }
}

function RestoreDivState()
{
  div = document.getElementsByTagName("div");
  for(i in div) {
    if (div[i].className == "ToggleDiv") {
      if (getCookieValue(div[i].id) == "open" )
      {
        ToggleDiv(div[i].id);
      }
    }
  }
}

function SpecialFormats(FormatNumber)
{
	cp = document.getElementById('CentralPanel');
  cpa = cp.getElementsByTagName("a")

	switch(FormatNumber)
	{
    case 0 :
    //Restore defaults
    cp.style.background="";
    cp.style.color="";

    for(i=0;i<cpa.length;i++)
    {
	    cpa[i].style.color="";
    }
    break;

		case 1 :
    cp.style.background="#111111";
    cp.style.color="#cccccc";

    //Brighten color of anchor text
    for(i=0;i<cpa.length;i++)
    {
	    cpa[i].style.color="#0000ff";
    }

    cp.style.background="url(Images/Stars.gif)"
    break;
  }
}

function LogPageHit(ThisPage)
{
	var params;
	params = "dbWrite.php";
	params += "?db=stthomas_pagelogs";
	params += "&tbl=pages";
	params += "&fld=PageHit";
	params += "&val=" + ThisPage;
	document.getElementById("phpLink").src=params;
}

function CountSundays(start, end)
//WARNING: Date objects are passed by reference!
{
	for(var count=0; start < end; start.setDate(start.getDate() + 1))
	{
		if(start.getDay() == 0)
		{
			count++;
		}
	}
	return count;
}

function CurrentPrayer(Destination)
{
	var d2 = new Date();
	var d1 = new Date("1 Jan " + d2.getFullYear());
	sc = CountSundays(d1, d2);
	sc = Math.max(1,sc);
	sc = Math.min(52,sc);
	ReadPrayerCycle(sc,Destination);
}

function AllPrayers(Destination)
{
	ReadPrayerCycle(0,Destination);
}

function ReadPrayerCycle(WeekNumber,Destination)
{
	var WhereClause = "";
  if(WeekNumber>0)
  {
	  WhereClause=" WHERE WeekNumber = " + WeekNumber
  }
	var sql = "SELECT WeekNumber,PrayerText FROM prayercycle" + WhereClause;
  $.get("dbReadTable.php", {db:'stthomas_misc', headList:'Week,Topics', attributeList:"align='center',align='left'", sql:sql},
    function(data)
    {
      $("#"+Destination).html(data);
    }
  );
}

function ReadCollects(Destination)
{
	var d = new Date();
	Sunday = ToMySqlDateFormat(ThisWeek(d.valueOf(),0));
	Saturday = ToMySqlDateFormat(ThisWeek(d.valueOf(),6));

	var sql = "SELECT DATE_FORMAT(FestivalDate,'%b %e') AS MD,Festival,Collect FROM collects WHERE FestivalDate BETWEEN '" + Sunday + "' AND '" + Saturday + "' ORDER BY FestivalDate";
  $.get("dbReadTable.php", {db:'stthomas_misc', headList:'', attributeList:"width='8%',width='22%',class='BCP DropCapital'", sql:sql},
    function(data)
    {
      $("#"+Destination).html(data);
    }
  );
}

function ThisWeek(ThisDate,DayRequired)
//Returns a date representing the DayRequired within the current week
//A week runs from Sunday to Saturday
//ThisDate: The date for which the information is required
//DayRequired: Sun=0, Sat=1, etc
//WARNING: Date objects are passed by reference. To avoid changes feeding back, ThisDate is expected as valueOf
{
	var dt = new Date(ThisDate);
	var dy = dt.getDay();
	if (dy != DayRequired)
	{
		dt.setDate(dt.getDate() + DayRequired - dy);
	}
	return dt;
}

function ToMySqlDateFormat(d)
{
  return d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
}

function writeSessionCookie (cookieName, cookieValue)
{
  if (testSessionCookie())
  {
    document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/";
    return true;
  }
  else
  {
    return false;
  }
}

function getCookieValue (cookieName)
{
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";"))
  {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else
  {
    return false;
  }
}

function testSessionCookie ()
{
  document.cookie ="testSessionCookie=Enabled";
  if (getCookieValue ("testSessionCookie")=="Enabled")
  {
    return true;
  }
  else
  {
    return false;
  }
}

function MakeRequest(args,destination)
{
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlHttp,destination);
    }
  };

  xmlHttp.open("GET", args, true);
  xmlHttp.send(null);
}

function getXMLHttp()
{
  var xmlHttp;

  try
  {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  return xmlHttp;
}

function HandleResponse(xmlHttp,destination)
{
	if(xmlHttp.statusText == "OK")
	{
		document.getElementById(destination).innerHTML = xmlHttp.responseText;
	}
	else
	{
		if(location.host > "")
		{
			document.getElementById(destination).innerHTML = xmlHttp.responseText
		}
		else
		{
			document.getElementById(destination).innerHTML = "Must use localhost to see MySql data.";
		}
	}
}

