﻿//function CheckCustomReportSelections:  Ensures PDF Report form is not submitted with 0 checkboxes checked.
function CheckCustomReportSelections(theForm) {
    var checkSelected = false;
    for (i = 0; i < theForm.elements.length; i++) {
        if (theForm.elements[i].checked) {
            checkSelected = true;
			//uncomment to check contents of pdf basket
			//alert(theForm.elements[i].value);
        }
    }
    if (!checkSelected) {
        alert("Please select at least one checkbox.");
        return (false);
    }
}

//function showHide:  Toggles the visiblitiy of the checkbox section lists when the titles are clicked
function showHide(section) {
    var objTitle = document.getElementById(section + 'Title');
    var objList = document.getElementById(section + 'List');
    
    if (objTitle.className == 'report-sect-opened') {
        objTitle.className = 'report-sect-closed';
        objList.style.display = 'none';
    }
    else {
        objTitle.className = 'report-sect-opened';
        objList.style.display = '';
    }
}

//function buildIndividualPagesListFromCookie: Appends checkbox elements to the form based on cookie selections
function buildIndividualPagesListFromCookie() {
    //working vars
    var hasItems = false;

    var theForm = document.forms['reportbuilder'];
    var pBasket = getCookie('pBasket');
    var pBasketArray = pBasket.split('*');

    //loop through form to check boxes corresponding to URLs in pBasketArray
    for (var i = 0; i < pBasketArray.length - 1; i++) {                 //loop:   Cookie Array
        createCheckbox(pBasketArray[i]);
        var hasItems = true
    }
    //Enable individualpages div if pBasket hasItems:
    if (hasItems) {
        document.getElementById('individualpages-wrapper').style.display = '';
        document.getElementById('dynamictitle-wrapper').style.display = '';
    }
}

//function createCheckbox: add a checkbox element to the form in the individualpages div
//for the pBasket element
function createCheckbox(pBasketElement) {
    //checkbox
    var chk = document.createElement("INPUT");
    chk.setAttribute("type", "checkbox");
    chk.setAttribute("name", "pBasket");
    chk.setAttribute("class", "reportbuilder-checkbox");
    chk.setAttribute('checked', 'checked');
    chk.setAttribute('defaultChecked', 'defaultChecked'); //IE workaround to auto check box
    chk.setAttribute("value", pBasketElement);
    document.getElementById('individualpages').appendChild(chk);

    //label
    var pBasketElementArray = pBasketElement.split('|');
    var lbl = document.createElement("LABEL");
    lbl.setAttribute("for", pBasketElement);
    lbl.setAttribute("class", "reportbuilder-label");
    lbl.innerHTML = formatPageTitle(pBasketElementArray[1]);
    document.getElementById('individualpages').appendChild(lbl);

    //others (e.g. closing div)
    var div = document.createElement("DIV");
    div.setAttribute("class", "clear");
    document.getElementById('individualpages').appendChild(div);
}

//function allController: Checks all items in the form (checked) or returns it to the previous selection (unchecked)
function allController(el) {
    var theForm = document.forms['reportbuilder'];
    checkAll(el.checked);
    document.getElementById('isFullReport').value = el.checked
}

//function checkAll: Checks or unchecks all items in the form depending on boolswitch value
function checkAll(boolswitch) {
    var theForm = document.forms['reportbuilder'];
    for (i = 0; i < theForm.elements.length; i++) {
        if (theForm.elements[i].type == 'checkbox' && theForm.elements[i].name != 'pBasket')
            theForm.elements[i].checked = boolswitch;
    }
}

//function sectionController: Checks or unchecks all items in the section
function sectionController(el) {
    checkSection(el.checked, el.name);
}

//function checkSection: Checks or unchecks all items in the section depending on boolswitch value
function checkSection(boolswitch, section) {
    var theSection = document.getElementsByName(section)
    for (i = 0; i < theSection.length; i++) {
        if (theSection[i].type == 'checkbox')
            theSection[i].checked = boolswitch;
    }
}


//function initSections: loads the contents of the javascript arrays into the corresponding checkboxes.
//each js file in /js/cr-files/ needs a call reference in here to fire their respective init functions.
function initSections() {
    initOverview();
    initCR();
    initGlobalHealth();
    initAccessMeds();
    initResearchPractices();
    initEthicalConduct();
    initSupplyChain();
    initSustainability();
    initOurPeople();
    initHumanRights();
    initPolicy();
	initCommunities();
    //if a new js file is created using TEMPLATE.js, replace TEMPLATE here with the Section name.
}
