//Internet Explorer?
isIE = (document.all) ? true:false;

// Function to allow replacement of target="_blank" with XHTML compliant rel="external"
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

// Function to allow replacement of target="_blank" with XHTML compliant rel="external" in form tags
function externalLinksForms() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("form");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

//Function to style abbr tags in IE
function styleAbbr() {
  var oldBodyText, newBodyText, reg
  if (isIE) {
    oldBodyText = document.body.innerHTML;
    reg = /<ABBR([^>]*)>([^<]*)<\/ABBR>/g;
    //newBodyText = oldBodyText.replace(reg, '<ABBR $1><SPAN class=\"abbr\" $1>$2</SPAN></ABBR>');
    //document.body.innerHTML = newBodyText;
    document.body.innerHTML.replace(reg, '<ABBR $1><SPAN class=\"abbr\" $1>$2</SPAN></ABBR>')
  }
}

window.onload = function() {
  externalLinks();
  externalLinksForms();
  styleAbbr();
  new BannerRotator('mainimages', 5000, 1000);
  //fixHeight();
}

// Date change function for Calendar view of Meetings
function calendarDateChange() {
	document.location='calendar.asp?year=' + calendar.year.options[calendar.year.selectedIndex].value + '&month=' + calendar.month.options[calendar.month.selectedIndex].value;
}

function normalpage() {
	strURL = new String(window.location)
	window.location = strURL.substr(0, strURL.length - 6);
}

function strReplace(str1, str2, str3) {
  while(str1.indexOf(str2) != -1) {
   str1 = str1.replace(str2, str3);
 }
  return str1;
}

function printPage()
{
	if (window.print)
	{
        	window.print();
        }
	else
	{
                alert("Sorry, your browser doesn't support this feature.\n" +
                      "Please use your browser's print button");
        }
}

function copyAddress()
{

document.forms['mainform'].c_add_1.value=document.forms['mainform'].add_1.value;
document.forms['mainform'].c_add_2.value=document.forms['mainform'].add_2.value;
document.forms['mainform'].c_add_3.value=document.forms['mainform'].add_3.value;
document.forms['mainform'].c_add_4.value=document.forms['mainform'].add_4.value;
document.forms['mainform'].c_post_code.value=document.forms['mainform'].post_code.value;

}

function fixHeight()
{
	if(document.getElementById)
	{
		var newHeight = 0;

		var left = document.getElementById("left").offsetHeight;
		var centre = document.getElementById("centre").offsetHeight;

		if(left > centre)
			newHeight = left;
		else
			newHeight = centre;

		newHeight = newHeight + "px";

		document.getElementById("left").style.height = newHeight;
		document.getElementById("centre").style.height = newHeight;
	}
	return true;
}


// Copied from intranet_admin.js as needed for the website
function websiteAddLoadEvent(func)
{
	var oldonload = window.onload;

	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}


/**********************************************/
// Image Gallery Functions
/**********************************************/

var playTime;
var blnSlideshowRunning = false;
var strControlPlay = '<a href="#" onclick="playGallerySlideshow(this.parentNode);return false;"><img src="/system/images/gallery/gallery-play.png" width="16" height="16" alt="Play" /> Play</a>';
var strControlStop = '<a href="#" onclick="stopGallerySlideshow(this.parentNode);return false;"><img src="/system/images/gallery/gallery-stop.png" width="16" height="16" alt="Stop" /> Stop</a>';

function showGalleryImage(intImageNumber)
{
	if(!document.getElementById || !document.getElementById('gallery-tb' + intImageNumber))
		return false;

	document.getElementById('gallery-mainimage').src = document.getElementById('gallery-imgsrc' + intImageNumber).value;
	document.getElementById('gallery-mainimage').alt = document.getElementById('gallery-imgtitle' + intImageNumber).value;
	document.getElementById('gallery-imagetitle').innerHTML = document.getElementById('gallery-imgtitle' + intImageNumber).value;
	document.getElementById('gallery-currentimage').value = intImageNumber;

	thumbnailImages = document.getElementById('gallery-thumbnails').getElementsByTagName('DIV');
	for(x = 0; x < thumbnailImages.length; x++)
	{
		if(thumbnailImages.item(x).className.indexOf('tbselected') > 0)
			thumbnailImages.item(x).className = 'gallery-tbimage';
	}
	document.getElementById('gallery-tb' + intImageNumber).className = 'gallery-tbimage gallery-tbselected';
	stopGallerySlideshow();

	return true;
}

