var browser=navigator.userAgent.toLowerCase();
var modalReturnTo = '';							//modalReturnTo stores the id of the item to bring to focus when the modal pane closes

function alignMenus(div) {
	//fine tune menu/heading sizes and alignments
	try {
		if (document.getElementById('serviceNav')) {
		switchAllOff();
		document.getElementById('serviceNav').style.height = document.getElementById('serviceNavBar').clientHeight + "px";
		for ( i = 1; i <= 6; i++ ) {
			if (!document.getElementById('menu'+i)) {
				i--;
				break;
			}
		}

		if (i) {
			margin = document.getElementById('serviceNavBar').clientWidth - i*(document.getElementById('menu1').clientWidth);
			if (browser.indexOf("msie 6") != -1) {
				margin = margin +2;
			} else if ((browser.indexOf("firefox") != -1) && (browser.indexOf("firefox/3") == -1)) {
				margin = margin -1;
			}
			document.getElementById(div+'Wrapper').style.marginLeft = margin + "px";
			document.getElementById(div+'Wrapper').style.marginRight = margin + "px";
			document.getElementById(div+'Title').style.lineHeight = document.getElementById('serviceNavBar').clientHeight + "px";
		}
		}
		if (document.getElementById('modalPaneTitle')) {
			document.getElementById(div+'Title').style.lineHeight = document.getElementById('modalPaneTitle').clientHeight + "px";
		}		
		} catch (e) {
	}	
}

function styleTitle(titleClass) {
	    //reset bg color on modal window title element
	    if (window.parent != window.self) {
			if ((parent.document.getElementById('modalPane')) && (typeof titleClass != "undefined")) {
				    var modalDivs = parent.document.getElementById('modalPane').getElementsByTagName("div");
				    for (var x = 0; x < modalDivs.length; x++) { 
						modalDivs[x].className = modalDivs[x].className.replace(/bg[H\d]/i, titleClass);
				    }
			}
			//change resizeModal mtm var to same margin!
			document.getElementById('modalPaneTitle').style.marginTop="-4px";
	    }
}

function toggleSection(id,forceOpen,cookieName) {
	//hide or show a section 
	if (typeof forceOpen == "undefined") {
		forceOpen = false;
	 }
	if (typeof cookieName == "undefined") {
		cookieName = false;
	 }
	try {
                if(document.getElementById('section'+id+'Off').style.display=="none" || forceOpen) {
                        document.getElementById('section'+id+'Off').style.display="inline";
                        document.getElementById('section'+id+'On').style.display="none";
                        if (document.getElementById('section'+id+'Panel')) document.getElementById('section'+id+'Panel').style.display = "block";
                        Nifty('#section'+id+'Title','remove 0');						
                        if(document.getElementById('serviceNavBar') && isTopSection(id)) {
                                Nifty('#section'+id+'Title','transparent tl');
                        } else {
                                Nifty('#section'+id+'Title','transparent top');
                        }
                        if (cookieName) createCookie(cookieName,'on',1);
                        //next 2 help IE keep space between sections if they are collapsed and have a sectionXControls
                        if (document.getElementById('section'+id+'Title')) document.getElementById('section'+id+'Title').style.marginBottom = "0";
                        if (document.getElementById('section'+id+'Wrapper')) document.getElementById('section'+id+'Wrapper').style.marginBottom = "1em";
                } else {
                        document.getElementById('section'+id+'Off').style.display="none";
                        document.getElementById('section'+id+'On').style.display="inline";
                        if (document.getElementById('section'+id+'Panel')) document.getElementById('section'+id+'Panel').style.display = "none";
                        Nifty('#section'+id+'Title','remove 0');
                        if(document.getElementById('serviceNavBar') && isTopSection(id)) {
                                Nifty('#section'+id+'Title','transparent bottom tl');
                        } else {
                                Nifty('#section'+id+'Title','transparent');
                        }
                        if (cookieName) createCookie(cookieName,'off',1);
                        //next 2 help IE keep space between sections if they are collapsed and have a sectionXControls
						if (document.getElementById('section'+id+'Title')) document.getElementById('section'+id+'Title').style.marginBottom = "1em";
                        if (document.getElementById('section'+id+'Wrapper')) document.getElementById('section'+id+'Wrapper').style.marginBottom = "0";        
                }

                //Nudge window to force browser to re-render. Using resizeBy is buggy in some browsers - the window origin can be reset too!
                window.scrollBy(0,1);
                window.scrollBy(0,-1);

                alignMenus('section'+id+'Panel');
        } catch (e) {
        }
}

