//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();
	
	// Initialise draggable DIV from /edit.js
	if(document && document.getElementById("editdiv"))
		initDragDiv();
	
  //fixHeight();
  TreeToggle();
}

// 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.\nPlease use your browser's print button");

	return false;
}


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;
}

function printPageXHTML()
{
	document.write('<a href="#" onclick="printPage(); return false;">print page</a> | ')
}

//**************************************************************
// This function clears a given text box if it contains specific 
// text. This is useful for search or username boxes where if 
// the user places the cursor in the box, text such as 
// 'search...' or 'username...' should disappear, but not text 
// that the user has typed
//
// Usage : <input type="text" value="search..." onfocus="clearSingleLineEditText(this, 'search...');" />
//**************************************************************
function clearSingleLineEditText(theSingleLineEdit, textToClear)
{
	if (theSingleLineEdit.value == textToClear)
	{
		theSingleLineEdit.value = '';
	}
}

/**************************************/
/* Overloaded onload function         */
/**************************************/

function addExternalLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	} 
	else 
	{
		window.onload = function()
						{
							oldonload();
							func();
						}
	}
}// ends addExternalLoadEvent()

/**************************************/
/* Tree Toggle Control                */
/**************************************/
function TreeToggle()
{
	if(!document.getElementById || !document.getElementById('tree'))
		return false;

	var elemToggleNodes;
	elemToggleNodes = document.getElementById('tree').getElementsByTagName('A');

	for(elemToggle = 0; elemToggle < elemToggleNodes.length; elemToggle++)
	{
		if(elemToggleNodes.item(elemToggle).className.indexOf('toggle') == 0)
		{
			elemToggleNodes.item(elemToggle).removeAttribute('href');
			elemToggleNodes.item(elemToggle).onclick = function()
			{
				var strNodeClass = '';

				if(this.className.indexOf('plus') > 0)
				{
					this.className = 'toggle minus';
					strNodeClass = 'open';
				}
				else if(this.className.indexOf('minus') > 0)
				{
					this.className = 'toggle plus';
					strNodeClass = '';
				}

				if(this.parentNode.nextSibling)
				{
					elemTargetNode = this.parentNode.nextSibling;

					if(elemTargetNode.nodeName == 'UL')
						elemTargetNode.className = strNodeClass;
					else if(elemTargetNode.nextSibling && elemTargetNode.nextSibling.nodeName == 'UL')
						elemTargetNode.nextSibling.className = strNodeClass;
				}
			}
		}
	}
}

/**********************************************/
// 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>')
}

