/*
 * FormValidator.js
 *
 * Created on 01-Dec-2005
 * 
 * Copyright (C) 2005-2006 Allan Lykke Christensen, allan.lykke.christensen@gmail.com
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/**
 * Validates input elements of HTML forms.
 *
 * Usage:
 *    // Create instance of the form validator
 *    var validator = new FormValidator()
 *    validator.setErrorIntroText("Please correct the following errors:\n\n");
 *    
 *    // Add items to validate
 *    validator.addValidationItem(new ValidationItemEmpty("fieldName", "validateField", "invalidField", "This field cannot be empty"));
 *
 *    // Validate the form
 *    validator.validate();
 *
 * @author <a href="mailto:allan.lykke.christensen@gmail.com">Allan Lykke Christensen</a>
 * @version 1.0
 */
function FormValidator() {

    /** Array containing all the items to be validated. */
    var validationItems = new Array();
    
    /** The element to focus on in the end of the validation. */
    var elementToFocus = false;
    
    /** Introduction text to display in the error dialogue. */
    var errorIntroText = "";
    
    /** Text to display before each error item in the error dialogue. */
    var errorItemPrefix = "- ";
    
    /** Text to display after each error item in the error dialogue. */
    var errorItemSuffix = "\n";
    
    /**
     * Sets the text to display in the error dialogue before displaying each
     * error item.
     *
     * @param introText
     *           Text to display in the error dialogue before displaying each
     *           error item.
     */
    function setErrorIntroText(introText) {
        errorIntroText = introText;
    }
    
    /**
     * Sets the text to displayed before each error item in the error dialogue.
     *
     * @param prefix
     *           Text to displayed before each error item in the error dialogue.
     */
    function setErrorItemPrefix(prefix) {
        errorItemPrefix = prefix;
    }

    /**
     * Sets the text to displayed after each error item in the error dialogue.
     *
     * @param suffix 
     *           Text to displayed after each error item in the error dialogue.
     */
    function setErrorItemSuffix(suffix) {
        errorItemSuffix = suffix;
    }
    
    /**
     * Adds a validation item to the form validator.
     *
     * @param validationItem {@code ValidationItem} to add to the form validator.
     */
    function addValidationItem(validationItem) {
        validationItems[validationItems.length] = validationItem;
    }
    
    /**
     * Validates the validation items added to the form validator.
     *
     * @return <code>true</code> if all the validation items validate, otherwise <code>false</code>.
     */
    function validate() {
        var validateError = false;
        var errorMessage = "";
        
        for (i=0; i<validationItems.length; i++) {
            validationError = validationItems[i].validate();
            var element = document.getElementById(validationItems[i].field);
            
            if (validationError) {
                element.className = validationItems[i].fieldValidClass;
            }
            else {
                errorMessage += errorItemPrefix + validationItems[i].errorMsg + errorItemSuffix;
                element.className = validationItems[i].fieldInvalidClass;
                
                if (elementToFocus == false) {
                    elementToFocus = element;
                }
            }
        }
        
        if (errorMessage == "") {
            return true;
        }
        else {
            alert(errorIntroText + errorMessage);
            elementToFocus.focus();
            return false;
        }
    }
    
    this.addValidationItem = addValidationItem;
    this.validate = validate;
    this.setErrorIntroText = setErrorIntroText;
    this.setErrorItemPrefix = setErrorItemPrefix;
    this.setErrorItemSuffix = setErrorItemSuffix;
}
