// added by Thomas Hjort march 2007
var newWindow = null;

function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popUpWin(url, type, strWidth, strHeight){
	
	closeWin();
	
	if (type == "fullScreen"){
		strWidth = screen.availWidth - 10;
		strHeight = screen.availHeight - 160;
	}
	
	var tools="";
	if (type == "standard" || type == "fullScreen" && (strWidth != null && strHeight != null)) tools = "resizable,toolbar=yes,location=yes,scrollbars=no,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "standard" || type == "fullScreen" && (strWidth == null || strHeight == null)) tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,top=0,left=0";
	if (type == "console") tools = "resizable,toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+strHeight+",left=0,top=0";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}


// JavaScript Document
function addLoadEvent(func) 
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	} 
	else 
	{
		window.onload = function() 
		{
			oldonload();
			func();
		}
	}
}



function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined') {
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined') {
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined') {
		window.attachEvent('onload', fn);
	}
	else {
		return false;
	}
	return true;
};

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;
};

var prefsLoaded = false;
var currentFontSize = 11;
var currentFontType = 1;
function revertStyles(){
	currentFontType = 1;
	setFontFace(1);
	
	currentFontSize = 11;
	changeFontSize(0);

}

function changeFontSize(sizeDifference){
	currentFontSize = parseInt(currentFontSize) + parseInt(sizeDifference);

	if(currentFontType == 1){
		if(currentFontSize > 14){
			currentFontSize = 14;
		}else if(currentFontSize < 11){
			currentFontSize = 11;
		}
	}else{
		if(currentFontSize > 16){
			currentFontSize = 16;
		}else if(currentFontSize < 11){
			currentFontSize = 11;
		}
	}
	setFontSize(currentFontSize);
};
function setFontSize(fontSize){
	var stObj = (document.getElementById) ? document.getElementById('mainarea') : document.all('mainarea');
	stObj.style.fontSize = fontSize + 'px';
};
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
};

function readCookie(name) {
  var nameEQ = name + "=";
  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 null;
};

window.onload = setUserOptions;

function setUserOptions(){
	if(!prefsLoaded){
		cookie = readCookie("fontFace");
		currentFontType = cookie ? cookie : 1;
		//setFontFace(currentFontType);

		cookie = readCookie("fontSize");
		currentFontSize = cookie ? cookie : 11;
		setFontSize(currentFontSize);

		prefsLoaded = true;
	}

}

window.onunload = saveSettings;

function saveSettings()
{
  createCookie("fontSize", currentFontSize, 365);
  createCookie("fontFace", currentFontType, 365);
}


/* addEvent function found at http://www.scottandrew.com/weblog/articles/cbs-events */
function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