function navigateGallery(intImageNumber, strDirection)
{
	if(intImageNumber == 0)
	{
		if(strDirection == 'next')
		{
			intImageNumber = (parseInt(document.getElementById('gallery-currentimage').value) + 1);
			intTotalNumberOfImages = parseInt(document.getElementById('gallery-totalimages').value);
			if(intImageNumber > intTotalNumberOfImages)
				intImageNumber = intTotalNumberOfImages;
		}
		else
		{
			intImageNumber = (parseInt(document.getElementById('gallery-currentimage').value) - 1);
			if(intImageNumber <= 0)
				intImageNumber = 1;
		}
	}

	if(!document.getElementById || !document.getElementById('gallery-tb' + intImageNumber))
	{
		if(document.getElementById('gallery-' + strDirection + 'page'))
			window.location = document.getElementById('gallery-' + strDirection + 'page').value + (blnSlideshowRunning ? '&autostart=1' : '');
		return true;
	}
	return showGalleryImage(intImageNumber);
}

function playGallerySlideshow(elemControl)
{
	if(blnSlideshowRunning)
		navigateGallery(0, 'next');

	blnSlideshowRunning = true;
	playTime = setTimeout('playGallerySlideshow()', 2000);

	if(elemControl)
		elemControl.innerHTML = strControlStop;
}

function stopGallerySlideshow(elemControl)
{
	blnSlideshowRunning = false;
	clearTimeout(playTime);

	if(elemControl)
		elemControl.innerHTML = strControlPlay;
}

function displayGallerySlideshowControls(intAutoStart)
{
	if(intAutoStart == 1)
		websiteAddLoadEvent(playGallerySlideshow);

	document.write('<span id="gallery-controls">')
	if(!blnSlideshowRunning)
		document.write(strControlPlay);
	else
		document.write(strControlStop);
	document.write('</span>')
}

/**************************************/
/* Shows and hides a series of banner images from an HTML unordered list by slowing fading them in and out */
/**************************************/
var arrBannerProperties = new Array();

function BannerRotator(szId, iRotatingSpeed, iFadingSpeed)
{
	iIndex = arrBannerProperties.length;

	this.index = iIndex;
	this.id = szId;
	this.list = new Array();
	this.images = new Array();
	this.rotatingSpeed = iRotatingSpeed;
	this.fadingSpeed = iFadingSpeed;

	arrBannerProperties[iIndex] = this;

	initialiseBannerRotation(this);
}

function initialiseBannerRotation(oBannerProperties)
{
	if(!document.getElementById || !document.getElementById(oBannerProperties.id))
	{
		return false;
	}

	var arrBannerList = document.getElementById(oBannerProperties.id).getElementsByTagName('LI');
	var arrBannerImages = document.getElementById(oBannerProperties.id).getElementsByTagName('IMG');

	if(arrBannerList.length != arrBannerImages.length)
	{
		alert('Error: Mismatch of banner images');
		return false;
	}

	for(iBannerCount = 0; iBannerCount < arrBannerList.length; iBannerCount++)
	{
		arrBannerList.item(iBannerCount).id = oBannerProperties.id + '_' + iBannerCount;
		arrBannerList.item(iBannerCount).style.display = (iBannerCount == 0 ? 'block' : 'none');
	}


	// Set the properties of the properties object
	oBannerProperties.list = arrBannerList;
	oBannerProperties.images = arrBannerImages;

	setTimeout("rotateBanner(0, " + oBannerProperties.index + ")", oBannerProperties.rotatingSpeed);
	preloadBannerImage(1, oBannerProperties);
}

