///////////////////////////////////////

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;	
	} else {
		window.onload = function() {
			oldonload();
			func();
		}	
	}
}

///////////////////////////////////////

addLoadEvent(resizeContentHeight);
addLoadEvent(timedFadeSessionMessage);

///////////////////////////////////////

function addClass(element, value) {
	if(!element.className) {
		element.className = value;
	} else {
		newClassName = element.className;
		newClassName+= " ";
		newClassName+= value;
		element.className = newClassName;
	}
}

////////////////////////////////////////////

function attachEventListener(target, eventType, functionRef, capture) {
    if (typeof target.addEventListener != "undefined") {
        target.addEventListener(eventType, functionRef, capture);
    }
    else if (typeof target.attachEvent != "undefined") {
        target.attachEvent("on" + eventType, functionRef);
    }else {
        return false;
    }
    return true;
}

////////////////////////////////////////////

function getHeight() {
	var myHeight = 0;
	if(typeof( window.innerHeight ) == 'number') {
		myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientHeight ) ) {
		myHeight = document.body.clientHeight;
	  }
	return myHeight;
}

////////////////////////////////////////////

function resizeContentHeight() {

	var bodyID = document.getElementsByTagName("body")[0].id;

	if(bodyID != "calendar" && bodyID != "noNav") {
		var h = getHeight();
			if(h <= 650) {
				var myHeight = "315";
			} else if(h > 650 && h < 775) {
				var myHeight = h * .50;
			} else if(h >= 775 && h < 850) {
				var myHeight = h * .56;
			} else if(h >= 850 && h < 895) {
				var myHeight = h * .58;
			} else {
				var myHeight = h * .63;
			}
		$("user_content").style.height = myHeight + "px";
		$("user_content").style.overflow = "auto";
	}
}

attachEventListener(window, "resize", resizeContentHeight, false);

////////////////////////////////////////////

function timedFadeSessionMessage() {
	setTimeout('fadeMessage()', 10000);
}

function fadeMessage() {
	if($("message").style.display == "block") Effect.Fade("message");
}

////////////////////////////////////////////

function swapSearchBy(sel) {

}

////////////////////////////////////////////

function revealSubnav(el, toggleMe) {
	hideSubnavs(toggleMe);
	Effect.toggle(toggleMe);
	
	if($(toggleMe).style.display == "none") {
		el.className = "togglerOpen";
	} else {
		el.className = "toggler";
	}
}

function hideSubnavs(keepOpen) {
	var otherNavs = $("mainnav").getElementsByTagName("ul");
	
	for(i=0; i<otherNavs.length; i++) {
		var myId = otherNavs[i].id;
		
		if(myId != keepOpen) {
			var mytogglers = otherNavs[i].parentNode.getElementsByTagName("span");
			Element.hide(myId);
			if(mytogglers.length > 0) mytogglers[0].className = "toggler";
		}
	}
}

////////////////////////////////////////////

function buttonJumpTo(refID) {
	var hidden = document.getElementById(refID);
	location.href = hidden.getAttribute("value");
}

////////////////////////////////////////////

function winPopUp(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="slideshow") strOptions="height="+strHeight+",width="+strWidth+"top=150,left=70,scrollbars=no, status=no, resizable=yes";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,scrollbars,resizable,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
if (strType=="centeredFullHeight") {
	var left = (screen.width  - strWidth)/2;
	var top = (screen.height - strHeight)/2;
	strHeight = screen.height;
	strOptions="status,scrollbars,resizable,height="+strHeight+",width="+strWidth+",top="+top+",left="+left;
}
window.open(strURL, 'newWin', strOptions);
}

////////////////////////////////////////////

function swap(targetId) {
	Element.toggle(targetId);
}

////////////////////////////////////////////


function doubleswap(targetId,targetId2) {
	Element.toggle(targetId);
	Element.toggle(targetId2);
}

////////////////////////////////////////////

function toggle( targetId ) {
	Element.toggle(targetId);
}

////////////////////////////////////////////

function toggleMemberItem(whichID, subID) {
	var allItems = document.getElementsByTagName("dl");
	
	for(i=0; i<allItems.length; i++) {
	
		if(allItems[i].className == "adminOptionsList") {
			if(allItems[i].id != whichID) {
				var myID = allItems[i].getAttribute("id");
				Element.toggle(myID);
			}
		}	
	}
	
	var cancelLink = whichID + "_a";
	var cancelLinkB = whichID + "_b";
	
	Element.toggle(subID);
	Element.toggle(cancelLink);
	Element.toggle(cancelLinkB);
	
	$("user_content").scrollTop = 0;
}

/////////////////////////////////////////////

function checkUncheckAll(formName) {
     var theForm = document.getElementById(formName);
     
     for(var i=0; i<theForm.elements.length; i++) {     
     if(theForm.elements[i].type == 'checkbox') {
			if(theForm.elements[i].checked == true) {
				theForm.elements[i].checked = false;
			} else {
				theForm.elements[i].checked = true;
			}
     	}
     }
}

////////////////////////////////////////////

function toggleCheckbox(checkboxID) {
	if($(checkboxID).checked) 
		$(checkboxID).checked = false;
	else 
		$(checkboxID).checked = true;
}

////////////////////////////////////////////

function updateLocationAddOptions(member_id) {
	var theURL = '/ajax/locationAdd.inc.php?member_id=' + member_id;
	new Ajax.Updater('locOptionsSwitcher', theURL, {onComplete: completeLocationAdder});
}

////////////////////////////////////////////

function updateLocationAddEdit(practice_id) {
	var theURL = '/ajax/locationAddNew.inc.php?practice_id=' + practice_id;
	new Ajax.Updater('locOptionsSwitcher', theURL, {onComplete: completeLocationAdder});
}

////////////////////////////////////////////

function removeMemberLocation(member_id, practice_id) {
	var theURL = '/ajax/locationRemove.inc.php?practice_id=' + practice_id + '&member_id=' + member_id;
	new Ajax.Updater('myLocs', theURL, {onComplete:function() {
		hiLiteDiv('myLocs'); }
	});
}

////////////////////////////////////////////

function hiLiteDiv(myDiv) {
	new Effect.Highlight(myDiv);
}

////////////////////////////////////////////

function completeLocationAdder() {
	Effect.Appear("locationAddOptions");
	Element.hide("myLocs");
}

////////////////////////////////////////////

function cancelLocAddEdit() {
	Element.hide('locationAddOptions');
	Effect.Appear("myLocs");
}