/* createElement function found at http://simon.incutio.com/archive/2003/06/15/javascriptWithXML */
function createElement(element) {
	if (typeof document.createElementNS != 'undefined') {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
}

function insertTop(obj) {
	// Create the two div elements needed for the top of the box
	d=createElement("div");
	d.className="bt"; // The outer div needs a class name
    d2=createElement("div");
    d.appendChild(d2);
	obj.insertBefore(d,obj.firstChild);
}

function insertBottom(obj) {
	// Create the two div elements needed for the bottom of the box
	d=createElement("div");
	d.className="bb"; // The outer div needs a class name
    d2=createElement("div");
    d.appendChild(d2);
	obj.appendChild(d);
}

function initCB()
{
	// Find all div elements
	var divs = document.getElementsByTagName('div');
	var cbDivs = [];
	for (var i = 0; i < divs.length; i++) {
	// Find all div elements with cbb in their class attribute while allowing for multiple class names
		if (/\bcbb\b/.test(divs[i].className))
			cbDivs[cbDivs.length] = divs[i];
	}
	// Loop through the found div elements
	var thediv, outer, i1, i2;
	for (var i = 0; i < cbDivs.length; i++) {
	// Save the original outer div for later
		thediv = cbDivs[i];
	// 	Create a new div, give it the original div's class attribute, and replace 'cbb' with 'cb'
		outer = createElement('div');
		outer.className = thediv.className;
		outer.className = thediv.className.replace('cbb', 'cb');
	// Change the original div's class name and replace it with the new div
		thediv.className = 'i3';
		thediv.parentNode.replaceChild(outer, thediv);
	// Create two new div elements and insert them into the outermost div
		i1 = createElement('div');
		i1.className = 'i1';
		outer.appendChild(i1);
		i2 = createElement('div');
		i2.className = 'i2';
		i1.appendChild(i2);
	// Insert the original div
		i2.appendChild(thediv);
	// Insert the top and bottom divs
		insertTop(outer);
		insertBottom(outer);
	}
}


// measures the innerHTML and expands footer to bottom of page
function resizeFooter (){  
			var oFooter = document.getElementById("footer");  
			var footerHeight = 50;
			var htmlheight = document.body.scrollHeight; 
			var windowheight;
			if (self.innerHeight) // all except Explorer
			{
				windowheight = self.innerHeight - 1;
			}
			else if (document.documentElement && document.documentElement.clientHeight)
				// Explorer 6 Strict Mode
			{
				windowheight = document.documentElement.clientHeight;
			}
			else if (document.body) // other Explorers
			{
				windowheight = document.body.clientHeight;
			}
		if ( htmlheight < windowheight ) { 
				document.body.style.height = windowheight + "px"; 
				oFooter.style.height = (windowheight - htmlheight) + 42 + "px"; 
			}  
			else { 
			document.body.style.height = footerHeight + "px"; 
			oFooter.style.height = footerHeight + "px"; 
		}  
	} 
	
function checkAlert (){
    var oAlertDiv = document.getElementById("alertDiv");
    if (oAlertDiv != null)
    {
        alert(oAlertDiv.innerHTML);
    }
}

function externalLinkHandler(e) {
    var target;
    if (!e) var e = window.event;
    if (e.target) target = e.target;
    else if (e.srcElement) target = e.srcElement;
    if (target.nodeType == 3) // defeat Safari bug
        target = target.parentNode;
    
    popUpWin("/Website/Pages/redirect.aspx?redirect=" + target.href, "standard", null, null);
    
    return false;
}

function isInArray(arr, value)
{
    //alert("array = " + arr + ", value = " + value);
    for (i in arr)
    {
        if (arr[i] == value)
        {
            return true;
        }
    }
    return false;
}

function parseATags() {
    var mainArea, aTags, aTag, href, domain;
    var httpRegex, match;
    httpRegex = /^https?:\/\/(?:(([^\.\/]+)\/)|(?:[^\.]+\.{1})?([^\.\/]+\.[^\.\/]+))/;
    
    if (forcePopupOfExternalLinks)
    {
        mainArea = document.getElementById("mainarea");
        if (mainArea != null)
        {
            aTags = mainArea.getElementsByTagName("a");
            for (aTag in aTags)
            {
                if (aTags[aTag].href != null)
                {
                    if (aTags[aTag].href.match(httpRegex) && aTags[aTag].id != "externalLink")
                    {
                        match = httpRegex.exec(aTags[aTag].href);
                        if (match[2] != undefined && match[2] != "")
                        {
                            domain = match[2];
                        }
                        else
                        {
                            domain = match[3];
                        }
                        
                        if (!isInArray(safeDomainArray, domain))
                        {
                            aTags[aTag].onclick = externalLinkHandler;
                        }
                    }
                }
            }
        }
    }
}

/*
function replaceTitle () {
    document.title = document.title.replace(/\<[^>]*\>/, " ");
}
*/

    /* calls the function after the page is loaded */
		addLoadEvent( initCB );
		addLoadEvent( resizeFooter );
		addLoadEvent( checkAlert );
		addLoadEvent( parseATags );
