/*
    ? 2003 Verity, Inc. All rights reserved. Verity?, KeyView? are trademarks of
    Verity, Inc. in the USA and numerous other countries. All other marks or
    symbols are those of their respective owners. This software is licensed, not
    sold, and is subject to worldwide copyright laws and the terms of the Verity,
    Inc. software license agreement contained in this package.
 *
 * File: verityTemplates.js
 *
 * Javascript for Verity K2 Enterprise Web Sample Templates - Functions and
 * variables global to Verity templates
 *
 * Depends upon:
 *      none
 */


/**
 *=====================================================================
 *                G L O B A L  V A R I A B L E S
 *=====================================================================
 */

/**
 * usage example:
 *   g_arrOnLoad[g_arrOnLoad.length] = "code to execute"; // execute "code to execute" when page loads
 *   g_arrOnResize[g_arrOnResize.length] = "code to execute"; // execute "code to execute" when page resizes
 */
var g_arrOnLoad = new Array();
var g_arrOnResize = new Array();

// There is a bug in IE that prevents IE from getting the propery
// offsetTop for the ResultListing div during a resize.
var g_intResultListingOffsetTop = -1;

// For form submit processing pages, set this var to true to have the page
// auto forward to the ReferringPage
var g_blnAutoForward = false;
var g_intAutoForwardTimeout = 3000; //milliseconds


/**
 *=====================================================================
 *                F U N C T I O N S
 *=====================================================================
 */
function evalArray(arrToEval)
{
    for (var i = 0; i < arrToEval.length; i++)
    {
        eval(arrToEval[i]);
    }
}

onload = function()
{
    evalArray(g_arrOnLoad);
}

onresize = function()
{
    evalArray(g_arrOnResize);
}


// Function to set height of main body
function setMainBodyHeight()
{
    var elmMainBody     = document.getElementById("MainBody");
//    var intFooterHeight = document.getElementById("Footer").offsetHeight;
//    var intFooterBottom = document.getElementById("Footer").offsetTop + intFooterHeight;
    
//    if (intFooterBottom < getWindowHeight()) {
//        elmMainBody.style.height = (getWindowHeight() - elmMainBody.offsetTop - intFooterHeight - 15).toString() + "px"; // the 15 is extra spacing for netscape
//    }
}

// Function to set height of results listing div
function setResultListingHeight()
{
    var elm = document.getElementById("ResultListing");
    if (elm)
    {
        if ( g_intResultListingOffsetTop == -1 ) {
            g_intResultListingOffsetTop = elm.offsetTop;
        }

        var intHeight = getWindowHeight() - g_intResultListingOffsetTop - 60;
        if ( intHeight < 300 )
        {
            intHeight = 300;
        }
        elm.style.height = intHeight + "px";
    }
}


// Set the cursor to the desired form element
function setFocus()
{
    if ( document.getElementById("Focus") )
    {
        document.getElementById("Focus").focus();
    }
    // For the templates if Focus is not defined then
    // look for search entry box
    else if ( document.getElementById("QueryText") )
    {
        document.getElementById("QueryText").focus();
    }
}


// If the global auto forward is set to true then go to
// referring page
function autoForward()
{
    if (g_blnAutoForward)
    {
        window.setTimeout("location.href = g_strReferringPage;", g_intAutoForwardTimeout);
    }
    return true;
}

// Generate a pop up window.
// Parameters:
//    strTargetURL -- URL of the page/frameset to be loaded into dialog
//    strWinName -- the dialog window's name
//    intWidth -- pixel width of the dialog window
//    intHeight -- pixel height of the dialog window
//    blnScroll -- enable scrollbar or not
//    objOpenerWin -- the opener of the non-modal dialog
// return the opened window
function openPopup(strTargetURL, strWinName, intWidth, intHeight, blnScroll, objOpenerWin)
{
    if (!strTargetURL)
    {
        alert("Missing strTargetURL parameter for openPopup().");
        return;
    }

    var objOpener = (!objOpenerWin) ? window : objOpenerWin;
    //set the width/height to default values if they are missing
    intWidth = (!intWidth) ? 600 : intWidth;
    intHeight = (!intHeight) ? 500 : intHeight;

    var blnScrollBars = (blnScroll == null) ? 1 : blnScroll;       //default to true

    if (strWinName != null)
    {
        //For IE, if the windowName contains non-alphNumeric characters,
        //then the window.open() won't work (for most of them, except '_' or maybe some others).
        //So replace all those characters with their corresponding unicode value.
        strWinName = strWinName.replace(/([\s\-\\\/\^\$\*\+\?\.\(\)\|\{\}\[\]!@#$%&\"\'<>~`=])/g,
                                        function (str, strMatch)
                                        {
                                            var intSubLen = str.length - 1;
                                            return str.substr(0, intSubLen) + strMatch.charCodeAt(0);
                                        }
                                       );
    }

    // Assemble window attributes and try to center the dialog.
    // The best we can do is center in screen.
    var intLeftOffset = (screen.width - intWidth) / 2;
    var intTopOffset = (screen.height - intHeight) / 2;

    var strWindowAttributes =
          "width=" + intWidth + ","
        + "height=" + intHeight + ","
        + "location=0,"
        + "menubar=0,"
        + "resizable=1,"
        + "scrollbars=" + blnScrollBars + ","
        + "status=0,"
        + "titlebar=0,"
        + "toolbar=0,"
        + "hotkeys=0,"
        + "screenx=" + intLeftOffset + ","  //NN Only
        + "screeny=" + intTopOffset + ","   //NN Only
        + "left=" + intLeftOffset + ","     //IE Only
        + "top=" + intTopOffset;            //IE Only

    // Generate the dialog and make sure it has focus.
    var objTargetWin =  objOpener.open(strTargetURL, strWinName, strWindowAttributes);
    objTargetWin.focus();
    return objTargetWin;
}

/**
 *=====================================================================
 *                E V E N T  H A N D L E R S
 *=====================================================================
 */

// Order is important here for IE
g_arrOnLoad[g_arrOnLoad.length] = "setResultListingHeight();";
g_arrOnLoad[g_arrOnLoad.length] = "setMainBodyHeight();";
// auto-focus is disabled because of problems with Moz/NN/FF
// if the formular is outside of the current view, focus()
// scrolls to the focus'd form element. This is screwy if the
// formular is displayed below the search results
//g_arrOnLoad[g_arrOnLoad.length] = "setFocus();";
g_arrOnLoad[g_arrOnLoad.length] = "autoForward();";

g_arrOnResize[g_arrOnResize.length] = "setResultListingHeight();";
g_arrOnResize[g_arrOnResize.length] = "setMainBodyHeight();";