/*
 * This is a set of functions for validating forms. Also see string_validation.js
 *
 * Copyright (C) 2003 by John Glorioso, Zitego Solutions, LLC
 * <jglorioso@zitego.com>. All rights reserved.
 *
 * Redistribution and use in source forms, with or without modification, are
 * permitted provided that the following condition is met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * @author John Glorioso
 * @version $Id: form_functions.js,v 1.1 2006/09/27 04:09:31 jglorioso Exp $
 */

/*
 * Trims all text fields in the given form.
 *
 * @param frm The form.
 */
function trimFields(frm)
{
    for (var i=0; i<frm.elements.length; i++)
    {
        var elem = frm.elements[i];
        if ( (elem.type == "text" || elem.type == "textarea") && elem.value != "" )
        {
            elem.value = trim(elem.value);
        }
    }
}

/*
 * Trims spaces from the given value and returns the trimmed value.
 *
 * @param txt The value to trim.
 */
function trim(txt)
{
    if ( txt == void(0) ) return "";
    //Loop through all characters forward and backward removing spaces
    while (txt.charAt(0) == " ")
    {
        txt = txt.substring(1);
    }
    while (txt.charAt(txt.length-1) == " " || txt.charAt(txt.length-1) == "\r" || txt.charAt(txt.length-1) == "\n")
    {
        txt = txt.substring(0, txt.length-1);
    }
    return txt;
}

/*
 * Displays an error mesage and moves focus to the specified element field.
 *
 * @param elem The form element to put focus on.
 * @param errMsg The message to alert.
 */
function fieldError(elem, errMsg)
{
    elem.focus();
    if (elem.type == "text" || elem.type == "textarea" || elem.type == "textfield" || elem.type == "password")
    {
        elem.select();
    }
    alert(errMsg);
    return false;
}

/*
 * Displays a conformation box and moves focus to the specified element field.
 *
 * @param elem The form element to put focus on.
 * @param errMsg The message to alert.
 */
function fieldConfirm(elem, msg)
{
    elem.focus();
    if (elem.type == "text" || elem.type == "textarea" || elem.type == "textfield" || elem.type == "password")
    {
        elem.select();
    }
    return confirm(msg);
}