jQuery.noConflict();
jQuery(document).ready(function($){
	// assign listeners tracking downloads etc
	if (typeof(pageTracker) == "object") 
	{
		$('a[href]').click(function(){
			var linksto = this.href;
			if (linksto.indexOf('mailto:')==0)
			{
				pageTracker._trackPageview('/mailto/'+$(this).attr('href').substring(7));	
			}
			else if (linksto.match(/\.(pdf|doc|zip|ppt|exe|xls|docx)+$/))
			{
				pageTracker._trackPageview('/download/'+$(this).attr('href'));
			}
			else if (linksto.match(/\.(avi|mov|wmv|flv|f4v|mp4)+$/))
			{
				pageTracker._trackPageview('/video/'+$(this).attr('href'));
			}
			else if (linksto.match(/\.(mp3|wav|aac|wmf)+$/))
			{
				pageTracker._trackPageview('/audio/'+$(this).attr('href'));
			}
			else if (!linksto.match(document.domain))
			{
				pageTracker._trackPageview('/exit/'+$(this).attr('href'));
			}
		});
	}

});

/* ============= john's Expand and collapse tree type menu [v1.1] ============= */
function ExpandMenu(iCat) {
	var oElement, oTitleElement;
	if (document.getElementById('cat_' + iCat)) {
		oElement = document.getElementById('cat_' + iCat);
		oTitleElement = document.getElementById('cattitle_' + iCat);
		if (oElement.style.display.length) {
			oElement.style.display = '';
			oTitleElement.style.backgroundImage='url(images/global/nav-arrow-expand.gif)';
		} else {
			oElement.style.display = 'none';
			oTitleElement.style.backgroundImage='url(images/global/nav-arrow.gif)';
		}
	}
}


/* ============= john's optional modal popup window code [v1.3] ============= */
function popupWindow(sUrl, iWidth, iHeight) {
	if (arguments.length>=4) {
		// zero based array so 3 not 4!
		bModal = arguments[3];
	}
	else if (sUrl.indexOf('google')>0) {
		// google maps doesn't like modal on IE
		bModal = false;
	}
	else {
		// default to modal...
		bModal = true;		
	}

	if (window.showModalDialog && bModal) {
		iWidth = iWidth + 8;
		iHeight = iHeight + 48;
		sWindowFeatures = "dialogTop:10px;dialogLeft:10px;dialogWidth:" + iWidth + "px;dialogHeight:" + iHeight + "px";
		oWindowReference = window.showModalDialog(sUrl, window, sWindowFeatures);
	}
	else {
		if (document.all) {
			iWidth = iWidth + 40;
			iHeight = iHeight + 20;
		}
		else {
			iWidth = iWidth + 20;
			iHeight = iHeight + 20;
		}
		sWindowName = 'popupwindow';
		sWindowFeatures = "left=10,top=10,height=" + iHeight + ",width=" + iWidth + ",location=no,menubar=no,directories=no,toolbar=no,status=yes,titlebar =yes,resizable=yes,scrollbars=yes";
		if (bModal) {
			sWindowFeatures = sWindowFeatures + "dependent=yes.modal=yes,dialog=yes,minimizable=no,alwaysRaised=yes";
		}
		if (window.oWindowReference) {
			try {
				oWindowReference.close();
			}
			catch(e) {
				// handle silently...	
			}
		}
		oWindowReference = window.open(sUrl, sWindowName, sWindowFeatures);
	}
	return false;
}


/*
-------------------------------------------------------- 
-------------------------------------------------------- 
Form validation
-------------------------------------------------------- 
-------------------------------------------------------- 
*/

