jsAddEvent(window, "load", jsLoaded, false);


//***************************************//
//** Function to kick off the required **//
//** functions when the page loads     **//
//***************************************//
function jsLoaded() {
	// Break the Email address in half...
	jsBreakEmail();
	// Un-hide the "boxaccessibility" (only shown if Javascript's enabled)...
	var reg = new RegExp("\\bboxaccessibility\\b");
	var divs = document.getElementsByTagName("div");
	for (var i = 0; i < divs.length; i++) {
		var div = divs.item(i);
		if (reg.test(div.className)) {
			div.style.display = "block";
		}
	}
	// Add the ISI_CONTENT class to all anchors in the temp_container's centre-column
	// NOTE: this is done for ReadSpeaker's benefit...
	//jsReadLinks();
}


//********************************************//
//** Cross-Browser function to add an event **//
//** handler to an event-generating element **//
//********************************************//
function jsAddEvent(element, type, fn, useCapture) {
	if (element.addEventListener) {
		// DOM Level 2 Event Model implementation...
		element.addEventListener(type, fn, useCapture);
		return true;
	} else if (element.attachEvent) {
		// Non-standard IE Hack...
		return element.attachEvent('on' + type, fn);
	} else {
		// Catch-all last resort...
		var fnOnload = element['on' + type];
		if (typeof(fnOnload) == "function") {
			element['on' + type] = function() {
				fnOnload();
				fn();
			}
		} else element['on' + type] = fn;
	}
}


//****************************************************************//
//** Script to read through each anchor in the centre column    **//
//** and update it's class to include ReadSpeaker's ISI_CONTENT **//
//** class (forcing it to be read with the content)             **//
//****************************************************************//
function jsReadLinks() {
	if (document.getElementById) {
		// Get the DIVs within temp_container...
		var colDIV = document.getElementById("temp_container").getElementsByTagName("div");
		// Look for the centre-column div...
		var divCC = null;
		var objReg = new RegExp("\\bcc\\b")
		for (var i = 0; i < colDIV.length; i++) {
			if (objReg.test(colDIV.item(i).className)) {
				divCC = colDIV.item(i);
				break;
			}
		}
		// Mark up each of the anchors within the centre-column...
		if (divCC != null) {
			var elA = null;
			var colA = divCC.getElementsByTagName("a");
			for (var i = 0; i < colA.length; i++) {
				var name = "ISI_CONTENT";
				elA = colA.item(i);
				if (elA.className.length > 0) name = " " + name;
				elA.className = elA.className + name;
			}
		}
	}
}


//******************************************************************//
//** Function to look up the email address in the Contact table   **//
//** (always in the third row, second cell of the table) and      **//
//** split it across two lines at the "@"                         **//
//******************************************************************//
function jsBreakEmail() {
	if (document.getElementById) {
		var elTemp = document.getElementById("main");
		if (elTemp != null) {
			colDivs = elTemp.getElementsByTagName("div");
			for (var i = 0; i < colDivs.length; i++) {
				var elDiv = colDivs.item(i);
				// Do we have a "boxcontact" item..?
				if (elDiv.className.indexOf("boxcontact") > -1) {
					// Get all the Anchors within the "boxcontact" div...
					var colAnchors = elDiv.getElementsByTagName("a");
					for (var j = 0; j < colAnchors.length; j++) {
						jsRecurseAnchorChildren(colAnchors.item(j));
					}
				}
			}
		}
	}
}


//********************************************************************//
//** Function to recurse through the children of an anchor, looking **//
//** for an email address.  Note that the textNode could be buried  **//
//** within multiple layers of divs or spans.                       **//
//********************************************************************//
function jsRecurseAnchorChildren(el) {
	if (el.nodeType == "3") {
		// It's a text node...
		var pos = el.nodeValue.indexOf("@");
		if (pos > 0) {
			el = el.splitText(pos);
			el.parentNode.insertBefore(document.createElement("br"), el);
			return true;
		}
	} else {
		// Recurse through this element's childNodes...
		for (var i = 0; i < el.childNodes.length; i++) {
			// If an email address has been found, ripple back up the call-stack...
			if (jsRecurseAnchorChildren(el.childNodes.item(i))) return true;
		}
	}
	return false;
}


