﻿// Body onload utility (supports multiple onload functions)
var gSafeOnload = new Array();

function SafeAddOnload(f) {
    // Browser Detection
    var isMac = (navigator.appVersion.indexOf("Mac") != -1) ? true : false;
    var NS4 = (document.layers) ? true : false;
    var IEmac = ((document.all) && (isMac)) ? true : false;
    var IE4plus = (document.all) ? true : false;
    var IE4 = ((document.all) && (navigator.appVersion.indexOf("MSIE 4.") != -1)) ? true : false;
    var IE5 = ((document.all) && (navigator.appVersion.indexOf("MSIE 5.") != -1)) ? true : false;
    var ver4 = (NS4 || IE4plus) ? true : false;
    var NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape') != -1) ? true : false;

    if (IEmac && IE4)  // IE 4.5 blows out on testing window.onload
    {
        window.onload = SafeOnload;
        gSafeOnload[gSafeOnload.length] = f;
    }
    else if (window.onload) {
        if (window.onload != SafeOnload) {
            gSafeOnload[0] = window.onload;
            window.onload = SafeOnload;
        }
        gSafeOnload[gSafeOnload.length] = f;
    }
    else
        window.onload = f;
}

function SafeOnload() {
    for (var i = 0; i < gSafeOnload.length; i++)
        gSafeOnload[i]();
}
// Toggle layer (hide if show, show if hide)
function ToggleDisplay(obj) {
    if (obj.style.display == "none")
        obj.style.display = "block";
    else
        obj.style.display = "none";
}

// Determine the control's (mainly textbox) default submit button
function ClickButton(e, buttonid) {
    var bt = document.getElementById(buttonid);
    if (typeof bt == 'object') {
        if (navigator.appName.indexOf("Netscape") > (-1)) {
            if (e.keyCode == 13) {
                bt.click();
                return false;
            }
        }
        if (navigator.appName.indexOf("Microsoft Internet Explorer") > (-1)) {
            if (event.keyCode == 13) {
                bt.click();
                return false;
            }
        }
    }
}

function popupEmail(url) {
    window.open(url, 'mywindow', 'menubar=1,resizable=1,width=800,height=600');
}

function GetWindowWidth() {
    var x = 0;
    if (self.innerHeight) {
        x = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        x = document.documentElement.clientWidth;
    }
    else if (document.body) {
        x = document.body.clientWidth;
    }
    return x;
}

function GetWindowHeight() {
    var y = 0;
    if (self.innerHeight) {
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        y = document.documentElement.clientHeight;
    }
    else if (document.body) {
        y = document.body.clientHeight;
    }
    return y;
}

function ShowTextLength(field, divTextLenId, maxLen) {
    var divTextLen = document.getElementById(divTextLenId);
    divTextLen.innerHTML = "(" + txtNumberOfChars + ": " + field.value.length + " / " + maxLen + ")";
    if (field.value.length > maxLen) {
        // if too long...trim it!
        field.value = field.value.substring(0, maxLen);
        divTextLen.innerHTML = "(" + txtNumberOfChars + ": " + field.value.length + " / " + maxLen + ")";
    }
}

function getAnchorPosition(anchorname) { var useWindow = false; var coordinates = new Object(); var x = 0, y = 0; var use_gebi = false, use_css = false, use_layers = false; if (document.getElementById) { use_gebi = true; } else if (document.all) { use_css = true; } else if (document.layers) { use_layers = true; } if (use_gebi && document.all) { x = AnchorPosition_getPageOffsetLeft(document.all[anchorname]); y = AnchorPosition_getPageOffsetTop(document.all[anchorname]); } else if (use_gebi) { var o = document.getElementById(anchorname); x = AnchorPosition_getPageOffsetLeft(o); y = AnchorPosition_getPageOffsetTop(o); } else if (use_css) { x = AnchorPosition_getPageOffsetLeft(document.all[anchorname]); y = AnchorPosition_getPageOffsetTop(document.all[anchorname]); } else if (use_layers) { var found = 0; for (var i = 0; i < document.anchors.length; i++) { if (document.anchors[i].name == anchorname) { found = 1; break; } } if (found == 0) { coordinates.x = 0; coordinates.y = 0; return coordinates; } x = document.anchors[i].x; y = document.anchors[i].y; } else { coordinates.x = 0; coordinates.y = 0; return coordinates; } coordinates.x = x; coordinates.y = y; return coordinates; }
function getAnchorWindowPosition(anchorname) { var coordinates = getAnchorPosition(anchorname); var x = 0; var y = 0; if (document.getElementById) { if (isNaN(window.screenX)) { x = coordinates.x - document.body.scrollLeft + window.screenLeft; y = coordinates.y - document.body.scrollTop + window.screenTop; } else { x = coordinates.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset; y = coordinates.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset; } } else if (document.all) { x = coordinates.x - document.body.scrollLeft + window.screenLeft; y = coordinates.y - document.body.scrollTop + window.screenTop; } else if (document.layers) { x = coordinates.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset; y = coordinates.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset; } coordinates.x = x; coordinates.y = y; return coordinates; }
function AnchorPosition_getPageOffsetLeft(el) { var ol = el.offsetLeft; while ((el = el.offsetParent) != null) { ol += el.offsetLeft; } return ol; }
function AnchorPosition_getWindowOffsetLeft(el) { return AnchorPosition_getPageOffsetLeft(el) - document.body.scrollLeft; }
function AnchorPosition_getPageOffsetTop(el) { var ot = el.offsetTop; while ((el = el.offsetParent) != null) { ot += el.offsetTop; } return ot; }
function AnchorPosition_getWindowOffsetTop(el) { return AnchorPosition_getPageOffsetTop(el) - document.body.scrollTop; }

// test for valid file types based on the file extension
function FileTypeValidator(fileName, fileTypes) {
    if (!fileName) return;

    var arr = fileName.toLowerCase().split(".")
    fileType = arr[arr.length - 1];
    var valid = false;
    for (var i = 0; i < fileTypes.length; ++i) {
        if (fileTypes[i] == fileType) {
            valid = true;
        }
    }
    return valid;
}

var btnClickCount = 0;
function DisableClick(buttonClientID)
{
    var objName = buttonClientID;
    document.getElementById(objName).disabled = true;
    btnClickCount = btnClickCount + 1;
    msg = 'Processing (' + btnClickCount + ')';
    document.getElementById(objName).value= msg;
    var t = setTimeout("DisableClick('" + buttonClientID + "')",1000);
}

