/**
 * Functions to support the terms page when used as a second
 * window.
 */

/* Terms dialog window methods. */

/**
 * Returns the selected operator as it is encoded on the form and in URLs.
 *
 * @param aForm the terms form
 * @return the selected value for the TCONN element
 * @since 10.1
 */
function getTermsEncodedOperator(aForm)
{
    var opArray = aForm.elements['tconn'];
    var opValue = ",";
    for (var i = 0; i < opArray.length; i++)
    {
        if (opArray[i].checked)
            opValue = opArray[i].value;
    }
    return opValue;
}

/**
 * Returns the selected operator.
 *
 * @param aForm the terms form
 * @return the selected value for the TCONN element
 */
function getTermsOperator(aForm)
{
    var opValue = getTermsEncodedOperator(aForm)
    if (opValue == "compound")
    {
        // Handle pre-10.1 terms.jsp files that don't provide this function.
        if (getSubfieldDelimiter)
            opValue = getSubfieldDelimiter();
        else
            opValue = "\r\n";
    }
    return opValue;
}

/**
 * Returns an array of selected terms. The array may be empty.
 *
 * This function will unselect the selected from elements.
 *
 * @param aForm the terms form
 * @return an array of the selected ST element values, or an
 * empty array
 */
function getSelectedTerms(aForm)
{
    var termArray = aForm.elements['st'];
    if (!termArray) // 0 'st' elements
        return new Array();
    if (!termArray.length) // 1 'st' element; termArray is InputObject
       termArray = new Array(aForm.st)
    var selTerms = new Array();
    var count = 0;
    for (var i = 0; i < termArray.length; i++)
    {
        if (termArray[i].type != "checkbox")
            continue;
        if (termArray[i].checked)
        {
            selTerms[count] = termArray[i].value;
            count++;
            //selTerms.push(termArray[i].value);
            termArray[i].checked = false;
        }
    }
    return selTerms;
}

/**
 * Retrieves the selected operator and terms and sends them to
 * the form which invoked this terms dialog.
 *
 * @param aForm the terms form
 */
function applyTerms(aForm)
{
    var op = getTermsOperator(aForm);
    var terms = getSelectedTerms(aForm);
    if (terms.length > 0)
        window.opener.setSelectedTerms(terms,op);
}


/**
 * Applies a single term and closes the window. The second
 * argument to setSelectedTerms is ignored.
 *
 * @param aTerm a single term
 */
function applyTermAndClose(aTerm)
{
    window.opener.setSelectedTerms([aTerm], ",");
    window.close();
}


/**
 * Retrieves the selected operator and terms, sends them to the
 * form which invoked this terms dialog, and closes the dialog.
 *
 * @param aForm the terms form
 */
function okTerms(aForm)
{
    var op = getTermsOperator(aForm);
    var terms = getSelectedTerms(aForm);
    if (terms.length > 0)
    {
        window.opener.setSelectedTerms(terms,op);
        window.close();
    }
}

/**
 * Builds a search query from the selected terms and
 * operator. Sends the query to the window which invoked the
 * terms dialog and closes the terms window. If no terms were
 * selected, does nothing.
 *
 * @param aSearchAction the search action; this is a parameter in
 * order to allow the JSP to call encodeURL
 * @param aForm the terms form
 * @param aFieldname the field to be searched
 */
function searchTerms(aSearchAction, aForm, aFieldname)
{
    var op = getTermsOperator(aForm);
    var terms = getSelectedTerms(aForm);
    if (terms.length > 0)
    {
       window.opener.location = aSearchAction + "?" + aFieldname + "=" +
            escape(terms.join(op));
       window.close();
    }
}

/**
 * Applies the currently selected terms using applyTerms and appends
 * the currently selected operator to the href attribute of the
 * given Link object. This allows previous and next links to
 * maintain the selected operator.
 *
 * @param aForm the terms form
 * @param aLink a Link object
 */
function applyTermsAddOperator(aForm, aLink)
{
    applyTerms(aForm);

    var op = getTermsEncodedOperator(aForm);
    var href = aLink.href + "&tconn=" + escape(op);
    aLink.href = href;
    return true;
}

/**
 * Causes the query links for individual terms to be directed to
 * the window which opened the terms dialog. Closes the terms
 * window.
 *
 * @param aLink the query link; this Link object's href attribute
 * will be used as the new location for the window which opened
 * the terms dialog
 */
function singleTermQuery(aLink)
{
    window.opener.location = aLink.href;
    window.close();
}



