﻿$(document).ready(function() {

    $('.print').click(function() { printWebPart($(this).children('img').attr('src'),0); return false; });
    $('#printall').click(function() { printWebPart('#couponholder',1); return false });

    function createWindow(tagid, singleimg) {
        if (tagid) {
            var popup_title = "Advanced Auto Online Coupon";
            if (singleimg == 0) { var htmlbody = "<img src=\"" + tagid + "\" alt=\"Coupon\" class=\"imglogo\" /><br/>"; }
            else { var 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=\"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; } \n</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();
    }

});