﻿//function CheckCustomReportSelections:  Ensures PDF Report form is not submitted with 0 checkboxes checked.
function CheckCustomReportSelections() {
    var theForm = document.forms['aspnetForm'];
    var checkSelected = false;
    for (i = 0; i < theForm.elements.length; i++) {
        if (theForm.elements[i].checked)
            checkSelected = true;
    }
    if (!checkSelected) {
        alert("Please select at least one checkbox.");
        return (false);
    }
}

// ----- START Toggle Show Hide function -----
function showhide(section, mode) {
    var divToHide = section + "_list";
    var imgToSwap = section + "_arrow";
    if (mode == 'show') {
        document.getElementById(imgToSwap).src = '/_layouts/IMAGES/ThirtyFive/CR2K8/arrow-res-open.gif';
        document.getElementById(divToHide).style.display = 'block';
    }
    else if (mode == 'hide') {
        document.getElementById(imgToSwap).src = '/_layouts/IMAGES/ThirtyFive/CR2K8/arrow-res-closed.gif';
        document.getElementById(divToHide).style.display = 'none';
    }
    else if (mode == 'toggle') {
        if (document.getElementById(divToHide).style.display == 'none') {
            document.getElementById(imgToSwap).src = '/_layouts/IMAGES/ThirtyFive/CR2K8/arrow-res-open.gif';
            document.getElementById(divToHide).style.display = 'block';
        }
        else {
            document.getElementById(imgToSwap).src = '/_layouts/IMAGES/ThirtyFive/CR2K8/arrow-res-closed.gif';
            document.getElementById(divToHide).style.display = 'none';
        }
    }
}
// ----- END Toggle Show Hide function -----

//function allController: Checks all items in the form (checked) or returns it to the previous selection (unchecked)
function allController(el) {
    var theForm = document.forms['aspnetForm'];
    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['aspnetForm'];
    for (i = 0; i < theForm.elements.length; i++) {
        if (theForm.elements[i].type == 'checkbox')
            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 parseCookie: Checks checkbox elements based on in page selections
function parseCookie() {
    var theForm = document.forms['aspnetForm'];
    var pBasket = getCookie('pBasket');
    //alert(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++) {                 //outer loop:   Cookie Array
        for (var j = 0; j < theForm.elements.length; j++) {             //inner loop:   Form Array
            if (theForm.elements[j].value == pBasketArray[i]) {
                theForm.elements[j].checked = true;
                alert(theForm.elements[j].name);
                showhide(theForm.elements[j].name, 'show');
            }
        }
    }
}

//function toggeSections: Sets up the form to expand or collapse the required sections
//By default they are all expanded so a call to showHide in here will collapse it on load
function toggleSections() {
    showhide('section1', 'show');
    showhide('section2', 'hide');
    showhide('section3', 'hide');
    showhide('section4', 'hide');
    showhide('section5', 'hide');
    showhide('section6', 'hide');
    showhide('section7', 'hide');
    showhide('section8', 'hide');
    showhide('section9', 'hide');
}