// SOM sitewide JavaScripts
// alert('this page is including:/res/scripts/sitewide.js');


// image rollover functionality - modified version of macromedia scripts
/*
EXAMPLE IMAGE ROLLOVER
<a href="#" onMouseOut="EN_swapImgRestore()" onMouseOver="EN_swapImage('image_lo','','image_hi.gif',0)"><img name="image_lo" border="0" src="image_lo.gif"></a>
*/

function EN_swapImgRestore() { //v3.0
  var i,x,a=document.EN_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function EN_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=EN_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function EN_swapImage() { //v3.0
  var i,j=0,x,a=EN_swapImage.arguments; document.EN_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=EN_findObj(a[i]))!=null){document.EN_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// quickIndex functionality 
function doQuickIndex(){
	if (siteToolForm.quickIndex.value != '')
	{
		document.location.href = siteToolForm.quickIndex.value;
	}
}

function doSiteSearch(_default)
{
	if (siteToolForm.coSiteSearch_term.value == '' || siteToolForm.coSiteSearch_term.value == _default)
	{
		alert('Please provide a search term');
	}
	else
	{
		siteToolForm.submit();
	}
}

function popupWindow(_url, _width, _height, _name)
{
	window.open(_url, _name, "status,height=" + _height + ",width=" + _width + ",scrollbars=no");
}

function somSwitchDisplayMode(_mode, _target, _action)
{
	document.somDisplayModeForm.target = _target;
	document.somDisplayModeForm.action = _action;
	document.somDisplayModeForm.displayMode.value = _mode;
	document.somDisplayModeForm.submit();
}

function clearText(thefield)
{
	if (thefield.defaultValue==thefield.value)
	thefield.value = ""
}


var gLoadFunctions = new Array();

function addOnload(func)

{
	if (!gLoadFunctions.length && window.onload)
		 gLoadFunctions[gLoadFunctions.length] = window.onload;
		 
	gLoadFunctions[gLoadFunctions.length] = func;
	window.onload = winOnLoad;
}

// The master onLoad function.
function winOnLoad()

{
	var index = 0;

	for (index = 0; index < gLoadFunctions.length; index++) {
		gLoadFunctions[index]();
	}
	
		// fix external links for XHTML
	 if (!document.getElementsByTagName) return; 
	 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"; 
	 }
}

function setPageStyles(nSubsiteId) {
	// only used for main site
	if(nSubsiteId == 0) {
		for(i=0; (a = document.getElementsByTagName("style")[i]); i++) {
			if (a.getAttribute("title").toLowerCase() == getUserStyleSheet().toLowerCase())
				a.disabled = false;
			else
				a.disabled = true;
		}
	}
}
function setUserStyleSheet(value) {
	var date = new Date();
	date.setTime(date.getTime()+(1*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = "userstylesheet="+value+expires+"; path=/";
	window.location.reload(true)
}
function getUserStyleSheet() {
	var nameEQ = "userstylesheet=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return "currentstyle";
}

function writePrintButton() {
	document.write('<a onkeypress="window.print();" onclick="window.print();"><img id="printButton" class="printButton" src="/images/buttons/print.png" alt="Print" title="Print"/></a>');
}

function isDefined(ckVar)
{
	var DEFINED_V=true;

   	function handleError() {
    	DEFINED_V=false;
    	return true;
   	}
   	var oldOnError=window.onerror;
   	window.onerror=handleError;
   	try { 
   		eval(ckVar);
   	}catch (e) 
   	{ DEFINED_V=false;
   	}
   	window.onerror=oldOnError;
   	return DEFINED_V;
}

function popUpWin(url, type, strWidth, strHeight){
	if (type == "fullScreen"){
		strWidth = screen.availWidth - 10;
		strHeight = screen.availHeight - 160;
	}
	var tools="";
	if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "console") tools = "resizable,toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+strHeight+",left=0,top=0";
	window.open(url, 'newWin', tools).focus();
}