window.onload = init;

function init() {
	msgStatusAllLinks();
	startlist();
}

window.onerror = function() {
    window.status = 'Error on page.';
}

function attachFocus() {

	if (this.className.indexOf(" focus")<0) {
		this.className+=" focus";
	}

	if (this.tempOnFocus) {
		// call existing onFocus event.
		this.tempOnFocus();
	}

}

function attachBlur() {

	if (this.className.indexOf(" focus")>0) {
		this.className = this.className.substring(0,this.className.indexOf(" focus"));
	} else {
		this.className.replace(" focus","");
	}

	if (this.tempOnBlur) {
		// call existing onBlur event.
		this.tempOnBlur();
	}

}

function msgStatus() {
	if (this.tagName=="INPUT") {
		switch(this.type) {
			case 'image': // form button is an <input type="image"> (graphic);
				if (this.getAttribute('src').indexOf('_a.')!=-1) {
					this.src = this.getAttribute('src').replace('_a.','_b.');
					txtStatus=this.getAttribute('alt');
				}
				if (this.getAttribute('src').indexOf('-a.')!=-1) {
					this.src = this.getAttribute('src').replace('-a.','-b.');
					txtStatus=this.getAttribute('alt');
				}
				break;
			case 'button': // form button is an <input type="button"> (webtext);
			case 'submit': // form button is an <input type="submit"> (webtext);
			case 'reset': // form button is an <input type="reset"> (webtext);
			case 'file': // form button is an <input type="file"> (webtext);
				//this.className="bnover";
				txtStatus=this.getAttribute('value');
				break;
			default:
				break;
		}
	} else {
		if (this.name) {
			for (i=0;i<document.getElementsByName(this.name).length;i++) {
				if (document.getElementsByName(this.name)[i]!=this) {
					if (document.getElementsByName(this.name)[i].tagName=='A') {
						if (document.getElementsByName(this.name)[i].getElementsByTagName('img').length!=0) {
							if (document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.indexOf('_a.')!=-1) {
								document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src = document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.replace('_a.','_b.');
							}
							if (document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.indexOf('-a.')!=-1) {
								document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src = document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.replace('-a.','-b.');
							}
						} else {
							document.getElementsByName(this.name)[i].className="rollover";
						}
					}
				}
			}
		}
		if (this.innerText) {
			txtStatus=this.innerText;
		} else {
			if (this.text) txtStatus=this.text;
		}
		if (this.getElementsByTagName('img').length!=0) {
			txtStatus=this.getElementsByTagName('img')[0].getAttribute('alt');
			if (this.getElementsByTagName('img')[0].src.indexOf('_a.')!=-1) {
				this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('_a.','_b.');
			}
			if (this.getElementsByTagName('img')[0].src.indexOf('-a.')!=-1) {
				this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('-a.','-b.');
			}
		} else {
			if (this.tagName=="AREA") {
				txtStatus=this.getAttribute('alt');
			}
		}
	}
	// txtStatus=toTitleCase(txtStatus);
	if (txtStatus) {window.status=toTitleCase(txtStatus);}
	return true;
}

function msgStatusOff() {
	txtStatus=window.defaultStatus;
	if (this.tagName=="INPUT") {
		switch(this.type) {
			case 'image': // form button is an <input type="image"> (graphic);
				if (this.getAttribute('src').indexOf('_b.')!=-1) {
					this.src = this.getAttribute('src').replace('_b.','_a.');
				}
				if (this.getAttribute('src').indexOf('-b.')!=-1) {
					this.src = this.getAttribute('src').replace('-b.','-a.');
				}
				break;
			case 'button': // form button is an <input type="button"> (webtext);
			case 'submit': // form button is an <input type="submit"> (webtext);
			case 'reset': // form button is an <input type="reset"> (webtext);
				this.className+=" bnout";
				break;
			default:
				break;
		}
	} else {
		if (this.name) {
			for (i=0;i<document.getElementsByName(this.name).length;i++) {
				if (document.getElementsByName(this.name)[i]!=this) {
					if (document.getElementsByName(this.name)[i].tagName=='A') {
						if (document.getElementsByName(this.name)[i].getElementsByTagName('img').length!=0) {
							if (document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.indexOf('_b.')!=-1) {
								document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src = document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.replace('_b.','_a.');
							}
							if (document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.indexOf('-b.')!=-1) {
								document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src = document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.replace('-b.','-a.');
							}
						} else {
							document.getElementsByName(this.name)[i].className="";
						}
					}
				}
			}
		}
	}
	if (this.getElementsByTagName('img').length!=0 && this.getElementsByTagName('img')[0].src.indexOf('_b.')!=-1 && this.getElementsByTagName('img')[0].name.indexOf('img_')==-1) {
		this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('_b.','_a.');
	}
	if (this.getElementsByTagName('img').length!=0 && this.getElementsByTagName('img')[0].src.indexOf('-b.')!=-1 && this.getElementsByTagName('img')[0].name.indexOf('img_')==-1) {
		this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('-b.','-a.');
	}
	window.status=txtStatus; return true;
}

