window.onload = initialize;

function initialize() {
	menu = new dropdownMenu();
	// Stretch Left Column
	if(	document.getElementById('insideContent') && document.getElementById('insideContentLeft')) {
		var leftColumnPadding = window.navigator.userAgent.indexOf("MSIE 5") ? 210 : 0;	// Work-around for IE5's incorrect box model
		document.getElementById('insideContentLeft').style.height = (parseInt(document.getElementById('insideContent').offsetHeight)-leftColumnPadding)+'px';
		}
	// Add pdf icon to pdf downloads
	addPDFicon();
	// Add arrows to multi-line links
	addArrows();
	// Add highlight to current page in left menu
	addInsideActivePage();
	}

function addInsideActivePage() {
	if(document.getElementById('insideContentLeft')==null) return; // return if page doesn't have leftmenu
	var windowURL = (''+window.location).substring((''+window.location).lastIndexOf('/')+1,(''+window.location).length); // current document name
	var leftLinks = document.getElementById('insideContentLeft').getElementsByTagName('A'); // Array of links in the left menu
	for(x=0; x<leftLinks.length; x++) {
		if( windowURL == leftLinks[x].href.substring(leftLinks[x].href.lastIndexOf('/')+1,leftLinks[x].href.length) ) { // If current document and link match
			if(leftLinks[x].parentNode.tagName == 'H5') leftLinks[x].parentNode.style.background = '#fff'; // If the link is in a heading, make the heading's bg white also.
			leftLinks[x].className = 'active';		// add active class, for white background
			}
		}
	}

function addArrows() {
	var links = document.getElementsByTagName('A');
	var linksWithArrows = [];
	// Find links with the "arrow" class
	for(x=0; x<links.length; x++) {
		if(/\barrow\b/.exec(links[x].className)) {
			linksWithArrows[linksWithArrows.length] = links[x];
			}
		 }
	// Append the arrow image to the link's contents
	for(x=0; x<linksWithArrows.length; x++) {
		var arrowImage = document.createElement('img');
		arrowImage.src = 'images/global-arrow-small-yellow.gif';
		arrowImage.style.margin = '0 0 0 3px';
		arrowImage.style.display = 'inline';
   		linksWithArrows[x].appendChild(arrowImage);
		}
	}

function addPDFicon() {
	var links = document.getElementsByTagName('A');
	var linksWithPDF = [];
	// Find links with the "pdf" class
	for(x=0; x<links.length; x++) {
		if(/\bpdf\b/.exec(links[x].className)) {
			linksWithPDF[linksWithPDF.length] = links[x];
			}
		 }
	// Append the pdf image to the link's contents
	for(x=0; x<linksWithPDF.length; x++) {
		linksWithPDF[x].target = '_blank'; // open link in new window
		linksWithPDF[x].title = 'Links opens in a new window:\n� '+linksWithPDF[x].href.substring(linksWithPDF[x].href.lastIndexOf('/')+1,linksWithPDF[x].href.length); // title text
		var linkContents = linksWithPDF[x].childNodes[0];
		var pdfImage = document.createElement('img');
		pdfImage.src = 'images/global-icon-pdf-small.gif';
		pdfImage.style.margin = '0 4px 0 0';
		pdfImage.style.display = 'inline';
		pdfImage.style.position = 'relative';
		pdfImage.style.top = '3px';
		linksWithPDF[x].replaceChild(pdfImage, linksWithPDF[x].childNodes[0]);
		linksWithPDF[x].appendChild(linkContents); 
		}
	}

function dropdownMenu() {
	this.menuTimeout = null;
	this.menuIndex = 1;
	this.menuPadding = window.navigator.userAgent.indexOf("MSIE 5") ? 0 : 20;	// Work-around for IE5's incorrect box model
	while(document.getElementById('menu'+this.menuIndex) != null) {
		// Add show/hide event handlers on toolbar A tags (these are the menu headings)
		document.getElementById('menu'+this.menuIndex).onmouseover = function() {
			clearTimeout(menu.menuTimeout); menu.showMenu(this.id); document.getElementById(this.id).style.color = '#ddc11b';
			}
		document.getElementById('menu'+this.menuIndex).onmouseout = function() {
			menu.menuTimeout = setTimeout("menu.hideMenus()",1000); document.getElementById(this.id).style.color = '#c1c6d3';
			}
		// Add show/hide event handlters on submenu div and A tags
		this.submenuItems = document.getElementById('submenu'+this.menuIndex).getElementsByTagName("A");
		document.getElementById('submenu'+this.menuIndex).style.visibility = 'hidden';	//	Make menu rendered (display: block) but hidden (visibility: hidden) so the submenu
		document.getElementById('submenu'+this.menuIndex).style.display = 'block';			//	 has a calculated width we can apply to the A tags inside it
		this.submenuWidth = parseInt(document.getElementById('submenu'+this.menuIndex).offsetWidth)-this.menuPadding;

		for(var y=0;y<this.submenuItems.length;y++) {
			this.submenuItems[y].style.width = this.submenuWidth+'px';		// Add width style so IE makes whole submenu item clickable, not just the text
			this.submenuItems[y].onmouseover = function() {
				clearTimeout(menu.menuTimeout);
				}
			this.submenuItems[y].onmouseout = function() {
				menu.menuTimeout = setTimeout("menu.hideMenus()",500);
				}
			}
		document.getElementById('submenu'+this.menuIndex).style.display = 'none';			// Revert the display styles we
		document.getElementById('submenu'+this.menuIndex).style.visibility = 'visible';		//  altered above.
		this.menuIndex++;
		}
	// Class member functions
	this.hideMenus = function() {
		this.menuIndex=1;
		while(document.getElementById('menu'+this.menuIndex) != null) {
			document.getElementById('menu'+this.menuIndex).style.color = '#fff';
			document.getElementById('submenu'+this.menuIndex++).style.display = 'none';
			}
		}
	this.showMenu = function(id) {
		menu.hideMenus();
		document.getElementById('sub'+id).style.display = 'block'; document.getElementById(id).style.color = '#c1c6d3';
		}
	}

function validateForm(form) {
  var el, group, checked, r;
  for (var e = 0; e < form.elements.length; e++) {
    el = form.elements[e];
    if (el.type == 'text' || el.type == 'textarea' ||
        el.type == 'password' || el.type == 'file' ) {
      if (el.name != 'Phone Number' && el.value == '') {
        alert('Please fill out the field ' + el.name);
        el.focus();
        return false;
      }
    }
    else if (el.type.indexOf('select') != -1) {
      if (el.selectedIndex == -1) {
        alert('Please select a value of the select field ' + el.name);
        el.focus();
        return false;
      }
    }
    else if (el.type == 'radio') {
      group = form[el.name];
      checked = false;
      if (!group.length)
        checked = el.checked;
      else
        for (r = 0; r < group.length; r++)
          if ((checked = group[r].checked))
            break;
      if (!checked) {
        alert('Please check one of the radio buttons ' + el.name);
        el.focus();
        return false;
      }
    }
    else if (el.type == 'checkbox') {
      group = form[el.name];
      if (group.length) {
        checked = false;
        for (r = 0; r < group.length; r++)
          if ((checked = group[r].checked))
            break;
        if (!checked) {
          alert('Please check one of the checkboxes ' + el.name);
          el.focus();
          return false;
        }
      }
    }
  }
  return true;
}