<!-- begin hide from older browsers

/*
Name:           contactFormValidate() -- contact form validation for use with template
                                         that allows visitor to send a form email
File:           contact_form_validate.js
Created:        Sunday, 18, November, 2001 at 5:38:04 AM
Last Modified:  Sunday, 18, November, 2001 at 5:38:04 AM
Version:        1.0
Author:         Jeff Gnass/AEGISTICS(R) (jeff.gnass@aegistics.com)
Copyright:      Copyright (c) 2001-2004 by Jeff Gnass/AEGISTICS(R) All Rights Reserved
Source:         Aegistics, Inc. (http://www.aegistics.com)

Description:    This JavaScript function, contactFormValidate(), checks for required form fields,
                and if a form field contains the string "email_addr", it further checks if the
                data entered is a valid email address.
                
                The reference to call this function is made with "contactFormValidate()" even though
                the JavaScriptfile is named "contact_form_validate.js" and the script is "included"
                into the cfm/cfml/htm/html template. Our naming convention style is inline caps for
                functions, and underscore-separated words, abbreviations, and nmeumonics for references
                to file names.
                
                IMPORTANT -- If have both "input=text" and "input=hidden" form elements that have
                the same name, i.e., "regEmailAddress", within the same named form, then any reference
                to these objects will throw an exception when JavaScript trys to reference the object.
                The object will be "undefined".

Known Issues:   See comments throughout program

Here's a sample form to test our verification with. Note that we
call verify() from the onSubmit() event handler, and return whatever
value it returns. Also note that we use the onSubmit() handler as
an opportunity to set properties on the form objects that verify()
will use in the verification process. 

Example usage:
<form action="../contact/contact.cfm" method="post"

    onSubmit="

    this.email_addr_to.required = true;

    this.email_name.required = true;

    this.email_addr.required = true;

    this.email_business_name.required = false;

    this.email_interest.required = false;

    this.email_body.required = true;

// examples of numeric range checks
    // this.mail_zip.required = true;
    // this.mail_zip.min = 0;
    // this.mail_zip.max = 99999;

    return contactFormValidate(this);

    "form onReset="return confirm('Really erase ALL data entries and start over?')">

*/

/////////////// function isBlank() follows HERE ///////////////////////////////////////////////////////////////////////////////////

// A utility function used with function visitorFormValidate() that returns true if a string 
// contains only whitespace (non-printable) 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;
}

/* 2001-11-18 -- work on this RegEx version ####### FIX FIX FIX ####### FIX FIX FIX ####### FIX FIX FIX ####### FIX FIX FIX #######
/////////////// function isEmailAddrValid() -- using RegEx follows HERE ///////////////////////////////////////////////////////////

// This function isEmailAddrValid() returns true if a string contains a properly
// formatted email address (address is NOT verified whether or not it exists!)

function isEmailAddrValid(the_email) {
    // cf emailAddr RegEx string (between the double-quotes): "^([[:alnum:]][-a-zA-Z0-9_%\.]*)?[[:alnum:]]@[[:alnum:]][-a-zA-Z0-9%\>.]*\.[[:alpha:]]{2,}$"
    // 2001-11-18-pm
    var emailAddrRegEx = /^([0-9][-a-zA-Z0-9_%\.]*)?[0-9]@[0-9][-a-zA-Z0-9%\>.]*\.[a-zA-Z]{2,}$/;
    if (the_email.search(emailAddrRegEx)) {
        return true;
    }
    else {
return false;
}
}
*/

/////////////// function isClientBrowserPlatformMac() follows HERE ////////////////////////////////////////////////////////////////

// A utility function isClientBrowserPlatformMac() that returns true if the client browser platform is a Mac.
// Test navigator.appVersion for occurance of strings "Mac", "PowerPC", or "PPC" in the browser signature.

function isClientBrowserPlatformMac() {
    if ( (navigator.appVersion.indexOf("Mac") != -1)
        || (navigator.appVersion.indexOf("PowerPC") != -1)
        || (navigator.appVersion.indexOf("PPC") != -1) ) {
        return true;
    }
    else {
        return false;
    }
}   

/////////////// function isEmailAddrValid() follows HERE //////////////////////////////////////////////////////////////////////////

