﻿// common functions

function Is() {
    var agent = navigator.userAgent.toLowerCase();
    this.major = parseInt(navigator.appVersion);
    this.minor = parseFloat(navigator.appVersion);
    this.ns = ((agent.indexOf('mozilla') != -1) && (agent.indexOf('spoofer') == -1) && (agent.indexOf('compatible') == -1) && (agent.indexOf('opera') == -1) && (agent.indexOf('webtv') == -1));
    this.ns4 = (this.ns && (this.major == 4));
    this.ns6 = (this.ns && (this.major >= 5));
    this.ie = (agent.indexOf("msie") != -1);
    this.ie3 = (this.ie && (this.major < 4));
    this.ie4 = (this.ie && (this.major >= 4));
    this.ie5 = (this.ie && (this.major == 4) && (agent.indexOf("msie 5.0") != -1));
    this.ieX = (this.ie && !this.ie3 && !this.ie4);
}

var is = new Is();

function MM_findObj(n, d) { //v4.01
    var p, i, x;
    if (!d) {
        d = document;
    }
    if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) {
        x = d.all[n];
    }
    for (i = 0; !x && i < d.forms.length; i++) {
        x = d.forms[i][n];
    }
    for (i = 0; !x && d.layers && i < d.layers.length; i++) {
        x = MM_findObj(n, d.layers[i].document);
    }
    if (!x && d.getElementById) {
        x = d.getElementById(n);
    }
    return x;
}



// function to remove whitespace from a string
function trimString(str) {
    return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function checkCookies() { //v3.0
    var cookieEnabled = (navigator.cookieEnabled) ? true : false
    //if not IE4+ nor NS6+
    if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled) {
        document.cookie = "testcookie"
        cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false
    }
    if (cookieEnabled) {
        return true
    } else {
        alert("This function requires that cookies are enabled in your browser options.  Please enable this option and try again.");
        return false
    }
}

function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) {
            break;
        }
    }
    return null;
}

// set cookie data
function SetCookie(name, value) {
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString()))
                  + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain))
                  + ((secure == true) ? "; secure" : "");
}

xOffset = 30;
yOffset = -5;