function rotateBanner(iBannerIndex, iBannerPropertiesIndex)
{
	var oBannerProperties = arrBannerProperties[iBannerPropertiesIndex];
	var iNextBannerIndex = (iBannerIndex + 1);

	if(iNextBannerIndex >= oBannerProperties.list.length)
	{
		iNextBannerIndex = 0;
	}

	fadeBanner(oBannerProperties.list.item(iBannerIndex).id, 'out', oBannerProperties.fadingSpeed);
	fadeBanner(oBannerProperties.list.item(iNextBannerIndex).id, 'in', oBannerProperties.fadingSpeed);

	setTimeout("rotateBanner(" + iNextBannerIndex + ", " + oBannerProperties.index + ")", oBannerProperties.rotatingSpeed);
	preloadBannerImage(iNextBannerIndex + 1, oBannerProperties);
}

function preloadBannerImage(iBannerIndex, oBannerProperties)
{
	if(iBannerIndex >= oBannerProperties.images.length)
	{
		return false;
	}

	var oBannerImage = oBannerProperties.images.item(iBannerIndex);

	if(!oBannerImage.longDesc)
	{
		return false;
	}

	if(oBannerImage.longDesc.length > 0 && oBannerImage.src != oBannerImage.longDesc)
	{
		oBannerImage.src = oBannerImage.longDesc;
		oBannerImage.longDesc = oBannerImage.src;
	}
}

function fadeBanner(szId, szFadeDirection, iMilliseconds)
{
	var iSpeed = Math.round(iMilliseconds / 100);
	var iTimer = 0;
	var iOpacityStart = 0;
	var iOpacityEnd = 100;

	if(szFadeDirection == 'out')
	{
		var iOpacityStart = 100;
		var iOpacityEnd = 0;
	}

	if(iOpacityStart > iOpacityEnd)
	{
		// Fade out
		for(var iCount = iOpacityStart; iCount >= iOpacityEnd; iCount--)
		{
			setTimeout("changeBannerOpacity(" + iCount + ",'" + szId + "', '" + (iCount == iOpacityStart ? 'fadeout' : (iCount == 0 ? 'hiding' : '')) + "')", (iTimer * iSpeed));
			iTimer++;
		}
	}
	else if(iOpacityStart < iOpacityEnd)
	{
		// Fade in
		for(var iCount = iOpacityStart; iCount <= iOpacityEnd; iCount++)
		{
			setTimeout("changeBannerOpacity(" + iCount + ",'" + szId + "', '" + (iCount == 0 ? 'fadein' : (iCount == iOpacityEnd ? 'showing' : '')) + "')", (iTimer * iSpeed));
			iTimer++;
		}
	}
}

function changeBannerOpacity(iOpacity, szId, szDisplay)
{
	var oElement = document.getElementById(szId);
	var oElementStyle = oElement.style;

	oElementStyle.opacity = (iOpacity / 100);
	oElementStyle.MozOpacity = (iOpacity / 100);
	oElementStyle.KhtmlOpacity = (iOpacity / 100);
	oElementStyle.filter = "alpha(opacity=" + iOpacity + ")";

	if(szDisplay == 'fadein')
	{
		oElementStyle.display = 'block';
		oElementStyle.width = oElement.offsetWidth + 'px';
		oElementStyle.position = 'absolute';
	}
	else if(szDisplay == 'fadeout')
	{
		oElementStyle.width = oElement.offsetWidth + 'px';
		oElementStyle.position = 'absolute';
	}
	else if(szDisplay == 'showing')
	{
		oElementStyle.position = 'static';
		oElementStyle.width = '100%';
	}
	else if(szDisplay == 'hiding')
	{
		oElementStyle.position = 'static';
		oElementStyle.width = '100%';
		oElementStyle.display = 'none';
	}
}