function setUpTabClickEvents() {

    //leave the function if the tab-buttons div is not available
    if (!document.getElementById) return false;
    if (!document.getElementById("tab-buttons")) return false;

    var listItems = document.getElementById('tab-buttons');
    for (var i = 0; i < listItems.childNodes.length; i++) {
        listItems.childNodes[i].onclick = function() {
            LloydsListTabber(this);
        }
    }
    return true;
}

var tmp;
var first = "item1";

function LloydsListTabber(clicked) {

    if (clicked.className == 'collapsed') {
        if (typeof tmp != 'undefined') {
            document.getElementById(tmp.id).className = 'collapsed';
            document.getElementById(tmp.id + "_tab-content").className = 'collapsed';
        } else {
            document.getElementById(first).className = 'collapsed';
            document.getElementById(first + "_tab-content").className = 'collapsed';
        }

        status = (clicked.className == 'collapsed') ? 'expanded' : 'collapsed';
        clicked.className = status;
        document.getElementById(clicked.id + "_tab-content").className = status;

        tmp = clicked;
    }

}

/* email bulletin */
function setUpSubscribeFormFieldEvents() {
    if (!document.getElementById) return false;
    if (!document.getElementById("subscribe-newsbulletin")) return false;
    // create a variable to hold the default field text
    var fieldText = "your email address";
    // get ref to the form field
    var emailBulletin = document.getElementById("email-bulletin-field");
    //get ref to the form itself to attach a submit event
    var eBulletinForm = document.getElementById("ebulletin-form");
    eBulletinForm.onsubmit = function() {
        if ((emailBulletin.value == fieldText) || (emailBulletin.value == '')) {
            var signupError = document.getElementById("signup-error");
            signupError.innerHTML = "Please enter a valid email address";
            //document.getElementById("subscribe-newsbulletin").style.height = "85px";
            return false;
        } else if (!echeck(emailBulletin.value)) {
            emailBulletin.value = "";
            emailBulletin.focus();
            return false;
        } else {
            return true;
        }
    }

    //set value property to default text value
    emailBulletin.value = fieldText;

    emailBulletin.onclick = function() {
        clearInput(this, fieldText);
    }
    emailBulletin.onfocus = function() {
        clearInput(this, fieldText);
    }
    emailBulletin.onblur = function() {
        clickRecall(this, fieldText);
    }
    return true;
}

/* used to clear and populate a form field */
function clearInput(thisfield, defaultText) {
    if (thisfield.value == defaultText) {
        thisfield.value = "";
    }
}

/* set to default text if focus is lost and nothing has been entered */
function clickRecall(thisfield, defaultText) {
    if (thisfield.value == "") {
        thisfield.value = defaultText;
    }
}

//email checker
function echeck(str) {
    var at = "@";
    var dot = ".";
    var lat = str.indexOf(at);
    var lstr = str.length;
    if (str.indexOf(at) == -1) {
        alertText();
        return false;
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        alertText();
        return false;
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        alertText();
        return false;
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        alertText();
        return false;
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        alertText();
        return false;
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        alertText();
        return false;
    }

    if (str.indexOf(" ") != -1) {
        alertText();
        return false;
    }

    return true;
}

function alertText() {
    var signupError = document.getElementById("signup-error");
    signupError.innerHTML = "Email address is invalid";
}

/* advanced search help */

function setUpSearchHelpButtons() {
    if (!document.getElementById) return false;
    if (!document.getElementById("advanced-search")) return false;
    var helpLinks = document.getElementsByClassName("help-link");
    var len = helpLinks.length;
    var i;

    function hideAllAnswers(numOfElements) {
        for (var j = 0; j < numOfElements; j++) {
            var elem = 'help' + (j + 1);
            document.getElementById(elem).style.display = "none";
        }
    }

    //attach the help functionality to each link
    for (i = 0; i < len; i++) {
        helpLinks[i].onclick = function() {
            hideAllAnswers(len);
            document.getElementById('help' + this.id).style.display = "block";
            return false;
        }
    }
    return true;
}

/* hides and shows the date panel - advanced search */
function hideShow() {

    if (!document.getElementById || !document.getElementsByTagName) return false;

    // is hideShow on the page?
    if (null != document.getElementById("hideShow")) {

        // get the parent tag called 'hideShow'
        var theParentTag = document.getElementById("hideShow");

        // get all the radio buttons inside the parent element
        var radioButtons = theParentTag.getElementsByTagName("input");

        //get the datePanel
        var datePanel = document.getElementById("datepanel");

        // open the date picker if the date range radio is selected
        // used for when the user goes back using the back button


        for (var i = 0; i < radioButtons.length; i++) {

            // open the date picker if the date range radio is selected
            // used for when the user goes back using the back button

            if (radioButtons[i].getAttribute("value") == "dateRange") {
                if (radioButtons[i].checked) {
                    datePanel.className = "date_panel";
                    //turn off the timeFrame drop down if the dateRange is selected
                    document.getElementById("timeFrameDropDown").disabled = true;
                }
            }

            radioButtons[i].onclick = function() {

                // get the attribute 'value' so we can check if it is the 'all' radio or not
                var theValue = this.getAttribute("value");

                // if it is then we display the 'datePanel'
                if (theValue == 'false') {
                    datePanel.className = "date_panel";
                    //turn off the timeFrame drop down if the dateRange is selected
                    document.getElementById("timeFrameDropDown").disabled = true;
                    // if not then we hide it
                } else {
                    datePanel.className = "date_panel_hidden";
                    //turn on the timeFrame drop down if the dateRange is selected
                    document.getElementById("timeFrameDropDown").disabled = false;
                }


            }
        }

    } else {
        // hideShow not on the page so nothing more will happen
        return false;
    }
return true;
}

