// NAVIGATION SCRIPTS //

//login / logout scripts
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));
}

//shows the login / logout text
var showlogcnt = 0; 
var name = undefined;
function showLogTxt()
{
  //assumes name var that is defined on the page with smart tags
  try
  {
	//travel 2 not replacing the smarttags, test to see if they are there
	function isNotReplaced(str)
	{
		return str.indexOf("<FNAME>") > -1 && str.indexOf("<LNAME>") > -1;
	}
	
	if(name == undefined || isNotReplaced(name)) throw "Null Name Var";
	
	function isEmpty(str)
	{
		return str == " " || str == "" || str == "</FNAME></LNAME>" || str == "</FNAME> </LNAME>";
	}
	  
	var imgLI = document.getElementById("spnLI");
	if( isEmpty(name) ) 
	{
		//display Login text
		imgLI.style.display = "inline";
		try{document.getElementById("divUser").style.display = "none"; } catch(e){}
		document.getElementById("spnLO").style.display = "none";
	}
	else 
	{
		//logout 
		imgLI.style.display = "none"; 
		try{document.getElementById("divUser").style.display = "inline"; } catch(e){}
		document.getElementById("spnLO").style.display = "inline";
	}
	var noLog = ReadAAACookie("AAANoLog");
	if(noLog != "true") SetAAACookie("AAAPage", document.location.href);
  }
  catch(e){if(showlogcnt<10){window.setTimeout(showLogTxt, 100); showlogcnt++;}}
}
window.setTimeout(showLogTxt, 750);

//called from login / logout pages
function doRedirect()
{
	SetAAACookie("AAANoLog","false"); //reset the no log to turn page logging back on.
	try
	{
		var Url = ReadAAACookie("AAAPage"); //the last page logged before login
	}
	catch(e)
	{
		window.status = e.toString();
		var Url = "http://www.aaaminneapolis.com/membership_manage.asp";
	}
	document.location.href = Url;
}
//end login / logout scripts
function adjustStyle(styleName, newStyle) 
{
	if (document.styleSheets[0].addRule) {
		// IE way
		document.styleSheets[0].addRule(styleName, newStyle);
	} else if (document.styleSheets[0].insertRule) {
		// Mozilla/Standard way
		var x = document.styleSheets[0];
		x.insertRule(styleName + " {" + newStyle + "}", x.cssRules.length);
	}
}
function doStyleAdjust(bShow)
{
	if(document.all)
	{
		if(bShow == undefined) bShow = true;
		var displaystyle = bShow ? "display:inline;" : "display:none;"; 
		adjustStyle('.servicesconfig', displaystyle );
		adjustStyle('.leftconfig', displaystyle);
	}
}
doStyleAdjust(false);
window.setTimeout(doStyleAdjust, 2500);

//GO TO PAGE WHEN DROPDOWN IS CLICKED
function goEx(URL,width,height)
{
	goTo(URL, "_new", height, width);	
	return false;
}
function goTo(URL,target,height,width)
{
 var winProps
 if (target != "_self")
 {
  winProps="left=250,top=150,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,height=" +
     ((height==null || height=='') ? 630 : height) + ",width=" + ((width==null || width=='') ? 800 : width);
  NewWin0=this.open(URL,"Win",winProps);
 }
 else
 {
  document.location.href=URL;
 }
}
//SCRIPTS FOR DROPDOWN MENU FUNCTIONS
var zindex=100
var ns4=document.layers
var ns6=document.getElementById&&!document.all
var ie4=document.all
var opr=navigator.userAgent.indexOf("Opera")
var detect = navigator.userAgent.toLowerCase(); 

//Other global variables
var numMenus = 8;
var lastTwoLinks = new Array();
lastTwoLinks[0]="dropmenu6";
lastTwoLinks[1]="dropmenu7";

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 (!document.all) 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);
}