function toggleInactiveLayer(id) {
	//gray out the background behind the modal pane
	
	try {
		if (id == "deactivateFrame") {
			//deactiveFrame layer is relative to userModal div but must be offset to the page origin
			document.getElementById(id).style.top = "-" + document.getElementById('modalPane').style.top;
			document.getElementById(id).style.left = "-" + document.getElementById('modalPane').style.left;	
		}

		var windowSize = getWindowSize();
		var width = document.body.scrollWidth;
		var height = document.body.scrollHeight;
		var w_height = windowSize[1];
		if (w_height > height) height = w_height;			//if body is short compared to window (probably opened in new window!)
		if (window.parent != window.self) height += 20;		//add a little overlap to stop a white line appearing

		
		if(document.getElementById(id) && document.getElementById(id).style.display=="block") {
			document.getElementById(id).style.display = "none";
			document.getElementById(id).style.width = "0"
			document.getElementById(id).style.height = "0"
		} else {
			document.getElementById(id).style.display = "block";
			document.getElementById(id).style.width = width + "px"
			document.getElementById(id).style.height = height + "px"
		}

	} catch (e) {
	}
}

function isTopSection(id) {
	//is this the top section? it will need different rounded corners
	for ( var i = 1; i <= id; i++ ) {
		if (document.getElementById('section'+i+'Wrapper')) {
			if (i == id) {
				return true;
			} else {
				return false;
			}
		}
	}	
	return false;
}

function showMenu(evt,id) {
	//show popup menu at mouse click location or center of browser window
	
	try {
		
		if (showPopups()) {
			var width, height;
			var clickAt = getClick(evt);
			var left = clickAt[0];
			var top = clickAt[1];
			//alert(left + ", " + top);
		
			var centerAt = getCenter();
			var x = centerAt[0];
			var y = centerAt[1];
			//alert(x + ", " + y);
	
			//get offsets from and dimensions of event trigger to check whether event was via keypress rather than true mouse click
			var offsetFrom = getOffset();
			var offset_x = offsetFrom[0];
			var offset_y = offsetFrom[1];
			//alert(offset_x + ", " + offset_y);
		
			//now event trigger dimensions
			//doesn't work perfectly...in IE when a link is an img and it is activated by the keyboard then the offsets are from the parent container (e.g. a table cell)
			//var trigger_x = document.getElementById(id+'Return').offsetWidth;
			//var trigger_y = document.getElementById(id+'Return').offsetHeight;
			//alert(trigger_x + ", " + trigger_y);
		}
		
		document.getElementById(id).style.display = "block";

		if (!showPopups()) throw(e);
	
		if (browser.indexOf("msie 6") != -1) {
			document.getElementById(id).style.width = "0"; 		//stop IE browsers increasing clientWidth by 16px on every click of the menu
		} else {
			document.getElementById(id).style.width = "auto"; 	//stop some other browsers (FF at least) increasing clientWidth by 16px on every click of the menu
		}
		width = document.getElementById(id).clientWidth;
		//alert(width);
		if (!width || width >= (2*x)-20 ) {
			//bad menu width, reset to default
			width = 250;
		}
		
		document.getElementById(id+"Scroll").style.height = "auto";
		height = document.getElementById(id+"Scroll").clientHeight;
		//alert(height);
		if (!height || height >= (2*y)-20) {
			//bad menu height, reset to default
			height = 200;			
		}
		document.getElementById(id+"Scroll").style.height = height+"px";
		
		//document.getElementById(id).style.height = "auto"; //consistent with width above (possibly not needed)
		height = document.getElementById(id).clientHeight;
		//alert(height);
		if (!height) {
			//bad menu height, reset to default
			height = 250;
		}
	
//		if (!left || !top || left <= 0 || top <= 0 || offset_x > trigger_x || offset_x < 0 ||  offset_y > trigger_y || offset_y < 0 ) {
		if (!left || !top || left <= 0 || top <= 0 || offset_x < 0 || offset_y < 0 ) {
			//bad click location...return screen center
			left = x + (width/2);	//add it because of left-width below
			top = y - (height/2);
			//alert(left + ", " + top);
		}
				
		// position menu at mouse event location
		if (left > x) {
			//offset menu to left of mouse click, the menu is on the left side of the screen
			document.getElementById(id).style.left = parseInt(left-width) + "px";
		} else {
			//offset menu to right of mouse click, the menu is on the right side of the screen
			document.getElementById(id).style.left = parseInt(left-10) + "px";		
		}
		document.getElementById(id).style.top = parseInt(top-10) + "px";
		document.getElementById(id).style.width = width + "px"; //need this to get IE to behave!
		document.getElementById(id+"Close").focus();
		
		//resizeModal();

		toggleInactiveLayer('deactivatePage');

		//service specific show menu function
		if (typeof showMenuAppend == 'function') {
			showMenuAppend(id);		//this will be defined in service specific JS
		} 
	} catch (e) {
		//alert(e);
		document.getElementById(id+"Close").focus();
		return true;
	}
	
	return false;
}

