/* * * * * * * * * * * * * * * * * * * * * * * * *
 Do not remove this notice.

 Copyright 2000-2004 by Mike Hall.
 (Except code noted by other authors; TEN, JDB.)
 See http://www.brainjar.com for terms of use.
 * * * * * * * * * * * * * * * * * * * * * * * * */


/* * * * * * * * * * * * * * * * * * * * * * * * *
 9/9/04 - TEN
 Changed to open in a new window without a header.
 * * * * * * * * * * * * * * * * * * * * * * * * */
function ShowSite(url)
{
	objWin = window.open(url, "", "location,menubar,toolbar,scrollbars,resizable,width=780,height=400");
	objWin.focus();
}


// -=-=-=-=-=-=- START: Landstar client-side error handler -=-=-=-=-=-=-
// by JDB, Sept. 2004

var objErrorWindow;
function menuErrorHandler(strMessage, strURL, intLine) {
	menuErrorAction(strMessage, strURL, intLine); // these arguments are intrinsic to onerror;
//	return true; // do nothing - cancel normal error action (cancel message box);
	return false; // normal error handling retained;
}
function menuErrorAction(strMessage, strURL, intLine) {
	// alert(" ================ ERROR DETECTED ================ \n\nPlease contact the Landstar help desk if this happens repeatedly. Please\nrefer to the information below when reporting this incident.\n________________________________________________________________     \n\nError Message:\n     " + strMessage + "\n\nError Page:\n     " + strURL + "\n\nLine Number: " + intLine + "\n________________________________________________________________\n\n----- Help Desk numbers ----- \nLandstar Express America: 1-800-435-4982 \nLandstar Gemini: 1-800-872-9415 \nLandstar Inway: 1-800-579-5641 \nLandstar Ligon: 1-800-872-9415 \nLandstar Global Logistics: 1-800-872-9415 \nLandstar Ranger: 1-800-872-9415\n");
	// Optional/TBD: Open error-processing window off-screen or in dynamically created i-frame ;
}
// Future use: To be called in onload of window opened in menuErrorAction() ;
function closeErrorWindow() {
	if (objErrorWindow && !objErrorWindow.closed) {
		objErrorWindow.close();
	}
}
window.onerror = menuErrorHandler;

// -=-=-=-=-=-=- END: Landstar client-side error handler -=-=-=-=-=-=-


//-------------------------------------------
// Code to determine the browser and version.
//-------------------------------------------

function objMenuBrowser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as Netscape 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s))) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
}

var myBrowser = new objMenuBrowser();

//--------------------------------------------------
// Code for handling the menu bar and active button.
//--------------------------------------------------

var activeButton = null;


function buttonClick(event, menuId) {

  // Cancel action if page is not yet loaded. ;
  if ( !isReady() ) return false;

  var button;

  // Get the target button element.

  if (myBrowser.isIE)
    button = window.event.srcElement; //BN:changed value back to previous "srcElement"  //.srcElement;toElement
  else
    button = event.currentTarget;

  // Blur focus from the link to remove that annoying outline.
  	button.blur();
  
  // Associate the named menu to this button if not already done.
  // Additionally, initialize menu display.

  if (button.menu == null) {
    button.menu = document.getElementById(menuId);
    // added first condition and else clause below to avoid Object Required error (JDB 09/2004) ;
    if (button.menu && button.menu.isInitialized == null) {
		menuInit(button.menu);
    } else {
		setTimeout("menuInit(button.menu)", 1000);
    }
  }
  
  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Set mouseout event handler for the button, if not already done.

  if (button.onmouseout == null)
    button.onmouseout = buttonOrMenuMouseout;

  // Exit if this button is the currently active one.

  if (button == activeButton)
    return false;

  // [END MODIFIED]

  // Reset the currently active button, if any.

  if (activeButton != null)
    resetButton(activeButton);

  // Activate this button, unless it was the currently active one.

  if (button != activeButton) {
    depressButton(button);
    activeButton = button;
  }
  else
    activeButton = null;

  return false;
}

// Returns true if page is fully loaded. (JDB 9/2004) ;
function isReady() {
	return ( window.document.readyState == "complete" ) ? true : false ;
}