//CLOSE ALL OPEN MENUS, POSITION & DROP CORRECT CURRENT MENU
function dropit(whichone,element){
aaa_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;

 // For IE, adjust position.

  if (ie4) {
    x += themenu.offsetParent.clientLeft;
    y += themenu.offsetParent.clientTop;
	y-=3;
	x-=3;
  }
  //LAST TWO MENU ITEMS (AS DEFINED IN VARIABLE AT TOP OF PAGE)
  //ARE DROPPED 60 PX TO THE LEFT TO AVOID GOING OFF THE PAGE
  //HOME MENU IS BUMPED A LITTLE TO THE RIGHT TO APPEAR CONSISTANT WITH OTHERS
  if (curmenuID==lastTwoLinks[0] || curmenuID==lastTwoLinks[1]){
	x-=13;
  }
  
  if(curmenuID == "dropmenu1") //home
  	x+=12;
  //Set the position and drop the menu
  themenuStyle.left = x + "px";
  themenuStyle.top  = y + "px";
  themenuStyle.visibility = "visible";
}

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(document.all) el.style.visibility='visible';
	 addRemoveddCover(el);
	 if(document.getElementById && !document.all)clearTimeout(aaaHideMenuTimer);
}
function restoreStyles(el)
{
	 addRemoveddCover();
	 if(document.all) el.style.visibility='hidden';
	 if(document.getElementById && !document.all)aaa_startTimeout();
}

//CAPTURE MOUSE CLICKS TO HIDE MENU WHEN PAGE IS CLICKED
if (document.all)
  document.onmousedown = pageMousedown;
else 
  document.addEventListener("mousedown", pageMousedown, true);

