function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/* Functions to unobtrusively open links in new windows when className is "new-window" */

/* Create the new window */
function openInNewWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {
		// Change "_blank" to something like "newWindow" to load all links in the same new window
	    var newWindow = window.open(this.getAttribute('href'), '_blank');
		if (newWindow) {
			if (newWindow.focus) {
				newWindow.focus();
			}
			return false;
		}
		return true;
	}
}

/*
Add the openInNewWindow function to the onclick event of links with a class name of "new-window"
*/
function getNewWindowLinks() {
	
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
		// Find all links 
		var links = document.getElementsByTagName('a');
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "non-html"
			if (/\bnew-window\b/.test(link.className)) {
				// append new window function to onclick & onkeypress events (for accessibility!)
				link.onclick = openInNewWindow;
				link.onkeypress = openInNewWindow;
			}
		}
		objWarningText = null;
	}
}

/*
CLEAR ON FOCUS
*/
function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}

/*
SHOW HIDE DIV
*/
function toggleBox(szDivID, iState) // 1 visible, 0 hidden
{
   var obj = document.layers ? document.layers[szDivID] :
   document.getElementById ?  document.getElementById(szDivID).style :
   document.all[szDivID].style;
   obj.visibility = document.layers ? (iState ? "show" : "hide") :
   (iState ? "visible" : "hidden");
}



/*
800 X 600 users changing styles
*/
function calculateWindowWidth() {
var div1=document.getElementById('wrapper');
var div2=document.getElementById('leftCol');
var div3=document.getElementById('containerLogo');
var div4=document.getElementById('containerLogoNoImg');
var div5=document.getElementById('containerColStretch');
var div6=document.getElementById('container2ColStretch');
var div7=document.getElementById('loginForm');
var div8=document.getElementById('containerButton');
var div9=document.getElementById('rightCol');

	if (navigator.userAgent.indexOf("Firefox")!=-1){
		if (window.innerWidth < 800) {
			div1.className="smallWrap";
			div2.className="smallLeftCol";
			if (div3){randomvalue("smallLeftCol");}
			if (div4){div4.className="smallLeftCol";}
			if (div5){div5.className="smallLeftCol";}
			if (div6){div6.className="smallLeft2Col";}
			if (div7){div7.className="smallLeft3Col";}
			if (div8){div8.className="smallLeft4Col";}
			div9.className="smallPosRightCol";
		}else if (window.innerWidth > 800){
			div1.className="largeWrap";
			div2.className="largeLeftCol";
			if (div3){randomvalue("largeLeftCol");}
			if (div4){div4.className="largeLeftCol";}
			if (div5){div5.className="largeLeftCol";}
			if (div6){div6.className="largeLeft2Col";}
			if (div7){div7.className="largeLeft3Col";}
			if (div8){div8.className="largeLeft4Col";}
			div9.className="largePosRightCol";
		}
	}else{
		if (document.documentElement.clientWidth < 800) {
			div1.className="smallWrap";
			div2.className="smallLeftCol";
			if (div3){randomvalue("smallLeftCol");}
			if (div4){div4.className="smallLeftCol";}
			if (div5){div5.className="smallLeftCol";}
			if (div6){div6.className="smallLeft2Col";}
			if (div7){div7.className="smallLeft3Col";}
			if (div8){div8.className="smallLeft4Col";}
			div9.className="smallPosRightCol";
		}else if (document.documentElement.clientWidth > 800) {
			div1.className="largeWrap";
			div2.className="largeLeftCol";
			if (div3){randomvalue("largeLeftCol");}
			if (div4){div4.className="largeLeftCol";}
			if (div5){div5.className="largeLeftCol";}
			if (div6){div6.className="largeLeft2Col";}
			if (div7){div7.className="largeLeft3Col";}
			if (div8){div8.className="largeLeft4Col";}
			div9.className="largePosRightCol";
		}
	}
}

function resizeAll(){
	window.onresize=calculateWindowWidth;
}

/*
ROLLOVER DYNAMIC ON LINKS
*/

function enableTooltips(id){
var links,i,h;
if(!document.getElementById || !document.getElementsByTagName) return;
h=document.createElement("span");
h.id="btc";
h.setAttribute("id","btc");
h.style.position="absolute";
document.getElementsByTagName("body")[0].appendChild(h);
if(id==null) links=document.getElementsByTagName("a");
else links=document.getElementById(id).getElementsByTagName("a");
for(i=0;i<links.length;i++){
	if (links[i].className == "divOver"){
		Prepare(links[i]);
    }
	}
}