function buttonMouseover(event, menuId) {
  
  // Cancel action if page is not yet loaded. ;
  if ( !isReady() ) return false;
  
  var button;

  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Activates this button's menu if no other is currently active.

  if (activeButton == null) {
    buttonClick(event, menuId);
    return;
  }

  // [END MODIFIED]

  // Find the target button element.

  if (myBrowser.isIE)
    button = window.event.srcElement;
  else
    button = event.currentTarget;

  // If any other button menu is active, make this one active instead.

  if (activeButton != null && activeButton != button)
    buttonClick(event, menuId);
}

var blnCreatedIFrame = false; // needed in depressButton(), below;

function depressButton(button) {

  if (myBrowser.isIE && !blnCreatedIFrame) {
	// create iFrame to wedge between form elements and menus;
	// ...only if the menu property exists;
	if (button.menu) {
	//	button.menu.triggerAnError(); // Use this to test error handling. ;
		button.menu.insertAdjacentHTML("afterEnd", "<iframe id='iFrameWedge' style='position:absolute;top:-900px;left:-900px;z-index:5;width:0px;height:0px;visibility:hidden;' src='/include/BlankPage.htm'></iframe>");
		blnCreatedIFrame = true;
	}
  }
  
  var x, y;

  // Update the button's style class to make it look like it's depressed.

  button.className += " menuButtonActive";

  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Set mouseout event handler for the button, if not already done.

  if (button.onmouseout == null)
    button.onmouseout = buttonOrMenuMouseout;
  if (button.menu.onmouseout == null)
    button.menu.onmouseout = buttonOrMenuMouseout;

  // [END MODIFIED]

  // Position the associated drop down menu under the button and show it.
  x = getPageOffsetLeft(button);
  y = getPageOffsetTop(button) + button.offsetHeight - 1; // JDB: "- 1" fix to prevent menus from flickering or disappearing as mouse moves across boundary;

  if (myBrowser.isIE) {
	
	// For IE, adjust position;
    x += button.offsetParent.clientLeft;
    y += button.offsetParent.clientTop;

	// Find the dimensions of a menu item;
	var menuList;
	var menuWidth;
	var menuHeight;
	
	// Use links to find width of menu;
	menuList = button.menu.getElementsByTagName("A");
	
	if (menuList.length > 0) {
		menuWidth = menuList[0].offsetWidth;
		menuHeight= button.menu.offsetHeight;
	} else {
		menuWidth = 0;
		menuHeight= 0;
	}

	// Use the wedge iFrame created elsewhere in this script;
	var objWedge = document.all.iFrameWedge
	
	// Position the wedge. Shim it above the select list 
	// and under the menu (using zIndex);
	objWedge.style.top = y;
	objWedge.style.left= x;
	objWedge.style.width=menuWidth + 5;
	objWedge.style.height=menuHeight;
	objWedge.style.zIndex = 900;
	objWedge.style.visibility = "visible";

  }

  button.menu.style.left = x + "px";
  button.menu.style.top  = y + "px";
  button.menu.style.visibility = "visible";
  
}

function resetButton(button) {

  // Restore the button's style class.

  removeClassName(button, "menuButtonActive");

  // Hide the button's menu, first closing any sub menus.

  if (button.menu != null) {
    closeSubMenu(button.menu);
    button.menu.style.visibility = "hidden";
  }
}

//-----------------------------------------
// Code to handle the menus and sub menus.
//-----------------------------------------

function menuMouseover(event) {

  // Cancel action if page is not yet loaded. ;
  if ( !isReady() ) return false;

  var menu;

  // Find the target menu element.

  if (myBrowser.isIE)
    menu = getContainerWith(window.event.srcElement, "DIV", "menu");
  else
    menu = event.currentTarget;

  // Close any active sub menu.

  if (menu.activeItem != null)
    closeSubMenu(menu);
}

