﻿var _sf_startpt = (new Date()).getTime()

Array.prototype.inArray = function(value) {
    var i;
    for (i = 0; i < this.length; i++) {
        if (this[i] === value) {
            return true;
        }
    }
    return false;
};

function addEvent(obj, type, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(type, fn, false);
        EventCache.add(obj, type, fn);
    }
    else if (obj.attachEvent) {
        obj["e" + type + fn] = fn;
        obj[type + fn] = function() { obj["e" + type + fn](window.event); }
        obj.attachEvent("on" + type, obj[type + fn]);
        EventCache.add(obj, type, fn);
    }
    else {
        alert("in else");
        obj["on" + type] = obj["e" + type + fn];
    }
}

var EventCache = function() {
    var listEvents = [];
    return {
        listEvents: listEvents,
        add: function(node, sEventName, fHandler) {
            listEvents.push(arguments);
        },
        flush: function() {
            var i, item;
            for (i = listEvents.length - 1; i >= 0; i = i - 1) {
                item = listEvents[i];
                if (item[0].removeEventListener) {
                    item[0].removeEventListener(item[1], item[2], item[3]);
                };
                if (item[1].substring(0, 2) != "on") {
                    item[1] = "on" + item[1];
                };
                if (item[0].detachEvent) {
                    item[0].detachEvent(item[1], item[2]);
                };
                item[0][item[1]] = null;
            };
        }
    };
} ();
addEvent(window, 'unload', EventCache.flush);

function bbExpCollapse(elename) {
    if (elename == "divBBPT") {
        var divEle1 = document.getElementById("divBBPT");
        if (divEle1.style.display == "block")
            divEle1.style.display = "none";
        else if (divEle1.style.display == "none")
            divEle1.style.display = "block";
    }
    if (elename == "divBBST") {
        var divEle1 = document.getElementById("divBBST");
        if (divEle1.style.display == "block")
            divEle1.style.display = "none";
        else if (divEle1.style.display == "none")
            divEle1.style.display = "block";
    }
}

function leftTrim(sString) {
    while (sString.substring(0, 1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    return sString;
}

function rightTrim(sString) {
    while (sString.substring(sString.length - 1, sString.length) == ' ') {
        sString = sString.substring(0, sString.length - 1);
    }
    return sString;
}


function trimAll(sString) {
    while (sString.substring(0, 1) == ' ' || sString.substring(0, 1) == '\n' || sString.substring(0, 1) == '\r' || sString.substring(0, 1) == '\t') {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length - 1, sString.length) == ' ' || sString.substring(sString.length - 1, sString.length) == '\n' || sString.substring(sString.length - 1, sString.length) == '\r' || sString.substring(sString.length - 1, sString.length) == '\t') {
        sString = sString.substring(0, sString.length - 1);
    }
    return sString;
}

function boldLeftLink() {
    var eleh3 = document.getElementById("titleh3");
    var h3txt;
    //alert("in ");

    //checks to see if InnerText supported by users browser by looking at body
    var hasInnerText = (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;

    //alert("hasInnerText:"+hasInnerText);

    //trims main title of page
    if (!hasInnerText)							 //supports Gecko Browsers 
        h3txt = trimAll(eleh3.textContent);
    else										 //supports IE
        h3txt = trimAll(eleh3.innerText);


   //finds corresponding left nav link to title 
    var elediv = document.getElementById("leftdiv");
    if (elediv !== null) {
        var child = elediv.getElementsByTagName("a");
        var i;
        var curlen = child.length;
        for (i = 0; i < curlen; i++) {
            var curtext;
 
            if (!hasInnerText) {
                curtext = trimAll(child[i].textContent);
            }
            else {
                curtext = trimAll(child[i].innerText);
            }
 
            //  alert("curtext="+curtext+"|");
            // alert("htxt="+h3txt+"|");
            if (curtext == h3txt) {
                //alert("IN IF");
                //child[i].innerHTML= "<b>" + child[i].innerHTML + "</b>";
 
                //if not topmost element in DOM tree
                if (child[i].parentNode != null) {
 
                    var liobj = child[i].parentNode;       //get li tag
                    //  alert("parent tag="+liobj.tagName);
                    liobj.id = "selectedli";                //set CSS style for object
 
                    break;
                }
            }
        }
    }
}

//addEvent(window,'onload',boldLeftLink);

function setRadioButtonChecked(strName, strType) {


    if (document.frmSearch.filtertype[1].checked == true) {

        if (strType == 1) {
            document.frmSearch.datefilterinterval.value = '';
        }
        else if (document.frmSearch.filtertype3.checked == false) {
            document.frmSearch.txtKey.value = '';
        }
    }
    else {
        document.frmSearch.filtertype[1].click();
        return false;
    }
}

function checkform() {
    if (document.frmSearch.filtertype[1].checked == true) {
        if (document.frmSearch.txtKey.value == '' && document.frmSearch.datefilterinterval.value == '') {
            alert('You must select one or more search options.');
            return false;
        }
        else if (document.frmSearch.filtertype3.checked == true && document.frmSearch.txtKey.value == '') {
            alert('You must select one or more search options.');
            return false;
        }
    }
    return true;
}

function linkPreview() {
    var links = document.getElementsByTagName("a");

    for (i = 0; i < links.length; i++) {
        var currentLink = links[i];
        var images = currentLink.getElementsByTagName("img");

        // Check if the link is an image. We don't want icons next to images.
        if (images.length == 0) {
            var linkHref = currentLink.href;

            // Find all links directed to amazon.com ...used to check links to certain sites
            //if (linkHref.match(/amazon.com/)){
            //   append(currentLink, "amazon");
            // }

            checkLinks(linkHref, currentLink)

        }
    }
}


function checkLinks(linkHref, currentLink) {
    var linkHrefParts = linkHref.split(".");

    // extension is the last element in the LinkSplit array
    var extension = linkHrefParts[linkHrefParts.length - 1];

    // In some browsers there is a "/" placed after the link. removes the "/"
    extension = extension.replace("/", "");

    if (extension in { doc: 1, mov: 1, pdf: 1, ppt: 1, txt: 1, xls: 1, zip: 1, wmv: 1, mde: 1, mp3: 1, mp4: 1 }) {
        append(currentLink, extension);

    }
}

//If the function finds a match, we call the ‘append()’ function add the proper ‘span’ elements after the link.
function append(currentLink, extension) {
    var span = document.createElement('span');
    span.innerHTML = "&nbsp;";
    span.title = extension + " icon";
    //Note there is an ‘&nbsp’ inserted into the span because there are some CSS issues with an empty class in Internet Explorer.
    currentLink.parentNode.insertBefore(span, currentLink.nextSibling);
    span.className = extension;
}


addEvent(window, 'load', linkPreview);
