// JavaScript Document

// Browser Detection

    var agt=navigator.userAgent.toLowerCase();

    var is_major = parseInt(navigator.appVersion);
    var is_minor = parseFloat(navigator.appVersion);

    var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
    var is_nav2 = (is_nav && (is_major == 2));
    var is_nav3 = (is_nav && (is_major == 3));
    var is_nav4 = (is_nav && (is_major == 4));
    var is_nav4up = (is_nav && (is_major >= 4));
    var is_navonly      = (is_nav && ((agt.indexOf(";nav") != -1) ||
                          (agt.indexOf("; nav") != -1)) );
    var is_nav6 = (is_nav && (is_major == 5));
    var is_nav6up = (is_nav && (is_major >= 5));
    var is_gecko = (agt.indexOf('gecko') != -1);


    var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    var is_ie3    = (is_ie && (is_major < 4));
    var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
    var is_ie4up  = (is_ie && (is_major >= 4));
    var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
    var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
    var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
    var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
    var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
    var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);

    var is_aol   = (agt.indexOf("aol") != -1);
    var is_aol3  = (is_aol && is_ie3);
    var is_aol4  = (is_aol && is_ie4);
    var is_aol5  = (agt.indexOf("aol 5") != -1);
    var is_aol6  = (agt.indexOf("aol 6") != -1);

    var is_opera = (agt.indexOf("opera") != -1);
    var is_opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
    var is_opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
    var is_opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
    var is_opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
    var is_opera5up = (is_opera && !is_opera2 && !is_opera3 && !is_opera4);

    var is_webtv = (agt.indexOf("webtv") != -1);

    var is_TVNavigator = ((agt.indexOf("navio") != -1) || (agt.indexOf("navio_aoltv") != -1));
    var is_AOLTV = is_TVNavigator;

    var is_hotjava = (agt.indexOf("hotjava") != -1);
    var is_hotjava3 = (is_hotjava && (is_major == 3));
    var is_hotjava3up = (is_hotjava && (is_major >= 3));

// end Browser Detection

// print page
function printPage() {
	window.print();
	return false;
}

function popUpEmail() {
	var EMAIL_FRIEND_URL = 'L10Apps/email_friend.php';
	var popUp = window.open(L10_SITE_URL+EMAIL_FRIEND_URL+'?refer='+L10_SITE_URL+location.pathname,'emailWindow','width=420,height=400,location=no,menubar=no,scrollbars=no');
}



/* -- Ecommerce Autofill stuff -- */

function refreshShippingAddressFields(index) {
	document.ecartShippingForm.firstname.value = shippingAddress[index]['firstname'];
	document.ecartShippingForm.middleinit.value = shippingAddress[index]['middleinit'];
	document.ecartShippingForm.lastname.value = shippingAddress[index]['lastname'];
	document.ecartShippingForm.address1.value = shippingAddress[index]['address1'];
	document.ecartShippingForm.address2.value = shippingAddress[index]['address2'];
	document.ecartShippingForm.city.value = shippingAddress[index]['city'];
	setShippingState(shippingAddress[index]['state']);
	document.ecartShippingForm.zip.value = shippingAddress[index]['zip'];
	document.ecartShippingForm.zipext.value = shippingAddress[index]['zipext'];
}

function setShippingState(autofill) {
	var shippingCombo = document.ecartShippingForm.state;
	for (var i = 0; i <= shippingCombo.options.length - 1; i++) {
		// alert('select box: ' + shippingCombo.options[i].value + "\n\n" + 'abbr: ' + abbr);
		if (shippingCombo.options[i].value == autofill) { shippingCombo.selectedIndex = i; }
	}
}


function refreshPaymentFields(index) {
	
	document.ecartPaymentForm.firstname.value = paymentAddress[index]['firstname'];
	document.ecartPaymentForm.middleinit.value = paymentAddress[index]['middleinit'];
	document.ecartPaymentForm.lastname.value = paymentAddress[index]['lastname'];
	document.ecartPaymentForm.address1.value = paymentAddress[index]['address1'];
	document.ecartPaymentForm.address2.value = paymentAddress[index]['address2'];
	document.ecartPaymentForm.city.value = paymentAddress[index]['city'];
	setPaymentState(paymentAddress[index]['state']);
	document.ecartPaymentForm.zip.value = paymentAddress[index]['zip'];
	document.ecartPaymentForm.zipext.value = paymentAddress[index]['zipext'];

	document.ecartPaymentForm.ccnumber.value = paymentAddress[index]['ccnumber'];
	setPaymentType(paymentAddress[index]['cctype']);
	setPaymentMonth(paymentAddress[index]['ccexpmonth']);
	setPaymentYear(paymentAddress[index]['ccexpyear']);

}