function setUpClickEventForResultsRadio() {
    if (!document.getElementById) return false;
    if (!document.getElementById("advanced-search")) return false;

        // initial state of all the form elements when the page loads in...
        document.getElementById("containingLogicText").disabled = true;
        document.getElementById("allWords").disabled = false;
        document.getElementById("exactPhrase").disabled = false;
        document.getElementById("containingAnyWords").disabled = false;
        document.getElementById("withoutWords").disabled = false;

    var containingLogic = document.getElementById("containingLogic");
    containingLogic.onclick = function() {
        document.getElementById("containingLogicText").disabled = false;
        // turn off all the other fields
        document.getElementById("allWords").disabled = true;
        document.getElementById("exactPhrase").disabled = true;
        document.getElementById("containingAnyWords").disabled = true;
        document.getElementById("withoutWords").disabled = true;
    }
    var findResult = document.getElementById("findResult");
    findResult.onclick = function() {
        document.getElementById("containingLogicText").disabled = true;
        // turn off all the other fields
        document.getElementById("allWords").disabled = false;
        document.getElementById("exactPhrase").disabled = false;
        document.getElementById("containingAnyWords").disabled = false;
        document.getElementById("withoutWords").disabled = false;
    }
    return true;
}

function addMoreInfoClickEvent() {
/* adds the more info reveal click event to the subscribe (products.htm - productList.jsp) page */
if (!document.getElementById) return false;
if (!document.getElementById("subscribe")) return false;

var moreInfo = document.getElementsByClassName("more-info");
    // add click event to link
    for (var j=0;j<moreInfo.length;j++) {
    moreInfo[j].onclick = function() {
        /*//hide all toggle panels
        var infoPanels = document.getElementsByClassName("info-panel");
        for(var i=0; i<infoPanels.length;i++) {
            //infoPanels[i].style.display = "none";
            infoPanels[i].className = "info-panel";
        }*/
        // once all panels closed - open the one requested
        var thePanelId = this.getAttribute("id");
        var panel = "info" + thePanelId;
        if (document.getElementById(panel).className == "info-panel") {
        document.getElementById(panel).className = "info-panel_on";
        } else {
        document.getElementById(panel).className = "info-panel";    
        }
    }

    }
    return true;
}

function addSubscribeNowClickEvent() {
/* adds the subscribe button click event on the subscribe (products.htm - productList.jsp) page */
if (!document.getElementById) return false;
if (!document.getElementById("subscribe")) return false;

var subscribeButton = document.getElementById("subscribe-button");
var theUrl = subscribeButton.getAttribute("href");
// attach click event to link    
subscribeButton.onclick = function() {
openSubscribePanel(theUrl);
return false;
}
return true;
}


function addArticleToolbarClickEvents() {
/* adds the subscribe button click event on the subscribe (products.htm - productList.jsp) page */
if (!document.getElementById) return false;
if (!document.getElementById("article-toolbar-top")) return false;
var colleagueButton = document.getElementById("send-to-colleague");
var colleagueUrl = colleagueButton.getAttribute("href");
// attach click event to links
colleagueButton.onclick = function() {
openArticleToolbarPanel(colleagueUrl);
return false;
}

var printButton = document.getElementById("printer-friendly");
var printUrl = printButton.getAttribute("href");
// attach click event to links
printButton.onclick = function() {
openArticleToolbarPanel(printUrl);
return false;
}
return true;
}


function openSubscribePanel(url) {
appWindow = window.open(url, "mcs", "'toolbar=no,location=no,status=no,menubar=no,directories=no,scrollbars=yes,resizable=yes,width=700,height=640')");
appWindow.focus();
}

/* todo refactor so all popups are shared */
function openArticleToolbarPanel(url) {
appWindow = window.open(url, "", "'toolbar=no,location=no,status=no,menubar=no,directories=no,scrollbars=yes,resizable=yes,width=710,height=520')");
appWindow.focus();
}

function pollResultsBars() {
    if (!document.getElementById) return false;
    if (!document.getElementsByClassName) return false;
    if (!document.getElementById("quickVote")) return false;
    var answers = document.getElementsByClassName("panswer");
    var numOfAnswers = answers.length;
    for (var i=1;i<numOfAnswers+1;i++) {
        var bar = document.getElementById("answer_"+i);
        //var percentage = bar.getAttribute("src"); // alternate use of src attribute
        var hiddenval = document.getElementById("hidden_"+i);
        var percentage = hiddenval.innerHTML; //proprietry but well supported 'innerHTML'
        //alert(percentage);
        var bg;
        if (i%2==0) {
            bg = '#065e33';
        } else {
            bg = '#666';
        }
        //alert("background:" + bg + " url(../images/pollbar.png) no-repeat -" + percentage + "% top;");
        bar.style.cssText = "background:" + bg + " url(../images/pollbar.png) no-repeat -" + percentage + "% top;"
    }
    return true;
}

function seePollResults() {
    if (!document.getElementById) return false;
    if (!document.getElementsByClassName) return false;
    if (!document.getElementById("see-results")) return false;

    var seeResults = document.getElementById("see-results");
    seeResults.onclick = function() {
        if (this.getAttribute("href").indexOf("jv") > -1 || this.getAttribute("href").indexOf("sv") > -1 || this.getAttribute("href").indexOf("av") > -1) {
            var qvGraph = document.getElementById("qv-graph");
             qvGraph.style.display = 'block';
        }
        return false;
    }
    return true;
}











