//ComWeb JavaScript file - 5Aug05
//06Jul07 DM menuListLG1
//08Aug07 AP Fix news ticker scroll
//02Apr11 DP AJAX Navigation
//15Apr11 DM multimedia control


//This function is used by subscribe footer link
function SmallerWindow(url) {
	SmallerWindow(url,500,700);
}
function SmallerWindow(url,iHeight,iWidth) {
	//var iHeight=(window.screen.height - 50)/2;
	//var iWidth=(window.screen.width)/2 + 50;
	//iHeight=500;
	//iWidth=700;
	var iTop = 50;//200
	var iLeft = 90;//200
	var winDetails = 'toolbar=no,menubar=no,scrollbars=yes,titlebar=no,left=' + iLeft + ',top=' + iTop + ',width=' + iWidth + ',height=' + iHeight;
	//alert(winDetails)
	var win3=window.open(url,'',winDetails); 
	win3.focus();
};

function externalAnchors()
{
	var anchors = document.getElementsByTagName("A");
	for (var i = 0; i<anchors.length; i++)
	{
		if ((anchors[i].className.indexOf('external')>-1) && (anchors[i].target==''))
		{
			anchors[i].target='_blank';
		}
	}
}


var bPaused = false, iTickerItem = 1, bItemPause = false, iLastScrollTop = -1, bFirstTime = true, oTicker, oTickerItems, oAllDivs, iPause = 6000;

function removeStyles(oTag)
{
	if (oTag.className)
		oTag.className = oTag.className.replace("cwInformation","").replace("cwContainer","").replace("cwNumber","");
	for (var iTag = 0; iTag < oTag.childNodes.length; iTag++){
		removeStyles(oTag.childNodes[iTag]);
	}
}

function scrollDown()
{
	if (bFirstTime){
		alternatingBanners();
		bFirstTime = false;
		var iTag = 0, oTickerDivs;
		oAllDivs = document.getElementsByTagName('DIV')
		for (var iFindTicker = 0; iFindTicker < oAllDivs.length; iFindTicker++){
			if ((oAllDivs[iFindTicker].className.indexOf('NewsTicker')>-1) && (oAllDivs[iFindTicker].className.indexOf('cwContainer')>-1)){
				oTicker = oAllDivs[iFindTicker].childNodes[0].childNodes[1];
				//oTickerItems = oAllDivs[iFindTicker].childNodes[0].childNodes[1];

				oTickerDivs = oAllDivs[iFindTicker].getElementsByTagName("DIV");
				if (false) {
					oTicker.className+=' NewsTickerContainer';
					
	//				removeStyles(oAllDivs[iFindTicker]);
					oAllDivs[iFindTicker].className = oAllDivs[iFindTicker].className.replace("cwInformation","").replace("cwContainer","").replace("cwNumber","");
					for (iTag = 0; iTag < oTickerDivs.length; iTag++)
						if (oTickerDivs[iTag].className)
							oTickerDivs[iTag].className = oTickerDivs[iTag].className.replace("cwInformation","").replace("cwContainer","").replace("cwNumber","");
				}

				iFindTicker = oAllDivs.length;
				oTicker.onmouseover = function() {bPaused=true};
				oTicker.onmouseout  = function() {bPaused=false};
			}
		}
	}
	if (oTicker)
		setTimeout('scrollDownAction();', iPause);
}

var bReset = true;
function scrollDownAction()
{
//	alert('BEFORE')
//	alert(oTicker.outerHTML)
//	oTicker.scrollTop=10;
//	alert('AFTER')
var iScrollLeft = 0, iItemNum = 0;
	if (!bPaused){
		if (bReset) {bReset = false; oTicker.scrollTop=1;}
		//alert(oTicker.scrollTop + " != " + iLastScrollTop)
		if (oTicker.scrollTop != iLastScrollTop){
			for (iTickerItem = 0; iTickerItem < oTicker.childNodes.length; iTickerItem++)
				if (oTicker.childNodes[iTickerItem].nodeType==1){
					//alert(oTicker.childNodes[iTickerItem].offsetParent.outerHTML)
					if (oTicker.scrollTop == (oTicker.childNodes[iTickerItem].offsetTop-1)){
						bItemPause = oTicker.childNodes[iTickerItem].className.toLowerCase().indexOf('tickeritem')>=0;
	//					alert(oTicker.childNodes[iTickerItem].className.toLowerCase().indexOf('tickeritem'));
						}
				}
			if (bItemPause){
				bItemPause = false;
				iLastScrollTop=oTicker.scrollTop;
				try{
					oTicker.scrollTop++;
				}catch (e){}
				setTimeout('scrollDownAction();', iPause);
			}
			else{
				iLastScrollTop=oTicker.scrollTop;
				try{
					oTicker.scrollTop++;
				}catch (e){}
				setTimeout('scrollDownAction();', 50);
			}
		}
		else{
			iLastScrollTop=0;
			bReset = true
			bItemPause = true
			setTimeout('scrollDownAction();', iPause);
		}
	}
	else{
		setTimeout('scrollDownAction();', 50);
	}
}