function setPaymentType(autofill) {
	var typeCombo = document.ecartPaymentForm.cctype;
	for (var i = 0; i <= typeCombo.options.length - 1; i++) {
		if (typeCombo.options[i].value == autofill) { typeCombo.selectedIndex = i; }
	}
}

function setPaymentMonth(autofill) {
	var monthCombo = document.ecartPaymentForm.ccexpmonth;
	for (var i = 0; i <= monthCombo.options.length - 1; i++) {
		if (monthCombo.options[i].value == autofill) { monthCombo.selectedIndex = i; }
	}
}

function setPaymentYear(autofill) {
	var yearCombo = document.ecartPaymentForm.ccexpyear;
	for (var i = 0; i <= yearCombo.options.length - 1; i++) {
		if (yearCombo.options[i].value == autofill) { yearCombo.selectedIndex = i; }
	}
}

function setPaymentState(autofill) {
	var stateCombo = document.ecartPaymentForm.state;
	for (var i = 0; i <= stateCombo.options.length - 1; i++) {
		if (stateCombo.options[i].value == autofill) { stateCombo.selectedIndex = i; }
	}
}

function swapStates(index) {
	
	var url = L10_SITE_URL +'L10Ecommerce/ajax.php?module=shipping&ajax=GetStates';
	var postinfo = 'options[countryid]='+index;
	
	var theAjax = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: postinfo,
			onComplete: insertStates
		});
	
}
function insertStates(theXML) {
	
	$('state-cell').innerHTML = theXML.responseText;
	
}


// End ecommerce autofill





// rollover menu at the top

// global menu state
//var menuReady = false;

// pre-cache menubar image pairs
// This is now written to disk when CMS Navigation is altered...saved in dropdowns.js

/*
if (document.images) {
    var imagesNormal = new Array();
    imagesNormal["speaking-engagements"] = new Image(23, 83);
    imagesNormal["speaking-engagements"].src = "images/n_speaking-engagements.gif";
    imagesNormal["shop"] = new Image(23, 84);
    imagesNormal["shop"].src = "images/n_shop.gif";
    imagesNormal["media"] = new Image(23, 83);
    imagesNormal["media"].src = "images/n_media.gif";
    imagesNormal["resources"] = new Image(23, 83);
    imagesNormal["resources"].src = "images/n_resources.gif";
    imagesNormal["tools-support"] = new Image(23, 83);
    imagesNormal["tools-support"].src = "images/n_tools-support.gif";
    imagesNormal["q-a"] = new Image(23, 83);
    imagesNormal["q-a"].src = "images/n_q-a.gif";
    imagesNormal["practitioners"] = new Image(23, 82);
    imagesNormal["practitioners"].src = "images/n_practitioners.gif";

	// there are no rollover effects for the top navigation
	
	var imagesHilite = imagesNormal; // new Array();

}

*/

function getElementStyle(elem, IEStyleProp, CSSStyleProp) {
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}

/*

menuItemLineHeight controls the spacing of the menu items

*/

// carry over some critical menu style sheet attribute values

/*
if (!is_ie) {
	var CSSRuleValues = {menuItemHeight:"24px",
						 menuItemLineHeight:"2em",
						 menuWrapperBorderWidth:"0px",
						 menuWrapperPadding:"0px",
						 defaultBodyFontSize:"100%"
						};
} else {
	var CSSRuleValues = {menuItemHeight:"4px",
						 menuItemLineHeight:".32em",
						 menuWrapperBorderWidth:"0px",
						 menuWrapperPadding:"0px",
						 defaultBodyFontSize:"100%"
						};
}
*/

// specifications for menu contents and menubar image associations
// This is now written to disk when CMS Navigation is altered...saved in dropdowns.js

