// this script taken from MSDN article "Scripting Support for Web Page Printing Sample"
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnscrpt/html/DHTMLPRINT.asp

if ( printIsNativeSupport() )
    window.print2 = window.print;
    window.print = printFrame;

// main stuff
function printFrame(frame, onfinish)
{
    if ( !frame ) frame = window;

    function execOnFinish()
    {
        switch ( typeof(onfinish) )
        {
            case "string": execScript(onfinish); break;
            case "function": onfinish();
        }
    }

    if ( iBrowser > 0 && frame.document.readyState !== "complete" &&
         !confirm("The document to print is not downloaded yet! Continue with printing?") )
        {
            execOnFinish();
            return;
        }

if ( window.print2 )
    { // IE5
        frame.focus();
        if ( frame.print2 )
        {
            if ( iBrowser < 1 )
            {
                beforeprint();
                frame.print2();
                setTimeout("alert; afterprint();", 6000);
            }
            else
            {
                frame.print2();
            }

        }
        else frame.print();
        execOnFinish();
        return;
    }

    var eventScope = printGetEventScope(frame);

    window.printHelper = function()
    {
        execScript("on error resume next: printWB.ExecWB 6, 1", "VBScript");
        printFireEvent(frame, eventScope, "onafterprint");
        printWB.outerHTML = "";
        execOnFinish();
        window.printHelper = null;
    }

    document.body.insertAdjacentHTML("beforeEnd",
    "<object id=\"printWB\" width=0 height=0 \
    classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\">");

    printFireEvent(frame, eventScope, "onbeforeprint");
    frame.focus();
    window.printHelper = printHelper;
    setTimeout("window.printHelper()", 0);
}


// helpers
function printIsNativeSupport()
{
    if ( iBrowser < 1 )
    {
        return true;
        return false;
    }
    else
    {
        var agent = window.navigator.userAgent;
        return iBrowser >= 5 && agent.indexOf("5.0b1") < 0;
    }
}

function printFireEvent(frame, obj, name)
{
    var handler = obj[name];
    switch ( typeof(handler) )
    {
        case "string": frame.execScript(handler); break;
        case "function": handler();
    }
}

function printGetEventScope(frame)
{
    var frameset = frame.document.all.tags("FRAMESET");
    if ( frameset.length ) return frameset[0];
    return frame.document.body;
}