/*
-------------------------------------------------------- 
RequiredFields [v2.2]
-------------------------------------------------------- 
*/
function RequiredFields(oForm) {
	var bCompleted = true;
	var bIsEmailAddress = true;
	var oField;
	var oEmailRegExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
	var bChecked = false;
	var bRequired = false;
	
	// loop through all form fields...
	for (var i=0; i<oForm.elements.length; i++) {
		if (oForm.elements[i].id) {
			// DOM 2
			oField = document.getElementById(oForm.elements[i].id);
		}
		else {
			// DOM 1
			oField = oForm.elements[i];
		}
		
		if (oField!=undefined && oField!=null && oField.id.length) {		
			// check if the field is required...
			bRequired = Array.find(aRequired, oField.name)!=-1;
			
			if (bRequired) {
				// required field...
				if (oField.type=='text'||oField.type=='file'||oField.type=='textarea'||oField.type=='password'||oField.type=='hidden') {
					if (oField.value.length==0) {
						bCompleted = false;
						highlight(oField);
					}
					else if (oField.name=='email') {
						if (!oEmailRegExp.test(oField.value)) {
							bIsEmailAddress = false;
							highlight(oField);
						}
					}
				}
				else if (oField.type=='radio') {
					bChecked = false;
					var aRadioField = oForm[oField.name];
					for (j=0;j<aRadioField.length;j++) {
						if (aRadioField[j].checked) {
							bChecked = true;
						}
					}
					if (!bChecked) {
						highlight(oField);
						bCompleted = false;
					}
				}
				else if (oField.type=='select'||oField.type=='select-one') {
					if (oField.options[oField.selectedIndex].value.length==0) {
						bCompleted = false;
						highlight(oField);
					}
				}
				else if (oField.type=='checkbox') {
					if (!oField.checked) {
						bCompleted = false;
						highlight(oField);
					}
				}
				else {
					bChecked = false;
					for (j=0;j<oField.length;j++) {
						if (oField[j].checked) {
							bChecked = true;
						}
					}
					if (!bChecked) {
						highlight(oField);
						bCompleted = false;
					}
				}
			}
			if (bCompleted) {
//			else {
				// check that value is not the default value...	
				if (oField.type=='text'||oField.type=='textarea'||oField.type=='password'||oField.type=='hidden') {
					if (oField.title&&oField.title.length) {
						if (oField.title.toLowerCase()==oField.value.toLowerCase()) {
							highlight(oField);
							bCompleted = false;
						}
					}
					else if (oField.alt&&oField.alt.length) {
						if (oField.alt.toLowerCase()==oField.value.toLowerCase()) {
							highlight(oField);
							bCompleted = false;
						}
					}
				}
			}
		}
			
		if (!bCompleted) {
			if (bRequired) {
				if (oField.title&&oField.title.length) {
					alert('Please enter ' + oField.title);
				}
				else if (oField.alt&&oField.alt.length) {
					alert('Please enter ' + oField.alt);
				}
				else {
					alert('Please complete all required fields');
				}
			}
			else {
				if (oField.title&&oField.title.length) {
					alert('Please enter ' + oField.title + ' or leave blank');
				}
				else if (oField.alt&&oField.alt.length) {
					alert('Please enter ' + oField.alt + ' or leave blank');
				}
				else {
					alert('Please set blank any non essential information you prefer not to provide');
				}
			}
			break;
		}
	} // end loop
	

	if (bCompleted&&!bIsEmailAddress) {
		alert('Please check your email address');
	}
	var bResult = bCompleted && bIsEmailAddress;

	if (bResult) {
		// loop through fields and find submit buttons then disable....
		// we have an issue where a disabled field is not passed back in the form so need to check that only one submit button...
		var aSubmitButtons = new Array();
		for (i=0;i<oForm.elements.length;i++) {
			if (oForm.elements[i].type=='submit') {
				aSubmitButtons[aSubmitButtons.length] = oForm.elements[i];
;
			}
		}
		if (aSubmitButtons.length==1) {
			// only one submit button so OK to disable...
			aSubmitButtons[0].style.cursor = 'wait';
			aSubmitButtons[0].disabled = true;	
			aSubmitButtons[0].value = "sending";
		}
		else {
			// more than one submit button so disable each psuedo style...
			for (i=0;i<aSubmitButtons.length;i++) {
				aSubmitButtons[i].style.color = "#5F5F5F";
				aSubmitButtons[i].style.backgroundColor = "#F4F4F4";
				aSubmitButtons[i].style.cursor = 'wait';
				aSubmitButtons[i].value = "sending";
			}
		}
	}
	return bResult;
}