/*
var menus = new Array();
menus[0] = {mBarImgId:"menuImg_0",
            mBarImgNormal:imagesNormal["speaking-engagements"],
            mBarImgHilite:imagesHilite["speaking-engagements"],
            menuItems:[ {text:"Sub Menu 1", href:"/construction.php"},
						{text:"Sub Menu 2", href:"/aerospace.php"},
						{text:"Sub Menu 3", href:"/heavy-haul.php"}
					   ],
            elemId:""
           };
menus[1] = {mBarImgId:"menuImg_1",
            mBarImgNormal:imagesNormal["shop"],
            mBarImgHilite:imagesHilite["shop"],
            menuItems:[ {text:"Sub Menu 4", href:"/construction.php"},
						{text:"Sub Menu 5", href:"/aerospace.php"},
						{text:"Sub Menu 6", href:"/heavy-haul.php"}
					   ],
            elemId:""
           };
menus[2] = {mBarImgId:"menuImg_2",
            mBarImgNormal:imagesNormal["media"],
            mBarImgHilite:imagesHilite["media"],
            menuItems:[],
            elemId:""
           };
menus[3] = {mBarImgId:"menuImg_3",
            mBarImgNormal:imagesNormal["resources"],
            mBarImgHilite:imagesHilite["resources"],
            menuItems:[ {text:"Pre-application", href:"/pre-application.php"}
					  ],
            elemId:""
           };
menus[4] = {mBarImgId:"menuImg_4",
            mBarImgNormal:imagesNormal["tools-support"],
            mBarImgHilite:imagesHilite["tools-support"],
            menuItems:[],
            elemId:""
           };
menus[5] = {mBarImgId:"menuImg_5",
            mBarImgNormal:imagesNormal["q-a"],
            mBarImgHilite:imagesHilite["q-a"],
            menuItems:[],
            elemId:""
           };
menus[6] = {mBarImgId:"menuImg_6",
            mBarImgNormal:imagesNormal["practitioners"],
            mBarImgHilite:imagesHilite["practitioners"],
            menuItems:[],
            elemId:""
           };
*/

// create hash table-like lookup for menu objects with id string indexes
function makeHashes() {
    for (var i = 0; i < menus.length; i++) {
        menus[menus[i].elemId] = menus[i];
        menus[menus[i].mBarImgId] = menus[i];
    }
}

// assign menu label image event handlers
function assignLabelEvents() {
    var elem;
    for (var i = 0; i < menus.length; i++) {
        elem = document.getElementById(menus[i].mBarImgId);
        elem.onmouseover = swap;
        elem.onmouseout = swap;
    }
}

// invoked from init(), generates the menu div elements and their contents.
// all this action is invisible to user during construction
function makeMenus() {
    var menuDiv, menuItem, itemLink, mbarImg, textNode, offsetLeft, offsetTop;

    // determine key adjustment factors for the total height of menu divs

    var menuItemH = 0;
    var bodyFontSize = parseInt(getElementStyle(document.body, "fontSize", "font-size"));
    // test to see if browser's font size has been adjusted by the user
    // and that the new size registers as an applied style property
    if (bodyFontSize == parseInt(CSSRuleValues.defaultBodyFontSize)) {
        menuItemH = (parseFloat(CSSRuleValues.menuItemHeight));
    } else {
        // works nicely in Netscape 7
        menuItemH = parseInt(parseFloat(CSSRuleValues.menuItemLineHeight) * bodyFontSize);
    }
    var heightAdjust = parseInt(CSSRuleValues.menuWrapperPadding) +
        parseInt(CSSRuleValues.menuWrapperBorderWidth);
    if (navigator.appName == "Microsoft Internet Explorer" &&
        navigator.userAgent.indexOf("Win") != -1 &&
        (typeof document.compatMode == "undefined" ||
        document.compatMode == "BackCompat")) {
        heightAdjust = -heightAdjust;
    }

    // use menus array to drive div creation loop
    for (var i = 0; i < menus.length; i++) {
        menuDiv = document.createElement("div");
        menuDiv.id = "popupmenu" + i;
        // preserve menu's ID as property of the menus array item
        menus[i].elemId = "popupmenu" + i;
        menuDiv.className = "menuWrapper";
        if (menus[i].menuItems.length > 0) {
            menuDiv.style.height = (menuItemH * menus[i].menuItems.length) -
            heightAdjust + "px";
        } else {
            // don't display any menu div lacking menu items
            menuDiv.style.display = "none";
        }
        // define event handlers
        menuDiv.onmouseover = keepMenu;
        menuDiv.onmouseout = requestHide;

        // set stacking order in case other layers are around the page
        menuDiv.style.zIndex = 2000;

        // assemble menu item elements for inside menu div
        for (var j = 0; j < menus[i].menuItems.length; j++) {
            menuItem = document.createElement("div");
            menuItem.id = "popupmenuItem_" + i + "_" + j;
            menuItem.className = "menuItem";
            menuItem.onmouseover = toggleHighlight;
            menuItem.onmouseout = toggleHighlight;
            menuItem.onclick = hideMenus;
            menuItem.style.top = menuItemH * j + "px";
            itemLink = document.createElement("a");
            itemLink.href = menus[i].menuItems[j].href;
            itemLink.className = "menuItem";
            itemLink.onmouseover = toggleHighlight;
            itemLink.onmouseout = toggleHighlight;
            textNode = document.createTextNode(menus[i].menuItems[j].text);
            itemLink.appendChild(textNode);
            menuItem.appendChild(itemLink);
            menuDiv.appendChild(menuItem);
        }
        // append each menu div to the body
        document.body.appendChild(menuDiv);
    }
    makeHashes();
    assignLabelEvents();
    // pre-position menu
    for (i = 0; i < menus.length; i++) {
        positionMenu(menus[i].elemId);
    }
    menuReady = true;
}