function menuItemMouseover(event, menuId) {

  var item, menu, x, y;

  // Find the target item element and its parent menu element.

  if (myBrowser.isIE)
    item = getContainerWith(window.event.srcElement, "A", "menuItem");
  else
    item = event.currentTarget;
  menu = getContainerWith(item, "DIV", "menu");

  // Close any active sub menu and mark this one as active.

  if (menu.activeItem != null)
    closeSubMenu(menu);
  menu.activeItem = item;

  // Highlight the item element.

  item.className += " menuItemHighlight";

  // Initialize the sub menu, if not already done.

  if (item.subMenu == null) {
    item.subMenu = document.getElementById(menuId);
    if (item.subMenu.isInitialized == null)
      menuInit(item.subMenu);
  }

  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Set mouseout event handler for the sub menu, if not already done.

  if (item.subMenu.onmouseout == null)
    item.subMenu.onmouseout = buttonOrMenuMouseout;

  // [END MODIFIED]

  // Get position for submenu based on the menu item.

  x = getPageOffsetLeft(item) + item.offsetWidth;
  y = getPageOffsetTop(item);

  // Adjust position to fit in view.

  var maxX, maxY;

  if (myBrowser.isIE) {
    maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) +
      (document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
    maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop) +
      (document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
  }
  if (myBrowser.isOP) {
    maxX = document.documentElement.scrollLeft + window.innerWidth;
    maxY = document.documentElement.scrollTop  + window.innerHeight;
  }
  if (myBrowser.isNS) {
    maxX = window.scrollX + window.innerWidth;
    maxY = window.scrollY + window.innerHeight;
  }
  maxX -= item.subMenu.offsetWidth;
  maxY -= item.subMenu.offsetHeight;

  if (x > maxX)
    x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth
      + (menu.offsetWidth - item.offsetWidth));
  y = Math.max(0, Math.min(y, maxY));

  // Position and show the sub menu.

  item.subMenu.style.left = x + "px";
  item.subMenu.style.top  = y + "px";
  item.subMenu.style.visibility = "visible";

  // Stop the event from bubbling.

  if (myBrowser.isIE)
    window.event.cancelBubble = true;
  else
    event.stopPropagation();
}

function closeSubMenu(menu) {

  if (menu == null || menu.activeItem == null)
    return;

  // Recursively close any sub menus.

  if (menu.activeItem.subMenu != null) {
    closeSubMenu(menu.activeItem.subMenu);
    menu.activeItem.subMenu.style.visibility = "hidden";
    menu.activeItem.subMenu = null;
  }
  removeClassName(menu.activeItem, "menuItemHighlight");
  menu.activeItem = null;
}

// [MODIFIED] Added for activate/deactivate on mouseover. Handler for mouseout
// event on buttons and menus.

function buttonOrMenuMouseout(event) {

  var el;

  // If there is no active button, exit.

  if (activeButton == null)
    return;

  // Find the element the mouse is moving to.

  if (myBrowser.isIE)
    el = window.event.toElement;
  else if (event.relatedTarget != null)
      el = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);

  // If the element is not part of a menu, reset the active button.

  if (getContainerWith(el, "DIV", "menu") == null) {
    resetButton(activeButton);
    activeButton = null;
	if (myBrowser.isIE)
	{
		document.all.iFrameWedge.style.visibility = "hidden";
		document.all.iFrameWedge.style.zIndex = -999;
	}
  }

}


// [END MODIFIED]

//--------------------------
// Code to initialize menus.
//--------------------------