/* ---------------------- */
/* Terms invoker methods. */

/* The terms link on the search, add, edit forms sets these
   variables. The setSelectedTerms function called by the terms
   dialog uses them. */

/** The name of the element whose value should be set to the
 * selected terms. */
var termsElement = null;
/** The form which contains the terms element. */
var termsForm = null;
/** The name of the terms window. */
var termsWindowName = "webtopTermsWindow";
/** The terms window. */
var termsWindow = null;
/** A flag indicating that the terms element should be cleared on
    the first apply. */
var clearTermsElement = false;


/**
 * Opens a terms window.
 *
 * @param aTermsAction the Terms action; this is a parameter in
 * order to allow the JSP page to call encodeURL
 * @param aForm the terms form
 * @param aFieldname the field whose terms are to be displayed
 * @param aFieldValueElement a form element name; this should  be
 * the name of a text input element
 * @param anOptype an operator type; "search" is used for search
 * operators
 * @param anOperator a terms search operator
 * @param termTypeMap a map of field names to default term type parameters
 */
function gotoTermsByName(aTermsAction, aForm, aFieldname,
                         aFieldValueElement, anOptype,
                         anOperator, termTypeMap)
{
    termsForm = aForm;
    termsElement = aFieldValueElement;
    var url = aTermsAction + "?f=" + aFieldname +
        "&optype=" + (anOptype == null ? "default" : anOptype);

    var curValue = termsForm[termsElement].value;
    if (curValue != null && curValue != "")
    {
        if (termTypeMap != null)
        {
            // When looking up thesaurus terms, automatically add
            // a wildcard to the end.
            if (termTypeMap[aFieldname.toUpperCase()] == "T")
            {
                var lastChar = curValue.charAt(curValue.length - 1);
                if (lastChar != '*' && lastChar != '%')
                    curValue += "*";
            }
        }
        url += "&c=" + escape(curValue);
        if (anOperator != null && anOperator != "")
            url += "&op=" + anOperator;
//        clearTermsElement = true;
    }
    termsWindow = window.open(url, termsWindowName,
       "outerHeight=750,outerWidth=700,scrollbars=1,resizable=1");
}


/**
 * Opens a terms window. Finds the name of the field whose terms
 * are to be displayed by retrieving the selected element in a
 * select list.
 *
 * @param aTermsAction the Terms action; this is a parameter in
 * order to allow the JSP page to call encodeURL
 * @param aForm the terms form
 * @param aFieldnameElement the form element name of the select
 * list containing field names
 * @param aFieldValueElement a form element name; this should  be
 * the name of a text input element
 * @param anOptype an operator type; "search" is used for search
 * operators
 * @param anOperator a terms search operator
 * @param termTypeMap a map of field names to default term type parameters
 */
function gotoTerms(aTermsAction, aForm, aFieldnameElement,
                  aFieldValueElement, anOptype, anOperator, termTypeMap)
{
    var sel = aForm[aFieldnameElement].selectedIndex;
    var fieldname = aForm[aFieldnameElement].options[sel].value;
    gotoTermsByName(aTermsAction, aForm, fieldname,
        aFieldValueElement, anOptype, anOperator, termTypeMap)
}

/**
 * Used by the terms dialog to set selected terms on the
 * form. May fail silently if needed information is missing.
 *
 * @param aTermsArray a term array
 * @param aConnector a term connector
 */
function setSelectedTerms(aTermsArray, aConnector)
{
    if (termsForm != null && termsElement != null &&
        aTermsArray != null && aTermsArray.length > 0)
    {
        if (clearTermsElement)
        {
            termsForm[termsElement].value = "";
            clearTermsElement = false;
        }
        if (aConnector == null || aConnector == "")
            aConnector = ",";
        var curValue = termsForm[termsElement].value;
        if (curValue == null || curValue == "")
            termsForm[termsElement].value = aTermsArray.join(aConnector);
        else
        {
            termsForm[termsElement].value =
                curValue + aConnector + aTermsArray.join(aConnector);
        }
        // Handle the "_null" element by invoking any onchange
        // event handler. Don't fail if there's no onchange
        // handler.
        if (termsForm[termsElement].onchange)
            termsForm[termsElement].onchange();
    }
}

/**
 * An onunload function for the terms-using window: closes the
 * terms window if open.
 */
function termsOpenerOnUnload()
{
    if (termsWindow != null && !termsWindow.closed)
        termsWindow.close();
}


