//-----------------------------------------------------------------------------
// File:  SearchPane.js
//
// Purpose:  This file contains the javascript code that is specicific to
//           the advanced and basic search.
//
// Depenedencies:  CollectionTreeManager.js, common.js
//----------------------------------------------------------------------------

function SearchPane()
{
    this.errors = [];

    this.errorString = "";

    SearchPane.instance = this;
}

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------

/**
 * Error messages
 */
SearchPane.beginDateAfterEndDate = "SearchPane.beginDateAfterEndDate";
SearchPane.noCategoriesSelected  = "SearchPane.noCategoriesSelected";
SearchPane.searchEmptyMessage    = "SearchPane.searchEmptyMessage";
SearchPane.searchFieldEmpty      = "SearchPane.searchFieldEmpty";

SearchPane.minYear = 1900;
SearchPane.currentYear = new Date().getFullYear();

/**
 * The single instance of the searchPane object
 */
SearchPane.instance = null;

/**
 * regexp for empty field expression test.
 */
SearchPane.emptyString = /^\s*$/;

/**
 * Set focus on the first search field.
 */
SearchPane.prototype.focusOnSearch = function()
{
    if (tab_state == "simple")
    {
        document.getElementById("search_terms").focus();
    }
    else
    {
        // Find the first input element in the form and set focus to it
        var formElements = document.search.elements;
        for (var i = 0; i < formElements.length; ++i)
        {
            if (formElements[i].type == "text")
            {
                formElements[i].focus();
                break;
            }
        }
    }
};

/**
 * add an error the the array of errors to display.
 * @param errorMessage the error message to add
 */
SearchPane.prototype.addError = function(errorMessage)
{
    this.errors.push(errorMessage);
};

/**
 * An associative array that contains the name of test elements to ignore when
 * determininng if at least one field is filled out
 */
SearchPane.textElementsToIgnore = {"toYear":"toYear",
                            "fromYear":"fromYear",
                            "alertName":"alertName",
                            "description":"description",
                            "frequency.multiplier": "frequency.multiplier"};

/**
 * Ensure that at least one of the required fields is filled out.
 * @param errors object to add errors to if any have occurred
 * @param form the form to validate against
 */
SearchPane.prototype.atLeastOne = function(form)
{
    var isNoSearchTerms = true;
    var formElements = form.elements;
    for (var i = 0; i < formElements.length; ++i)
    {
        if (formElements[i].type == "text")
        {
            if (typeof SearchPane.textElementsToIgnore[formElements[i].name] != "undefined")
            {
                continue;
            }

            isNoSearchTerms &= SearchPane.emptyString.test(formElements[i].value);
            if (!isNoSearchTerms)
            {
                break;
            }
        }
    }

    if (isNoSearchTerms)
    {
        this.addError(SearchPane.searchEmptyMessage);
    }
};

//-----------------------------------------------------------------------------
// Finds out if any of the checkboxes in the search form are checked
//-----------------------------------------------------------------------------
SearchPane.prototype.areAnyCollectionsChecked = function()
{
    var root = document.getElementById("categoryBlock");
    var nodes = root.getElementsByTagName("input");
    var areAnyCollectionsChecked = false;

    for (var i = 0; i < nodes.length && !areAnyCollectionsChecked; i++)
    {
        if (nodes[i].type == "checkbox" && nodes[i].checked)
        {
            areAnyCollectionsChecked = true;
        }
    }

    if (!areAnyCollectionsChecked)
    {
        this.addError(SearchPane.noCategoriesSelected);
    }
};

function switchTabs(caller)
{
    var simple_tab   = document.getElementById('simple-tab');
    var advanced_tab = document.getElementById('advanced-tab');
    var simple_tab_container = document.getElementById('simple-tab-content');
    var advanced_tab_container = document.getElementById('advanced-tab-content');
    //do nothing if the user clicks on the currently highlighted tab
    if(caller == tab_state){
        return;
    }
    if (tab_state == 'simple' ) {
        // switch to advanced
        simple_tab.className = '';
        advanced_tab.className = 'Selected';
        advanced_tab.blur();
        simple_tab_container.className = 'Removed';
        advanced_tab_container.className = '';
        tab_state = 'advanced';
    } else {
        // switch to simple
        simple_tab.className = 'Selected';
        simple_tab.blur();
        advanced_tab.className = '';
        simple_tab_container.className = '';
        advanced_tab_container.className = 'Removed';
        tab_state = 'simple';
    }

    SearchPane.instance.focusOnSearch();
}

SearchPane.prototype.validateThisForm = function(form, displayAlert)
{
    if (typeof(displayAlert) == "undefined")
    {
        displayAlert = true;
    }

    // Reset the errors array
    this.errors = [];

    var ret = true;

    this.atLeastOne(form);

    this.validateDates();

    this.areAnyCollectionsChecked();

    if (this.errors.length > 0)
    {
        this.errorString = "";
        for (i = 0; i < this.errors.length; ++i)
        {
            this.errorString += this.errors[i];
            this.errorString += "\n\n";
        }

        if (displayAlert)
        {
            window.alert(this.errorString);
        }

        ret = false;
    }

    return ret;
};

SearchPane.prototype.isValidDateElement = function(ele)
{
    return (ele !== null) && (ele.tagName == "input") && (ele.type == "text");
};

SearchPane.prototype.validateDates = function()
{
    var a = document.getElementById('fromYear');
    var b = document.getElementById('toYear');

    if (!a || !b)
    {
        return true;
    }

    // Validate each field
    this.dateFieldIsValid(a);
    this.dateFieldIsValid(b);

    if (a.value !== "" && b.value !== null && a.value > b.value)
    {
        this.addError(SearchPane.beginDateAfterEndDate);
    }
};

SearchPane.prototype.dateFieldIsValid = function(field)
{
    var value = field.value;

    if (value.length === 0)
    {
        // empty fields are fine
    }
    else if (! value.match(/^[0-9]{4}$/))
    {
        if (field.id == 'fromYear')
        {
            this.addError('The start date must have four digits.');
        }
        else
        {
            this.addError('The end date must have four digits.');
        }
    }
    else if (value < SearchPane.minYear)
    {
        if (field.id == 'fromYear')
        {
            this.addError('The earliest searchable start year is '+SearchPane.minYear);
        }
        else
        {
            this.addError('The earliest searchable end year is ' +SearchPane.minYear);
        }
    }
    else if (value > SearchPane.currentYear)
    {
        if (field.id == 'fromYear')
        {
            this.addError('The latest searchable start year is ' + SearchPane.currentYear + '.');
        }
        else
        {
            this.addError('The latest searchable end year is ' + SearchPane.currentYear + '.');
        }
    }
};