var winPrint;
var docPrint;

function createPage(contents, domain, title, header) {
    createSizedPage(contents, domain, title, header, 800, 600);
}

function createSizedPage(contents, domain, title, header, width, height)
{
    winPrint=window.open("","PrintWin","toolbar=0,status=0,menubar=0,fullscreen=no,width="+width+",height="+height+",resizable=0,top=5,left=5");
    winPrint.focus();
    docPrint=winPrint.document;

    docPrint.open();
    docPrint.writeln('<html>');
    docPrint.writeln('<head>');
    if (title != null && title.length > 0) docPrint.writeln('<title>'+title+'</title>');
    docPrint.writeln('<script>');
    docPrint.writeln('var winMain=window.opener;');
    docPrint.writeln('</script>');

    // Remove the javascript from the contents, because it locks up IE
    var done = false;
    var start, end;
    var re1 = new RegExp("<script", "i");
    var re2 = new RegExp("<\x2Fscript>", "i");
    while (!done) {
        start = contents.search(re1);
        if (start != -1) {
            end = contents.search(re2);
            if ( (end != -1) && (end > start) ) {
                var i = end + 9;
                contents = contents.substring(0, start) + contents.substring(i, contents.length);
            }
            else {
                done = true;
            }
        }
        else {
            done = true;
        }
    }

    // Remove "overflow: auto" styles because they cause problems when
    // printing a long page (both IE and Firefox)
    var ov_re = new RegExp('style="overflow: auto;', 'i');
    done = false;
    while (!done) {
        start = contents.search(ov_re);
        if (start != -1) {
            contents = contents.substring(0, start) + 'style="' + contents.substring(start + 22, contents.length);
        }
        else {
            done = true;
        }
    }

    // Replace onmouseover and onmouseout to prevent JavaScript errors
    var mouse_re1 = new RegExp('onmouseover=', 'gi');
    var mouse_re2 = new RegExp('onmouseout=', 'gi');
    contents = contents.replace(mouse_re1, "onmouseoverX=");
    contents = contents.replace(mouse_re2, "onmouseoutX=");

    docPrint.writeln('<link href="'+domain+'/stylesheets/common/dtncommon.css" type="text/css" rel="stylesheet" />');
    docPrint.writeln('<link href="'+domain+'/stylesheets/common/layout/default.css" type="text/css" rel="stylesheet" />');
    docPrint.writeln('<link href="'+domain+'/stylesheets/addons/dtnaddons.css" type="text/css" rel="stylesheet" />');
    docPrint.writeln('<link href="'+domain+'/stylesheets/ag/dtnag.css" type="text/css" rel="stylesheet" />');
    docPrint.writeln('<link href="'+domain+'/stylesheets/weather/dtnweather.css" type="text/css" rel="stylesheet" />');
    docPrint.writeln('</head>');
    docPrint.writeln('<body>');
    if(header != null && header.length > 0) docPrint.writeln('<div class="page_header">'+header+'</div>');
    docPrint.writeln('<div id="content">'+contents+'</div>');
    docPrint.writeln('</body>');
    docPrint.writeln('</html>');
    docPrint.close();


    // IE bug: If you do not do this, on some pages, the print
    // window appears, then immediately disappears
    setTimeout('finishPrinting()', 2000);
}

function finishPrinting() {
    winPrint.print();
    winPrint.close();
}
