// Common Scripts
// version 1.0
// created by: Robert Gilks
// date: 31/8/00
// modified:

var loadCommon = true;

// determine object model
var isDom = document.getElementById ? true:false;
var isNN4 = document.layers ? true:false;
var isIE = document.all ? true:false;

var browserVersion = parseFloat(navigator.appVersion);
var isOpera = (navigator.userAgent.indexOf("Opera") != -1);
var isMac = (navigator.appVersion.indexOf("Mac") != -1);
var isNetscape = (navigator.appName.indexOf("Netscape") != -1);


// Client-side Browser Check
var agt = navigator.userAgent.toLowerCase();
var version = parseFloat(navigator.appVersion);
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3 = (is_ie && (version < 4));
var is_ie4 = (is_ie && (version == 4) && (agt.indexOf("msie 5")==-1 && agt.indexOf("msie 6")==-1) );
var is_ie4up = (is_ie && (version >= 4));
var is_ie5 = (is_ie && (version == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5 = (is_ie && (version == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie5up = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
var is_nav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
              && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
              && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_mac = (agt.indexOf("mac")!=-1);				
var is_nav4_5up = (is_nav && (version >= 4.5));
var is_ie4up  = (is_ie && (version >= 4));   


// test if regular expressions are supported
var regExpSupported = false;
if (window.RegExp) {
	var tempStr = "a";
	var tempReg = new RegExp(tempStr);
	if (tempReg.test(tempStr)) regExpSupported = true;
}

// Detect if Macromedia Shockwave Flash version 5 is present on the client.
var flashPlugin = false;
var flash5Installed = false;
var flash6Installed  = false;
var flash7Installed  = false;

if (navigator.plugins && navigator.plugins.length > 0) {
	if (navigator.plugins["Shockwave Flash"]) {
    	flashPlugin = true;	
	   	var words = navigator.plugins["Shockwave Flash"].description.split(" ");	
	   	for (var i = 0; i < words.length; ++i) {
			if (isNaN(parseInt(words[i]))) continue;
			if (words[i] == '5.0') flash5Installed = true;
			if (words[i] == '6.0') flash6Installed = true;
			if (words[i] == '7.0') flash7Installed = true;
			//this.revision = parseInt(words[i + 1].substring(1));
	   	}
		
	}
} else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0
	&& (navigator.userAgent.indexOf("Windows 95")>=0
		|| navigator.userAgent.indexOf("Windows 98")>=0
			|| navigator.userAgent.indexOf("Windows NT")>=0)) {
				document.write('<scr' + 'ipt language=VBScript\> \n');
				document.write('on error resume next \n');
				document.write('flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5")))\n');
				document.write('flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6")))\n');
				document.write('flash7Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.7")))\n');
				document.write('</scr' + 'ipt\> \n');
}

if (flash7Installed) {
	flash6Installed = true; //currently nothing requiring just flash 7
}

flashPlugin = flash5Installed || flash6Installed;


// set the cookie in order to do server-side redirects dependant on flash plugin
document.cookie = "flashPlugin = " + flashPlugin + "; flash6Installed = " + flash6Installed;
//to delete cookie during testing
//var exp = new Date();  
//exp.setTime (exp.getTime() - 1);
//document.cookie = "flashPlugin = true; expires=" + exp.toGMTString();

// Deal with Netscape 4 page reload bug.
var winX = null;
var winY = null;
if (isNN4) {
	winX = window.innerWidth;
	winY = window.innerHeight;
}

// Form headers and footers
var balloonHeader;
var balloonFooter;
if (pageColour != "") {
	balloonHeader = '<table border="0" cellspacing="0" cellpadding="0"><tr><td><img src="'+imgPath+'/'+pageColour+'/rbTopLeft.gif" width="13" height="13"/></td><td background="'+imgPath+'/'+pageColour+'/rbTop.gif"><img src="'+imgPath+'/common/spacer.gif" width="1" height="13"/></td><td><img src="'+imgPath+'/'+pageColour+'/rbTopRight.gif" width="13" height="13"/></td></tr><tr><td height="100%" background="'+imgPath+'/'+pageColour+'/rbLeft.gif"><img src="'+imgPath+'/common/spacer.gif" width="13" height="1"/></td><td class="balloon">';
	balloonFooter = '</td><td height="100%" background="'+imgPath+'/'+pageColour+'/rbRight.gif"><img src="'+imgPath+'/common/spacer.gif" width="13" height="1"/></td></tr><tr><td><img src="'+imgPath+'/'+pageColour+'/rbBotLeft.gif" width="13" height="13"/></td><td background="'+imgPath+'/'+pageColour+'/rbBot.gif"><img src="'+imgPath+'/common/spacer.gif" width="1" height="13"/></td><td><img src="'+imgPath+'/'+pageColour+'/rbBotRight.gif" width="13" height="13"/></td></tr></table>';
} else {
	balloonHeader = '<table border="0" cellspacing="0" cellpadding="0" class="balloonBox"><tr><td class="balloon">';
	balloonFooter = '</td></tr></table>';
}
/*
if (isNN4) {
	var balloonHeader = '<table border="0" cellspacing="0" cellpadding="0"><tr><td colspan="3"><img src="'+imgPath+'/'+pageColour+'/rbTopLeft.gif" width="13" height="13"/></td><td><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td><img src="'+imgPath+'/common/spacer.gif" width="1" height="1"/></td></tr><tr><td class="thinBotRow"><img src="'+imgPath+'/common/spacer.gif" width="1" height="1"/></td></tr><tr><td class="botNav"><img src="'+imgPath+'/common/spacer.gif" width="1" height="11"/></td></tr></table></td><td colspan="3"><img src="'+imgPath+'/'+pageColour+'/rbTopRight.gif" width="13" height="13"/></td></tr><tr><td height="100%"><img src="'+imgPath+'/common/spacer.gif" width="1" height="1"/></td><td class="thinBotRow" height="100%"><img src="'+imgPath+'/common/spacer.gif" width="1" height="1"/></td><td class="botNav" height="100%"><img src="'+imgPath+'/common/spacer.gif" width="11" height="1"/></td><td class="balloon">';
	var balloonFooter = '</td><td class="botNav" height="100%"><img src="'+imgPath+'/common/spacer.gif" width="11" height="1"/></td><td class="thinBotRow" height="100%"><img src="'+imgPath+'/common/spacer.gif" width="1" height="1"/></td><td height="100%"><img src="'+imgPath+'/common/spacer.gif" width="1" height="1"/></td></tr><tr><td colspan="3"><img src="'+imgPath+'/'+pageColour+'/rbBotLeft.gif" width="13" height="13"/></td><td><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td class="botNav"><img src="'+imgPath+'/common/spacer.gif" width="1" height="11"/></td></tr><tr><td class="thinBotRow"><img src="'+imgPath+'/common/spacer.gif" width="1" height="1"/></td></tr><tr><td><img src="'+imgPath+'/common/spacer.gif" width="1" height="1"/></td></tr></table></td><td colspan="3"><img src="'+imgPath+'/'+pageColour+'/rbBotRight.gif" width="13" height="13"/></td></tr></table>';
}
*/
/*
Non-round edges
if (isNN4) {
	balloonHeader = '<table border="0" cellspacing="0" cellpadding="13"><tr><td class="balloon">';
	balloonFooter = '</td></tr></table>';
}
*/
var formHeader = balloonHeader;
var formFooter = balloonFooter+'<br><br>';


function nn4Resize() {
	if (isNN4 && (winX != window.innerWidth || winY != window.innerHeight)) {
		history.go(0);
	}
}

// Open a window of the desired size in the centre of the screen.
function openWindow(target_file, width, height, hasScrollBars, showLocation) {
	// ADD NAME FIELD and make sure it get's focus!!!
	var theWidth = width;
	var theHeight = height;
	var scrollBars = "scrollbars";
	if (showLocation == false || showLocation == null) showLocation = 0;
	if (hasScrollBars == false) scrollBars = "scrollbars=0";
	if ((theWidth == "")||(theWidth == null)) theWidth = 740;
	if ((theHeight == "")||(theHeight == null)) theHeight = 550;
	var theLeft = (screen.availWidth - theWidth)/2;
	var theTop = (screen.availHeight - theHeight)/2; 
	var popupWin = window.open(target_file, '_' + Math.round(Math.random() * 1000000), 'top='+theTop+',left='+theLeft+',menubar=0,toolbar=0,location='+showLocation+',directories=0,status=0,'+scrollBars+',resizable,width='+theWidth+',height='+theHeight);
}
function closepopup(){ if(false == my_window.closed){ my_window.close (); }else {    alert('Window already closed!'); }}
// return an element on the page given the ID
// no support for netscape 4
function getElement(elementId) {
	var el;					
	if (document.all) {
		el = document.all[elementId];
	} else {
		el = document.getElementById(elementId);
	}
	return el;
}

// Test that a string could be a valid e-mail address 
function isEmail(str) {
	if (regExpSupported) {
		var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{2,4})(\\]?)$");
		return (!r1.test(str) && r2.test(str));
	} else {
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	}
}

function isValidSubdomain(str) {
	if (regExpSupported) {
		var r = new RegExp("[^a-zA-Z-]+"); // only letters and a - character allowed
		return (!r.test(str));
	} else {
		return (str.indexOf("&") == -1);
	}
}



function checkValidEmail(theForm, fieldName, message) {		
	if (isNN4) {
		if (eval('document.layers["pageBody"].'+theForm+'.'+fieldName+'.value') == "") {
			// no email address is valid
			return true;
		}
		if (!isEmail(eval('document.layers["pageBody"].'+theForm+'.'+fieldName+'.value')))	{
			alert(message)
			eval('document.layers["pageBody"].'+theForm+'.'+fieldName+'.focus()');
			return false;
		}
	} else {
	if (eval(theForm+'.'+fieldName+'.value') == "") {
		// no email address is valid
		return true;
	}
	if (!isEmail(eval(theForm+'.'+fieldName+'.value')))	{
		alert(message)
		eval(theForm+'.'+fieldName+'.focus()');
		return false;
	}
	}
	return true;
}

function checkMatch(theForm, firstFieldName, secondFieldName, message) {
	if (isNN4) {
		if (eval('document.layers["pageBody"].'+theForm+'.'+firstFieldName+'.value != '+'document.layers["pageBody"].'+theForm+'.'+secondFieldName+'.value')) {
			alert(message); 
			eval('document.layers["pageBody"].'+theForm+'.'+secondFieldName+'.value = ""');
			eval('document.layers["pageBody"].'+theForm+'.'+secondFieldName+'.focus()');
			return false;
		}
	} else {
		if (eval(theForm+'.'+firstFieldName+'.value != '+theForm+'.'+secondFieldName+'.value')) {
			alert(message); 
			eval(theForm+'.'+secondFieldName+'.value = ""');
			eval(theForm+'.'+secondFieldName+'.focus()');
			return false;
		}
	}
	return true;
}


function checkValidUniqueUrl(theForm, fieldName, message) {		
	if (isNN4) {
		if (eval('document.layers["pageBody"].'+theForm+'.'+fieldName+'.value') == "") {
			// no URL is valid
			return true;
		}
		if (!isValidSubdomain(eval('document.layers["pageBody"].'+theForm+'.'+fieldName+'.value')))	{
			alert(message)
			eval('document.layers["pageBody"].'+theForm+'.'+fieldName+'.focus()');
			return false;
		}
	} else {
		if (eval(theForm+'.'+fieldName+'.value') == "") {
			// no URL is valid
			return true;
		}
		if (!isValidSubdomain(eval(theForm+'.'+fieldName+'.value')))	{
			alert(message)
			eval(theForm+'.'+fieldName+'.focus()');
			return false;
		}
	}
	return true;
}

// Test that the fields specified in theArr (nested array)
// are filled in theForm ( reference to a HTML form within a document )
// if not filled pop-up an alert and focus on the unfilled field.
// var theArr = new Array( new Array('FIELD NAME','message'), new Array('FIELD NAME','message')...);
function checkFieldsFilled(theForm, theArr) {
	for (var i in theArr) {			
		if (typeof(theArr[i]) == 'object') {
			if (isNN4) {
				if (eval('document.layers["pageBody"].'+theForm+'.'+theArr[i][0]+'.value == ""')){
					alert(theArr[i][1]); 
					if (theArr[i][2] != "noFocus") eval('document.layers["pageBody"].'+theForm+'.'+theArr[i][0]+'.focus()');
					return false;
				}
			} else {
			if (eval(theForm+'.'+theArr[i][0]+'.value == ""')){
				alert(theArr[i][1]); 
				if (theArr[i][2] != "noFocus") eval(theForm+'.'+theArr[i][0]+'.focus()');
				return false;
			}
			}
			checkFieldsFilled(theArr[i]);					
		}									
	}
	return true;					
}

// Checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy)
function checkDate (theForm, theDate) {

	var today = new Date();	
	
	if (isNN4) {
		year = eval('document.layers["pageBody"].'+theForm+'.'+theDate[2][0]+'.value');
		month = eval('document.layers["pageBody"].'+theForm+'.'+theDate[1][0]+'.value');
		day = eval('document.layers["pageBody"].'+theForm+'.'+theDate[0][0]+'.value');
	} else {
	year = eval(theForm+'.'+theDate[2][0]+'.value');
	month = eval(theForm+'.'+theDate[1][0]+'.value');
	day = eval(theForm+'.'+theDate[0][0]+'.value');
	}	
	
	if (day == '' && month == '' && year == '') return true;
	
	month = month - 1
	// First check than they fields are within reasonable ranges
	if ((day < 1) || (day >  31)) { dateAlert(theDate, theForm, '0'); return false; }		
	if ((month < 0) || (month > 11)) { dateAlert(theDate, theForm, '1'); return false; }	
	if ((year < 1850)) { dateAlert(theDate, theForm, '2');	return false; }
    
	var test = new Date(year,month,day);
	
	// Now check that the date actually exists
	if (day != test.getDate()) { dateAlert(theDate, theForm, '0'); return false; }	
	if (month != test.getMonth()) {	dateAlert(theDate, theForm, '1'); return false; }	    
	if (year != y2k(test.getYear())) { dateAlert(theDate, theForm, '2'); return false; }
	return true
}

function dateAlert(theDate, theForm, field) {
	alert(theDate[field][1]);
	if (isNN4) {
		eval('document.layers["pageBody"].'+theForm+'.'+theDate[field][0]+'.focus()');
	} else {
	eval(theForm+'.'+theDate[field][0]+'.focus()');
}
}

function y2k(number) {
	return (number < 1000) ? number + 1900 : number;
}

//This function replaces double quotes with single quotes.
//To be used for field that reqire confirmboxes to put up on other pages
// eg when deleteing a document/advert/user. If not used may cause javascript error!
function rplQuote(theForm, theField){
	var objField;
	if (isNN4) {
		objField = eval('document.layers["pageBody"].'+theForm + "." + theField);
	} else {
		objField = eval(theForm + "." + theField);
	}
	var tempString = objField.value
	while (tempString.indexOf('"')!= -1){
		tempString = tempString.replace('"',"'");
	}
	objField.value = tempString
}

//function used when remove button for uploaded images if clicked
//	1) creates comma seperated list of image files to be deleted.
//	2) sets hidden field to zero length string
//	3) Sets Img.src to spacer.gif

var commaSpacer = ""
function removeImage(objFileName,objDeleteList,objImage, strPath){
	objDeleteList.value = objDeleteList.value + commaSpacer + objFileName.value;
	objFileName.value = "";
	objImage.src = strPath + "/images/common/spacer.gif";
	commaSpacer = ","
}

var bolCheckedAll = false;

function checkAll(theForm,strCBoxName,theButton){
	if (bolCheckedAll){
		for (i = 0;i<theForm.elements[strCBoxName].length;i++){
			theForm.elements[strCBoxName][i].checked = false;
		}
		theButton.value ="Check All";
		bolCheckedAll = false;
	}
	else {
		for (i = 0;i<theForm.elements[strCBoxName].length;i++){
			theForm.elements[strCBoxName][i].checked = true;
		}
		theButton.value ="Clear All";
		bolCheckedAll = true;
	}
}

// change the values that a chart displays
// works in conjunction with charts.xsl include
function setChart(movieId, chartType, label, colour, args) {
	if (flashPlugin == true) {
		
		var str = "";
		str = '<object id="'+movieId+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="500" height="180">';
		str += '<param name="movie" value="/interfaces/flash/'+chartType+'/'+chartType+'.swf?title='+label+'&amp;colour='+colour+'&amp;args='+args+'"/>';
		str += '<param name="quality" value="high"/>';
		str += '<param name="bgcolor" value="#FFFFFF"/>';
		str += '<embed src="/interfaces/flash/'+chartType+'/'+chartType+'.swf?title='+label+'&amp;colour='+colour+'&amp;args='+args+'" quality="high" bgcolor="#FFFFFF" swliveconnect="true" name="+movieId+" width="500" height="180" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"/>';
		str += '</object>';
		
		if (isIE) {
			if (isDom) { // IE 5.5 +
				var ref = document.getElementById(movieId);
				ref.SetVariable("args", args);
				ref.SetVariable("colour", colour);
				ref.SetVariable("title", label);
				ref.Rewind();
				ref.Play();
			} else { // Other IE
				var ref = document.all[movieId+"Div"];
				ref.innerHTML = str;
			}
			return;
		} else if (isDom) { // Netscape 6
			var ref = document.getElementById(movieId+"Div");
			ref.innerHTML = str;
			return;	
		}
	} else {
		openWindow("/flash/getFlash.asp?feature=Interactive%20Charts", "400", "200");
	}
}

// Allow ony certain types of information to be entered into text fields. 
// <input type="text" class="inputText" name="OfficeTel" onKeyUp="allowOnly('telephone',this)" onBlur="allowOnly('telephone',this)">
function allowOnly(type, field) {
	if (regExpSupported) {
		if (type == 'telephone') {
			// to ensure only numbers, spaces and -'s can be put into a text field
			field.value = field.value.replace(/[^0-9 -]/g, '');
			field.value = field.value.replace(/  /g, ' ');
			field.value = field.value.replace(/--/g, '-');
			field.value = field.value.replace(/ -/g, ' ');
			field.value = field.value.replace(/- /g, '-');
			return;
		}
		if (type == 'integer') {
			field.value = field.value.replace(/[^0-9]/g, '');
			return;
		}
		if (type == 'decimal') {
			field.value = field.value.replace(/[^0-9\.]/g, '');
			field.value = field.value.replace(/\.\./g, '.');			
			return;
		}
	}		
}

var intClickCnt = 1;
function doClickNoneditable(theButton){
	intClickCnt++
	if (intClickCnt >= 3){
		alert("Please click the '" + theButton.value + "' button to edit this field.");
		intClickCnt=0;
		theButton.focus()
	}
}

//Function to be overwritten on individual pages
function after_Onload(){}

//Called from popup onload events
function focus_Onload(){
	focus();
	after_Onload();
}


// The following function is used on the menu to show hidden items.
// It requires 2 arrays to have been created
// menuIds[0] is an array of all the top level menu items.
// menuIds[1] is an array of all the second level menu items.
function showHideChildren() {
// first argument to function is the parent id, then the children ids	
	var linkRef, ref;
	
	// hide all the second level menu items
	for ( i = 1 ; i < menuIds[0].length ; i++ ) {		
		getElement(menuIds[0][i]).style.display = 'none';
	}
	
	// switch all the indicators that show '-' to '+' except for the item just clicked
	for ( i = 2 ; i < menuIds[1].length ; i+=2 ) {
		if (menuIds[1][i+1] == '1' && menuIds[1][i] != arguments[0]) {
			ref = getElement(menuIds[1][i]+"Indicator");
			if (ref.innerHTML == "-") {
				ref.innerHTML = '+';
			}
		}
	}
	
	// switch the indicator of the item just clicked, if it changes to a '-' draw the menu items
	// beneath it.
	ref = getElement(arguments[0]+"Indicator");
	if (ref.innerHTML == "-") {
		ref.innerHTML = '+';
	} else {
		ref.innerHTML = '-';
		for (var i = 1 ; i < arguments.length ; i++ ) {
			ref = getElement(arguments[i]).style.display = 'block';
			/*
			linkRef = getElement(arguments[i]+"Link");
			if ( arguments[i] == "menuItem"+pageId ) {
				linkRef.style.className = "a.activeLinkLevel1";				
			}
			*/
		}
	}
}

// Switches the hints on and off by adding a setting to the query string
function switchHints(setting) {		
	if (String(location).indexOf("?") != -1) { // check if query string exists
		if (String(location).indexOf("showHints=false") != -1) {
			if (setting == 'false') return;
			location = String(location).replace(/showHints=false/i,"showHints=true");
			return;
		}
		if (String(location).indexOf("showHints=true") != -1) {
			if (setting == 'true') return;
			location = String(location).replace(/showHints=true/i,"showHints=false");
			return;
		}		
		location += '&showHints=' + setting;
		return; 	
	} else {
		location += '?showHints=' + setting;
	}
}

// Used to print email links without the address being explicit on the page for webcrawlers to find.
// e.g <script type="text/javascript">printMailLink("totaltennis", "lta.org.uk", "totaltennis.net helpdesk", "Error");</script>
function printMailLink(mailbox, domain, text, subject) {
	document.write("<a href=\"mailto");
	document.write(":" + mailbox + "@");
	document.write(domain + "?subject=" + subject + "\">");
	document.write(text + "<\/a>");
}

//Used to display confirmation box and then lodaing window if true
//pass in parameters for confirmation box text, the location of the window and the width and height
//to close the window you will need to refer to the my_window variable
//as my_window.close()
function confirmAndLoadPage(conftext, confwindow, confWindowWidth, confWindowHeight)
{
	if(confirm(conftext))
	{
		my_window = window.open(confwindow,'statuswin','toolbar=no,menubar=no,scrollbars=yes,resizable=yes,status=yes,width='+confWindowWidth+',height='+confWindowHeight)
		return true;
	}
	else
	{
	return false;
	}
}