
// Popup Windows
function strOpenWindowFeatures(iWindowWidth, iWindowHeight) {
	var iMouseX = 10;
	var iMouseY = 10;
	if ( window.event != null ) {
		iMouseX = window.event.screenX;
		iMouseY = window.event.screenY; }
	var iScreenX = window.screen.availWidth;
	var iScreenY = window.screen.availHeight;
	var iWindowLeft = iMouseX;
	if ( iWindowLeft + iWindowWidth > iScreenX ) { iWindowLeft = iMouseX - iWindowWidth - 10; }
	if ( iWindowLeft < 10 ) { iWindowLeft = 10; }
	var iWindowTop = iMouseY;
	if ( iWindowTop + iWindowHeight > iScreenY - 50 ) { iWindowTop = iMouseY - iWindowHeight - 60; }
	if ( iWindowTop < 10 ) { iWindowTop = 10; }
	return "width=" + iWindowWidth + ", height=" + iWindowHeight + ", left=" + iWindowLeft + ", top=" + iWindowTop + ", resizable=yes";
}
function fnOpenWindow(strWindowURL, strWindowName, iWindowWidth, iWindowHeight) {
	var objNewWindow = window.open(strWindowURL, strWindowName, strOpenWindowFeatures(iWindowWidth, iWindowHeight), false);
	if ( objNewWindow != null ) {
		objNewWindow.focus();
	}
}

// Simplified Popup
// Usage: <a onclick="return popUp(this.href,250,250);" href="popup.aspx">Link</a>
function popUp(theLink, popUpWidth, popUpHeight) {
	var popUpWindow = window.open(theLink, "popUp", "width=" + popUpWidth +
	", height=" + popUpHeight +
	", left=50, top=50, resizable=yes, scrollbars=yes", false);
	if ( popUpWindow != null ) {
		popUpWindow.focus();
	}
	return false;
}
function closePopUp() {
window.close();
}

// This function is also in the admin.js file,
//  and fully documented there...
function fnSimulateRepeaterCommand(strLinkButtonUniqueID, strHiddenFieldUniqueID, strHiddenFieldValue) {
	//alert("fnSimulateRepeaterCommand('" + strLinkButtonUniqueID + "', '" + strHiddenFieldUniqueID + "', '" + strHiddenFieldValue + "');");

	// Split and Join the UniqueID property of the LinkButton to get
	//  the DHTML ID and .NET __doPostBack ID for the server control.
	var aryLinkButtonID = strLinkButtonUniqueID.toString().split(":");
	var strLinkButtonClientID = aryLinkButtonID.join("_");
	var strLinkButtonPostBackID = aryLinkButtonID.join("$");

	// Split and Join the UniqueID property of the HtmlInputHidden
	//  to get the DHTML ID for the server control.
	var aryHiddenFieldID = strHiddenFieldUniqueID.toString().split(":");
	var strHiddenFieldClientID = aryHiddenFieldID.join("_");

	// Look for DHTML objects for the LinkButton and HtmlInputHidden.
	var objLinkButton = document.getElementById(strLinkButtonClientID);
	var objHiddenField = document.getElementById(strHiddenFieldClientID);

	// A variable to verify that everything will work...
	var blnOK = false;
	//alert("typeof __doPostback = '" + typeof __doPostBack + "'");
	if ( typeof __doPostBack == 'function' ) {
		if ( objLinkButton != null ) {
			if ( typeof objLinkButton == 'object' ) {
				if ( objHiddenField != null ) {
					if ( typeof objHiddenField == 'object' ) {
						if ( objHiddenField.value != null ) {
							if ( strHiddenFieldValue.toString().length > 0 ) {
								blnOK = true;
							} else { alert("ERROR! strHiddenFieldValue parameter is an empty string!"); }
						} else { alert("ERROR! DHTML element with id='" + strHiddenFieldClientID + "' has no 'value' attribute!"); }
					} else { alert("ERROR! DHTML element with id='" + strHiddenFieldClientID + "' is not an object!"); }
				} else { alert("ERROR! Could not find a DHTML element with id='" + strHiddenFieldClientID + "'"); }
			} else { alert("ERROR! DHTML element with id='" + strLinkButtonClientID + "' is not an object!"); }
		} else { alert("ERROR! Could not find a DHTML element with id='" + strLinkButtonClientID + "'"); }
	} else { alert("ERROR! Could not find a '__doPostBack' function!"); }

	if ( blnOK == true ) {
		objHiddenField.value = strHiddenFieldValue.toString();
		try {
			__doPostBack(strLinkButtonPostBackID,'');
		} catch (err) {
			alert("JavaScript error:\n" + err.description);
		}
	}
}

