﻿
// Print Friendly
// Requires: jQuery, printElement.js

    function PrintSomething(template, title, root) {
        
        if (typeof $ == 'undefined') {
            txt = "Oops, there was an error in attempting to print.\n\n";
            txt += 'Error description: $ Jquery not loaded.\n\n';
            txt += "Click OK to continue.\n\n";
            alert(txt);
            return;
        }

        if (typeof $().printElement == 'undefined') {
            txt = "Oops, there was an error in attempting to print.\n\n";
            txt += 'Error description: printElement.js is not loaded.\n\n';
            txt += "Click OK to continue.\n\n";
            alert(txt);
            return;
        }
        
        var txt = "";

        try {

            $('#PrintArea').show();
         
            var HTML = BuildHTML(template, root);

            if (HTML === undefined || HTML == "") {
                txt = "Oops, there was an error in attempting to print.\n\n";
                txt += 'Error description: \"' + template + '\" template is not available for printing.\n\n';
                txt += "Click OK to continue.\n\n";
                alert(txt);
                return;
            }
        
            $('#PrintArea').html(HTML);
            $('#PrintArea').printElement(
            {
            printMode: 'popup',
            leaveOpen: false,
            pageTitle: title,
            overrideElementCSS: [{ href: root + '/App_Master/DKSHomePrint.css', media: 'screen' }, { href: root + '/App_Master/DKSHomePrint.css', media: 'print'}]
            });

            $('#PrintArea').find('*').remove();
        }
        catch(err) {
            txt = "Oops, there was an error in attempting to print.\n\n";
            txt += "Error description: " + err.description + "\n\n";
            txt += "Click OK to continue.\n\n";
            alert(txt);
        }
    }

    function BuildHTML(template, root) {

        var containerTopHTML = "";
        var headHTML = "";
        var mainHTML = "";
        var footerHTML = "";
        var containerBottomHTML = "";

        if (template == "E News 2") {

            // Container
            containerTopHTML += "<style> .slideshowItemWrapper {display: block !important;}</style>"
            containerTopHTML += "<div style='width: 660px ; font-size: 16px !important;'>";
            // Head
            headHTML += "<img src='" + root + "/DKSimages/nav-logo.gif' alt='head' />";

            // Main Content
            mainHTML = $('#pfContent').html();

            // Footer
            footerHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
            footerHTML += $('#pfFooter').html();
            // Container
            containerBottomHTML += "</div>";
            // Return HTML
            return containerTopHTML + headHTML + mainHTML + footerHTML + containerBottomHTML;
        }
        else if (template == "Charitable Giving") {

            // Container
            containerTopHTML += "<style> .slideshowItemWrapper {display: block !important;}</style>"
            containerTopHTML += "<div style='width: 660px ; font-size: 16px !important;'>";
            // Head
            headHTML += "<img src='" + root + "/DKSimages/nav-logo.gif' alt='head' />";

            // Main Content
            mainHTML += "<div style='margin: 18px 0 8px 12px;>'" + $('#BreadCrumbResizer').html() + "</div>";
            mainHTML += "<img src='" + root + "/DKSimages/header/header-CharitableGiving.gif' alt='CHARITABLE GIVING' style='' />";

            var slideshowItems = $('.slideshowItemWrapper').clone();
            for (var i = 0; i < slideshowItems.length; i++) {
                if (i == 0)
                    continue;
                $(slideshowItems[i]).remove();
            }
            var imageSrc = $('img', slideshowItems).attr('src');
            mainHTML += "<img src='" + root + imageSrc + "' style='width: 660px; padding-bottom: 10px;' />";
            mainHTML += "<div style='font-size: 14px;'>";
            mainHTML += $('.summary', slideshowItems).html();
            mainHTML += "</div>";
            mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
            mainHTML += $('#pfLSC').html();
            mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
            mainHTML += $('#pfUOTI').html();
            mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
            mainHTML += $('#pfITS').html();
            // Footer
            footerHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
            footerHTML += "<div style='font-size: 10px;'>" + $('#pfFooter').html() + "</div>";
            // Container
            containerBottomHTML += "</div>";
            // Return HTML
            return containerTopHTML + headHTML + mainHTML + footerHTML + containerBottomHTML;
        }
        if (template == "Community Issues") {

            // Container
            containerTopHTML += "<style> .slideshowItemWrapper {display: block !important;}</style>"
            containerTopHTML += "<div style='width: 660px ; font-size: 16px !important;'>";
            // Head
            headHTML += "<img src='" + root + "/DKSimages/nav-logo.gif' alt='head' />";

            // Main Content
            mainHTML += "<div style='margin: 18px 0 8px 12px;>'" + $('#BreadCrumbResizer').html() + "</div>";
            mainHTML += "<img src='" + root + "/DKSimages/header/header-CommunityIssues.gif' alt='COMMUNITY ISSUES' style='' />";

            var slideshowItems = $('.slideshowItemWrapper').clone();
            for (var i = 0; i < slideshowItems.length; i++) {
                if (i == 0)
                    continue;
                $(slideshowItems[i]).remove();
            }
            var imageSrc = $('img', slideshowItems).attr('src');
            mainHTML += "<img src='" + root + imageSrc + "' style='width: 660px; padding-bottom: 10px;' />";
            mainHTML += "<div style='font-size: 14px;'>";
            mainHTML += $('.summary', slideshowItems).html();
            mainHTML += "</div>";
            mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif'/>";
            mainHTML += $('#pfLSC').html();
            mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif'/>";
            mainHTML += $('#pfUOTI').html();
            mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif'/>";
            mainHTML += $('#pfITS').html();
            // Footer
            footerHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
            footerHTML += "<div style='font-size: 10px;'>" + $('#pfFooter').html() + "</div>";
            // Container
            containerBottomHTML += "</div>";
            // Return HTML
            return containerTopHTML + headHTML + mainHTML + footerHTML + containerBottomHTML;

        } else if (template == "Home") {

            // Container
            containerTopHTML += "<style> .slideshowItemWrapper {display: block !important;}</style>"
            containerTopHTML += "<div style='width: 660px ; font-size: 16px !important;'>";
            // Head
            headHTML += "<img src='" + root + "/DKSimages/nav-logo.gif' alt='head' />";

            // Main Content
            mainHTML += "<br/>";

            
            var slideshowItems = $('.slideshowItemWrapper').clone();
            for (var i = 0; i < slideshowItems.length; i++) {
                if (i == 0)
                    continue;
                $(slideshowItems[i]).remove();
            }

            var imageSrc = $('img', slideshowItems).attr('src');

            mainHTML += "<img src='" + root + "" + imageSrc + "' style='width: 660px; margin-top: 10px;' />";

            // What We Do
            mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
            mainHTML += "<img src='" + root + "/DKSimages/header/header-WhatWeDo.gif' alt='WHAT WE DO' />";
            mainHTML += "<br/><br/>";
            mainHTML += $('.mainMidContentWWDCnt').html();

            // Updates
            mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
            mainHTML += "<img src='" + root + "/DKSimages/header/header-Updates.gif' alt='UPDATES' />";
            mainHTML += "<br/><br/>";
            mainHTML += $('.mainMidContentUpdatesCntLt').html();

            // Left Updates

            // Footer
            footerHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
            footerHTML += "<div style='font-size: 10px;'>" + $('#pfFooter').html() + "</div>";
            // Container
            containerBottomHTML += "</div>";
            // Return HTML
            return containerTopHTML + headHTML + mainHTML + footerHTML + containerBottomHTML;

        } else if (template == "Community Issues Level 2") {
            // Container
            containerTopHTML += "<style> .slideshowItemWrapper {display: block !important;}</style>"
            containerTopHTML += "<div style='width: 660px ; font-size: 16px !important;'>";
            // Head
            headHTML += "<img src='" + root + "/DKSimages/nav-logo.gif' alt='head' />";

            // Main Content
            mainHTML += "<div style='margin: 18px 0 24px 12px;>'" + $('#BreadCrumbResizer').html() + "</div>";
            // Grab First Slideshow, remove all others
            var pageContent = $('#pfFieldSet').clone();
            var slideshowItems = pageContent.find('.slideshowItemWrapper');
            for (var i = 0; i < slideshowItems.length; i++) {
                if (i == 0)
                    continue;
                $(slideshowItems[i]).remove();
            }
            // Change 'More Stories' to Image
            $(pageContent).find('.more').html("<img src='" + root + "/DKSimages/moreNewsGreen.JPG' alt='' />");
            // image width
            $(pageContent).find('#pfRightImgLinks').css('width', '180px');
           
            mainHTML += scrubHeightSpacer(pageContent).html();

            // Side Info
            var newsContent1 = $('#pfNews1').clone();
            if ($.trim(scrubEmptyPTags(newsContent1).text().replace(/&nbsp;/g, '')).length > 0) {

                mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
                mainHTML += "<p>" + $('#pfNewsHead1').text() + "</p>";
                mainHTML += $(scrubEmptyPTags(newsContent1)).html();
            }
            var newsContent2 = $('#pfNews2').clone();
            if ($.trim(scrubEmptyPTags(newsContent2).text()).replace(/&nbsp;/g, '').length > 0) {

                mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
                mainHTML += "<p>" + $('#pfNewsHead2').text() + "</p>";
                mainHTML += $(scrubEmptyPTags(newsContent2)).html();
            }

            // Footer
            footerHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
            footerHTML += "<div style='font-size: 10px;'>" + $('#pfFooter').html() + "</div>";
            // Container
            containerBottomHTML += "</div>";
            // Return HTML
            return containerTopHTML + headHTML + mainHTML + footerHTML + containerBottomHTML;

        } else if (template == "Charitable Giving Level 2") {
            // Container
            containerTopHTML += "<style> .slideshowItemWrapper {display: block !important;}</style>"
            containerTopHTML += "<div style='width: 660px; font-size: 16px !important;'>";
            // Head
            headHTML += "<img src='" + root + "/DKSimages/nav-logo.gif' alt='head' />";

            // Main Content
            mainHTML += "<div style='margin: 18px 0 24px 12px;>'" + $('#BreadCrumbResizer').html() + "</div>";
            // Grab First Slideshow, remove all others
            var pageContent = $('#pfFieldSet').clone();
            var slideshowItems = pageContent.find('.slideshowItemWrapper');
            for (var i = 0; i < slideshowItems.length; i++) {
                if (i == 0)
                    continue;
                $(slideshowItems[i]).remove();
            }
            // Change 'More Stories' to Image
            $(pageContent).find('.more').html("<img src='" + root + "/DKSimages/moreNewsPurple.JPG' alt='' />");

            // image width
            $(pageContent).find('#pfRightImgLinks').css('width','180px');
            
            mainHTML += scrubHeightSpacer(pageContent).html();

            // Side Info
            var newsContent1 = $('#pfNews1').clone();
            if ($.trim(scrubEmptyPTags(newsContent1).text().replace(/&nbsp;/g, '')).length > 0) {

                mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
                mainHTML += "<p>" + $('#pfNewsHead1').text() + "</p>";
                mainHTML += $(scrubEmptyPTags(newsContent1)).html();
            }
            var newsContent2 = $('#pfNews2').clone();
            if ($.trim(scrubEmptyPTags(newsContent2).text()).replace(/&nbsp;/g, '').length > 0) {

                mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
                mainHTML += "<p>" + $('#pfNewsHead2').text() + "</p>";
                mainHTML += $(scrubEmptyPTags(newsContent2)).html();
            }

            // Footer
            footerHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
            footerHTML += "<div style='font-size: 10px;'>" + $('#pfFooter').html() + "</div>";
            // Container
            containerBottomHTML += "</div>";
            // Return HTML
            return containerTopHTML + headHTML + mainHTML + footerHTML + containerBottomHTML;

        } else if (template == "Partner With Us Level 2") {
            // Container
            containerTopHTML += "<style> .slideshowItemWrapper {display: block !important;}</style>"
            containerTopHTML += "<div style='width: 660px ; font-size: 16px !important;'>";
            // Head
            headHTML += "<img src='" + root + "/DKSimages/nav-logo.gif' alt='head' />";

            // Main Content
            mainHTML += "<div style='margin: 18px 0 24px 12px;>'" + $('#BreadCrumbResizer').html() + "</div>";

            var pageContent = $('#pfFieldSet').clone();
            $(pageContent).find('.pfMainLeft').each(function () { $(this).width(630); });
            mainHTML += scrubRightSide(scrubHeightSpacer(pageContent)).html();

            // Right Side Version
            var rightSideContent = $('#pfRightSide').clone();
            if ($.trim(scrubEmptyPTags(rightSideContent).text().replace(/&nbsp;/g, '')).length > 0) {

                mainHTML += "<div style='page-break-after:auto;'></div>";
                mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
                mainHTML += $(rightSideContent).html();
            }
            // Side Info
            var newsContent = $('#pfNews1').clone();
            if ($.trim(scrubEmptyPTags(newsContent).text().replace(/&nbsp;/g, '')).length > 0) {

                mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
                mainHTML += "<p>" + $('#pfNewsHead1').text() + "</p>";
                mainHTML += $(scrubEmptyPTags(newsContent)).html(); ;
            }

            // Footer
            footerHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
            footerHTML += "<div style='font-size: 10px;'>" + $('#pfFooter').html() + "</div>";
            // Container
            containerBottomHTML += "</div>";
            // Return HTML
            return containerTopHTML + headHTML + mainHTML + footerHTML + containerBottomHTML;

        } else if (template == "Partner With Us") {

            // Container
            containerTopHTML += "<style> .slideshowItemWrapper {display: block !important;}</style>"
            containerTopHTML += "<div style='width: 660px ; font-size: 16px !important;'>";
            // Head
            headHTML += "<img src='" + root + "/DKSimages/nav-logo.gif' alt='head' />";

            // Main Content
            mainHTML += "<div style='margin: 18px 0 24px 12px;>'" + $('#BreadCrumbResizer').html() + "</div>";

            var pageContent = $('#pfFieldSet').clone();

            var slideshowItems = pageContent.find('.slideshowItemWrapper');
            for (var i = 0; i < slideshowItems.length; i++) {
                if (i == 0)
                    continue;
                $(slideshowItems[i]).remove();
            }
            // Change 'More Stories' to Image
            $(pageContent).find('.more').html("<img src='" + root + "/DKSimages/moreNewsPurple.JPG' alt='' />");
            $(pageContent).find('.pfMainLeft').each(function () { $(this).width(630); });
            mainHTML += scrubRightSide(scrubHeightSpacer(pageContent)).html();

            // Right Side Version
            var rightSideContent = $('#pfRightSide').clone();
            if ($.trim(scrubEmptyPTags(rightSideContent).text().replace(/&nbsp;/g, '')).length > 0) {
                rightSideContent.find('.printBorder1').css('width', '630px');
                $('table', rightSideContent).css('height', '');
                $('td', rightSideContent).css('padding-top', '12px');
                mainHTML += "<div style='page-break-after:auto;'></div>";
                mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
                mainHTML += $(rightSideContent).html();
            }
            // Side Info
            var newsContent = $('#pfNews1').clone();
            if ($.trim(scrubEmptyPTags(newsContent).text().replace(/&nbsp;/g, '')).length > 0) {
                mainHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
                mainHTML += "<p>" + $('#pfNewsHead1').text() + "</p>";
                mainHTML += $(scrubEmptyPTags(newsContent)).html();
            }
            // Footer
            footerHTML += "<img src='" + root + "/DKSimages/638-dots.gif' />";
            footerHTML += "<div style='font-size: 10px;'>" + $('#pfFooter').html() + "</div>";
            // Container
            containerBottomHTML += "</div>";
            // Return HTML
            return containerTopHTML + headHTML + mainHTML + footerHTML + containerBottomHTML;
        } else {
            return "";
        }
    }

    function scrubEmptyPTags(el) {

        var pTags = $(el).find('p');
        $(pTags).each(function () {
            var c = $(this).html();
            if (c == '' || c == '&nbsp;') {
                $(this).remove();
            }
        });

        return el;
    }
    function scrubHeightSpacer(el) {

        var hs = $(el).find('.heightSpacer');
        $(hs).each(function () { 
                $(this).remove();
        });
        return el;
    }
    function scrubRightSide(el) {
        var hs = $(el).find('#pfRightSide');
        $(hs).each(function () {
            $(this).remove();
        });
        return el;
    }