function menuInit(menu) {

  var itemList, spanList;
  var textEl, arrowEl;
  var itemWidth;
  var w, dw;
  var i, j;

  // For IE, replace arrow characters.

  if (myBrowser.isIE) {
    menu.style.lineHeight = "2.5ex";
    spanList = menu.getElementsByTagName("SPAN");
    for (i = 0; i < spanList.length; i++)
      if (hasClassName(spanList[i], "menuItemArrow")) {
        spanList[i].style.fontFamily = "Webdings";
        spanList[i].firstChild.nodeValue = "4";
      }
  }

  // Find the width of a menu item.

  itemList = menu.getElementsByTagName("A");
  if (itemList.length > 0)
    itemWidth = itemList[0].offsetWidth;
  else
    return;

  // For items with arrows, add padding to item text to make the
  // arrows flush right.

  for (i = 0; i < itemList.length; i++) {
    spanList = itemList[i].getElementsByTagName("SPAN");
    textEl  = null;
    arrowEl = null;
    for (j = 0; j < spanList.length; j++) {
      if (hasClassName(spanList[j], "menuItemText"))
        textEl = spanList[j];
      if (hasClassName(spanList[j], "menuItemArrow"))
        arrowEl = spanList[j];
    }
    if (textEl != null && arrowEl != null) {
      textEl.style.paddingRight = (itemWidth 
        - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";
      // For Opera, remove the negative right margin to fix a display bug.
      if (myBrowser.isOP)
        arrowEl.style.marginRight = "0px";
    }
  }

  // Fix IE hover problem by setting an explicit width on first item of
  // the menu.

  if (myBrowser.isIE) {
    w = itemList[0].offsetWidth;
    itemList[0].style.width = w + "px";
    dw = itemList[0].offsetWidth - w;
    w -= dw;
    itemList[0].style.width = w + "px";
  }

  // Mark menu as initialized.

  menu.isInitialized = true;
}

//---------------------------
// General utility functions.
//---------------------------

function getContainerWith(node, tagName, className) {

  // Starting with the given node, find the nearest containing element
  // with the specified tag name and style class.

  while (node != null) {
    if (node.tagName != null && node.tagName == tagName &&
        hasClassName(node, className))
      return node;
    node = node.parentNode;
  }

  return node;
}

function hasClassName(el, name) {

  var i, list;

  // Return true if the given element currently has the given class name.

  list = el.className.split(" ");
  for (i = 0; i < list.length; i++)
    if (list[i] == name)
      return true;

  return false;
}

function removeClassName(el, name) {

  var i, curList, newList;

  if (el.className == null)
    return;

  // Remove the given class name from the element's className property.

  newList = new Array();
  curList = el.className.split(" ");
  for (i = 0; i < curList.length; i++)
    if (curList[i] != name)
      newList.push(curList[i]);
  el.className = newList.join(" ");
}

function getPageOffsetLeft(el) {

  var x;

  // Return the x coordinate of an element relative to the page.

  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) {

  var y;

  // Return the x coordinate of an element relative to the page.

  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}



/* ===== FOR DEALING WITH WINDOWED CONTROLS ISSUE ===== */

var intContentOffsetX = 0;  //-5 see LandstarOnlineStyle.css;
var intContentOffsetY = 0; //-80 see LandstarOnlineStyle.css;
var intContentOffsetW = -17; // drop-down arrow width + 2;
var intContentOffsetH = -4; 

function getObjectCoords(obj){
	var xOffset, yOffset, wOffset, hOffset, elParent;
	if(typeof obj.offsetTop == 'number'){
		yOffset = obj.offsetTop||0;
		xOffset = obj.offsetLeft||0;
		wOffset = obj.offsetWidth||0;
		hOffset = obj.offsetHeight||0;
		elParent = obj.offsetParent;
		while(elParent){
			yOffset += elParent.offsetTop||0;
			xOffset += elParent.offsetLeft||0;
		//	wOffset = elParent.offsetWidth||0;
		//	hOffset = elParent.offsetHeight||0;
			elParent = elParent.offsetParent;
		}
	} else if(typeof obj.top == 'number') { //some Net 4 DIVs
		yOffset = obj.top;
		xOffset = obj.left;
		wOffset = 0 // obj.width;
		hOffset = 0 // obj.height;
	} else if(typeof obj.y != 'number') { //Net 4 IMG & A
		yOffset = obj.y;
		xOffset = obj.x;
		wOffset = 0 // obj.width;
		hOffset = 0 // obj.height;
	} else { //information is not available
		yOffset = -1; //maybe use NaN instead.
		xOffset = -1;
		wOffset = 0;
		hOffset = 0;
		// or just - return null; - at this point.
	}
	xOffset += intContentOffsetX;
	yOffset += intContentOffsetY;
	wOffset += intContentOffsetW;
	hOffset += intContentOffsetH;
	return {x:xOffset,y:yOffset,w:wOffset,h:hOffset};
}