function showModal(id,returnTo,bg) {
	//show modal pane at center of browser window
	
	try {	
		var width, height;
	
		modalReturnTo = returnTo;				//modalReturnTo is a global var to store the id of the item to bring to focus when the modal pane closes
		
		if (showPopups()) {
			var centerAt = getCenter();
			var x = centerAt[0];
			var y = centerAt[1];
			//alert(x + ", " + y);
		}

		//fix lame IE6 that renders form input above standard layer model
		if (browser.indexOf("msie 6")!=-1) {
			if (document.getElementById('mainBody')) {
				var formInput = document.getElementById('mainBody').getElementsByTagName("input");
				for (var i = 0; i < formInput.length; i++) {
					if (formInput[i].className == "fInput") {
						formInput[i].style.visibility = "hidden";
						//formInput[i].style.display = "none";
					}
				}
				var formSelect = document.getElementById('mainBody').getElementsByTagName("select");
				for (var i = 0; i < formSelect.length; i++) {
					if (formSelect[i].className == "fInput") {
						formSelect[i].style.visibility = "hidden";
						//formSelect[i].style.display = "none";
					}
				}
			}		
		}
		
		document.getElementById(id).style.display = "block";

		if (!showPopups()) throw(e);

		var windowSize = getWindowSize();
		width = parseInt((windowSize[0] / 100) * 90);
		height = parseInt((windowSize[1] / 100) * 90);

		if (height == 0 || height<520) height = 520;
		if (width == 0 || width<600) width = 600;
		//alert(width);
		//alert(height);
						
		// position menu at center of screen
		document.getElementById(id).style.left = parseInt(x - (width/2)) + "px";
		
		//Math explanation for line below!
		//1/20 height of window + scroll offset will place window 5% down from top. Max height window will be centered then
		//(windowSize[1] / 20) + (y - (windowSize[1] / 2))
		//(windowSize[1] * .05) + (y - (windowSize[1] * .5));
		//y - (windowSize[1] * .45)
		document.getElementById(id).style.top = parseInt(y - (windowSize[1] * .45)) + "px";

		document.getElementById(id).style.width = width + "px"; //need this to get IE to behave!
		//document.getElementById(id+'Iframe').style.height = (height - (document.getElementById(id).clientHeight - 55)) + "px"; //need this to get IE to behave!
		
		toggleInactiveLayer('deactivatePage');
		document.getElementById(id+"Close").focus();

		//service specific show modal function
		if (typeof showModalAppend == 'function') {
			showModalAppend(id);		//this will be defined in service specific JS
		} 

	} catch (e) {
	}
}

function showPopups() {
	//should we show our modal panes (absolute positioned div)?
	if (browser.indexOf("msie")!=-1 && browser.indexOf("mac")!=-1) return false;
	if (browser.indexOf("netscape")!=-1 && browser.indexOf("mac")!=-1) return false;
	if (!document.getElementById) return false;
	//anything else?
	
	return true;
}

