// displays the popup menu for the group links
function showmenu(menuid, obj, e, anchor, hAdjust, vAdjust) {
	if(!document.getElementById || !document.createElement) return;

	//if no links array is defined do nothing
	//if(typeof links == "undefined") return;
	
	//if(links.length == 0) return;
	
	//prevent other events
	if (window.event) event.cancelBubble = true;
	else if (e.stopPropagation) e.stopPropagation();

	//stop previous timers
	cancelhide();
	
	//configure the mouseout event
	obj.onmouseout = hidemenu;
	//obj.onmouseout = hidemenu();

	//setup the menu object
	var menuobj = document.getElementById(menuid);
	
	//load the links
	//loadlinks(menuobj);

	//set adjustments based on selected 'anchor' corner
	var anchorX = 0;
	var anchorY = 0;
	if(anchor.charAt(0) == 's') anchorY = menuobj.offsetHeight;
	if(anchor.charAt(1) == 'e') anchorX = menuobj.offsetWidth;

	//set the position and show the menu
	var position = findPos(obj);
	menuobj.style.left = position[0] - anchorX + hAdjust + "px";
	menuobj.style.top = position[1] - anchorY + vAdjust + "px";
	menuobj.style.visibility = "visible";
	menuobj.onmouseover = cancelhide;
	menuobj.onmouseout = hidemenu;
	
	function hidemenu() {
		//hideTimer = setTimeout(menuobj.style.visibility = "hidden", 300);
		hideTimer = setTimeout('hideObject(\'' + menuobj.id + '\')', 900);
	}
	
	function cancelhide() {
		if (typeof hideTimer != "undefined")
		clearTimeout(hideTimer);
	}
}

function hideObject(id){
	obj = document.getElementById(id);
	if(typeof obj != 'undefined'){
		obj.style.visibility = 'hidden';
	}
}

// helper function for popup menu
//function hidemenu() {
//	hideTimer = setTimeout("menuobj.style.visibility = 'hidden'", 300);
//}

// helper function for popup menu
//function cancelhide() {
//	if (typeof hideTimer != "undefined")
//	clearTimeout(hideTimer);
//}

// loads popup menu links from the array defined on the main page
function loadlinks(menuobj){
	if(!document.getElementById || !document.createElement) return;
	
	//only do this once
	if(menuobj.hasChildNodes()) return;

	//load the links from the page based array
	for(var i=0; i<links.length;i++){
		link = document.createElement("a");
		linktext = document.createTextNode(links[i][1]);
		link.setAttribute("href",links[i][0]);
		link.setAttribute("target", "_blank");
		link.appendChild(linktext);
		menuobj.appendChild(link);
		menuobj.appendChild(document.createElement("br"));
	}
}

// loads other benefits links from the array defined on the main page
function loadOtherBenefits(menuobj){
	if(!document.getElementById || !document.createElement) return;
	
	//only do this once
	if(menuobj.hasChildNodes()) return;

	//load the links from the page based array
	for(var i=0; i<benefits.length;i++){
		link = document.createElement("a");
		linktext = document.createTextNode(benefits[i][0]);
		link.setAttribute("href",benefits[i][1]);
		link.setAttribute("target", "_blank");
		link.appendChild(linktext);
		menuobj.appendChild(link);
		menuobj.appendChild(document.createElement("br"));
	}
}

// helper function that finds the positon of an object, used to position the popup menu
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		// this loop will continue until offsetParent doesn't exist and obj evals to null
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


// redirect to proper content page based on submitted postal code
function contactUs(postalCode, errorMsg){
	var url;
	if (postalCode.match(/[ABCE]\d[A-Z]\s?\d[A-Z]\d/i)){
		// atlantic
		url = 'ContactUs/Atlantic';
	}else if(postalCode.match(/[GHJKLMNP]\d[A-Z]\s?\d[A-Z]\d/i)){
		// central
		url = 'ContactUs/Central';
	}else if(postalCode.match(/[RSTV]\d[A-Z]\s?\d[A-Z]\d/i)){
		// western
		url = 'ContactUs/Western';
	}else if(postalCode.match(/[XY]\d[A-Z]\s?\d[A-Z]\d/i)){
		//other
		url = 'ContactUs';		
	}else{
		alert(errorMsg);
		return false;
	}

	var location = window.location.protocol + '//' + window.location.host + window.location.pathname;
	var newlocation = location + ((location.lastIndexOf('/') == location.length-1)?'':'/') + url + window.location.search;

	window.location = newlocation;
	
	return false;
}

function OpenNewWindow(url,winwidth,winheight,shownav) {
	if(shownav)
		NewWindow=window.open(url,'_blank','status=yes,toolbar=yes,location=no,directories=yes,statusbar=yes,menubar=yes,scrollbar=yes,scrollbars=yes,resizable=yes,copyhistory=no,width='+winwidth+',height='+winheight);
	else
		NewWindow=window.open(url,'JohnsonPopup','toolbar=no,location=no,directories=no,statusbar=yes,menubar=no,scrollbar=yes,scrollbars=yes,resizable=yes,copyhistory=no,width='+winwidth+',height='+winheight);
}

function OpenNewWindowNamed(url,popupName,winwidth,winheight) {
	NewWindow=window.open(url,popupName,'toolbar=no,location=no,directories=no,statusbar=yes,menubar=no,scrollbar=yes,scrollbars=yes,resizable=yes,copyhistory=no,width='+winwidth+',height='+winheight)
}

function adjustFrameHeight(){
	var iframe = document.getElementById("contentFrameID");
	var frameHeight = window.frames['contentFrame'].document.body.scrollHeight;
	if(frameHeight > 480){
		iframe.style.height = (frameHeight + 20) + "px";
	}else{
		iframe.style.height = "500px";
	}
	return false; 
}
