var pageloaded = 0;
var imagesloaded = 0;
var brMSIE = document.all;
var brOK = document.getElementById;
var brNS = document.getElementById && !document.all;
var brOP = window.opera && window.print;
var newWin10 = new Array(10);
var queryNum;
var ejt = Math.floor(Math.random() * 5) + 5;
var eja = 500;
var ejc = 0;
var ejpfx = "../";
var ejsl = 7;
var ejsp = "01g";
var imagePath = "../images/";
var imageStr = "blank,magnify,stoic,stoic2,000,001,002,003,004,005,0ey,0gl,";
var cntX = 0;
var cntY = 0;
var ernieOn = true;
var debugOn = false;
var contentWidth, contentHeight;
var topIndex = 100;
var fontToggle = 0;
var jspecview1 = "jspecviewApplet01";
var jme1 = "jmeApplet01";
var dragObj = null;
var dX = 0;
var dY = 0;
var eX = 0;
var eY = 0;
var visPop = "";
var calcEntry = "";
var ii;
r = new Array();
k = new Array(32); for (ii = 0; ii < k.length; ii++) { k[ii] = 0; }
var mepsData = false;

function nullFunction() {
}

function trim(str) {
    if (str != null) { return str.replace(/^\s+|\s+$/g, ""); }
}

function trimAll(str) {
    return str.replace(/\s/g, "");
}

function isNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;
    for (var i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

function makeWin10(num, url, maxSz, x, y) {
    var winSt = "scrollbars,resizable,menubar,toolbar,location,status,directories";
    url = url.toString();
    maxSz = (!maxSz) ? true : maxSz;
    x = (!x) ? parseInt(contentWidth) : x;
    y = (!y) ? parseInt(contentHeight) : y;
    if (maxSz == true) { winSt = "scrollbars,resizable"; }
    winSt += ",height=" + y + ",width=" + x + ",top=0,left=0";
    var winVal = "" + "win" + num;
    if (brOP) { winSt = ""; winVal = ""; }
    if (!newWin10[num] || newWin10[num].closed) {
        newWin10[num] = window.open(url, winVal, winSt);
        if (!newWin10[num].opener) { newWin10[num].opener = window; }
        newWin10[num].focus();
    }
    else {
        newWin10[num].focus();
    }
}

function focusOpener() {
    if (window.opener) { window.opener.focus(); }
}

function getURL() {
    var callingURL = "";
    var rtnValue = "";
    if (parent.document.location) { callingURL = parent.document.location; }
    if (parent.document.URL) { callingURL = parent.document.URL; }
    rtnValue = callingURL;
    if (callingURL.indexOf('?') != -1) {
        callingURL = unescape(callingURL.substring(callingURL.indexOf('?') + 1, callingURL.length));
        queryNum = callingURL.split('&');
        for (var i = 0; i < queryNum.length; i++) {
            queryNum[i] = queryNum[i].substring(queryNum[i].indexOf('=') + 1, queryNum[i].length);
        }
    }
    return rtnValue;
}

function loadImage(imagetoload) {
    var tempimage = new Image;
    tempimage.src = imagetoload;
}

function loadImages(imagelist) {
    if (document.images) {
        var imgTyp = ".gif";
        var imagearray = imagelist.split(",");
        for (var q = 0; q < imagearray.length; q++) {
            imgTyp = (imagearray[q].indexOf(".") >= 1) ? "" : ".gif";
            loadImage(imagePath + imagearray[q] + imgTyp);
        }
    }
    imagesloaded = 1;
    eJ(500);
}

function blankPics() {
    var d, eid;
    for (var i = 0; i < 10; i++) {
        eid = "pos" + i;
        d = document.getElementById(eid);
        if (d) { d.src = "../images/blank.gif"; }
    }
}

function eJ(loc) {
    if (ernieOn) {
        eJi(loc);
    }
}

function eJi(loc) {
    var t;
    if (imagesloaded == 1 && loc == eja) {
        var n = Math.floor(Math.random() * 5), p;
        n = (n == loc) ? loc + 1 : n;
        n = (n > 4) ? 0 : n;
        if (ejc < ejt) {
            p = "0" + Math.floor(Math.random() * 6) + "g";
            if (loc < 5) { eJs(n, p); }
            if (loc == 500) { eJs(ejsl, ejsp); }
        }
        if (ejt == 6 && ejc == ejt) { tE0(); t = setTimeout("tE1()", 2000); eja = 235; }
        if (ejt != 6 && ejc == ejt) { tE0(); t = setTimeout("tE2()", 2000); eja = 235; }
    }
}

function eJs(loc, pic, eFlg) {
    var p = "" + ejpfx + "images/0" + pic.substring(0, 2) + ".gif";
    var eid = "pos" + loc;
    var d = document.getElementById(eid);
    if (!eFlg) { blankPics(); }
    d.src = "" + p;
    if (!eFlg) { eja = loc; }
    if (!eFlg) { ejc++; }
}

// ernie start location
ejsl = 2;
// initial ernie pic
ejsp = "01g";
// sets final act on random end count
function tE0() {
    blankPics();
    var eid = "posb";
    var d = document.getElementById(eid);
    d.src = "../images/blank.gif";
    eJs(3, "01g", 1); eJs(4, "glg", 1);
}
// ernie wins
function tE1() {
    eJs(4, "01g"); eJs(3, "eyg", 1);
}
// golum wins
function tE2() {
    eJs(3, "glg");
}

function toggleStyle() {
    if (fontToggle == 1) {
        setTagStyle("p", "font", "16px Times");
        setTagStyle("H1", "font", "bold 35px Times");
        setTagStyle("H2", "font", "bold 24px Times");
        setTagStyle("H3", "font", "bold 18px Times");
        setTagStyle("H4", "font", "bold 14px Times");
        setTagStyle("H5", "font", "12px Times");
        setTagStyle("li", "font", "16px Times");
        fontToggle = 0;
    }
    else {
        setTagStyle("p", "font", "20px Times");
        setTagStyle("H1", "font", "bold 35px Times");
        setTagStyle("H2", "font", "bold 24px Times");
        setTagStyle("H3", "font", "bold 18px Times");
        setTagStyle("H4", "font", "bold 18px Times");
        setTagStyle("H5", "font", "14px Times");
        setTagStyle("li", "font", "20px Times");
        fontToggle = 1;
    }
}

function setTagStyle(t, p, v) {
    if (brOK) {
        var elements = document.getElementsByTagName(t);
        for (var i = 0; i < elements.length; i++) {
            elements.item(i).style[p] = v;
        }
    }
}

function changePageBlock() {
}

function showPop(objId, X, Y) {
    getContentDims();
    hidePop();
    var NX = (!X) ? 0 : X;
    var NY = (!Y) ? 0 : Y;
    if (isNumeric(objId)) { objId = "popObj" + objId; }
    if (NX == 0 && NY == 0) { center(objId, 50); }
    if (NX > 300) { shiftTo(objId, cntX * 2 - X, Y); changeVis(objId, "visible"); }
    if (NX < 300 && NX > 0) { shiftTo(objId, X, Y); changeVis(objId, "visible"); }
    visPop = objId;
}

function showPopMod(objId) {
    hidePop();
    var X = 0;
    var Y = 0;
    var obj = document.getElementById('shim');
    while (obj != null) {
        X += obj.offsetLeft;
        Y += obj.offsetTop;
        obj = obj.offsetParent;
    }
    X = X + 5;
    Y = Y + 5;
    if (isNumeric(objId)) { objId = "popObj" + objId; }
    shiftTo(objId, X, Y); changeVis(objId, "visible");
    visPop = objId;
}

function hidePop() {
    if (visPop != "") { hide(visPop); }
}

function getObj(objId) {
    if (isNumeric(objId)) { objId = "mvObj" + objId; }
    if (document.getElementById && document.getElementById(objId)) {
        return document.getElementById(objId).style;
    }
    return false;
}

function changeVis(objId, newVis) {
    var object = getObj(objId);
    if (object) {
        object.visibility = newVis;
    }
}

function show(objId) {
    changeVis(objId, "visible");
}

function hide(objId) {
    if (isNumeric(objId)) { shiftTo(objId, 10, 900); }
    changeVis(objId, "hidden");
}

function shiftTo(objId, X, Y) {
    var object = getObj(objId);
    if (object) {
        object.left = "" + X + "px";
        object.top = "" + Y + "px";
    }
}

function zTop() {
    topIndex++;
    return topIndex;
}

function objColor(objId, color) {
    var object = getObj(objId)
    if (object.bgColor) {
        object.bgColor = color
    } else if (typeof object.backgroundColor != "undefined") {
        object.backgroundColor = color
    }
}

function getObjWidth(objId) {
    if (isNumeric(objId)) { objId = "mvObj" + objId; }
    var objWidth
    if (brOK) {
        if (document.getElementById(objId).offsetWidth >= 0) {
            objWidth = document.getElementById(objId).offsetWidth
        }
    }
    return objWidth
}

function getObjHeight(objId) {
    if (isNumeric(objId)) { objId = "mvObj" + objId; }
    var objHeight
    if (brOK) {
        if (document.getElementById(objId).offsetHeight >= 0) {
            objHeight = document.getElementById(objId).offsetHeight
        }
    }
    return objHeight
}

function getContentDims() {
    if (window.innerWidth) {
        contentWidth = window.innerWidth;
    }
    if (document.body.clientWidth) {
        contentWidth = document.body.clientWidth;
    }
    if (window.innerHeight) {
        contentHeight = window.innerHeight;
    }
    if (document.body.clientHeight) {
        contentHeight = document.body.clientHeight;
    }
    if (document.documentElement.clientWidth) {
        contentWidth = document.documentElement.clientWidth;
    }
    if (document.documentElement.clientHeight) {
        contentHeight = document.documentElement.clientHeight;
    }
    cntX = contentWidth / 2;
    cntY = contentHeight / 2;
}

function center(objId, j) {
    if (isNumeric(objId)) { objId = "mvObj" + objId; }
    var x = Math.round(((contentWidth) / 2) - (getObjWidth(objId) / 2))
    var y = (!j) ? Math.round((((contentHeight) / 2) + 100) - (getObjHeight(objId) / 2)) : j
    shiftTo(objId, x, y);
    changeVis(objId, "visible");
}

function change(objId, b) {
    if (isNumeric(objId)) { objId = "mvObj" + objId; }
    if (brOK) {
        var thObj = document.getElementById(objId);
        if (thObj) {
            thObj.innerHTML = b;
        }
    }
}

function feedback(txt, fbn) {
    var opArg = (!fbn) ? "" : fbn
    var outTxt = (!txt) ? "" : txt
    var fbId = "feedback" + opArg
    change(fbId, outTxt)
}

function globalInit() {
    getContentDims();
    if (brOK) { initDrag(); }
    if (brMSIE) {
        jspecview1 = "jspecviewApplet02";
    }
    if (getURL().toLowerCase().indexOf("localhost") > -1) { debugOn = true; }
    localInit();
}

function initDrag() {
    document.onmousedown = gDrObj;
    document.onmousemove = mDrObj;
    document.onmouseup = dDrObj;
}

function gDrObj(evt) {
    evt = (evt) ? evt : ((window.event) ? event : null);
    var objectID = (evt.target) ? evt.target.id : ((evt.srcElement) ? evt.srcElement.id : null);
    if (objectID.indexOf('mvObj') != -1 || objectID.indexOf('popObj') != -1) dragObj = document.getElementById(objectID);
    if (dragObj) {
        dragObj.style.zIndex = zTop();
        dX = evt.clientX - dragObj.offsetLeft;
        dY = evt.clientY - dragObj.offsetTop;
        return;
    }
    else {
        dragObj = null;
        return;
    }
}

function mDrObj(evt) {
    evt = (evt) ? evt : ((window.event) ? event : null);
    if (dragObj) {
        dragObj.style.left = evt.clientX - dX + 'px';
        dragObj.style.top = evt.clientY - dY + 'px';
        eX = dragObj.style.left;
        eY = dragObj.style.top;
        return false;
    }
}

function dDrObj() {
    if (dragObj) {
        dragObj = null;
        return false;
    }
}

function chrCk(sTxt, vTxt) {
    var validChrs = vTxt;
    var isValid = true;
    var Char;
    for (var i = 0; i < sTxt.length && isValid == true; i++) {
        Char = sTxt.charAt(i);
        if (validChrs.indexOf(Char) == -1) {
            isValid = false;
        }
    }
    return isValid;
}

function right(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

function left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}

function nlToBar(dataStr) {
    return dataStr.replace(/(\r\n|[\r\n])/g, "|");
}

function rand(l, u) {
    return Math.floor((Math.random() * (u - l + 1)) + l);
}

var req = null;
var isMSIE = false;
var ajaxReturnTxt = "";
var ajaxFocus = "nih";
function loadXmlData(url, data) {
    try {
        if (window.XMLHttpRequest) { // native XMLHttpRequest object
            req = new XMLHttpRequest();
        } else { // IE/Windows ActiveX version
            isMSIE = true
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        req.onreadystatechange = processStateChange;
        req.open("POST", url + "?" + data, true);
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        req.send(data);
    } catch (e) {
        alert(e);
    }
}

function processStateChange() {
    if (req.readyState != 4)
        return;
    if (req.status != 200) {
        if (req.status == 404) {return; }
        alert(req.statusText);
        return;
    }
    ajaxReturnTxt = req.responseText; ajaxDataAction();
}

function ajaxDataAction() {
    if (ajaxFocus == "nih") { nihDataAction(); return; }
}

function loadXmlData2(url) {
    try {
        if (window.XMLHttpRequest) { // native XMLHttpRequest object
            req = new XMLHttpRequest();
        } else { // IE/Windows ActiveX version
            isMSIE = true
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        req.onreadystatechange = processStateChange;
        req.open("GET", url, true);
        req.setRequestHeader("User-Agent", navigator.userAgent);
        req.send(null);
    } catch (e) {
        alert(e);
    }
}

function addJS(srcFile) {
    var headID = document.getElementsByTagName("head")[0];
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.onload = scriptLoaded;
    newScript.src = srcFile;
    headID.appendChild(newScript);
}

function scriptLoaded() { }

function processPress(e) {
    var key = e.keyCode || e.which;
    if (key == 13) {
        osrScript(document.getElementById("script").value);
    }
}