function Prepare(el){
var tooltip,t,b,s,l;
t=el.getAttribute("title")+"...";
if(t==null || t.length==0) t="link:";
el.removeAttribute("title");
tooltip=CreateEl("span","tooltip");
s=CreateEl("span","top");
s.appendChild(document.createTextNode(t));
tooltip.appendChild(s);
/*b=CreateEl("b","bottom");
l=el.getAttribute("href");
if(l.length>30) l=l.substr(0,27)+"...";
b.appendChild(document.createTextNode(l));
tooltip.appendChild(b);*/
setOpacity(tooltip);
el.tooltip=tooltip;
el.onmouseover=showTooltip;
el.onmouseout=hideTooltip;
el.onmousemove=Locate;
}

function showTooltip(e){
document.getElementById("btc").appendChild(this.tooltip);
Locate(e);
}

function hideTooltip(e){
var d=document.getElementById("btc");
if(d.childNodes.length>0) d.removeChild(d.firstChild);
}

function setOpacity(el){
el.style.filter="alpha(opacity:95)";
el.style.KHTMLOpacity="0.95";
el.style.MozOpacity="0.95";
el.style.opacity="0.95";
}

function CreateEl(t,c){
var x=document.createElement(t);
x.className=c;
x.style.display="block";
return(x);
}

function Locate(e){
var posx=0,posy=0;
if(e==null) e=window.event;
if(e.pageX || e.pageY){
    posx=e.pageX; posy=e.pageY;
    }
else if(e.clientX || e.clientY){
    if(document.documentElement.scrollTop){
        posx=e.clientX+document.documentElement.scrollLeft;
        posy=e.clientY+document.documentElement.scrollTop;
        }
    else{
        posx=e.clientX+document.body.scrollLeft;
        posy=e.clientY+document.body.scrollTop;
        }
    }
document.getElementById("btc").style.top=(posy+20)+"px";
document.getElementById("btc").style.left=(posx-20)+"px";
}

/*BACKGROUND HOME PAGE*/
function randomvalue(classe) {
    if (document.getElementById("containerLogo")){
		var mathRes = Math.floor(Math.random() * (1 + 2 - 0) + 0);
        document.getElementById("containerLogo").className = classe + " headerHome" + mathRes;
		document.getElementById("primNavUl").className = "homepage" + mathRes;
    }
}
/******POPUP****************/
function openWindow(src,name){	
	window.open(src,name,'toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0,left=250,top=100,width=460,height=500');
}

function openPrintWindow(src,name){	
	window.open(src,name,'toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0,left=250,top=100,width=530,height=500');
}

/****Set Blank Summary*****/

/* This function is used to check to see if the Summary Title has been filled on a page  */
/* if it hasn't but Summary Text has been filled in then the page title will be used for */
/* Summary Title.                                                                        */

function equaliseTitle()
{
    if(document.getElementById('qaApprovePublish_anchor') != null)
    {
        document.getElementById('qaApprovePublish_anchor').onclick = function()
        {
            if(document.getElementById('ctl00$cpMainContent$Summary$TextField1$ctl00$TextField') != null)
            {
                var field1 = document.getElementById('ctl00$cpMainContent$Summary$TextField1$ctl00$TextField');
                var field2 = document.getElementById('ctl00_cpMainContent_Summary_NoteField9_ctl00_TextField');
                
                if(field2.value != "" && field1.value == "")
                {
		            field1.value = document.title;
                }
            }
        }
    }
}

/* This function performs the same action as the DefaultButton  */
/* attribute except that it validates in XHTML                  */

function KeyDownHandler(btn)
{
    // process only the Enter key
    if (event.keyCode == 13)
    {
        // cancel the default submit
        event.returnValue=false;
        event.cancel = true;
        // submit the form by programmatically clicking the specified button
        btn.click();
    }
}

addLoadEvent(getNewWindowLinks);
addLoadEvent(calculateWindowWidth);
addLoadEvent(resizeAll);
addLoadEvent(enableTooltips);
addLoadEvent(equaliseTitle);