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 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 = "shim,blank,stoic,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 jme1 = "jmeApplet01";
var dX = 0;
var dY = 0;
var eX = 0;
var eY = 0;
var calcEntry = "";

function nullFunction() {
}

function reloadPage() {
}

function enterFunction() {
}

function osrScript() {
}

function update() {
}

function trim(str) {
    if (str != null) { return str.replace(/^\s+|\s+$/g, ""); }
}

function trimAll(str) {
    return str.replace(/\s/g, "");
}

function isNumeric(sText) {
    if (sText == null) { return false; }
    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 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 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) {
    var object = getObj(objId);
    //object.zIndex = zTop();
    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 globalInit() {
    getContentDims();
    if (getURL().toLowerCase().indexOf("localhost") > -1) { debugOn = true; }
    localInit();
}

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";
//var toId = null;
var xhrTimeout;
function loadXmlData(url, data, data2, async) {
    data2 = (!data2) ? null : data2;
    async = (!async) ? true : false;
    try {
        if (window.XMLHttpRequest) { // native XMLHttpRequest object
            req = new XMLHttpRequest();
        } else { // IE/Windows ActiveX version
            isMSIE = true
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        //if (!toId) { toId = setTimeout("req.abort();endReq();", 10000); }
        req.open("POST", url + "?" + data, async);
        if (async == true) {
            req.onreadystatechange = processStateChange;
        }
        //req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        req.send(data2);
        if (async == false) {
            ajaxReturnTxt = req.responseText; ajaxDataAction();
        }
        xhrTimeout = setTimeout("ajaxTimeout();", 20000);
    } catch (e) {
        alert(e);
    }
}

function ajaxTimeout() { req.abort(); nihLoadedAction = "timeout"; ajaxReturnTxt = ""; ajaxDataAction(); }

function processStateChange() {
    if (req.readyState != 4) { return; }
    if (req.status != 200) { return; }
    clearTimeout(xhrTimeout);
    ajaxReturnTxt = req.responseText; ajaxDataAction();
}

function processStateChange2() {
    if (req.readyState != 4) { return; }
    if (req.status != 200) { return; }
    //clearTimeout(xhrTimeout);
    ajaxReturnTxt = req.responseText; ajaxDataAction2();
}

function ajaxDataAction() {
    //req.abort(); req = null; clearTimeout(xhrTimeout);
    if (ajaxFocus == "nih") { nihDataAction(); }
}

function ajaxDataAction2() {
    //req = null; clearTimeout(toId); toId = null;
    if (ajaxFocus == "nih") { nihDataAction(); }
}

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.open("GET", url, true);
        req.onreadystatechange = processStateChange2;
        //req.setRequestHeader("User-Agent", navigator.userAgent);
        req.setRequestHeader("Accept", "text/xml");
        req.send(null);
    } catch (e) {
        alert(e);
    }
}

function processPress(e, num) {
    var key = e.keyCode || e.which;
    if (key == 13) {
        if (num == 1) { osrScript(document.getElementById("script").value); }
        if (num == 2) { update(); }
        if (num == 3) { reloadPage(); }
        if (num == 4) { enterFunction(); }
    }
}

function createCookie(name, value, days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    else expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

Array.prototype.max = function () {
    var max = this[0];
    var len = this.length;
    for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
    return max;
}

Array.prototype.min = function () {
    var min = this[0];
    var len = this.length;
    for (var i = 1; i < len; i++) if (this[i] < min) min = this[i];
    return min;
}