function ieHovers()
{
	// The test below should pass for IE6 but fail for IE7
	if (typeof document.body.style.maxHeight == 'undefined'){
		var IE5 = "MSIE 5", IE6 = "MSIE 6", IE7 = "MSIE 7";
		var ver = navigator.appVersion;
		if (!((document.body.className.indexOf("menu-1")>-1) && (document.body.className.indexOf("framed2")>-1))){
			if ((ver.indexOf(IE5)>-1) || (ver.indexOf(IE6)>-1) || (ver.indexOf(IE7)>-1))
			{	
				processDivHovers("menuList");
				processDivHovers("menuListLG1");
				processDivHovers("instanceNav");
			}
		}
	}
}

function processDivHovers(sDiv){
	var sCurrentClass, oMenuLIs, sApplyToClass, iMenuLI;
	sApplyToClass = 'bullet_closed';
	var oMenuDiv = document.getElementById(sDiv);
	var parentLI;
	if (oMenuDiv){
		oMenuLIs = oMenuDiv.getElementsByTagName("LI");
		for (iMenuLI = 0; iMenuLI < oMenuLIs.length; iMenuLI++)
		{
			//alert(oMenuLIs[iMenuLI].innerHTML);
			sCurrentClass = oMenuLIs[iMenuLI].className;
			//if (sCurrentClass.indexOf(sApplyToClass)>1)
			if (true)
			{
				oMenuLIs[iMenuLI].origClassName=oMenuLIs[iMenuLI].className;
				oMenuLIs[iMenuLI].hoverClassName=oMenuLIs[iMenuLI].className + " "+(oMenuLIs[iMenuLI].className+' ').replace(/ /g, 'iehover ')+" iehover";
				oMenuLIs[iMenuLI].onmouseover = function() {this.className = this.hoverClassName;};
				oMenuLIs[iMenuLI].onmouseout  = function() {this.className = this.origClassName;};
        	    //alert(oMenuLIs[iMenuLI].origClassName);
        	    //alert(oMenuLIs[iMenuLI].hoverClassName);
			}
			//alert(sCurrentClass);
			if (sCurrentClass.indexOf("currentPage")>0){
				//alert(sCurrentClass);
				oMenuLIs[iMenuLI].className=oMenuLIs[iMenuLI].className.replace("bullet_closed", "bullet_open");
				parentLI = oMenuLIs[iMenuLI].parentNode.parentNode;
				while (parentLI.nodeName=="LI"){
					parentLI.className=parentLI.className.replace("bullet_closed", "bullet_open");
					parentLI = parentLI.parentNode.parentNode;
				}
								
			}
			
		}
	}
}

function expandMenu(iLevel){
	var oMenuDiv = document.getElementById("menuList");
	var oMenuLIs = oMenuDiv.getElementsByTagName("LI");
	var iThisLevel;
	var sCurrentClass;
	for (var iMenuLI = 0; iMenuLI < oMenuLIs.length; iMenuLI++)
	{
		sCurrentClass = oMenuLIs[iMenuLI].className;
		if (sCurrentClass.indexOf("menulevel")>-1){
			iThisLevel = sCurrentClass.substring(sCurrentClass.indexOf("menulevel")+9,sCurrentClass.indexOf("menulevel")+10);
			iThisLevel = parseInt(iThisLevel) + 1; //we want menu level X to be visible, but closed
			if (iThisLevel>iLevel){
				oMenuLIs[iMenuLI].className = sCurrentClass.replace("bullet_open", "bullet_closed");
/*				if (sCurrentClass.indexOf("bullet_closed")>-1) {oMenuLIs[iMenuLI].className += " bullet_closed";} */
			} else {
				oMenuLIs[iMenuLI].className = sCurrentClass.replace("bullet_closed", "bullet_open");
/*				if (sCurrentClass.indexOf("bullet_open")>-1) {oMenuLIs[iMenuLI].className += " bullet_open";} */
			}
		}
	}
}

