﻿

function afaPrintClick(/*string*/gridId)
{
    var g = document.getElementById(gridId);
    var w = window.open('', 'AFAPrintWindow', 'width=500,height=500,resizable=1,scrollbars=1,toolbar=0,location=0,directories=0,status=0,menubar=1,copyhistory=0');
    w.document.title = 'airforce-magazine.com Print Window';
    w.document.write('<html>');
    w.document.write('<head>');
    w.document.write('<title>airforce-magazine.com Print Window</title>');
    w.document.write('<link rel="stylesheet" type="text/css" href="/Style Library/core/controls.css"/>');
    w.document.write('<link rel="stylesheet" type="text/css" href="/Style Library/core/core.css"/>');
    w.document.write('<link rel="stylesheet" type="text/css" href="/Style Library/AFA_default.css" />');
    w.document.write('</head>');
    w.document.write('<body>');
    w.document.write('<a href="javascript:void(0)" onclick="window.print()">Print</a><br /><br />');
    var monthYear = GetMonthYear();

    if (monthYear != "") 
    {
        w.document.write('<h1 class="homePageTitle">AIR FORCE Magazine<br />' + monthYear + '</h1><br />');
    }
    else
    {
        w.document.write('<h1 class="homePageTitle">AIR FORCE Magazine</h1><br />');
    }
    
    var workString = removeTablesWithImages(g.innerHTML);
    workString = removeStandAloneImages(workString);
    w.document.write(workString);
    w.document.write('</body>');
    w.document.write('</html>');
    w.document.close(); //This is the key!!!
}

function removeStandAloneImages(strText)
{
    var re = new RegExp("<img[^>]*>", "gi");
    return strText.replace(re, "");
}

function removeTablesWithImages(strText)
{
    var workText = strText;
    var currentIndex = workText.length;
    var startTableIndex = currentIndex;
    var endTableIndex = currentIndex;
    var startIMGIndex = currentIndex;

    while (currentIndex > 0 && startTableIndex > 0 && endTableIndex > 0)
    {
        startTableIndex = workText.lastIndexOf("<table style=\"width: 100%\" summary=\"\">", currentIndex);
        endTableIndex = workText.lastIndexOf("table>", currentIndex) + 6;
        startIMGIndex = workText.lastIndexOf("<img", currentIndex);

        while (startIMGIndex > endTableIndex)
        {
            currentIndex = startIMGIndex - 1;
            startIMGIndex = workText.lastIndexOf("<img", currentIndex);
        }

        if ((startIMGIndex > startTableIndex) && (startIMGIndex < endTableIndex))
        {
            workText = workText.slice(0, startTableIndex) + workText.slice(endTableIndex);
        }
        
        currentIndex = startTableIndex - 1;
    }
    return workText;
}
// ControlKeyPress(field, e, btnId)
//	Took basic functionality of the old method, but tried to 
//	better handle the scripting DOM.
function ControlKeyPress(field, e, btnId)
{
    var keycode;
    var btn;

    if (!window.event && !e) // just exit here
    {
        return true;
    }

    if (window.event) // ie-type event capable
    {
        keycode = window.event.keyCode;
    }
    else
    {
        keycode = e.which;
    }

    if (keycode == 13)
    {
        // this is how we check for newer browser...
        if (document.getElementById)
        {
            btn = document.getElementById(btnId);

            if (btn)
            {
                btn.click();
                return false;
            }
        }
        else
        {
            // if we care about ns4, then change
            // this else clause to handle it.
        }
    }

    return true;
}

function GetMonthYear()
{
    var month = "";
    var year = "";
    
    if (document.getElementById("month") != null) 
    {
        month = document.getElementById("month").innerHTML;
    }

    if (document.getElementById("year") != null) 
    {
        year = document.getElementById("year").innerHTML;
    }

    if ((month != "") && (year != ""))
    {
        return month + " " + year;
    }
    else 
    {
        return "";
    }
}