/* --------------------------------- */
/* ArchiveValidator.js               */
/* --------------------------------- */
/* Created:      2006-03-02          */
/* Issued:       2006-03-02          */
/* Modified:     2006-03-03          */
/* Copyright (c) 2007 by             */
/* Philip Shaw, all rights reserved. */
/* --------------------------------- */

/**
 *  Requires an endMonth integer varible to be set in the page to mark the last
 *  archived page date, indexed from 1 = January.
 */

/**
 *  The start year for archived pages.
 */
var startYear = 2000;

/**
 *  The start month for archived pages.
 */
var startMonth = 7;

/**
 *  A line separator for alerts.
 */
var line = "__________________________________________________\n\n";

/**
 *  Check the dated archive page is available.
 *  @return true if between the start and end dates of the archive, or values
 *          cannot be checked for various reasons.
 */
function checkArchiveDate() {

    if (typeof document.getElementById != 'undefined') {

        var monthSelect = document.getElementById('month');

        var yearSelect = document.getElementById('year');

        if ((monthSelect != null) &&
            (yearSelect != null)) {

            var monthIndex = monthSelect.selectedIndex;

            var yearIndex = yearSelect.selectedIndex;

            if ((yearIndex == 0) &&
                ((monthIndex + 1) < startMonth)) {

                return alertTooEarly();
            }
            else {

                if ((yearIndex == (yearSelect.options.length - 1)) &&
                    (monthIndex >= endMonth)) {

                    return alertTooLate();
                }
                else {

                    return true;
                }
            }
        }
        else {

            return true;
        }
    }
    else {

        return true;
    }
}


/**
*  Raise an alert the selection pre-dates the start of the archive.
*  @return Always returns false.
*/
function alertTooEarly() {

    alert("Selected date is too early\n" +
          line +
          "The archive page for the date you selected does not " +
          "exist,\nthe date is before the origin of this site. " +
          "Please choose\na date from July 2000 onwards.\n" +
          line);

    return false;
}

/**
*  Raise an alert the selection post-dates the end of the archive.
*  @return Always returns false.
*/
function alertTooLate() {

    alert("Selected date is too late\n" +
          line +
          "The archive page for the date you selected has not " +
          "been\npublished yet. Please see the current site log " +
          "page for the\nmost recent entries.\n" +
          line);

    return false;
}