function bodyResize() {
	window.location.href = window.location.href;
}

function msgStatusAllLinks() {
	for(i=0; i<document.links.length; i++) {
		if (!document.links[i].onmouseover) {
			document.links[i].onmouseover=msgStatus;
			document.links[i].onmouseout=msgStatusOff;
		}
	}
	for(i=0; i<document.getElementsByTagName('input').length; i++) {
		switch(document.getElementsByTagName('input')[i].type) {
			case 'image': // form button is an <input type="image"> (graphic);
			case 'button': // form button is an <input type="button"> (webtext);
			case 'submit': // form button is an <input type="submit"> (webtext);
			case 'reset': // form button is an <input type="reset"> (webtext);
				document.getElementsByTagName('input')[i].onmouseover=msgStatus;
				document.getElementsByTagName('input')[i].onmouseout=msgStatusOff;
				break;
			case 'text':
			case 'password':

				// Add element's existing blur/focus event (if it has one) before adding a new blur/focus event 'attachBlur/Focus'
				// This ensures that any existing onBlur/onFocus event the element has is still executed, as well as the 'attachBlur/Focus' event.
				// Check if IE or Netscape and use appropriate syntax accordingly.
				if (navigator.appName == 'Microsoft Internet Explorer') {

					if (document.getElementsByTagName('input')[i].onblur) {
						// Assign existing onBlur to a temp event 'tempOnBlur' - Later executed in attachBlur function.
						document.getElementsByTagName('input')[i].tempOnBlur = document.getElementsByTagName('input')[i].onblur;
					}

					if (document.getElementsByTagName('input')[i].onfocus) {
						// Assign existing onBlur to a temp event 'tempOnFocus' - Later executed in attachFocus function.
						document.getElementsByTagName('input')[i].tempOnFocus = document.getElementsByTagName('input')[i].onfocus;
					}

					// attach new event. This will cause the input field colour to change.
					document.getElementsByTagName('input')[i].onfocus=attachFocus;
					document.getElementsByTagName('input')[i].onblur=attachBlur;

				}
				else {
				
					// attach existing blur/focus event.
					document.getElementsByTagName('input')[i].addEventListener('focus',attachFocus,false);
					document.getElementsByTagName('input')[i].addEventListener('blur',attachBlur,false);

				}		
				break;
			default:
				break;
		}
	}
	for(i=0; i<document.getElementsByTagName('textarea').length; i++) {
		document.getElementsByTagName('textarea')[i].onfocus=attachFocus;
		document.getElementsByTagName('textarea')[i].onblur=attachBlur;
	}
}

// toTitleCase
function toTitleCase(strString) {
	// strString = fnTrim(strString);
	var firstSpace = strString.indexOf(' ');
	var currentWord;
	strString = strString.charAt(0).toUpperCase()+strString.substring(1,strString.length);
	if (firstSpace > -1) strString=strString.substring(0,firstSpace+1) + toTitleCase(strString.substring(firstSpace+1,strString.length));
	return strString;
}

// setFocus (clear form text field contents onfocus)
function setFocus(oCtl) {
    if (oCtl.value == oCtl.defaultValue) oCtl.value = '';
}

// toggleNav (show/hide sublist). Used by Calendar.
function toggleNav(strID) {

	var oDiv;

	if (oDiv = document.getElementById(strID)) {
		oDiv.style.display = oDiv.style.display == 'none' ? 'block' : 'none';
	}
	
	return false;

}

// toggleListActionSymbol used by 2nd level navigation code to ensure that a 3rd level's parent div has the correct action symbol (e.g. '+' or '-')
function toggleListActionSymbol(strID) {
	var oSideNavSelectedListActionSymbol;
    oSideNavSelectedListActionSymbol = document.getElementById("imgListAction" + strID);
	alert(oSideNavSelectedListActionSymbol.src);
	// Toggle the symbol.
	if (oSideNavSelectedListActionSymbol.src == 'img/bn_expand.gif') {
		oSideNavSelectedListActionSymbol.src = 'img/bn_contract.gif';
	} else {
		oSideNavSelectedListActionSymbol.src = 'img/bn_expand.gif';	
	}
    return false;
}