/*
-------------------------------------------------------- 
highlight [v1.3]
john's highlight form elements
-------------------------------------------------------- 
*/
function highlight(oField) {
	// pick mode - highlight table cell (true) or form field (false)...
	var bHighlightTd = true;
	var sStyleParentBorder = '';
	var sStyleParentBackgroundColor = '';
	var sStyleFieldBorder = '1px solid #585EAA';
	var sStyleFieldBackgroundColor = '#FFFFFF';
	
	if (oField.type=='select'||oField.type=='select-one'||oField.type=='text'||oField.type=='textarea'||oField.type=='password') {
		if (bHighlightTd && oField.id) {
			if (sStyleParentBackgroundColor.length) {
				document.getElementById(oField.id).parentNode.style.backgroundColor = sStyleParentBackgroundColor;
			}
			if (sStyleParentBorder.length) {
				document.getElementById(oField.id).parentNode.style.border = sStyleParentBorder;
			}
			if (sStyleFieldBackgroundColor.length) {
				document.getElementById(oField.id).style.backgroundColor = sStyleFieldBackgroundColor;
			}
			if (sStyleFieldBorder.length) {
				document.getElementById(oField.id).style.border = sStyleFieldBorder;
			}
		}
		else {
			if (sStyleFieldBackgroundColor.length) {
				oField.style.backgroundColor = sStyleFieldBackgroundColor;
			}
			if (sStyleFieldBorder.length) {
				oField.style.border = sStyleFieldBorder;
			}
		}
		// set focus...
		oField.focus();
	}
	else {
//		oField.parentNode.style.backgroundColor = 'lightyellow';
//		oField.parentNode.style.border = '1px solid red';
		// radio...
		for (var i=0;i<oField.length;i++) {

			if (bHighlightTd && oField[i].id) {
				if (sStyleParentBackgroundColor.length) {
					document.getElementById(oField[i].id).parentNode.style.backgroundColor = sStyleParentBackgroundColor;
				}
				if (sStyleParentBorder.length) {
					document.getElementById(oField[i].id).parentNode.style.border = sStyleParentBorder;
				}
				if (sStyleFieldBackgroundColor.length) {
					oField[i].style.backgroundColor = sStyleFieldBackgroundColor;
				}
				if (sStyleFieldBorder.length) {
					oField[i].style.border = sStyleFieldBorder;
				}
			}
			else {
				if (sStyleFieldBackgroundColor.length) {
					oField[i].style.backgroundColor = sStyleFieldBackgroundColor;
				}
				if (sStyleFieldBorder.length) {
					oField[i].style.border = sStyleFieldBorder;
				}
			}
		}
	}
//	alert(oField.name);
}


/*
-------------------------------------------------------- 
extend Array with Find Method [v1.0]
-------------------------------------------------------- 
*/
Array.find = function(ary, element){
	for(var i=0; i<ary.length; i++){
		if(ary[i] == element){
			return i;
		}
	}
	return -1;
}

//var aRequired = new Array();


