// scripts used in WHZC website

//set / read cookies
function SetAAACookie(cookieName,cookieValue,nDays,path) 
{
	path = path ? path : "/"
	var today = new Date();
	var expire = new Date();
	nDays = nDays ? nDays : 1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue)
					 + ";expires="+expire.toGMTString()+";path="+path;
}
function ReadAAACookie(cookieName) 
{
 	var theCookie=""+document.cookie;
	 var ind=theCookie.indexOf(cookieName);
 	if (ind==-1 || cookieName=="") return ""; 
	 var ind1=theCookie.indexOf(';',ind);
	 if (ind1==-1) ind1=theCookie.length; 
	 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

//flyout menu scripts

//opens a new window
function goTo(URL,target,height,width)
{
	 var winProps
	 if (target != "_self")
	 {
	  winProps="toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,height=" +
		 ((height==null || height=='') ? 600 : height) + ",width=" + ((width==null || width=='') ? 900 : width);
	  NewWin0=this.open(URL,"Win",winProps);
	 }
	 else
	 {
	  document.location.href=URL;
	 }
}
//THIS FUNCTION IS ALSO CALLED ALREADY IN SOME PAGES
function GoPage(URL,target,height,width)
{
	goTo(URL,target,width, height);
}

function doMenuClick(tag)
{
 	var tag1 = tag.childNodes[0];
	if(tag1.onclick) tag1.onclick();
	else
		document.location.href = tag1.href;
}

//SCRIPTS FOR FLYOUT MENUS 
var numMenus = 0; //for drop down menus, if exist
var numFlyouts = 0; 
//OVERWRITING TOP NAV FUNCTIONS

//CLOSE DROPDOWN MENU FUNCTION
function closeMenus()
{
	addRemoveddCover();
	for (var i=1;i<=numMenus;i++)
	{
		var menuName="dropmenu"+i.toString();
		document.getElementById(menuName).style.display="none";
	}
	//add for flyouts
	for (var i=1;i<=numFlyouts;i++)
	{
		var menuName="flymenu"+i.toString();
		if (document.getElementById)document.getElementById(menuName).style.display="none";
	}
}

//CLOSE ALL OPEN MENUS, POSITION & DROP CORRECT CURRENT MENU
function dropit(whichone,element)
{
	closeMenus();

	curmenuID=ns6? document.getElementById(whichone).id : eval(whichone).id

	themenu=ns6? document.getElementById(whichone): eval(whichone)
	themenuStyle=(ns6||ie4)? themenu.style : themenu

	themenuoffsetX=(ie4&&opr==-1)? document.body.scrollLeft : 0
	themenuoffsetY=(ie4&&opr==-1)? document.body.scrollTop : 0

	var x, y
	x = getPageOffsetLeft(element);
	y = getPageOffsetTop(element)+element.offsetHeight;

	
	//CHANGED FROM TOP, ACCOUNT FOR FLYOUTS
	if(whichone.indexOf("flymenu") > -1) //flyoutmenu
	{
		x += 160;
		y -= 18;
	}
	else //same as original top nav
	{
		// For IE, adjust position.
		if (ie4) 
		{
			x += themenu.offsetParent.clientLeft;
			y += themenu.offsetParent.clientTop;
			y-=2;
			x-=1;
		}
	}

  //Set the position and drop the menu
  themenuStyle.left = x + "px";
  themenuStyle.top  = y + "px";
  themenuStyle.display = "block";
}

var zindex=100;
var detect = navigator.userAgent.toLowerCase();   
var ns4=document.layers != null; 
var ns6=document.getElementById && !document.all; 
var ie4= String(detect).indexOf("msie") > -1; 
var opr=navigator.userAgent.indexOf("Opera") > -1;


function checkBrowserType(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
//Places an iframe underneath the dynamic menus so they appear on top of dropdown form elements in IE
function addRemoveddCover(menu)
{
	if (!ie4) return; 
	
	   var ddcover = document.getElementById('ddCover');
	   if(menu)
	   { 
			var ddMenu = (typeof menu == "object") ? menu : document.getElementById(menu); 
			ddcover.style.width = ddMenu.offsetWidth;
			ddcover.style.height = ddMenu.offsetHeight;
			ddcover.style.top = ddMenu.style.top;
			ddcover.style.left = ddMenu.style.left;
			ddcover.style.zIndex = ddMenu.style.zIndex - 1;
			ddcover.style.display = "block";
	   }
	   else
	   {
			ddcover.style.display = "none";
		}
}

function doTheDrop(menu,element)
{
   //CLEAR HIDE MENU TIMER IF EXISTS & DROP MENU
   if (window.aaaHideMenuTimer) 
    	clearTimeout(aaaHideMenuTimer);
        
	dropit(menu,element); 
	addRemoveddCover(menu);
}

function getPageOffsetLeft(el) 
{
  var x;

  // Return the x coordinate of an element relative to the page.

  x = el.offsetLeft;//alert("el.offsetLeft="+el.offsetLeft);
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);
  return x;
}

function getPageOffsetTop(el) 
{
  var y;

  // Return the y coordinate of an element relative to the page.


  y = el.offsetTop;//alert("el.offsetTop="+el.offsetTop);
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);
  return y;
}