function hideModal(id,src) {
	//hide modal pane
	
	try {	
		if (id == "modalPane") {			
			if (typeof src != "undefined" && src != '') {
					window.frames[id+'Iframe'].location = src;
					//document.getElementById(id+'Iframe').src = src;
			 }
			document.getElementById(id+'Iframe').style.height = "55px";
		}

		//fix lame IE6 that renders form input above standard layer model
		if (browser.indexOf("msie 6")!=-1) {
			if (document.getElementById('mainBody')) {
				var formInput = document.getElementById('mainBody').getElementsByTagName("input");
				for (var i = 0; i < formInput.length; i++) {
					if (formInput[i].className == "fInput") {
						formInput[i].style.visibility = "visible";
						//formInput[i].style.display = "inline";
					}
				}
				var formSelect = document.getElementById('mainBody').getElementsByTagName("select");
				for (var i = 0; i < formSelect.length; i++) {
					if (formSelect[i].className == "fInput") {
						formSelect[i].style.visibility = "visible";
						//formSelect[i].style.display = "inline";
					}
				}
			}		
		}

		document.getElementById(id).style.display = 'none';
		//document.getElementById(id+'Title').innerHTML = "";
		//document.getElementById(id+'Title').innerHTML = "Loading...";

		if (showPopups()) toggleInactiveLayer('deactivatePage');

		//service specific hide modal function
		if (typeof hideModalAppend == 'function') {
			hideModalAppend(id);		//this will be defined in service specific JS
		} 
	} catch (e) {
		//alert(e);
	}
}

function cleanupModal(id) {
	//run after a modal pane is closed. container for miscellaneous cleanup items
	
	if (typeof id == "undefined" || id == '') {
		id = modalReturnTo;
	 }
	
	try {	
		document.getElementById(id).focus();	//reset tabindex back to item that opened menu
	} catch (e) {
	}
	
	return false;
}

function getCenter() {
	//return the browser window center location
	
	var scrollTop = 0;
	var scrollLeft = 0;

	var windowSize = getWindowSize();
	var width = windowSize[0];
	var height = windowSize[1];

	if (document.documentElement) {
		scrollTop = document.documentElement.scrollTop;
		scrollLeft = document.documentElement.scrollLeft;
	} else  if (document.body) {
		scrollTop = document.body.scrollTop;
		scrollLeft = document.body.scrollLeft;
	}

	return [parseInt((width/2)+scrollLeft), parseInt((height/2)+scrollTop)] 
}

function getWindowSize() {
	//return the browser window dimensions

	var width = 800;
	var height = 600;

	try {
		if (window.innerHeight) {
			width = window.innerWidth;
			height = window.innerHeight;
		} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			width = document.documentElement.clientWidth;
			height = document.documentElement.clientHeight;
		} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
			width = document.body.clientWidth;
			height = document.body.clientHeight;
		}
	} catch (e) {
	}

	//alert(width + ", " + height);
	return [width, height] 
}

function getClick(evt) {
	//return the mouse click location
	var left, top;
	if (!evt) {
		evt = event;
	}
	if (evt.pageX) {
		left = evt.pageX;
		top = evt.pageY;
//	} else if (evt.offsetX || evt.offsetY) {
//		left = evt.offsetX;
//		top = evt.offsetY;
	} else if (evt.clientX) {
		if (document.documentElement) {
			scrollLeft = document.documentElement.scrollLeft;
			scrollTop = document.documentElement.scrollTop;
		} else {
			scrollLeft = document.body.scrollLeft;
			scrollTop = document.body.scrollTop;
		}
		left = evt.clientX + scrollLeft;
		top = evt.clientY + scrollTop;
	}
	
	return [left, top];
}

function getOffset() {
	//return the current mouse offset from event location
	
	var left = 0;
	var top = 0;
	
	try {
		left = window.event.offsetX;
		top = window.event.offsetY;
	} catch (e) {
	}

	//alert(left + ", " + top);
	return [left, top]
}