/*
//see Bullet Fix in the right column at http://support.microsoft.com/kb/257819
function fixBulletPosition(e,all){
	if(!all)
		bullets.push(e);
	else{
		try{
			for(var i=bullets.length-1;i>=0;i--){
				e=bullets[i];
				var o=e.currentStyle.direction;
				var w=e.offsetWidth;
				if(o==document.getElementById("mainColumn").currentStyle.direction){
					e.style.position="relative";
					e.style.zIndex="9";
				}
				if(o!="rtl")
					e.style.marginLeft="-"+w+"px";

				var p=getParentByTagName(e,"UL");
				if(p){
					if(o!="rtl"){
						var pl=parseInt(p.style.marginLeft,10);
						if(!pl)
							pl=parseInt(p.currentStyle.marginLeft,10);
						if(!pl||w>pl)
							p.style.marginLeft=w+"px";
					}
				}
			}
		}
		catch(e){}
	}
}
*/


function alternatingBanners(){
	var numImages = 10;
	var now = new Date();
	var hour = now.getMinutes();
	document.body.className+=" altBanner"+((hour%numImages)+1);
}

/*******************************************
 * AJAX site navigation functions
 ******************************************/

var oDisableLayer;
var AJAX_QUERYSTRING = "&bAJAXRequest=true&divoptions=-referencelinks";
 
