// Multiple onload function created by: Simon Willison
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
  initPage();
});

function initPage(){
	initHoverMenu();
	initQuickCalendar();
	addClearOnclick();
}

function addClearOnclick(){
	//adds an onclick event which clears the text from a text box the first time it's clicked.  The text box's
	//onclick event will be set to null.  Works on textboxes whose 'rel' value is 'clear'
	var inputs = document.getElementsByTagName('input');

	for(i=0;i<inputs.length;i++){
		if(inputs[i].getAttribute('clear')=='clear'){
			inputs[i].onclick=function(){
				this.value='';
				this.onclick=null;
			};
		}
	}
}
function initQuickCalendar(){
	//initializes the dropdown calendar in the header section of the pages
	nav_select = document.getElementById('calendarNav_select');
	//	nav_select.onchange = function(){if(this.value!=''){window.location.href='http://www.rogercpareview.com/cpa-exam-review-calendar.cfm' + this.value;}}
	nav_select.onchange = function(){if(this.value!=''){window.location.href='http://www.rogercpareview.com'+this.value;}}
	document.getElementById('calendarNav_link').className = 'hidden';
	nav_select.className='';
}

/* Main nav menu functions */
function initHoverMenu(){
	var navWrapper = document.getElementById('navigation_wrapper');
	var nav = document.getElementById('main_nav');
	var classAnchor = 'hoverMenu';
	if (navWrapper.className.indexOf(classAnchor)>=0){
		var patt1 = /^subnav\d+/g;
		var patt2 = /subnav\d+/g;
		for(i=0;i<nav.childNodes.length;i++){ //cycling through LIs
			for(j=0;j<nav.childNodes[i].childNodes.length;j++){ //cycling through ULs
				if (patt1.test(nav.childNodes[i].childNodes[j].className)){
					nav.childNodes[i].childNodes[j].onmouseover=selectMySubmenu;
				}
			}
		}
		navWrapper.onmouseover=clearSubmenu;
	}
}

function selectMySubmenu(e){
	var navWrapper = document.getElementById('navigation_wrapper');
	var patt2 = /subnav\d+/g;
	
	navWrapper.className = navWrapper.className.replace(patt2,'');
	navWrapper.className += ' ' + this.className;
}

function clearSubmenu(e){
	var navWrapper = document.getElementById('navigation_wrapper');
	var patt2 = /subnav\d+/g;
	
	var evt = e || window.event;
	var evtTarget = evt.target || evt.srcElement;
	
	if (evtTarget.id==this.id){navWrapper.className = navWrapper.className.replace(patt2,'');}
}

function getChildElement(obj,index){
//Allows user to access strictly element nodes, as opposed to childNodes[i] which includes all types of nodes (e.g. text nodes, gah!)
	var elementCounter = 0;
	var i = 0;
	for(i=0;i<obj.childNodes.length;i++){
		if(obj.childNodes[i].nodeType==1){
			if(elementCounter==index){return obj.childNodes[i];}
			elementCounter++;
		}
	}
}