function resizeModal() {
	//resize the modal iframe appropriately for its content
	try {
		var max_h = 0;	//maximum height for modal pane
		var max_w = 0;	//maximum width for modal pane

		if (window.parent != window.self) {
			var windowSize = parent.getWindowSize();
			max_w = parseInt((windowSize[0] / 100) * 90);
			max_h = parseInt((windowSize[1] / 100) * 90);
		}
		
		if (max_h == 0 || max_w == 0) return;
		if (max_h<520) max_h = 520;
		if (max_w<600) max_w = 600;

		//content height
		if (document.getElementById('modalPaneScroll')) document.getElementById('modalPaneScroll').style.height = "auto";
		var h = document.getElementById('mainBody').clientHeight;
		
		//control height
		var mph = 0;			//modal pane height
		if (parent.document.getElementById('modalPane')) {
			mph = parent.document.getElementById('modalPane').clientHeight;
				if (mph == 0) mph = parent.document.getElementById('modalPane').offsetHeight;
		}
		var ifh = 0; 			//iframe height
		if (parent.document.getElementById('modalPaneIframe')) {
			ifh = parent.document.getElementById('modalPaneIframe').clientHeight;
				if (ifh == 0) ifh = parent.document.getElementById('modalPaneIframe').offsetHeight;
		}
		var ch = mph - ifh;
						
		//setup modal scroll height
		if (document.getElementById('modalPaneScroll')) {
			//iframe control height
			var mtm = -4;			//modal title top margin
			var msh = 0;			//modal scroll height
			if (document.getElementById('modalPaneScroll')) {
				msh = document.getElementById('modalPaneScroll').clientHeight;
					if (msh == 0) msh = document.getElementById('modalPaneScroll').offsetHeight;
			}
			var ich = h - msh - mtm;

			if (msh>(max_h - ch - ich)) msh=(max_h - ch - ich);
			document.getElementById('modalPaneScroll').style.height = msh + "px";
		}

		//set up modal pane width
		parent.document.getElementById('modalWrapper').style.width = max_w + "px";

		//set up modal pane height
		if (h>(max_h - ch)) h=(max_h - ch);					//don't let the modal pane grow too big (hard for small screens)
		
		parent.document.getElementById('modalPaneIframe').style.height = h + "px";
	} catch (e) {
	}
}

function addModalFrame(id,bg,url,close,closeimg,noFrames) {
	//create modal pane structure for iframe

	try {	
		if (showPopups() && niftyOk) {
			ed=document.createElement("div");
			ed.id=id;
			ed.style.position = "absolute";
			ed.style.display = "none";
			ed.style.borderBottomStyle = "none";
		
			insertTopCorners(ed,bg);
			insertModalPanel(ed,bg,url,close,id,closeimg,noFrames); 
			insertBottomCorners(ed);
			
			df = document.createElement("div");
			df.id = "deactivateFrame";
			ed.appendChild(df);

			document.getElementById("modalWrapper").appendChild(ed);
		}
	} catch (e) {
	}
}

function addModalMenu(id,bg,head,list,close,closeimg) {
	//create dynamic download menu

	try {	
		dl=document.createElement("div");
		dl.id=id;
		dl.name=id;
		dl.className="modalMenu";
		dl.style.position = "absolute";
		dl.style.display = "none";
	
		insertTopCorners(dl,bg);
		insertMenuPanel(dl,bg,head,list,close,id,closeimg); 
		insertBottomCorners(dl);
		
		document.getElementById("dynamicMenus").appendChild(dl);
	} catch (e) {
	}
}

function insertModalPanel(el,bg,url,close,id,closeimg,noFrames){
	//create a structured modal pane with a close button
	
	dp=document.createElement("div");
	dp.className = "styledPanel";
	dp.innerHTML = "";
//	dp.innerHTML = "<h2 id=\"modalPaneTitle\" class=\"" + bg + "\">Loading...<\/h2>";	

	insertClose(dp,close,url,id,closeimg);

	dpf=document.createElement("div");
	dpf.id = id + "IframeWrapper";
	dpf.innerHTML = ""; //This makes IE 5.1+ on Mac behave!
	dpf.innerHTML = "<iframe id=\"" + id + "Iframe\" name=\"" + id + "Iframe\" scrolling=\"no\" frameborder=\"0\" width=\"100%\" height =\"55\" marginheight=\"0\" marginwidth=\"0\"  src=\"" + url + "\" title=\"" + id + "\">" + noFrames + "</iframe>";
	dp.appendChild(dpf);

	insertCloseForm(dp,close,url,id)

	el.appendChild(dp);
}

