/* IMAGE ROLLOVERS */
var OFF_SUFFIX = "_off";
var ON_SUFFIX = "_on";
var DEF_IMG_PATH = "images/";

function imageInit(imgPath) {
    if (!imgPath) imgPath = DEF_IMG_PATH;
    if (window.magicImages) getImgs(magicImages,imgPath);
}

/* IMAGE ROLLOVERS:
Define an array named "magicImages", containing each IMG name you want to rollover BEFORE including this file
Make sure all "off" graphic names end with the string specified in OFF_SUFFIX (e.g. "pic1_off.gif")
Make sure all "on" graphic names end with the string specified in ON_SUFFIX (e.g. "pic1_on.gif")
Edit IMG_PATH to reflect correct directory structure
Name each <IMG...> with the _root_ of its name (e.g. "pic1")
Pass "over()" & "out()" the name of the IMG you want to swap (e.g. onMouseover="over('pic1');" )
*/

//var IMG_PATH = "/resources/images/";
function getImgs(imgArray,imgPath) {
    if (!imgPath) imgPath = DEF_IMG_PATH;
    if (document.images) {
        for (var i=0; i<imgArray.length; i++) {
            var currName = imgArray[i];
            eval(currName + OFF_SUFFIX + " = new Image()");
            eval(currName + ON_SUFFIX + " = new Image()");
            eval(currName + OFF_SUFFIX + ".src = '" + imgPath + currName + OFF_SUFFIX + ".gif'");
            eval(currName + ON_SUFFIX + ".src = '" + imgPath + currName + ON_SUFFIX + ".gif'");
        }
    }
}
function over(targetName,refName) {
    if (!refName) refName = targetName;
    eval("document.images['" + targetName + "'].src = " + refName + ON_SUFFIX + ".src");
}
function out(targetName,refName) {
    if (!refName) refName = targetName;
    eval("document.images['" + targetName + "'].src = " + refName + OFF_SUFFIX + ".src");
}

/*  TAB ONLOAD LABELING FUNCTION */

function loadTabs() {
    for (var i=0; i<=tab.length; i++) {
        var  thisTabLabel = document.getElementById("tab" + i + "_textlink")
        if (thisTabLabel) {
            thisTabLabel.innerHTML = tab[i].label;
            thisTabLabel.href = tab[i].url;
            thisTabLabel.target = "content";
        }
    }
}

/*  TAB SWITCHING FUNCTION */

// The following Lines Indicate the first tab to be highlighted on page load
var lasttabnumber = 0;
var lastTabLeftImage = "tab0_leftimage";
var lastTabMiddleCell = "tab0_middlecell";
var lastTabTextLink = "tab0_textlink";
var lastTabRightImage = "tab0_rightimage";

function switchNav(tabnumber,activateUrl) {
    // Set Object ID references for newly selected tab
    var newTabLeftImage = "tab" + tabnumber + "_leftimage";
    var newTabMiddleCell = "tab" + tabnumber + "_middlecell";
    var newTabTextLink = "tab" + tabnumber + "_textlink";
    var newTabRightImage = "tab" + tabnumber + "_rightimage";
        
        // If present tab is not the previous tab, make it active, otherwise exit the function
    if (tabnumber != lasttabnumber) {
        document.getElementById(newTabLeftImage).src = "/resources/images/tb_left_on.gif"
        document.getElementById(newTabMiddleCell).className = "tabon"
        document.getElementById(newTabMiddleCell).background = "/resources/images/tb_bg_on.gif"
        document.getElementById(newTabTextLink).className = "tabontext"
        document.getElementById(newTabRightImage).src = "/resources/images/tb_right_on.gif"
    } else {
        return true;
    }
        
    // Turn previously activated tab off
    document.getElementById(lastTabLeftImage).src = "/resources/images/tb_left_off.gif";
    document.getElementById(lastTabMiddleCell).className = "taboff";
    document.getElementById(lastTabMiddleCell).background = "/resources/images/tb_bg_off.gif";
    document.getElementById(lastTabTextLink).className = "tabofftext";
    document.getElementById(lastTabRightImage).src = "/resources/images/tb_right_off.gif";
    
    // Remember which tab was activated, so it can be turned off the next time around   
    lasttabnumber = tabnumber;
    lastTabLeftImage = newTabLeftImage;
    lastTabMiddleCell = newTabMiddleCell;
    lastTabTextLink = newTabTextLink;
    lastTabRightImage = newTabRightImage;
    
    // LINK TO URL PASSED TO FUNCTION
    if (activateUrl) top.content.location.href = tab[tabnumber].url;    
    return true;
}

/*  BUTTON WIDTH SETTING FUNCTION FOR INTERNATIONALIZATION */

function setButtons() {
    var inputCollection = document.all.tags("input");
    if (inputCollection) {
        for (i=0;i<inputCollection.length;i++) {
            var inputType = inputCollection[i].type;
            if ((inputType!="button")&&(inputType!="submit")&&(inputType!="reset")) var doNothing = "Do Nothing";
            else setButtonWidth(inputCollection[i]);
        } 
        return true;
    } else {
        return false;
    }
}
function setButtonWidth(buttonObj) {
    //SET LOCAL AND GLOBAL MARGIN WIDTHS
    var globalMarginWidth = 10;
    var localMarginWidth = 9;
    //SET LOCAL AND GLOBAL MINIMUM WIDTHS
    var globalMinimumWidth = 65;
    var localMinimumWidth = 65;
    
    var buttonWidth;
    var labelLength = buttonObj.value.length;
    //Count Capital letters and narrow lowercase letters
    var upperCompare = buttonObj.value.toUpperCase();
    var upperCount = 0;
    //Define Skinny Characters (This should be added to)
    var skinnyChars = ["I","l","i","j","1"," ","f","r","T"];
    var skinnyCharCount = 0;
    //Set Margins, Minimum and Maximum Field Lengths
    if (buttonObj.value) {
        for (j=0; j<labelLength; j++) {
            if (buttonObj.value.charAt(j) == upperCompare.charAt(j)) {
                //Count Upper Case Characters
                if (buttonObj.value.charAt(j) != " ") upperCount++;
            } else {
                for (k=0;k<skinnyChars.length;k++) {
                    //Count Skinny Characters
                    if (buttonObj.value.charAt(j) == skinnyChars[k]) skinnyCharCount++;
                }
            }
        }
        //DEFINE BUTTON WIDTH
        if (buttonObj.className == "localbutton") {
            buttonWidth = (labelLength * 5.1)+(upperCount * 0.7)-(skinnyCharCount * 3) + (localMarginWidth * 2);
        } else {
            buttonWidth = (labelLength * 8)+(upperCount * 2)-(skinnyCharCount * 6) + (globalMarginWidth * 2);
            if (buttonWidth < globalMinimumWidth) buttonWidth = globalMinimumWidth;
        }
        buttonObj.style.width = buttonWidth;
        return true;
    } else {
        return false;
    }
}


/* STRING TRUNCATOR */
function truncate(str,num) {
    var trunStr = (str.length <= num+1) ? str : str.substring(0,num) + "...";
    return trunStr;
}

/*  BUTTON ENABLING/DISABLING */

function disable(btn) {
    btn.disabled = true;
}
function enable(btn) {
    btn.disabled = false;
}

/*  FUNCTION TO OPEN HELP */
function openHelp() {
    window.open('/websitemanager/resources/help/WSMHelp.jsp','','width=535,height=525,scrollbars=yes,resizable=yes,toolbars=no');
}