/*
 * $Id: verify.js,v 1.1 2007/08/23 19:10:04 Denise Exp $
 * verify.js
 *
 * Chris Farris <chris@room17.com>
 * v0.1
 *
 * Javascript application to verify form inputs prior to submission to
 * the server.
 * There is only support text,textarea and password fields.
 *
 * Revision History:
 * v0.1 8/8/00 JCF
 *	Initial creation
 *
 * Copyright (C) 2000 Chris Farris
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 * This code adapted from the book _JavaScript: The Definitive Guide_.
 * Written by David Flanagan.  Copyright (c) 1996 O'Reilly & Associates.
 * This example is provided WITHOUT WARRANTY either expressed or implied.
 * You may study, use, modify, and distribute it for any purpose.
 */

// A function to find whitespace in a string.
function isblank(s) {
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t'))
            return false;
    }
    return true;
}

/* verify(form)
 *
 * Passed a form object this will return true or false if the
 * input vars pass the expectations defined in the form object.
 */
function verify(f) {
    var msg;
    var empty_fields = "";
    var errors = "";

    // Define our Regexps
    var REmail=/^\S+\@[\w-]+\.[\w\.-]+$/;
    var RPhone=/^[0-9+.() -]*$/;
    var RSSN=/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/;
    var RTxt=/^[A-Za-z-+. ']*$/;
    var RAddr=/^[0-9A-Za-z+. ,'\-\/\:]*$/;
//  var RDate=/^[0-9]+[\/ -][0-9]+[\/ -][0-9]+++$/;

    // Loop through each element in the form.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];

//      if (((e.type == "text") || (e.type == "textarea")) && e.required)

        // We only support text,textarea and password fields right now.
        if ((e.type == "text") || (e.type == "textarea") || (e.type == "password")) {
			
            // Do required Check
            if (e.required &&
                ((e.value == null) || (e.value == "") || isblank(e.value))) {
                empty_fields += "\n          " + e.verbal;
                continue;
            }
			
			// Do equals check
			if( e.equals )
			{
				if( e.value != e.equals.value )
				{
					errors += e.verbal + "s do not match";
					errors += ".\n";
					continue;
				}
			}

            // Now check for fields that are supposed to be numeric.
            if ((e.want_num) && ! isblank(e.value)) {
                var v = parseFloat(e.value);
                if (isNaN(v) ) {
                    errors += "- The field " + e.verbal + " must be a number";
                    errors += ".\n";
                } // endif
            } // endif

            // Email Check
            if (e.want_email) {
                if (e.value != "" && !e.value.match(REmail)) {
                    errors += "- The field " + e.verbal + " is not a valid email address.\n";
                } // endif
            } // end if want_email

            // SSN Check
            if (e.want_ssn) {
                if (e.value != "" && !e.value.match(RSSN)) {
                    errors += "- The field " + e.verbal + " is not a valid social security number.\n";
                } // endif
            } // end if want_ssn

            // Phone Number Check
            if (e.want_phone) {
                if (e.value != "" && !e.value.match(RPhone)) {
                    errors += "- The field " + e.verbal + " is not a valid phone number.\n";
                } // endif
            } // end if want_phone

            // Date Check
            if (e.want_date) {
                if (e.value != "" && !e.value.match(RDate)) {
                    errors += "- The field " + e.verbal + " is not a valid date. \n";
                } // endif
            } // end if want_date

            // Text Check
            if (e.want_txt) {
                if (e.value != "" && !e.value.match(RTxt)) {
                    errors += "- The field " + e.verbal + " does not contain valid characters.\n";
                } // endif
            } // end if want_txt

            // Addr Check
            if (e.want_addr) {
                if (e.value != "" && !e.value.match(RAddr)) {
                    errors += "- The field " + e.verbal + " does not contain valid characters.\n";
                } // endif
            } // end if want_addr
        } // endif required.
    } // end for

    if (!empty_fields && !errors) return true;

    msg  = "______________________________________________________\n\n"
    msg += "Your request was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "- The following required field(s) are empty:"
            + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false;
} // end verify()