/*
OLD CODE
// toggleSiteNav used by 2nd level navigation code to display/hide any third level nav elements.
function toggleSiteNav(strID) {

	var oSideNavDiv;
	var oSideNavListActionSymbol;
	var oSideNavSelectedListActionSymbol;
*/	
	/* ***** CODE PROCEDURE *****
	
	1)Set all list symbols (e.g. +, -) to '+'.
	2)Set the list symbol to '-' for the selected div.
	3) Close all other open divs before opening the currently selected one:
	Loop through all divs which have the first part of their ID called 'expandNav'.  The full div names may look like expandNav1, expandNav2, etc... 
	but at this point in the code the actual amount of Divs (expandNav1, expandNav2, etc.) is unknown so we can only refer to them based on the first part of their IDs.

		Step 3.1.  Get a reference to the Div that contains the entire side nav.  It has the basic structure as follows;
		<div id="sidenav">
		<ul class="mainlist">
			<li>2nd level nav item one</li>
			<div id="expandNav1">third level nav here</div>
			...
		</ul>
		</div>

		Step 3.2.  Once obtaining to the sidenav ID in Step 1, get all 'div' tag elements (this will effectively reference all the expandable 3rd level nav groups)
		Step 3.3.  Set all 3rd level nav group Div's to be hidden, and set the display of the group Div (defined by the strID value passed into this function) to be 'block'
    
    */

/*
	// 1) Change the appearance of the little + and - symbols appropriately.  Set all symbols to '+'. 
    oSideNavListActionSymbol = document.getElementById("sidenavUL").getElementsByTagName("img");
    for (i=0; i<oSideNavListActionSymbol.length; i++) {		
	    oSideNavListActionSymbol[i].src = 'img/bn_expand.gif';	
    }

	// 2) Set the action symbol to '-' to indicate the list is opened for this particular div.
    oSideNavSelectedListActionSymbol = document.getElementById("imgListAction" + strID);
    oSideNavSelectedListActionSymbol.src = 'img/bn_contract.gif';

    
    // 3)
	oSideNavDiv = document.getElementById("sidenav").getElementsByTagName("div");
	for(var i = 0; i < oSideNavDiv.length; i++){
		// Set all Divs to display 'none' except for the one defined by strID, which should be set to 'block'.
		oSideNavDiv[i].style.display = oSideNavDiv[i].id == 'expandNav' + strID ? 'block' : 'none';				
	}

	return false;

}
*/

// toggleSiteNav used by 2nd level navigation code to display/hide any third level nav elements.
function toggleSiteNav(strID) {

	var oSideNavDiv;

	oSideNavDiv = document.getElementById("sidenav").getElementsByTagName("div");
	for(var i = 0; i < oSideNavDiv.length; i++){
		// Set all Divs to display 'none' except for the one defined by strID, which should be set to 'block'.
		oSideNavDiv[i].style.display = oSideNavDiv[i].id == 'expandNav' + strID ? 'block' : 'none';				
	}

	return false;

}

// Dropdown Submenu
// submenus function in modern browsers via CSS only;
// however, IE5, IE5.5, IE6 require this Javascript in order to function

function startlist() { 
            if (document.all&&document.getElementById) {
                        if (document.getElementById("navlist")) { 
                                    navRoot = document.getElementById("navlist");
                                    for (i=0; i<navRoot.childNodes.length; i++) { 
                                                node = navRoot.childNodes[i];
                                                if (node.nodeName=="LI") { 
                                                            node.onmouseover=function() { 
                                                                        this.className+=" over";			
                                                            }
                                                            node.onmouseout=function() {
                                                                        this.className=this.className.replace(" over", "");
                                                            }
                                                }
                                    }
                       }
            }
}

// Functions below used by:
// Login form
// Site Search form
// Event Search form

function checkLoginForm(oForm) {
	if ((oForm.txtUserName.value == 0) || (oForm.txtPassword.value == 0)) {
		alert('Please complete the username and password fields');
		oForm.txtUserName.focus();	
		return false;
	} else {
		oForm.submit();
		return false;
	}
}

function checkSearchForm(oForm) {
	if ((oForm.txtKeyword.value == 0) || (oForm.txtKeyword.value.length < 3)) {
		alert('Please enter a search term at least 3 characters in length.');
		oForm.txtKeyword.focus();	
		return false;
	} else {
		oForm.submit();
		return false;
	}
}