/*
-------------------------------------------------------- 
KeepSessionAlive [v1.0]
-------------------------------------------------------- 
need to add to calling page:-
	var sTimeoutUrl = 'http://www.mydomain.com/myscript';
	addWindowOnload(KeepSessionAlive);
-------------------------------------------------------- 
*/
function KeepSessionAlive() {
	// prevent session from server timeout...
	//var sTimeoutUrl = 'http://www.mydomain.com/myscript';
	var oXmlReq = null;
    if (window.XMLHttpRequest) {
		oXmlReq = new XMLHttpRequest();
	}
    else if (window.ActiveXObject) {
		oXmlReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
    if (oXmlReq!=null) {
		oXmlReq.open ('GET', sTimeoutUrl, true);
		oXmlReq.send (null);
	}
	KeepSessionAliveTimer = setTimeout("KeepSessionAlive()", 10 * 60000);
}



/* ========================== cookie handlers =========================== */
function setCookie(sKey, sValue) {
	var futdate = new Date()		//Get the current time and date
	var expdate = futdate.getTime()  //Get the milliseconds since Jan 1, 1970
	expdate += 3600*1000  //expires in 1 hour(milliseconds)
	futdate.setTime(expdate)
	var newCookie = sKey + "=" + escape(sValue) + "; path=/;"	//Set the new cookie values up
//	newCookie += " expires=" + futdate.toGMTString()
	window.document.cookie=newCookie //Write the cookie
}
function getCookie(sKey) {
	var oCookie = document.cookie;
	var sKey = sKey + "=";
	var iBegin = oCookie.indexOf("; " + sKey);
	if (iBegin == -1) {
		iBegin = oCookie.indexOf(sKey);
		if (iBegin != 0) {
			return null;
		}
	} 
	else {
		iBegin = iBegin + 2;
	}
	var iEnd = oCookie.indexOf(";", iBegin);
	if (iEnd == -1) {
		iEnd = oCookie.length;
	}
	return unescape(oCookie.substring(iBegin + sKey.length, iEnd));
}
function clearCookie(sKey) {
	setCookie(sKey, '');
	alert('Information has been deleted');
	return false;
}
function acceptsCookie() {
	sCookieKey = 'cookiesenabled';
	setCookie(sCookieKey, true);
	var result = getCookie(sCookieKey);
	if (result==null||!result.length) {
		return false;
	}
	else {
		return true;
	}
}


/*
-------------------------------------------------------- 
-------------------------------------------------------- 
Event Handling...
-------------------------------------------------------- 
-------------------------------------------------------- 
*/

/*
-------------------------------------------------------- 
addEvent [v1.0]
--------------------------------------------------------
usage: 
	addEvent(document.getElementById('tb1'), 'keyup', handleKeyUp, false);
	addEvent(document.getElementById('tb1'), 'keyup', function(){handleKeyUp(your, parms, here)}, false);
--------------------------------------------------------
*/
function addEvent(element, eventType, func, capt) {
	if (element.addEventListener) {
		element.addEventListener(eventType, func, capt);
	}
	else if (element.attachEvent) {
		var r = element.attachEvent('on' + eventType, func);
		return r;
	} 
	else {
		if (element['on' + eventType]==null) {
			element['on' + eventType] = func;
		}
		else {
			oldEventType = element['on' + eventType];
			element['on' + eventType] = function(e) {
				oldEventType(e);
				element['on' + eventType][func]();
			}
		}
	}
}


/*
--------------------------------------------------------
addWindowOnload [v1.0]
handle multiple window onload events
--------------------------------------------------------
usage: 
	addWindowOnload(function(){ myFunctionName('myArgument') });
	addWindowOnload(myFunctionName);
--------------------------------------------------------
*/
function addWindowOnload(func){
	addEvent(window, 'load', func, false);
}



/*
--------------------------------------------------------
add getElementById functionality to IE4
-------------------------------------------------------- 
*/
if (!document.getElementById && document.all) { 
	document.getElementById = new Function('id', 'return document.all[id]') 
}


// write out javascript dependant functionality...
document.write('\<script type="text/javascript" src="js/prototype.js"\>\</script\>');
document.write('\<script type="text/javascript" src="js/scriptaculous.js?load=effects"\>\</script\>');
document.write('\<script type="text/javascript" src="js/lightbox.js"\>\</script\>');
document.write('\<link rel="stylesheet" type="text/css" href="css/lightbox.css" media="screen" \/\>');
