﻿var btnCreatePDFControlId = "";
var totalFormSubmit = 0;

_spBodyOnLoadFunctionNames.push("isFullReportRequested");

// ----- START Toggle Show Hide function -----
function showhide(section, mode) {
    var divToHide = section + "_pages";
    var imgToSwap = section + "_arrow";
    if (mode == 'show') {
        document.getElementById(imgToSwap).src = '/stylesheets/Images/pdf-open.gif';
        document.getElementById(divToHide).style.display = 'block';
    }
    else if (mode == 'hide') {
        document.getElementById(imgToSwap).src = '/stylesheets/Images/pdf-closed.gif';
        document.getElementById(divToHide).style.display = 'none';
    }
    else if (mode == 'toggle') {
        if (document.getElementById(divToHide).style.display == 'none') {
            document.getElementById(divToHide).style.display = 'block';
            document.getElementById(imgToSwap).src = '/stylesheets/Images/pdf-open.gif';
        }
        else {
            document.getElementById(divToHide).style.display = 'none';
            document.getElementById(imgToSwap).src = '/stylesheets/Images/pdf-closed.gif';
        }
    }
    
}
// ----- END Toggle Show Hide function -----


//function checkSection: Checks or unchecks all items in the section depending on boolswitch value
//THIS METHOD ALLOWS FOR CHECKING OF BOXES THAT DO NOT NECCESSARILY SHARE THE SAME NAME, E.G. IF THEY ARE SERVER CONTROLS.
//function checkSection(boolswitch, section) {
//    
//    var theSection = document.getElementById(section + '_pages');   
//    for (i = 0; i < theSection.childNodes.length; i++) {
//        if (theSection.childNodes[i].nodeType == 1) {                           //nodeType == 1 = element node: reaches the <li> child element
//            if (theSection.childNodes[i].childNodes[0].type == 'checkbox') {    //childNodes[0] of the <li> element should be a checkbox
//                theSection.childNodes[i].childNodes[0].checked = boolswitch;    //if it is, toggle these checkboxes.
//            }
//        }
//    }
//}

//function parseCookie: Checks checkbox elements based on in page selections
function parseCookie() {
    var theForm = document.forms[0];
    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
        var sPagePath = pBasketArray[i].toLowerCase().split('|')[0];
        for (var j = 0; j < theForm.elements.length; j++) {             //inner loop:   Form Array            
            if (theForm.elements[j].value && (theForm.elements[j].value.toLowerCase().indexOf(sPagePath) > -1)) {
                theForm.elements[j].checked = true;
                showhide(theForm.elements[j].name, 'show');
            }
        }
    }
}

function isFullReportRequested() {
    var qs = new Querystring();
    if (qs.contains("fullReport")) {
        fullReport();
    }    
}
function fullReport() {
    checkSection(true, 'section1');
    checkSection(true, 'section2');
    checkSection(true, 'section3');
    checkSection(true, 'section4');
    checkSection(true, 'section5');
    checkSection(true, 'section6');
    checkSection(true, 'section7');
    checkSection(true, 'section8');
    checkSection(true, 'section9');
    checkSection(false, 'section10');        
    //document.getElementById('hdnCoverPage').value = '';
//document.getElementById('hdnCoverPage').value = 'capita_coversheet.pdf';
document.getElementById('hdnCoverPage').value = 'SR2010_cs.pdf';
    document.getElementById(btnCreatePDFControlId).click();
}

//function allController: Checks all items in the form (checked) or returns it to the previous selection (unchecked)
function allController(el) {
    if (el.checked) {
        //check all checkboxes
        checkAll(true);
    }
    else {
        //uncheck all checkboxes
        checkAll(false);
        //check items based on cookie
        //loadSelectionsFromCookie();
    }
}

//function checkAll: Checks or unchecks all items in the form depending on boolswitch value
function checkAll(boolswitch) {
    var theForm = document.forms[0];
    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(boolswitch, section) {
    var theSection = document.getElementsByName(section);
    for (i = 0; i < theSection.length; i++) {
        if (theSection[i].type == 'checkbox')
            theSection[i].checked = boolswitch;
    }
}

//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', 'hide');
    showhide('section2', 'hide');
    showhide('section3', 'hide');
    showhide('section4', 'hide');
    showhide('section5', 'hide');
    showhide('section6', 'hide');
    showhide('section7', 'hide');
    showhide('section8', 'hide');
    showhide('section9', 'hide');
    showhide('section10', 'hide');
}



// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
//
//Barry Fogarty 12 Feb 2009
//
//Cookie management functions - could be modularised into its own JS or common?
//
//-------------------------------------------------------------------------------------------------------------------------------------


function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function setCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function eraseCookie(name) {
    setCookie(name, "", -1);
}

function downloadCRProgress(){
    var CrProgressPageName = 'cr-progress-2010';
    var arrCheckBoxes = document.getElementsByTagName('input');
    for (var i = 0; i < arrCheckBoxes.length - 1; i++) {
        if (arrCheckBoxes[i].value.toLowerCase().indexOf(CrProgressPageName.toLowerCase()) > 0) {
            unCheckAllSections();
            arrCheckBoxes[i].checked = true;
            //document.getElementById('hdnCoverPage').value = 'ProgressCover.pdf';
            document.getElementById('hdnCoverPage').value = 'SR2010-ProgressPerformance.pdf';
            
            
            
            document.getElementById(btnCreatePDFControlId).click();
            break;
        }
    }
}
function downloadCaseStudies() {
    unCheckAllSections();
    checkSection(true, 'section7');
    document.getElementById('hdnCoverPage').value = 'SR2010-CaseStudies.pdf';
    //document.getElementById('hdnCoverPage').value = 'CaseStudiesCover.pdf';
    document.getElementById(btnCreatePDFControlId).click();
}
function downloadPolicies() {
    unCheckAllSections();
    checkSection(true, 'section8');
    document.getElementById('hdnCoverPage').value = 'SR2010-Policies.pdf';
    //document.getElementById('hdnCoverPage').value = 'PoliciesCover.pdf';
    document.getElementById(btnCreatePDFControlId).click();
}

function showDialog(){
    var oDiv = document.getElementById('reportGenerationDialog');            
    oDiv.style.display = "block";
    repositionDialog();
    totalFormSubmit++;
    if(totalFormSubmit > 1){
        //As the response is attachment so the page is not refreshed and sharepoint adds flag inside onsubmit button to avoid double submission. So 
        //to submit your page again need to override onsubmit method and always return true, and call post back method explicitly
        //WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(btnCreatePDFControlId, "", true, "", "", false, false));
        document.forms[0].onsubmit = function() {return true;}
    }
    setTimeout("hideDialog()",10000);
}

function hideDialog(){
    document.getElementById('hdnCoverPage').value = '';
    var oDiv = document.getElementById('reportGenerationDialog');
    oDiv.style.display = "none";    
}

function repositionDialog() {  
    var oDiv = document.getElementById('reportGenerationDialog');
    var posTop = getClientHeight()/2 + getScrollTop() - 100;
    var posLeft = getClientWidth()/2 + getScrollLeft() - 100;
    oDiv.style.top = posTop + "px";
    oDiv.style.left = posLeft + "px";
}

function getClientWidth() {
	return filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}

function getClientHeight() {
	return filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function getScrollLeft() {
	return filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function getScrollTop() {
	return filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function unCheckAllSections(){
    checkSection(false, 'section1');
    checkSection(false, 'section2');
    checkSection(false, 'section3');
    checkSection(false, 'section4');
    checkSection(false, 'section5');
    checkSection(false, 'section6');
    checkSection(false, 'section7');
    checkSection(false, 'section8');
    checkSection(false, 'section9');
    checkSection(false, 'section10');
}


function downloadSummaryReport() {
    unCheckAllSections();
    checkSection(true, 'section10');
    //document.getElementById('hdnCoverPage').value = 'CR Report Final Feb 10 AH.pdf';
    //document.getElementById('hdnCoverPage').value = 'Capita_CR10_PrintFriendly.pdf';	
    //document.getElementById(btnCreatePDFControlId).click();
    window.open('/pdffiles/Capita CR Summary Report 2010.pdf');
}

