// A utility function that returns true if a string contains only
// whitespace characters.
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;
}



// A utility function that returns true if a string is a valid email
// THIS COULD USE SOME WORK
function checkEmail(anEmailAddress)
{
    var filter= /^([a-zA-Z0-9\-\.\_]+)(\@)([a-zA-Z0-9\-\.]+)(\.)([a-zA-Z]{2,4})$/i;
    return filter.test(anEmailAddress);
}


// This is the function that performs form verification. It will be invoked
// from the onSubmit() event handler. The handler should return whatever
// value this function returns.
function verify(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";

    // Loop through the elements of the form, looking for all
    // text and textarea elements that are required.
    // Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    var len = f.elements.length;

    for(var i = 0; i < len; i++)
    {
        var e = f.elements[i];

        if ((e.type == "text") || (e.type == "textarea") ||
            (e.type == "file") || (e.type == "password") ||
            (e.type == "select-one"))
        {

            //check if it is empty
            if ((e.value == null) ||
                (e.value == "")   ||
                isblank(e.value)
               )
            {
                    // First check if the field is required
                if (e.required)
                {
//		    alert("e.name=" + e.name );

                    empty_fields += "\n          " + e.title;
                    continue;
                }
            }
            else
            {

                // Now check for fields that are supposed to be numeric.
                if (e.numeric || (e.min != null) || (e.max != null))
                {
                    var v = parseFloat(e.value);
                    if (isNaN(v) ||
                        ((e.min != null) && (v < e.min)) ||
                        ((e.max != null) && (v > e.max))
                       )
                    {
                        errors += "- The field " + e.title + " must be a number";
                        if (e.min != null)
                        {
                            errors += " that is greater than " + e.min;
                        }

                        if (e.max != null && e.min != null)
                        {
                            errors += " and less than " + e.max;
                        }
                        else if (e.max != null)
                        {
                            errors += " that is less than " + e.max;
                        }

                        errors += ".\n";
                    }
                }



                // now check to see if entered text is too long
                if (e.maxstringlength < e.value.length) {
                    errors += "- The field " + e.title +
                              " exceeds its maximum length of " +
                              e.maxstringlength;
                    errors += ".\n";
                    continue;
                }



                // Enforce a general list of acceptable characters
                if (e.enforceReasonableCharacters)
                {
                    e.acceptableCharacters = "0123456789" +
                                             "abcdefghijklmnopqrstuvwxyz" +
                                             "ABCDEFGHIJKLMNOPQRSTUVWXYZ()-_@.' ";
                }



                // Now check if there is a defined set of allowable characters,
                // and if so, generate warning.
                if (e.acceptableCharacters != null)
                {
                    uninvitedGuests = "";  // Illegal characters

                    for (charCount=0; charCount < e.value.length; charCount++)
                    {
                        currChar = e.value.charAt(charCount);
                        if (e.acceptableCharacters.indexOf(currChar) == -1)
                        {
                            uninvitedGuests += currChar;
                        }
                    }

                    if (uninvitedGuests.length > 0)
                    {
                       errors += "- The field " + e.title +
                                 " contains the following unacceptable characters: " +
                                 uninvitedGuests;
                       errors += ".\n";
                       continue;
                    }
                }



                // Ensure it's a valid email address, if necessary
                if (e.isemailaddress)
                {
                    //alert( "About to check for valid email" );
                    // only bother if it's not empty
                    if ( !((e.value == null) ||
                           (e.value == "")   ||
                           isblank(e.value)) )
                    {
                        if (!checkEmail(e.value))
                        {
                            errors += "- The field " + e.title +
                                      " is not a valid email address";
                            errors += ".\n";
                            continue;
                        }
                    }
                }



                // Ensure it's a valid zip code, if necessary
                if (e.iszip)
                {
                    // only bother if it's not empty
                    if ( !((e.value == null) ||
                           (e.value == "")   ||
                           isblank(e.value)  ))
                    {
                        var s = "";
                        for (z=0;z < e.value.length;++z)
                        {
                            if (e.value.charAt(z) != "-")
                            {
                                s = s + e.value.charAt(z);
                            }
                        }

                        var v = parseFloat(s);
                        if (isNaN(v))
                        {
                            errors += "- The field " + e.title + " must be a number";
                            errors += ".\n";
                            continue;
                        }
                        else
                        {
                            e.value = s;
                            if ((e.value.length != 4) &&
								(e.value.length != 5) &&
                                (e.value.length != 9) &&
                                (e.value.length != 11))
                            {
                                errors += "- The field " + e.title +
                                          " must be 4, 5, 9 or 11 digits, " +
                                          "not including dashes.";
                                errors += ".\n";
                                continue;
                            }
                        }
                    }
                }




                // Ensure it's a valid phone number, if necessary
                if (e.isphone)
                {
                    // only bother if it's not empty
                    if ( !((e.value == null) ||
                           (e.value == "")   ||
                           isblank(e.value)  ))
                    {
                        var s;
                        s="";
                        for (z=0;z < e.value.length;++z)
                        {
                            if ( (e.value.charAt(z) != "-") && 
                                 (e.value.charAt(z) != "(") && 
                                 (e.value.charAt(z) != ")") )
                            {
                                s = s + e.value.charAt(z);
                            }
                        }

                        var v = parseFloat(s);
                        if (isNaN(v))
                        {
                            errors += "- The field " + e.title +
                                      " must be a number";
                            errors += ".\n";
                            continue;
                        }
                        else
                        {
                            e.value = s;
                            if (e.value.length < 10)
                            {
                                errors += "- The field " + e.title +
                                          " must be at least 10 digits," +
                                          " not including dashes or parenthesis.";
                                errors += ".\n";
                                continue;
                            }
                        }
                    }
                }
            } //end not empty block

        }

    }

    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted.
    // Otherwise return true.
    if (!empty_fields && !errors) return true;

    msg =  "______________________________________________________\n\n";
    msg += "The form 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;
}