//*********************************************//
//** Function to resize the text on the page **//
//*********************************************//
function jsTextResize(mode) {
	var cookieName  = "pccTextSize"
	var sizeDefault = 8;
	var sizeNow, sizeNew;
	
	if (document.getElementsByTagName) {
		// Get the necessary nodes...
		var elHTML = document.getElementsByTagName("html")[0];
		var elBody = document.getElementsByTagName("body")[0];
		// Get the current text size...
		sizeNow = parseInt(jsTextSize(elBody));
		if (!sizeNow) sizeNow = sizeDefault;
		// Process according to the specified mode...
		switch (mode) {
			case "small":
				sizeNew = sizeNow - 1;
				break;
			case "large":
				sizeNew = sizeNow + 1;
				break;
			case "reset":
				// Reset the size to the default
				sizeNew = sizeDefault;
				break;
			default:
				// Called onLoad to set the value to the cookie value...
				try {sizeNew = parseInt(jsCookieRead(cookieName));}
				catch (e) {alert(e);}
				if (!sizeNew || isNaN(sizeNew)) sizeNew = sizeDefault;
				break;
		}
		// Set the Font Sizes...
		var sizeNewString = sizeNew + "pt";
		elHTML.style.fontSize = sizeNewString;
		elBody.style.fontSize = sizeNewString;
		//Write the Cookie to store the value...
		jsCookieWrite(cookieName, sizeNew, 365);
	}
}


//**********************************************//
//** Function to return the current font size **//
//**********************************************//
function jsTextSize(el) {
	var size = 0;
	if (el.style && el.style.fontSize) size = el.style.fontSize;
	else if (typeof(getComputedStyle) != 'undefined') size = getComputedStyle(el, '').getPropertyValue('font-size');
	else if (el.currentStyle) size = el.currentStyle.fontSize;
	// May return "XX" or "XXpt", so trim the alpha-characters...
	if (isNaN(size)) {
		for (var end = size.length - 1; end >= 0; end--) {
			if (!isNaN(size.substr(end, 1))) break;
		}
		size = size.substr(0, ++end);
	}
	return (isNaN(size) ? null : size);
}


//************************************************//
//** Function to write a new client-side Cookie **//
//************************************************//
function jsCookieWrite(name, value, daysToExpiry) {
	if (name && value) {
		var cookie = name + "=" + value + ";path=/;";//domain=.plymouth.gov.uk;";
		if (daysToExpiry) {
			if (isNaN(daysToExpiry)) days = 365;
			var expires = new Date((new Date()).getTime() + (daysToExpiry * 24 * 60 * 60 * 1000));
			cookie += "expires=" + expires.toGMTString() + ";";
		}
		document.cookie = cookie;
	}
}


//*******************************************//
//** Function to read a client-side Cookie **//
//*******************************************//
function jsCookieRead(name) {
	var value = null;
	var reg = new RegExp("\\b" + name + "\\b");
	var cookies = document.cookie.split(";");
	for(var i = 0; i < cookies.length; i++) {
		var cookie = cookies[i].split("=");
		if (reg.test(cookie[0])) {
			value = cookie[1];
			break;
		}
	}
	return value;
}


//**************************************//
//** Function to open an outlook form **//
//**************************************//
function OpenOutlookDoc(whatform) 
{
   var nameSpace = null;
   var mailFolder = null; 
   var mailItem = null;
   var tempDoc = null; 
   var outlookApp = null;
      try 
      { 
      	outlookApp = new ActiveXObject("Outlook.Application"); 
      	nameSpace = outlookApp.getNameSpace("MAPI"); 
      	mailFolder = nameSpace.getDefaultFolder(6); 
      	mailItem = mailFolder.Items.add(whatform); 
     	mailItem.Display(0)
      } 
      catch(e) 
      { 
      	document.write(e);
      } 
}

//*************************************************************//
//** Function to add double quotes around the catalog search **//
//** added 10 11 2010 KMT                                    **//
//*************************************************************//
function addQuotes()
{
var strQuote="%22";

var txt=form.getElementById("criteriaField").value;

var txt= '"' + txt + '"';
//document.write(txt);
//document.forms.["calmSearch"].submit();
//document.submit("%22"+txt);
}

