﻿$(document).ready(function() {

    $('.print').click(function() { printWebPart($(this),0); return false; });
    $('#printall').click(function() { printWebPart('#couponholder',1); return false });

    function createWindow(tagid, singleimg) {
        if (tagid) {
            var popup_title = "Advanced Auto Online Coupon";
            var htmlbody = '';
            if (singleimg == 0) { var htmlbody = $('<div/>').append($(tagid).clone()).html(); }
            else { htmlbody = $(tagid).html(); }
            var html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" " +
                    " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
                      "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
                      "<head>\n<title>" + popup_title + "</title>\n" +
                      "\n<script type=\"text/javascript\">\n" +
                         "function step1() {\n" +
                          "  setTimeout('step2()', 100);\n" +
                          "}\n" +
                          "function step2() {\n" +
                          "  window.print();\n" +
                          "  window.close();\n" +
                          "}\n" +
                      "</scr" + "ipt>" + "<style type=\"text/css\" media=\"all\">* { margin: 0; padding: 0; } ul { list-style: none; } li { display: inline; float: left; } li.beat-club { display: none; } .quote-coupons { overflow: hidden; } a { position: relative; } a span { bottom: 8px; color: #000000; font-size: 10px; position: absolute; right: 10px; } img { border: none; }</style><style type=\"text/css\" media=\"print\">a.print { border:none !important; } .printheader,.disclaimer { display:none !important; }</style><style type=\"text/css\" media=\"screen\">a { font:bold 15px arial;text-decoration:underline; color:#A8A8A8; } .disclaimer { font:normal 10px arial; color:#ccc; }</style></head>\n" +
                      "<body onLoad='step1()'>\n\n" +
                      "<a title=\"Print this page\" href=\"window.print();\">Print this page</a><br/><br/>" +
                      htmlbody + "<p class=\"disclaimer\">Only one coupon per customer allowed.</p>" +
            "\n\n</body>\n</html>";
            return html;
        }
        return false;
    }
    function printWebPart(divname,singleimg) {
        var printWP = window.open("about:blank", "_new");
        printWP.document.open();
        printWP.document.write(createWindow(divname,singleimg));
        printWP.document.close();
    }

});