//SET STYLES AND RESTORE STYLES SHOW AND HIDE MENU IN IE
//SET AND CLEAR TIMEOUT (USED FOR HIDING MENU) IN NN
function setStyles(el)
{
	 if(ie4) el.style.display='block';
	 addRemoveddCover(el);
	 if(document.getElementById && !ie4)clearTimeout(aaaHideMenuTimer);
}
function restoreStyles(el)
{
	 addRemoveddCover();
	 if(ie4) el.style.display='none';
	 if(document.getElementById && !ie4)startTimeout();
}
//CAPTURE MOUSE CLICKS TO HIDE MENU WHEN PAGE IS CLICKED
if (ie4)
  document.onmousedown = pageMousedown;
else 
  document.addEventListener("mousedown", pageMousedown, true);

function pageMousedown(event)
{
	//GET THE ELEMENT THAT WAS CLICKED
	var el;
	if (ie4)
	{
		el = window.event.srcElement;
	}
	else
	{
		el = (event.target.tagName ? event.target : event.target.parentNode);
	}
	//IF THE ELEMENT THAT WAS CLICKED DOES NOT HAVE THE CSS CLASSNAME FOR THE DROPDOWN MENU
	//LINK AND TD, CLOSE THE DROPDOWNS
	if(!isDropDownLink(el))
		closeMenus();
}

function isDropDownLink(el)
{
	try
	{
		var classname = el.className;
		if(classname == undefined) return false;
		
		var Classes = [ "dropdown", "homedrop", "joindrop", "traveldrop", "insurancedrop", "savingsdrop"];
		for(var i=0;i<Classes.length;i++)
		{
			if(classname.indexOf(Classes[i]) > -1) return true;
		}
		return false;
	}
	catch(e) { return false; }
}


//FOR NN6+ HIDE THE MENU AFTER 1 SECOND
function startTimeout()
{	
	aaaStart = new Date();	
	aaaDHFlag = true;	
	aaaHideMenuTimer = setTimeout("DoHide()", 1000);
}
function DoHide()
{	
	if (!aaaDHFlag) return;	
	var elapsed = new Date() - aaaStart;	
	if (elapsed < 1000) {		
		aaaHideMenuTimer = setTimeout("DoHide()", 1100-elapsed);		
		return;	}	
	aaaDHFlag = false;	
	closeMenus();	
}
//end flyout menu scripts

//get a random number between 0 and x
function getRand(len)
{
	return Math.floor(Math.random()*len)
}

function doPrintView(id)
{
	alert("Print View coming soon!");
	//TODO
}	