var g_PrintGif_sOrientation = "Portrait";
var g_PrintGif_sSize = "Letter";
var g_PrintGif_sScale = "true";

function getObj(name)
{
    if (!name || name == "")
        return null;
    return getObjFromDoc(document, name);
}

function getObjFromDoc(doc, name)
{
    if (!name || !doc)
        return null;
    if (doc.getElementById)
        return doc.getElementById(name);
    else if (doc.all)
        return doc.all[name];
    return null;
}

function getObjects(name)
{
    if (!name || name == "")
        return null;
    return getObjectsFromDoc(document, name);
}

function getObjectsFromDoc(doc, name)
{
    if (!name || !doc)
        return null;
    if (doc.all)
    {
        if (!doc.all[name])
            return null;
        if (typeof(doc.all[name].length) == "undefined") // If only one element is matched, doc.all returns
            return new Array(doc.all[name]);             // the element itself, rather than a collection
        else
            return doc.all[name];
    }
    else if (doc.getElementsByName)
        return doc.getElementsByName(name);
    return null;
}

function getChildObjsByTag(parent, name)
{
    if (!parent || !name || name == "")
        return null;

    var result = null;

    if (parent.getElementsByTagName)
    {	// Try the W3C DOM method first
        result = parent.getElementsByTagName(name.toUpperCase());
        if (!result  ||  (result.length == 0))
            result = parent.getElementsByTagName(name.toLowerCase());
    }

    // It might not work for MSIE5.0 for "*" (any tag). In that case, try again using the Microsoft DOM
    // level 0 method
    if (result.length == 0 && name == "*")
        result = null;

    // If the W3C DOM method did not succeed, try again using the Microsoft DOM level 0 method
    if (parent.all  &&  parent.all.tags  &&  (result == null))
    {
        result = parent.all.tags(name.toUpperCase());
        if (!result  ||  (result.length == 0))
            result = parent.all.tags(name.toLowerCase());
    }

    return result;
}

// Get a CSS size value (10px, 15pt, also without a unit spec.) and return a number (integer)
function parseIntFromCssSize(size)
{
    if (size == "")
        return 0;
    // Try the input directly first. It may succeed.
    var num = parseInt(size, 10);
    // If not, the input may have a unit specification (px, pt, etc.). Truncate it and try again.
    if (isNaN(num))
        num = parseInt(size.toString().slice(0, -2), 10);
    // If the parsing fails again, the input is probably wrong. Return null in that case.
    if (!isNaN(num))
        return num;
    else
        return null;
}

function isVersionOf_IE_Less5_5()
{
    return ((typeof(document.body.filters) != "undefined")  &&  (typeof(document.body.isDisabled) == "undefined"));
}

function printDoc()
{
    getObj("bottomTable").style.display = "none";
    //if (isVersionOf_IE_Less5_5())
    //alert("<xsl:value-of select="locale:getLocaleText('PRINT_INSTRUCTIONS_MAC')"/>");
    //else
    window.print();
    window.close();
}

function setPrintSize(paper, isLandscape)
{
    var sizeArr = new Array(2);
    var width;
    var height;
    switch (paper.toUpperCase())
    {
        case "A3":
        {
            width = 900;
            height = 1480;
            break;
        }
        case "LETTER":
        {
            width = 640;
            height = 910;
            break;
        }
        case "LEGAL":
        {
            width = 660;
            height = 1140;
            break;
        }
        case "A4":
        default:
        {
            width = 640;
            height = 910;
            break;
        }
    }	// switch(...)
    if (isLandscape)
    {
        var swap = height;
        height = width;
        width = swap;
    }
    sizeArr[0] = width;
    sizeArr[1] = height;
    return sizeArr;
}

function resizeContentHTML(objContent, sizeArr, bScale)
{
    // Select and iterate all primitive-representing elements inside the container
    var entDIVs =  getChildObjsByTag(objContent, "DIV");
    for (var j = 0; j < entDIVs.length; j++)
    {
        var prims = getChildObjsByTag(entDIVs[j], "IMG");
        if (!prims)
            break;
        // Determine the width and height of the page by finding the rightmost and bottommost edges
        var srcWidth = 0;
        var srcHeight = 0;
        var curRight, curBottom;
        for (var i = 0;  i < prims.length;  i++)
        {
            curRight = parseIntFromCssSize(prims[i].style.left) + prims[i].offsetWidth;
            curBottom = parseIntFromCssSize(prims[i].style.top) + prims[i].offsetHeight;
            if (curRight > srcWidth)
                srcWidth = curRight;
            if (curBottom > srcHeight)
                srcHeight = curBottom;
        }
        if (srcWidth == 0 || srcHeight == 0)
            break;
        // Now, that we have the width and height of the entire content, calculate the dimensions of the
        // destination printed content
        var destWidth = srcWidth;
        var destHeight = srcHeight;
        if( bScale )
        {
            if (srcWidth > sizeArr[0])
            {
                destWidth = sizeArr[0];
                destHeight = srcHeight / srcWidth * destWidth;
            }
            if (destHeight > sizeArr[1] || destHeight > srcHeight)
            {
                if (srcHeight >= sizeArr[1])
                {
                    destHeight = sizeArr[1];
                    destWidth = srcWidth / srcHeight * destHeight;
                }
            }
        }
        destWidth = Math.floor(destWidth);
        destHeight = Math.floor(destHeight);
        // Calculate the ratio between the source and the destination
        var changeRatio = destWidth / srcWidth;
 							      
        // Alter a containing DIV so that the content will occupy space in the document flow
        entDIVs[j].style.width = destWidth + "px";
        entDIVs[j].style.height = destHeight + "px";
        // Now iterate all primitives and scale them
        for (var i = 0;  i < prims.length;  i++)
        {
            var modelPrim = prims[i];
            // After cloning, scale the positioning of the element to the new proportions
            // Now scale the dimensions of the image itself
            
                
            pLeft = parseIntFromCssSize(modelPrim.style.left);
            pTop = parseIntFromCssSize(modelPrim.style.top);
            pWidth = parseIntFromCssSize(modelPrim.width);
            pHeight = parseIntFromCssSize(modelPrim.height);
                
            with (modelPrim.style)
            {
                left = (pLeft * changeRatio) + "px";
                top = (pTop * changeRatio) + "px";
                width = (pWidth * changeRatio) + "px";
                height = (pHeight * changeRatio) + "px";
            }
        }
    }
    return;
}