function insertMenuPanel(el,bg,head,list,close,id,closeimg){
	//create a structured menu with a close button
	
	mp=document.createElement("div");
	mp.id=id + "Panel";
	mp.name=id + "Panel";
	mp.className = "menuPanel";
	mp.style.borderBottomStyle = "none";

	mpdiv=document.createElement("div");
	mpdiv.className = bg;
	mpdiv.innerHTML = ""; //This makes IE 5.1+ on Mac behave!
	mpdiv.innerHTML = "<h4 style=\"padding-top: 0;\"><a name=\"" + id + "\"></a>" + head + "</h4>";
	mp.appendChild(mpdiv);

	insertClose(mp,close,'',id,closeimg);
	
	mpscroll=document.createElement("div");
	mpscroll.id=id + "Scroll";
	mpscroll.name=id + "Scroll";
	mpscroll.className = "content";

	mpul=document.createElement("ul");
	mpul.className = "text";
	mpul.innerHTML = ""; //This makes IE 5.1+ on Mac behave!
	var list_items = '';
	for (var i = 0; i < list.length; i++) {
		if (list[i][0] && list[i][1]) {
			list_items = list_items + "<li><a href=\"" + list[i][0] + "\" tabindex=\"2\" target=\"_blank\" title=\"" + list[i][1] + "\">" + list[i][1] + "</a></li>";
		}
	}
	mpul.innerHTML = list_items;
	mpscroll.appendChild(mpul);
	mp.appendChild(mpscroll);

	insertCloseForm(mp,close,'',id)

	el.appendChild(mp);
}

function insertTopCorners(el,bg){
	//create rounded corner styles for top of modal pane

	tc=document.createElement("div");
	tc.className = "topCorners";
	tc.innerHTML = ""; //This makes IE 5.1+ on Mac behave!
	tc.innerHTML = "<div class=\"l1\"></div><div class=\"l2 " + bg + "\"></div><div class=\"l3 " + bg + "\"></div><div class=\"l4 " + bg + "\"></div>"
	el.appendChild(tc);
}

function insertBottomCorners(el){
	//create rounded corner styles for bottom of modal pane
	
	bc=document.createElement("div");
	bc.className = "bottomCorners";
	bc.innerHTML = ""; //This makes IE 5.1+ on Mac behave!
	bc.innerHTML = "<div class=\"l4\"></div><div class=\"l3\"></div><div class=\"l2\"></div><div class=\"l1\"></div>"
	el.appendChild(bc);
}

function insertClose(el,close,url,id,closeimg){
	//create a close icon in the top right of a modal pane
	
	c=document.createElement("div");
	c.className = "closeIcon";
	
	var cleanupReturn = id + "Return";
	if (id == "modalPane") {
		c.id = id + "CloseDiv";
		if (browser.indexOf("msie 6")!=-1) c.style.top = ".3em"; 
		cleanupReturn = undefined;
	}
	
	c.innerHTML = ""; //This makes IE 5.1+ on Mac behave!
	c.innerHTML = "<a href=\"#\" id=\"" + id + "Close\" onClick=\"hideModal('" + id + "', '" + url + "'); return(cleanupModal('" + cleanupReturn + "'));\" tabindex=\"2\" title=\"" + close + "\"><img src=\"" +  closeimg + "\" alt=\"" + close + "\"><\/a>"
	el.appendChild(c);
}

function insertCloseForm(el,close,url,id){
	//create a close icon in the top right of a modal pane

	var cleanupReturn = id + "Return";
	if (id == "modalPane") {
		cleanupReturn = undefined;
	}
	
	f=document.createElement("form");
	f.className = "text center";
	f.innerHTML = ""; //This makes IE 5.1+ on Mac behave!
	f.innerHTML = "<input tabindex=\"2\" class=\"fButt\" type=\"button\" onClick=\"hideModal('" + id + "', '" + url + "'); return(cleanupModal('" + cleanupReturn + "'));\" value=\"" + close + "\">";
	el.appendChild(f);
}