//use to add any custom javascripts to this site

// function: add images into array
var imagesArray = [];
function addImagesArray() {
    var j = 0;
    for (i = 0; i < document.images.length; i++) {
        //if image is a button or nav button, add into array
        if ((document.images[i].src.indexOf('btn') > 0) || (document.images[i].src.indexOf('nav') > 0)) {
            imagesArray[j] = document.images[i].src;
            //alert(imagesArray[j]);
        }
    }
}

// function: preload images
function preloadImages() {
    //first add images to array
    addImagesArray();

    var preImages = new Array();
    for (i = 0; i < imagesArray.length; i++) {
        preImages[i] = new Image();
        preImages[i].src = imagesArray[i];
    }
}

/////////////////////////////////////////////////////////////
// the following three functions are used as default functions
// for setting over and out mouseover states for top nav
/////////////////////////////////////////////////////////////
function menuOver(img) {
    //get file type
    strFileType = getFileType(img.src)
    if (img.src.indexOf("On." + strFileType) == -1) {
        img.src = img.src.replace("." + strFileType, "Over." + strFileType)
    }
}
function menuOut(img) {
    //get file type
    strFileType = getFileType(img.src)
    if (img.src.indexOf("Over." + strFileType) != -1) {
        img.src = img.src.replace("Over." + strFileType, "." + strFileType)
    }
}

function getFileType(strFileName){
    strFileType = ""
    arrParts = strFileName.split(".")
    strFileType = arrParts[arrParts.length-1]
    return strFileType
   
}

/////////////////////////////////////////////////////////////
// END
/////////////////////////////////////////////////////////////

// function: to clear login field
function clearSearchField(e) {
    if ("Search" == e.value) {
        e.value = '';
    }
    else if (e.value == '') {
        e.value = "Search";
    }
}
function clearSearchField1(e) {
    if ("Enter another search term" == e.value) {
        e.value = '';
    }
    else if (e.value == '') {
        e.value = "Enter another search term";
    }
}

// function: force enter click
function forceClick(e, elemId) {
    var elem = document.getElementById(elemId);
    var evt = (e) ? e : window.event;
    var intKey = (evt.which) ? evt.which : evt.keyCode;

    if (intKey == 13) {
        elem.click();
        return false;
    }
    return true;
}

//function: is alphanumeric
function characterCheck(str) {
    var regex = /[a-zA-z0-9]/;
    if (str.match(regex)) {
        return true;
    } else {
        return false;
    }
}

// function: do search
function doSearch(e, elemId, searchId) {
    var elem = document.getElementById(elemId);
    var evt = (e) ? e : window.event;
    var intKey = (evt.which) ? evt.which : evt.keyCode;
    var search = $.trim(document.getElementById(searchId).value);

    //alert(search);
    if ((search != "") && (characterCheck(search))) {
        document.location.href = '/search.aspx?q=' + document.getElementById(searchId).value + '&sa=Search';
    }
    else {
        alert("Please submit a valid search");
    }
    return false;
}

// function: add rollover for SmartWebs buttons (handles GIFs and PNGs)
function addSWBtnRollovers() {
    //alert("images # " + document.images.length);
    for (i = 0; i < document.images.length; i++) {
        //if button is a smartwebs button

        if (document.images[i].src.indexOf('swbtn_') > 0) {
            //if button is a png
            //alert("src: " + document.images[i].src);
            imgSrc = document.images[i].src
            //alert(imgSrc)
            arrParts = imgSrc.split(".")
            ext = arrParts[arrParts.length - 1]
            //alert(ext)
            if ((ext == "gif") && (imgSrc.indexOf("Over") < 0)) {
                document.images[i].onmouseover = function() { swapSWButtons(this, 'gif'); };
                document.images[i].onmouseout = function() { swapSWButtons(this, 'gif'); };
            }
            if ((ext == "png") && (imgSrc.indexOf("Over") < 0)) {
                document.images[i].onmouseover = function() { swapSWButtons(this, 'png'); };
                document.images[i].onmouseout = function() { swapSWButtons(this, 'png'); };
            }
        }
    }
}

// function: swap the rollover buttons
function swapSWButtons(img, ext) {
    thisSrc = img.src
    if (thisSrc.indexOf("Over") > 0) {
        thisSrc = thisSrc.replace("Over." + ext, "." + ext)
        img.src = thisSrc
    } else {
        thisSrc = thisSrc.replace("." + ext, "Over." + ext)
        img.src = thisSrc
    }
}

// function: add png class for PNG images
function addPNGfix() {
    //alert("images # " + document.images.length);
    for (i = 0; i < document.images.length; i++) {
        //get image extension
        imgSrc = document.images[i].src
        //alert(imgSrc)
        arrParts = imgSrc.split(".")
        ext = arrParts[arrParts.length - 1].toLowerCase()

        //if image is a PNG
        if (ext == "png") {
            //alert(ext);
            //if button is a png
            $(document.images[i]).addClass("png");
        }
    }
}

function init() {
    preloadImages(); //preload buttons and navigation
    addSWBtnRollovers(); //add swbutton rollovers
    //setHeight(); //set height of leftnav
    //setBackgroundBanner(); //set background banner (inner pages)
    addPNGfix(); //add PNG fix
}
window.onload = init;