// Cross-Browser Block Element Display Toggler
function toggleElement(elId) {
	var visibleStyle = "block";
	if (!document.all) {
		var el = document.getElementById(elId);
		if((el != null) && (typeof(el) == 'object')) {
			var elName = el.tagName.toLowerCase();
			if (elName == "tr") {
				visibleStyle = "table-row";
			}
			else if (elName == "tbody") {
				visibleStyle = "table-row-group";
			}
		} else {
			alert("ElementId '" + elId + "' is null or not an object.");
		}
	}
	var v = ((document.getElementById(elId).style.display == visibleStyle) || (document.getElementById(elId).style.display == ""));
	document.getElementById(elId).style.display = v ? "none" : visibleStyle;
	return false;
}

// Toggle visibility and link value
function toggleFields(containerId, anchorId, invisValue, visValue) {
	toggleElement(containerId);
	var v = document.getElementById(containerId).style.display == "none";
	document.getElementById(anchorId).lastChild.nodeValue = v ? invisValue : visValue;
	// \u2013 = &#8211; = en dash
	//document.getElementById(anchorId).title = v ? nvisValue : visValue;
}

// Used for multiple window.onload events
// http://www.sitepoint.com/blogs/2004/05/26/closures-and-executing-javascript-on-page-load/
/*
addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
	// more code to run on page load 
});
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
			window.onload = function() {
			oldonload();
			func();
		}
	}
}
function addEvent(elm, evType, fn, useCapture) {
	// Cross-browser event handling by Scott Andrew
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}

// This function is used for the Suckerfish (flyout) menus
sfHover = function() {
	var sf = getElementsByClassName('ul', 'ClearFix nav');
	if (sf == null) { return; }
	var sfEls = sf.getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

// Use this to access elements by class instead of id
function getElementsByClassName(elTagName, elClassName) {
	if (document.getElementsByTagName) {
		var elementList = document.getElementsByTagName(elTagName);
		for (var i = 0; i < elementList.length; i++) {
			if (document.all) {
				theClassName = elementList[i].getAttribute('className');
			} else {
				theClassName = elementList[i].getAttribute('class');
			}
			if (theClassName == elClassName) {
				return elementList[i];
			}
		}
	}
}

// Bookmark this page
// From http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=13
/*
usage:

<a href="<%= Request.Url.AbsoluteUri %>" title="Owensboro Medical Health System Bookmark"
rel="sidebar" onclick="bookmarkPage(); return false;">Bookmark This Page</a>

Where href, title, and rel values are used by Firefox
*/
function bookmarkPage() {
	if ( window.sidebar && window.sidebar.addPanel ) {
		// Gecko (Netscape 6 etc.) - add to Sidebar
		window.sidebar.addPanel(this.title, this.href, '');
	} else if ( window.external && ( navigator.platform == 'Win32' || ( window.ScriptEngine && ScriptEngine().indexOf('InScript') + 1 ) ) ) {
		/*
		IE Win32 or iCab - checking for AddFavorite produces errors in
		IE for no good reason, so I use a platform and browser detect.
		adds the current page as a favourite; if this is unwanted,
		simply write the desired page in here instead of 'location.href'
		*/
		window.external.AddFavorite(location.href, document.title);
	} else if ( window.opera && window.print ) {
		// Opera 6+ - add as sidebar panel to Hotlist
		return true;
	} else if ( document.layers ) {
		// NS4 & Escape - tell them how to add a bookmark quickly (adds current page, not target page)
		window.alert('Please click OK then press Ctrl+D to create a bookmark');
	} else {
		// other browsers - tell them to add a bookmark (adds current page, not target page)
		window.alert('Please use your browser\'s bookmarking facility to create a bookmark');
	}
}
function createBookmarkLink() {
	var theContainer = document.getElementById('EmailPrint');
	if (theContainer) {
		var theFirstLi = theContainer.getElementsByTagName('li')[1];
		/*
		create this
		<li class="Bookmark"><a href="<%= Request.Url.AbsoluteUri %>"
		title="Owensboro Medical Health System Bookmark" rel="sidebar"
		onclick="bookmarkPage(); return false;">Bookmark Page</a></li>
		*/
		var theBookmarkLi = document.createElement('li');
		theBookmarkLi.className = 'Bookmark';
		var theBookmarkAnchor = document.createElement('a');
		theBookmarkAnchor.href = '';
		theBookmarkAnchor.title = 'Owensboro Medical Health System Bookmark';
		theBookmarkAnchor.rel = 'sidebar';
		theBookmarkAnchor.onclick = function (){ bookmarkPage(); };
		var theBookmarkText = document.createTextNode('Bookmark Page');
		theBookmarkAnchor.appendChild(theBookmarkText);
		theBookmarkLi.appendChild(theBookmarkAnchor);
		/* Paste the new bookmark li as second li element in the list */
		var theUl = theContainer.getElementsByTagName('ul')[0];
		theUl.insertBefore(theBookmarkLi, theFirstLi.nextSibling);
	}
}
addEvent(window, 'load', createBookmarkLink, false);
function addEvent(elm, evType, fn, useCapture) {
	// Cross-browser event handling by Scott Andrew
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}