
// Script and variables to indicate the current browser

var nn4 = false;    // Netscape Navigator or Communicator
var ie4 = false;
var ie5 = false;
var ie6 = false;
var ie7 = false;
var moz = false;    // Mozilla, NS 6+, Konqueror, etc
var dom1 = false;   // fully supports DOM1
var dom2 = false;   // fully supports (important bits of) DOM2
var old = false;    // very old browser
var isIE5Mac = false;
var is_mac = false;


function browser_detect()
{
    // do it the official W3C way

    if ( window.document.implementation != null)
    {
         dom1 = window.document.implementation.hasFeature("HTML","1.0");
         dom2 = window.document.implementation.hasFeature("HTML","2.0") &&
            window.document.implementation.hasFeature("Events","2.0") &&
            window.document.implementation.hasFeature("Core","2.0") &&
            window.document.implementation.hasFeature("CSS2","2.0");
    }

    // Mozilla based browsers contain the Gecko rendering engine

    moz = (window.navigator != null )
        ? ( window.navigator.userAgent.indexOf("ecko") != -1 )
        : false;

    nn4 = (window.document.layers != null && !moz);

    // IE has incremental support for the Document Object Model

	ie7 = (window.document.all && document.documentElement && (typeof document.documentElement.style.maxHeight !="undefined"));
    ie6 = (window.document.all && dom1 && !ie7);
    ie5 = (window.document.all && window.document.getElementsByTagName && !ie6 && !ie7);
    ie4 = (window.document.all && !ie7 && !ie6 && !ie5);

    old = (!ie4 && !ie5 && !ie6 && !ie7 && !dom1 && !moz);
   
    if (old) {
        //alert("Redirecting user with old browser!");
        top.location.href = "/mpa/browserNotSupported.do";
        return;
    }
    
    
    // Added a check for whether we are on a mac as ie5 on the
    // mac appears as ie6
    var agt=navigator.userAgent.toLowerCase();
    is_mac  = (agt.indexOf("mac")!=-1);
    if (is_mac && (ie6 || ie7)) {
        ie4 = false;
        ie5 = true;
        ie6 = false;
        ie7 = false;
        isIE5Mac = true;
    }
    //alert("IsMac=" + is_mac);
}

browser_detect();

//----------------------------------------------------------------------
// Browser independent method that gets an element by its id
// Removed need for other functions to worry about cross-browser
// ways to get document nodes. Supports DOM and IE Object Model
// way of doing things. This function does not suport the Netscape 4
// Object Model.
function getElement(elementId) {
    if (moz || dom2) {
        return document.getElementById(elementId);
    }
    
    if ( ie4 || ie5 || ie6 || ie7) {
        return document.all(elementId);
        
    }
}

//
// Script to allow clicking on labels to toggle checkboxes/radios in an 
// enclosed span.  You would do something like this:
//
// <span onclick="toggleCheckbox('level704')">
//     <span id="level704">
//         <input type="checkbox" ... onclick="cancelCheckboxEventBubble(event)"/> label
//     </span>
// </span>
// 
function toggleCheckbox(name) {
    var spanTag = getElement(name);
    if (spanTag.childNodes != null) {
        for (var i = 0; i < spanTag.childNodes.length; i++) {
            if ((spanTag.childNodes[i].type == "checkbox") || (spanTag.childNodes[i].type == "radio")) {
                var osCheckBox = spanTag.childNodes[i];
                osCheckBox.checked = !(osCheckBox.checked);    
            }
        }
    }    
}

// This function should be called from the brand
// checkbox onclick handler to prevent the event
// from bubbling up the hierarchy and being unset
// when the onclick event handler on the enclosing td
// fires and toggles the checkbox
function cancelCheckboxEventBubble(evt) {
    evt = (evt) ? evt : ((event) ? event : null);
    if (evt) {
        evt.cancelBubble = true;
    }
}

//
// de-select all checkboxes/radios on the form.
//
function uncheckall(form) { 
	for (var i = 0; i < form.elements.length; i++) {
        var anElement = form.elements[i];
        if ((anElement.type == "checkbox") || (anElement.type == "radio")) {
            anElement.checked = false;
        }
    }
}
//
// select all checkboxes/radios on the form.
//
function checkall(form) {
    for (var i = 0; i < form.elements.length; i++) {
        var anElement = form.elements[i];
        if ((anElement.type == "checkbox") || (anElement.type == "radio")) {
            anElement.checked = true;
        }
    }
}


//--------------------------------------------------------------------------
// This function should be used in conjunction with an onkeypress handler
// to prevent form submission by hitting ENTER key.
// Specifically, this is used on text input fields where there is a known
// bug in IE where auto-submit on a form containing only 1 single text input 
// field causes the submit button to be lost.
//--------------------------------------------------------------------------

function noenter(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
	var keyCode;
	if ( ie4 || ie5 || ie6 || ie7 ) {
		//alert("IE: " + evt.keyCode);
		keyCode = evt.keyCode;
	} else {
		//alert("non-IE: " + evt.which);
		keyCode = evt.which;
	}
	return !(evt && keyCode == 13);
}