function showPopup(targetObjectId, eventObj, addOffsetX, addOffsetY) {
    if (eventObj) {
        hideCurrentPopup();
        eventObj.cancelBubble = true;
        captureMousePosition(eventObj);
        xMousePos = xMousePos + xOffset + addOffsetX;
        yMousePos = yMousePos + yOffset + addOffsetY;
        moveObject(targetObjectId, xMousePos, yMousePos);
        if (changeDivVisibility(targetObjectId, 'visible')) {
            if (is.ie) {
                positionShim(targetObjectId);
            }
            changeDivVisibility('DivShim', 'visible');
            window.currentlyVisiblePopup = targetObjectId;
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

function changeDivVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if (styleObject) {
        if (newVisibility == "hidden") {
            styleObject.display = "none";
        } else {
            styleObject.display = "block";
        }
        return true;
    } else {
        // we couldn't find the object, so we can't change its visibility
        return false;
    }
}

function showFixedPopup(targetObjectId, eventObj) {
    if (eventObj) {
        hideCurrentPopup();
        eventObj.cancelBubble = true;
        if (changeDivVisibility(targetObjectId, 'visible')) {
            if (is.ie) {
                positionShim(targetObjectId);
            }
            changeDivVisibility('DivShim', 'visible');
            window.currentlyVisiblePopup = targetObjectId;
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

function hideCurrentPopup() {
    // note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
    if (window.currentlyVisiblePopup) {
        if (is.ie) {
            changeDivVisibility('DivShim', 'hidden');
        }
        changeDivVisibility(window.currentlyVisiblePopup, 'hidden');
        window.currentlyVisiblePopup = false;
    }
}

document.onclick = hideCurrentPopup;

function positionShim(divobj) {
    var DivRef = document.getElementById(divobj);
    var IfrRef = document.getElementById('DivShim');
    if (IfrRef) {
        IfrRef.parentNode.removeChild(IfrRef);
    }
    IfrRef = document.createElement('iframe');
    IfrRef.id = 'DivShim';
    IfrRef.src = 'javascript:false;';
    IfrRef.className = 'divshim';
    DivRef.parentNode.appendChild(IfrRef);
    IfrRef.style.width = DivRef.offsetWidth;
    IfrRef.style.height = DivRef.offsetHeight;
    IfrRef.style.top = DivRef.style.top;
    IfrRef.style.left = DivRef.style.left;
}

function initializeHacks() {
    // this ugly little hack resizes a blank div to make sure you can click
    // anywhere in the window for Mac MSIE 5
    if ((navigator.appVersion.indexOf('MSIE 5') != -1)
    && (navigator.platform.indexOf('Mac') != -1)
    && getStyleObject('blankDiv')) {
        window.onresize = explorerMacResizeFix;
    }
    resizeBlankDiv();
    // this next function creates a placeholder object for older browsers
    createFakeEventObj();
}

function createFakeEventObj() {
    // create a fake event object for older browsers to avoid errors in function call
    // when we need to pass the event object to functions
    if (!window.event) {
        window.event = false;
    }
}
function resizeBlankDiv() {
    // resize blank placeholder div so IE 5 on mac will get all clicks in window
    if ((navigator.appVersion.indexOf('MSIE 5') != -1)
    && (navigator.platform.indexOf('Mac') != -1)
    && getStyleObject('blankDiv')) {
        getStyleObject('blankDiv').width = document.body.clientWidth - 20;
        getStyleObject('blankDiv').height = document.body.clientHeight - 20;
    }
}

function explorerMacResizeFix() {
    location.reload(false);
}

xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page
function captureMousePosition(e) {
    if (document.layers) {
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth + window.pageXOffset;
        yMousePosMax = window.innerHeight + window.pageYOffset;
    } else if (document.all) {
        scrOfY = 0;
        scrOfX = 0;
        if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
            //DOM compliant
            scrOfY = document.body.scrollTop;
            scrOfX = document.body.scrollLeft;
        } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
            //IE6 standards compliant mode
            scrOfY = document.documentElement.scrollTop;
            scrOfX = document.documentElement.scrollLeft;
        }
        xMousePos = window.event.x + scrOfX;
        yMousePos = window.event.y + scrOfY;
        xMousePosMax = document.body.clientWidth + scrOfX;
        yMousePosMax = document.body.clientHeight + scrOfY;
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth + window.pageXOffset;
        yMousePosMax = window.innerHeight + window.pageYOffset;
    }
}

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if (document.getElementById && document.getElementById(objectId)) {
        // W3C DOM
        //alert("W3C DOM");
        return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
        // MSIE 4 DOM
        //alert("MSIE 4 DOM");
        return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
        // NN 4 DOM.. note: this won't find nested layers
        //alert("NN 4 DOM");
        return document.layers[objectId];
    } else {
        return false;
    }
}

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if (styleObject) {
        if ((newXCoordinate + parseInt(String(styleObject.width))) >= xMousePosMax) {
            // keep X coordinate within bounds (make sure the layer doesn't go off the edge of the page)
            newXCoordinate = newXCoordinate - ((newXCoordinate + parseInt(String(styleObject.width)) - xMousePosMax) + 30);
        }
        styleObject.left = newXCoordinate + 'px';
        styleObject.top = newYCoordinate + 'px';
        return true;
    } else {
        // we couldn't find the object, so we can't very well move it
        return false;
    }
}

// String functions
function Trim(STRING) {
    STRING = LTrim(STRING);
    return RTrim(STRING);
}

function RTrim(STRING) {
    while (STRING.charAt((STRING.length - 1)) == " ") {
        STRING = STRING.substring(0, STRING.length - 1);
    }
    return STRING;
}

function LTrim(STRING) {
    while (STRING.charAt(0) == " ") {
        STRING = STRING.replace(STRING.charAt(0), "");
    }
    return STRING;
}