// A utility function used with function visitorFormValidate() that returns true if a string 
// is a valid Email address.

function isEmailAddrValid(the_email) {
    var the_at_first = the_email.indexOf("@");
    var the_at_last = the_email.lastIndexOf("@");
    var the_dot_first = the_email.indexOf(".");
    var the_dot_last = the_email.lastIndexOf(".");
    var a_space = the_email.indexOf(" ");
    if ((the_at_first != -1) && // there is an "@"
        (the_at_first != 0) &&  // and the "@" is NOT at position 0
        (the_at_last == the_at_first) && // and there is only one "@"
        (the_dot_last != -1) && // and there is a "."
        (the_dot_first != 0) && // and the first "." is not at position 0
        (the_dot_last > the_at_first + 1) &&  // and there is something between the "@" and the last "."
        (the_dot_last < the_email.length - 1) && // and there is something after the last "."
        (a_space == -1))  // and there are no spaces
        {
        // alert("Here we are in our isEmailAddrValid() function, and \n\n     It looks like a valid email address!");
        return true;
        }
        else {
        // alert("Here we are in our isEmailAddrValid() function, and \n\n     Sorry, your email address is invalid!");
        return false;
    }
}

/////////////// function contactFormValidate() follows HERE //////////////////////////////////////////////////////////////////////

// This is the function that performs form validation. It will be invoked
// from the onSubmit() event handler. The handler should return whatever
// value this function returns.
function contactFormValidate(f) {
     var email_errors = "";
     var empty_fields = "";
     var errors = "";
     var selectValue = "";
     var msg = "";
     var msg_name = "";

     // Loop through the elements of the form, looking for all 
     // text and textarea elements that don't have an "required" property
     // defined. 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 validate that they are numbers and that they are in the right range.
     // Put together error messages for fields that are wrong.
     for(var i = 0; i < f.length; i++) {
var e = f.elements[i];

    // check all form object properties that are "select" related 

 if (((e.type == "select-one") || (e.type == "select-multiple")) && e.required) {

var selectValue = e.options[e.selectedIndex].value

if (selectValue == "blank") {
    if (e.name == "email_customer_category") {
    msg_name = "Customer category";
            }
    else if (e.name == "email_addr_to") {
    msg_name = "Send email to";
            }
    empty_fields += "\n          " + msg_name;

    continue;
}

 }

 // check all form object properties that are "text" related 

 if (((e.type == "text") || (e.type == "textarea")) && e.required) {
     if ((e.value == null) || (e.value == "") || isblank(e.value)) {

    if (e.name == "email_name") {
      msg_name = "Name";
            }
    else if (e.name == "email_addr") {
    msg_name = "Email addr";
            }
    else if (e.name == "email_business_name") {
    msg_name = "Business name";
            }
    else if (e.name == "email_body") {
    msg_name = "Email message";
            }
    empty_fields += "\n          " + msg_name;

    continue;

}

 // check for properly formatted email address

 if ((e.name == "email_addr") && !isblank(e.name) && !isEmailAddrValid(e.value)) {
    msg_email  = "Your email address does not appear to be valid";
msg_email += "\n          or is not the proper format";
    email_errors += "\n" + msg_email;

    continue;

}

// 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.name + " 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";
    }
}
 }
 }

     // If there are no errors, return true to allow the form to be submitted.
     // Otherwise, display the error messages, and return false to block
     // the form from being submitted. 
     if (!empty_fields && !email_errors && !errors) return true;

     if ( !isClientBrowserPlatformMac() ) {
         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 the form.\n";
         msg += "______________________________________________________\n\n";
     }
     else {
         msg  = "The form was not submitted because of the following error(s).\n";
         msg += "\n\n";
     }
     
     if (empty_fields) msg += "These required field(s) are empty:\n" + empty_fields + "\n";
     if (empty_fields && email_errors) msg += email_errors + "\n";
     if (!empty_fields && email_errors) msg +=  email_errors + "\n";
     if (empty_fields && errors) msg += errors + "\n";
     if (email_errors && errors) msg += errors + "\n";
     if (!empty_fields && !email_errors && errors) msg += errors + "\n";
     alert(msg);
     return false;

}

// end hide from older browsers -->