// initialize global that helps manage menu hiding
var timer;

// invoked from mouseovers inside menus to cancel hide
// request from mouseout of menu bar image et al.
function keepMenu() {
    clearTimeout(timer);
}

function cancelAll() {
    keepMenu();
    menuReady = false;
}

// invoked from mouseouts to request hiding all menus
// in 1/4 second, unless cancelled
function requestHide() {
    timer = setTimeout("hideMenus()", 250);
}

// "brute force" hiding of all menus and restoration
// of normal menu bar images
function hideMenus() {
    for (var i = 0; i < menus.length; i++) {
       document.getElementById(menus[i].mBarImgId).src = menus[i].mBarImgNormal.src;
       var menu = document.getElementById(menus[i].elemId)
       menu.style.visibility = "hidden";
    }
}

// set menu position just before displaying it
function positionMenu(menuId){
    // use the menu bar image for position reference of related div
    var mBarImg = document.getElementById(menus[menuId].mBarImgId);
    var offsetTrail = mBarImg;
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 &&
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    var menuDiv = document.getElementById(menuId);
    menuDiv.style.left = offsetLeft + "px";
    menuDiv.style.top = offsetTop + mBarImg.height + "px";
}

// display a particular menu div
function showMenu(menuId) {
    if (menuReady) {
        keepMenu();
        hideMenus();
        positionMenu(menuId);
        var menu = document.getElementById(menuId);
        menu.style.visibility = "visible";
    }
}

// menu bar image swapping, invoked from mouse events in menu bar
// swap style sheets for menu items during rollovers
function toggleHighlight(evt) {
    evt = (evt) ? evt : ((event) ? event : null);
    if (typeof menuReady != "undefined") {
        if (menuReady && evt) {
            var elem = (evt.target) ? evt.target : evt.srcElement;
            if (elem.nodeType == 3) {
                elem = elem.parentNode;
            }
            if (evt.type == "mouseover") {
                keepMenu();
                elem.className ="menuItemOn";
            } else {
                elem.className ="menuItem";
                requestHide();
            }
            evt.cancelBubble = true;
        }
    }
}

function swap(evt) {
    evt = (evt) ? evt : ((event) ? event : null);
    if (typeof menuReady != "undefined") {
        if (evt && (document.getElementById && document.styleSheets) && menuReady) {
            var elem = (evt.target) ? evt.target : evt.srcElement;
            if (elem.className == "menuImg") {
                if (evt.type == "mouseover") {
                    showMenu(menus[elem.id].elemId);
                    elem.src = menus[elem.id].mBarImgHilite.src;
                } else if (evt.type == "mouseout") {
                    requestHide();
                }
                evt.cancelBubble = true;
            }
        }
    }
}

// create menus only if key items are supported
function initMenu() {
    if (document.getElementById && document.styleSheets) {
        setTimeout("makeMenus()", 5);
        window.onunload=cancelAll;
    }
}


/// end rollover functions
