// EMSL Public Javascript

//Globals - None right now

//Functions

// highlightPage :: highlights the navigation links for the section we're on, and the secondary page if it exists
//                  Should be called onload by every page


function highlightPage(firstLevel, secondLevel) {
    if(document.getElementById) {
        var pageLink = document.getElementById(firstLevel);
        if(pageLink.style) {
            pageLink.style.backgroundColor = '#CAD9D4';
            if(pageLink.firstChild) {
                pageLink.firstChild.style.color = '#006C5F';
                pageLink.firstChild.style.backgroundColor = '#CAD9D4';
            }
        }
        if(secondLevel != null) {
            var secondaryLink = document.getElementById(secondLevel);
            if(secondaryLink.style) {
                secondaryLink.style.backgroundColor = 'white';
                //secondaryLink.firstChild.style.color = 'black';
                //secondaryLink.firstChild.style.textDecoration = 'none';
                //secondaryLink.firstChild.style.cursor = 'default';
            }
        }
    }
}


// captureClick :: calls newWindow, and returns false so that the href in the link doesn't activate.
//                 When this is used with a certain anchor format we can have nice sized javascript windows,
//                 or normal new windows if Javascript is turned off

function captureClick(url) {
    newWindow(url);
    return false;
}


// newWindow :: creates a new window pointing to a given URL.  The window has the features we want.

function newWindow(url) {
    var nw=window.open(url, "emslWindow", "scrollbars,toolbar,menubar,location,status,resizable,width=750,height=450");

    if(parseInt(navigator.appVersion) >= 4) {
        var nwLeft = (screen.availWidth - 770) / 2;
        var nwTop = (screen.availHeight - 450) / 2;

        if(parseInt(navigator.appVersion) > 4) {
            moveWindow(nw, nwLeft, nwTop);
        }
    }
    nw.focus();
}


//  moveWindow :: given a window object, moves the window to the given coordinates
//                This generates errors in IE4 and Netscape 4; they don't like the exception handling.
//                But it's necessary to support newer browsers, so we leave it in.

function moveWindow(newWindow, windowLeft, windowTop) {
    try {
        newWindow.moveTo(windowLeft, windowTop);
    }
    catch (e) {
        //alert("Exception Caught!");
    }
}


// captureEUSClick :: the same as captureClick, but it calls the eusWindow function

function captureEUSClick(url) {
    eusWindow("userSystem", url, 770, 680);
    return false;
}


//-- eusWindow :: takes a name, url, width and maximum height of a window, adjusts the height as necessary, and spawns new window

    function eusWindow(windowName, url, winWidth, maxHeight) {
        var winHeight = ((screen.availHeight-80) > maxHeight) ? maxHeight : screen.availHeight-80;

        var nw=window.open(url, windowName, "scrollbars,status,resizable,width=" + winWidth + ",height=" + winHeight);

        if(parseInt(navigator.appVersion) >= 4) {
            var nwLeft = parseInt((screen.availWidth - winWidth) / 2);
            var nwTop = parseInt((screen.availHeight - winHeight) / 2);

            if(parseInt(navigator.appVersion) >= 4) {
                moveWindow(nw, nwLeft, nwTop-50);
            }
        }
        nw.focus();
    }


//-- isEmail :: checks the given string to determine whether it follows valid email address format

    function isEmail(str) {
        // are regular expressions supported?
        var supported = 0;

        if (window.RegExp) {
            var tempStr = "a";
            var tempReg = new RegExp(tempStr);
            if (tempReg.test(tempStr))
                supported = 1;
        }

        if (!supported)
            return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

        var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
        var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");

        return (!r1.test(str) && r2.test(str));
    }


//  MM_reloadPage :: reloads the page for Netscape 4.  Called when page is loaded and onresize

function MM_reloadPage(init) {
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

// End of JavaScript