// JavaScript Document
// JavaScript Document

//This loads the main menu functions like hover for IE and keyboard tabbing.
sfHover = function() {
    var sfEls = document.getElementById("nav").getElementsByTagName("LI");
    for (var i = 0; i < sfEls.length; i++) {
        sfEls[i].onmouseover = function() {
            this.className += (this.className.length > 0 ? " " : "") + "sfhover";
        }
        sfEls[i].onmouseout = function() {
            this.className = this.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
        }
    }
}
mcAccessible = function() {
    var mcEls = document.getElementById("nav").getElementsByTagName("A");
    for (var i = 0; i < mcEls.length; i++) {
        mcEls[i].onfocus = function() {
            this.className += (this.className.length > 0 ? " " : "") + "sffocus"; //a:focus
            this.parentNode.className += (this.parentNode.className.length > 0 ? " " : "") + "sfhover"; //li < a:focus
            if (this.parentNode.parentNode.parentNode.nodeName == "LI") {
                this.parentNode.parentNode.parentNode.className += (this.parentNode.parentNode.parentNode.className.length > 0 ? " " : "") + "sfhover"; //li < ul < li < a:focus
                if (this.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "LI") {
                    this.parentNode.parentNode.parentNode.parentNode.parentNode.className += (this.parentNode.parentNode.parentNode.parentNode.parentNode.className.length > 0 ? " " : "") + "sfhover"; //li < ul < li < ul < li < a:focus
                }
            }
        }
        mcEls[i].onblur = function() {
            this.className = this.className.replace(new RegExp("( ?|^)sffocus\\b"), "");
            this.parentNode.className = this.parentNode.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
            if (this.parentNode.parentNode.parentNode.nodeName == "LI") {
                this.parentNode.parentNode.parentNode.className = this.parentNode.parentNode.parentNode.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
                if (this.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "LI") {
                    this.parentNode.parentNode.parentNode.parentNode.parentNode.className = this.parentNode.parentNode.parentNode.parentNode.parentNode.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
                }
            }
        }
    }
}
addLoadEvent(sfHover);
addLoadEvent(mcAccessible);


// this function looks for form fields other than submit and clears the default value onFocus
function resetFields(whichform) {
	for (var i=0; i<whichform.elements.length; i++) {
		var element = whichform.elements[i];
		if (element.type == "submit") continue;
		element.onfocus = function() {
			if (this.value == this.defaultValue) {
				this.value = "";
			}
		}
		element.onblur = function() {
			if (this.value == "") {
				this.value = this.defaultValue;
			}
		}
	}
}


//Prepares form fields so they clear on focus
function prepareForms() {
	for (var i=0; i<document.forms.length; i++) {
		var thisform = document.forms[i];
		resetFields(thisform);
	}
}
addLoadEvent(prepareForms);


/* If a form has a label tag "for" an element, the onclick method will focus the cursor on the field associated with the label tag */
function focusLabels() {
	if (!document.getElementsByTagName) return false;
	var labels = document.getElementsByTagName("label");
	for (i=0; i<labels.length; i++) {
		if (!labels[i].getAttribute("for")) continue;
		labels[i].onlclick = function() {
			var id = this.getAttribute("for");
			if (!document.getElementById(id)) return false;
			var element = document.getElementById(id);
			element.focus();
		}
	}
}
addLoadEvent(focusLabels);
			


// All window onload events get triggered by this function.
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* xhtml does not support the "target" attribute for opening a new window.
While not perfect this script comes to the aid.
Make it work by adding a " rel="external"> to the opening <a> tag.
This makes it work and is more accessible than other methods.
*/

function externalLinks() {
  if (!document.getElementsByTagName) return false;//added missing false here 062708
    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";
      anchor.title = (anchor.title != "") ? anchor.title+" (opens in a new window)" : "opens in a new window";
      anchor.className = (anchor.className != '') ? anchor.className+' external' : 'external';
      }
  }
}
addLoadEvent(externalLinks);

function highlightPage() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("nav")) return false;
	var nav = document.getElementById("nav");
	var links = nav.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
		var linkurl = links[i].getAttribute("href");
		var currenturl = window.location.href;
		if (currenturl.indexOf(linkurl) != -1) {
			links[i].className = "sffocus";
			links[i].parentNode.parentNode.parentNode.className = "sffocus";
			   }
			}
		}
addLoadEvent(highlightPage);

function highlightPageSecNav() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("sec_nav")) return false;
    var secnav = document.getElementById("sec_nav");
	  var links = secnav.getElementsByTagName("a");
	    for (var i=0; i<links.length; i++) {
		    var linkurl = links[i].getAttribute("href");
		    var currenturl = window.location.href;
		    if (currenturl.indexOf(linkurl) != -1) {
			    links[i].className = "match";
			   }
			}
		}
addLoadEvent(highlightPageSecNav);