function loadPage(url){
 var xmlhttp;
 //var thisdate = new Date();
 //url += "&hash=" + thisdate.getTime();
 url += AJAX_QUERYSTRING;
 xmlhttp=null;
 if (window.XMLHttpRequest){// code for all new browsers
  xmlhttp=new XMLHttpRequest();
   }
 else if (window.ActiveXObject){// code for IE5 and IE6
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
 if (xmlhttp!=null)
 {
  oDisableLayer.style.display="inline";
  aXmlHttp[iXmlHttp]=xmlhttp;
  xmlhttp.onreadystatechange=new Function("pageFetched("+(iXmlHttp)+", '"+url.replace('\'','\\\'')+"')"); // make sure single quotes don't affect it
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  iXmlHttp++;
 }
 else{
   doMessage("Your browser does not support XMLHTTP.");
 }
 return false;
}

// This function will fetch a DIV by ID from an xmlhttp return stream
function getDivById(bodyTag,sId){
	var oDivs = bodyTag.getElementsByTagName('DIV');
	var oReturn = null;
	for (var i=0; i<oDivs.length; i++){
		if (oDivs[i].id==sId){
			oReturn=oDivs[i];
			i=oDivs.length;
		}
	}
	return oReturn;
}

function updateDiv(sDiv, oDiv){
	//alert(sDiv);
	if (document.getElementById(sDiv)){
		if (getDivById(oDiv,sDiv)){
			document.getElementById(sDiv).innerHTML = getDivById(oDiv,sDiv).innerHTML;
		} else {
			alert(getDivById(oDiv,sDiv));
		}
	}
}

function updateCurrentMenu(newCurrent){
	var ulMainMenu = document.getElementById('mainMenu');
	var aLIs = ulMainMenu.getElementsByTagName('LI');
	var oLI, oA;
	var sNewPageID = getPageID(newCurrent, false)
	for (var iLI = 0; iLI < aLIs.length; iLI++){
		oLI = aLIs[iLI];
		oA = oLI.childNodes[0];
		oLI.className = oLI.className.replace(' currentPage','').replace(' ancestorPage','');
		//alert(oA.href + " = " + newCurrent + ' : ' + (oA.href==newCurrent));
		//if (oA.href==newCurrent){
		if (getPageID(oA.href, false)==sNewPageID){
			oLI.className += ' currentPage';
		}
	}
}

function getPageID(sURL, bTrace){
	var iPageIDPos = sURL.toLowerCase().indexOf("page=")+5;
	var iPageIDEnd = sURL.toLowerCase().indexOf("&", iPageIDPos);
	if (iPageIDEnd == -1){
		iPageIDEnd = sURL.toLowerCase().indexOf("#", iPageIDPos);
		if (iPageIDEnd == -1){iPageIDEnd = 9999;}
	}
	if (bTrace){
		alert(sURL);
		alert(iPageIDPos);
		alert(iPageIDEnd);
		alert(sURL.substring(iPageIDPos, iPageIDEnd));
	}
	return sURL.substring(iPageIDPos, iPageIDEnd);
}

function pageFetched(iThisXmlHttp,url_orig){
 var url = url_orig.replace(AJAX_QUERYSTRING, "");
 var xmlhttp = aXmlHttp[iThisXmlHttp];
 if (xmlhttp.readyState==4){// 4 = "loaded"
   if (xmlhttp.status==200){// 200 = OK
     if (xmlhttp.responseText.indexOf("ComWeb - Unexpected server error") == -1) // not an invalid page - at least not custom 500 error
     {
	
	    // make sure we have the right page and not an error / requires authentication
        if (xmlhttp.responseText.indexOf("class=\"menu-1") > -1) // invalid page
        {
            // follow the hyperlink
            document.location.replace(url);

        } else { // hopefully ok
    	
	        // Have to remove the old TOC before retrieving the new content
	        var oTOC = document.getElementById("TOC");
	        if (oTOC){oTOC.parentNode.removeChild(oTOC);}

	        var bLeftMenu = true;
        	
	        var oDiv = document.createElement('DIV');
	        oDiv.innerHTML = xmlhttp.responseText;
	        updateDiv("mainContent", oDiv);
	        updateDiv("breadcrumbs", oDiv);
	        updateDiv("footer", oDiv);
	        updateDiv("titleBlock", oDiv);
	        updateDiv("rightContent", oDiv);
	        updateDiv("leftFeatureContent", oDiv);

	        // If there is a new TOC that isn't in content (left/right/main) append it to headerTail
	        oTOC = getDivById(oDiv, "TOC");
	        if (oTOC){
		        if (getDivById(oDiv, "TOC").parentNode.id=="headerTail"){
			        document.getElementById("headerTail").appendChild(getDivById(oDiv, "TOC"));
		        }
	        }
        	
	        var iStartBody = xmlhttp.responseText.indexOf("<body");
	        var iEndBody = xmlhttp.responseText.indexOf(">",iStartBody)+1;
	        oDiv.innerHTML = xmlhttp.responseText.substring(iStartBody, iEndBody).replace("body", "div") + "</div>";
	        document.getElementsByTagName('BODY')[0].id=oDiv.childNodes[0].id;
	        document.getElementsByTagName('BODY')[0].className=oDiv.childNodes[0].className;
	        updateCurrentMenu(url);
	        var pageID = url.toLowerCase().substring(url.toLowerCase().indexOf('page=')+5, url.length)
	        if (pageID.indexOf('&')>-1){
		        pageID=pageID.substring(0,pageID.indexOf('&'));
	        }
	        //alert(pageID)
	        window.location.hash = "page:" + pageID;
	        oDisableLayer.style.display="none";

        }
     } else {
        // 500 error - follow the hyperlink to display the problem
        document.location.replace(url);
     }
}
  else
  {
    //doMessage("Problem retrieving XML data");
	//alert(url);
	document.location.replace(url);
  }
 }
}


var oMessage;
function doMessage(sMessage){
// if(!oMessage) oMessage = document.getElementById("doMessage");
// oMessage.innerHTML+=sMessage+" *:* "
	alert(sMessage);
}

var aXmlHttp = new Array(200);
var iXmlHttp = 1;
function changeURL(url){
 document.getElementById("URL").value=url;
 return false;
}


function initAJAXMenu(){
	var sThisSite, sDestSite;
	sThisSite = window.location.href.toLowerCase();
	if ((sThisSite.indexOf('comweb.asp')>-1)&&(!(isNaN(getQuerystring("page", window.location.href.toLowerCase()))))) { // make sure page id we are on is a valid number (ie not sitemap)
		sThisSite = sThisSite.substring(sThisSite.indexOf("/sites/")+7, sThisSite.indexOf("/comweb.asp"));
		var ulMainMenu = document.getElementById('mainMenu');
		var aAnchors = ulMainMenu.getElementsByTagName('A');
		var oAnchor;
		oDisableLayer = document.getElementById("disableLayer");
		for (var iAnchor = 0; iAnchor < aAnchors.length; iAnchor++){
			oAnchor = aAnchors[iAnchor];
			if (oAnchor.href.toLowerCase().indexOf('comweb.asp')>-1){
				sDestSite = oAnchor.href.toLowerCase();
				sDestSite = sDestSite.substring(sDestSite.indexOf("/sites/")+7, sDestSite.indexOf("/comweb.asp"));
				if((sThisSite == sDestSite)&&(!(isNaN(getQuerystring("page", oAnchor.href.toLowerCase()))))){  // make sure page id we are on is a valid number (ie not sitemap)
					oAnchor.onclick=function(){return loadPage(this.href)};
				}
			}
		}
		if (document.location.hash.indexOf('page:')>-1){
			var sURL = window.location.href;
			//alert(sURL);
			if (sURL.toLowerCase().indexOf('page=')>-1){
				sURL = sURL.substring(0,sURL.toLowerCase().indexOf('page=')+5) + document.location.hash.replace('#page:','');
				//alert(sURL);
				loadPage(sURL);
			}
		}
	}
}

// reg exp function used to get querysting paramaters out of the url passed to iy
function getQuerystring(key, url, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(url);
  if(qs == null)
    return default_;
  else
    return qs[1];
}



//===== MULIMEDIA CONTROLS ===========
var PAUSE = 0, PLAY = 1, LOAD = 2, REWIND = 3, POPUP = 4, FRAME = 5;
var oMovie;
var aMovies = new Array(10);
var aPlayedOnce = new Array(10);
var iMovie = 0;
function addMovie(objID){
	aMovies[iMovie]=objID;
	aPlayedOnce[iMovie]=false;
	iMovie++;
}
function doMovie(movie, action, param){
	for (var i = 0; i<aMovies.length; i++){if (aMovies[i]!=undefined){document.getElementById(aMovies[i]).StopPlay();}}
	oMovie = window.document.getElementById(aMovies[movie-1]);
	if(action == PLAY){
		if (!aPlayedOnce[movie-1]) {oMovie.Rewind();bPlayedOnce=true;}
		oMovie.Play();}
	else if(action == PAUSE){oMovie.StopPlay();}
	else if(action == REWIND){oMovie.Rewind();oMovie.Play();}
	else if(action == LOAD){oMovie.LoadMovie(0,param);}
	else if(action == POPUP){oMovie.StopPlay(); popupXY(oMovie.movie,600,400);}
	else if(action == FRAME){oMovie.StopPlay(); oMovie.GoToFrame(param);}
}


/*******************************************
 * Accessibility link functions for font sizes
 ******************************************/

var defaultFontSize = 70;

window.onload = function(e) {
	var cookie = readCookie("size");
	var fontSize = cookie ? cookie : defaultFontSize;
	document.body.style.fontSize = fontSize + "%";
	createCookie("size", fontSize, 2);
}

function increaseFontSize() {
	var cookie = readCookie("size");
	var tempSize = cookie ? cookie : defaultFontSize;
	var fontSize = parseInt(tempSize) + 10;

	document.body.style.fontSize = fontSize + "%";
	createCookie("size", fontSize, 2);
	return false;
}

function decreaseFontSize() {
	var cookie = readCookie("size");
	var tempSize = cookie ? cookie : defaultFontSize;
	var fontSize = parseInt(tempSize) - 10;

	document.body.style.fontSize = fontSize + "%";
	createCookie("size", fontSize, 2);
	return false;
}

function normalFontSize() {
	var fontSize = parseInt(defaultFontSize);
	document.body.style.fontSize = fontSize + "%";
	eraseCookie("size");
	return false;
}

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 var 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;
}

function eraseCookie(name)
{
	createCookie(name,"",0);
}