function pageMousedown(event){
//GET THE ELEMENT THAT WAS CLICKED
var el;
if (document.all){
    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 (el.className != "dropdownlink" && el.className != "dropdownhover" && el.className != "dropdown")
	aaa_closeMenus();
}
//CLOSE DROPDOWN MENU FUNCTION
function aaa_doMenuClose()
{
	if(document.all) aaa_closeMenus();
	if(document.getElementById && !document.all) aaa_startTimeout();
}
function aaa_closeMenus()
{
	addRemoveddCover();
	for (var i=1;i<=numMenus;i++)
	{
		var menuName="dropmenu"+i.toString();
		if (document.getElementById)document.getElementById(menuName).style.visibility="hidden"
		if (document.all)document.all(menuName).style.visibility="hidden"
	}
}

//FOR NN6+ HIDE THE MENU AFTER 1 SECOND
function aaa_startTimeout(){	
	aaaStart = new Date();	
	aaaDHFlag = true;	
	aaaHideMenuTimer = setTimeout("aaa_DoHide()", 1000);

}
function aaa_DoHide(){	
	if (!aaaDHFlag) return;	
	var elapsed = new Date() - aaaStart;	
	if (elapsed < 1000) {		
		aaaHideMenuTimer = setTimeout("aaa_DoHide()", 1100-elapsed);		
		return;	}	
	aaaDHFlag = false;	
	aaa_closeMenus();	
}
//END SCRIPTS FOR DROPDOWN MENU FUNCTIONS

////NON-NAVIGATION RELATED SCRIPTS TO BE INCLUDED ON EACH PAGE OF 730 SITE
//returns the first table node it encounters backwards in the DOM from the element passed
function getParentTag(el, tagname)
{
	if(!tagname) tagname = "TABLE";
	
	try{var parent = el.parentNode;} catch(e) {}
	while(parent)
	{
		if(parent.tagName == tagname) return parent;
		parent = parent.parentNode;
	}
	return null;
}

function getTagByClassName(classname, tagname)
{
	if(tagname == undefined) tagname = "TD";
	var tags = document.getElementsByTagName(tagname);
	for(var i=0;i<tags.length;i++)
	{
		if(tags[i].className.toLowerCase() == classname.toLowerCase())

		{
			return tags[i];
		}	
	}
	return null;

}
function isMainPage()
{
	return document.getElementById("divMainPage") != null;
}
//special formatting for the center aligned design, 
//adjusts for the different national office page design styles
//special cases are travel booking pages, main (risdall designed) pages, and any page that has the
//bottom section appearing in the services td (1/3 of RT Drive Trips pages), also the TourBookEditorialContent page
//global table size vars
var travelSize = 1000; //890
var rsize = 812;
var hsize = 800;
function setTableAlignments()
{
	if(isTravelPage()) return;
	/*var bIsSafari = false;
	try
	{
		bIsSafari = navigator.vendor.indexOf("Safari") > -1;
		bIsSafari = navigator.userAgent.indexOf("Safari") > -1;
	}
	catch(e){}
	//align left for safari
	if(!bIsSafari)
	{
		try
		{
			var toptd = getTagByClassName("menubarColor");
			var btmtd = getTagByClassName("bottomconfig");
			toptd.align = "center";
			btmtd.align = "center";
			var tbl = getTable("top");
			tbl.align = "center";
			tbl = getTable("bottom");
			tbl.align = "center"; 
		}
		catch(e){}
	}
	else
	{
		var tbl = getTable("top");
		tbl.align = "left";
	}*/
	
	//set width / alignment of content table
	var td, parentEl;
	var bSizeFooter = true;
	var bIsTravelForm = isTravelPage();
	
	//Special Formatting if the bottom section is appearing inside the services td 
	var btm = getTagByClassName("bottomconfig");
	var tag1 = getParentTag(btm, "TD"); 
	var tag2 = tag1 ? getParentTag(tag1, "TD") : null;
	if((tag1 && tag1.className == "servicesconfig") || (tag2 && tag2.className == "servicesconfig")) 
	{
		bSizeFooter = false;
		size("bottom", 592);
		try
		{
			btm = document.getElementById("divFooterLinks");
			btm.style.backgroundColor = "#FFFFFF"; //same as services td
			document.getElementById("divImgBlock").style.display = "none";
		}
		catch(e){}
	}
	
	//set top and bottom sizes a bit bigger for all booking pages
	var psize = bIsTravelForm ? travelSize : rsize; 
	psize = isMainPage() ? hsize : psize;
	size("top", psize);
	if(bSizeFooter != false)
		size("bottom", psize);
	
	try
	{
		td = getTagByClassName("servicesconfig");
		parentEl = getParentTag(td);
		parentEl.width = psize;
		parentEl.align = "center";
		td.vAlign = "top";
	}
	catch(e){}
	
	//removed spacing created by br tags
	var fonttag = getTagByClassName("display", "FONT");
	var footertable = fonttag ? getParentTag(fonttag) : null;
	
	if(document.all)
	{
		try
		{
			var evilBrTag = footertable.previousSibling;
			var evilBrTag2 = fonttag.childNodes[1];
			if(evilBrTag.tagName == "BR") evilBrTag.style.display = "none";
			try{if(evilBrTag2.tagName == "BR") evilBrTag2.style.display = "none";} catch(e) {}
		}
		catch(e){}
	}
	else //firefox
	{
		//then try grabbing the br by how many there are in the doc minus those in footer
		try
		{
			var brTags = document.getElementsByTagName("BR");
			brTags[brTags.length - 5].style.display = "none"; 
		}
		catch(e){}
	}
	
	if(bIsTravelForm)
	{
		if(isTourBookContentPage())
		{
			try
			{
				//td that encloses the left menu
				td = getTagByClassName("leftconfig");
				parentEl = getParentTag(td); 
				parentEl = getParentTag(parentEl, "TD");
				//fix the td the encloses the main, center section (in this case the rightconfig td), width = 100% is expanding it too far
				td = getTagByClassName("rightconfig");
				parentEl = getParentTag(td); 
				parentEl = getParentTag(parentEl, "TD"); //get the 100% width td
				parentEl.width="";
				//get the main table (different one from other pages, size, center and set white background.
				td = getTagByClassName("servicesconfig");
				parentEl = getParentTag(td); 
				parentEl = getParentTag(parentEl);
				parentEl.width = psize;
				parentEl.align = "center";
				parentEl.style.backgroundColor = "#FFFFFF";
				parentEl.bgcolor = "#FFFFFF";
			}
			catch(e){}
		}
	}
	
	//show center section, previously hidden in IE
	doStyleAdjust(true);
}

function isTravelPage()
{ 
	//old way: ( document.forms.length >= 1 && !document.getElementById("divQF") && document.getElementById("tblTravelMenu"))	
	//travel 2 test, look for 'document' div element: 
	return document.getElementById("document") != null || document.getElementById("rvlxMainContainer") != null;
}

var trys = 0;
function setT2Layout()
{
	if(checkBrowserType('msie') && document.readyState != "complete") 
	{
		window.setTimeout(setT2Layout, 100);
		return;
	}
	
	try
	{
		var leftTd = document.getElementById("tdLeft");
		var centerTd = document.getElementById("tdCenter");
		var leftEl = document.getElementById("clubLeftDiv");
		var centerEl = document.getElementById("mainContainer");
		var topEl = document.getElementById("clubTopDiv");
		leftTd.appendChild(leftEl);	
		centerTd.appendChild(topEl);	
		centerTd.appendChild(centerEl);	
		document.getElementById("tblMain").style.display = "block";
	}
	catch(e)
	{
		if(trys < 30)
		{
			trys++;
			window.setTimeout(setT2Layout, 250);
		}
	}
}

//if(isTravelPage()) window.setTimeout(setT2Layout, 500);

function isTourBookContentPage()
{
	var tags = document.getElementsByTagName("IFRAME");
	var bhasIFrame = false;
	var Frame;
	for(var i=0;i<tags.length;i++)

	{
		if(tags[i].src.toLowerCase().indexOf("destinationinfo") > -1 || 
			tags[i].src.toLowerCase().indexOf("destinationportal") > -1)
		{
			Frame = tags[i];
			bhasIFrame = true;
		}
	}
	//code to address lenght of left menu in tourbookeditorial pages, needs debugging comment out for now
	/*if(Frame)
	{
		var hei = document.all ? Frame.document.body.scrollHeight : Frame.document.body.offsetHeight;
		sizeFFLeftNavSection(hei, true);
	}*/


	
	return bhasIFrame && document.forms["ChangeDestinationForm"] && document.forms["TravNavFormData"];
}
//for Firefox, has problems with dynamic sizing in content area and the left nav that is 100% height
var leftNavHeight = 688; //generic estimate for most left menu heights, set this on page if height is different
function sizeFFLeftNavSection(hei, bIncIE, bNoPadding, padAmt)
{
	try
	{
		if(document.all && bIncIE != true) 
		{
			return;
		}
		else //FF or include ie
		{
			if(!hei)
			{
				var content = getTagByClassName("servicesconfig"); 
				//var content = document.documentElement;
				hei = document.all ? content.scrollHeight : content.offsetHeight;
				//hei = content.scrollHeight;
				//document.getElementById("debug").innerHTML = "<br>hei=" + hei; 
			}
			var leftTd = getTagByClassName("leftconfig");
			//leftTd.style.height = leftTd.offsetHeight > hei ? leftTd.offsetHeight : hei + 50;
			padAmt = padAmt == undefined ? 315 : padAmt;
			var newheight = bNoPadding == true ? hei : hei + padAmt;
			leftTd.style.height = leftNavHeight > newheight ? leftNavHeight : newheight;
			//document.getElementById("debug").innerHTML = "<br>setting height to:" + leftTd.style.height; 
			//document.getElementById("debug").innerHTML += "leftNavHeight = " + leftTd.style.height + " content height = " + hei + "<br/>"; 
		}
	}
	catch(e){}
}
function bump(section, px)
{
	var table = getTable(section);
	try
	{
		table.style.position = "relative";
		table.style.left = px;
	}
	catch(e){}
}
function size(section, wid)
{
	//accepts args string "top", "bottom", "middle", "both" (for top and bottom) or array of the strings (both option not allowed in array)
	if(section == "both")
	{
		section = ["top", "bottom"]
	}
	else if (typeof(section).toLowerCase() == "string")
		section = [ section ]; 
		
	for (var i=0;i<section.length;i++)
	{
		var table = getTable(section[i]);
		try
		{
			table.width = wid;
			table.style.width = wid;
			if(section[i] == "bottom") 
				document.getElementById("footshadow").width=wid;
		}
		catch(e){}
	}
}
function getTable(section)
{
	switch(section)
	{
		case "top":
			return document.getElementById("tblTop");
		break;
		case "middle":
			var td = getTagByClassName("servicesconfig");
			return getParentTag(td);
		break;
		case "bottom":
			return document.getElementById("tblBottom");
		break;
	}
}

//TRAVEL 2 SCRIPTS//
//travel 2 override functions for positioning lrp div
function showLrp() 
{
	var bodyContainer = window.document.body;
	showAjaxSpinner(bodyContainer, 'lrpOverlay', 'lrp');
	
	var pos = window.pageYOffset ? parseInt(window.pageYOffset) : 
		document.documentElement.scrollTop ? parseInt(document.documentElement.scrollTop):
			parseInt(document.body.scrollTop);
			
	pos = pos.toString() != "NaN" ? pos : 0;
	document.getElementById("lrp").style.top = pos + 150 + "px";
	document.getElementById("lrp").style.left = "250px";
	show('lrp');
}	

//fix "arrow" progres bar style in IE6
var Trys = 0; 
function styleFix()
{
	var arrow = document.getElementById("infoPay");
	if( arrow != null)
	{
		arrow.style.backgroundImage = arrow.className == "incomplete" ? "url(/AAA/common/images/Travel2/pBar_Info_Incomplete.gif)" :
			arrow.className == "current" ? "url(/AAA/common/images/Travel2/pBar_Info_Current.gif)" : 
				"url(/AAA/common/images/Travel2/pBar_Info_Complete.gif)";
	}
	else
	{
		if(Trys < 50)
		{
			Trys++;
			window.setTimeout(styleFix, 250);
		}
	}
}
if(checkBrowserType("msie 6.0")); styleFix();
