

// =====================================================
// Signum Systems Corp. / Kris Klimaszewski
// All contents copyright ©. All rights reserved.
// =====================================================
// form.js


//==============
// Formmail CGI script amendement--scrambles and cloaks the email address of a Signum recepient to prevent harvesting.
//    Goal:  <input type="hidden" name="recipient" value="somebody@signum.com">
//    Usage: In lieu of the line shown in Goal above, enter: <script>formadr("somebody")</script>
//==============
function formadr(who) {
  monkey   = '&#';			    // break up the decimal version of the monkey char
  monkey  += '64;';

  document.write('<input type="hidden" name="recipient" value="' + who + monkey + 'signum.com">');
}



//==============
// JavaScript processing below
//==============


//==============
// Return true if a string is empty.
//==============
var submitcount = 0;				// # of times Submit button's been pressed

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 a form - non-optional fields, ranges for #s, and multiple submission.
// Call this func from within the onSubmit event handler like this:
//
//            <form method="POST"
//                  onSubmit = "this.Name.optional=true; this.Phone.optional=false; return FormVerify(this);"
//                  enctype="text/plain" action="mailto:somebody@signum.com?subject=My subject this is.">
//==============
function FormVerify(f) {
  var msg = "";
  var empty_fields = "";
  var errors = "";

  // Make a list of form elements without "optional" property.
  // If elem has "min" or "max" property, verify that it's a number in the right range.
  for (var i = 0; i < f.length; i++) {
    var e = f.elements[i];
    if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
      if ((e.value == null) || (e.value == "") || isblank(e.value)) { // empty el.?
        empty_fields += "\n             " + e.name;
        continue;
      }  // if empty
      
      if ((e.numeric || e.min != null) || (e.max != null)) { // numeric el.?
        var v = parseFloat(e.value);
        if (isNaN(v) ||							// not a #
          ((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 non-#
      } // if numeric w. limits
    } // if required text or textarea
  } // for all elem

  // If all's well, return true (on Submit).
  // otherwise, show msg & return false (stop Submit).

  if (!empty_fields && !errors && (submitcount==0)) {
    submitcount++;
    return true;
  }

  if (empty_fields) {
    msg += "Please fill in the following required field(s):"
        +  empty_fields + "\n";
    msg += errors;
    alert(msg);
  } // if empty fields
  if (submitcount > 0)
    window.location="http://www.signum.com/_private/submitted.html";		// generates history (can use relative link too)
  return false;
  
} // FormVerify()

//
// http://javascript.internet.com/forms/email-validation---basic.html
//
function ValidateEmailAddress(email) {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
    //alert("Valid email address.")
    return true;
  } else {
    //alert("Oops! Invalid email address.");
    return false;
  }
}


//
// http://javascript.about.com/library/blemaile.htm
// This func assumes the Outlook-Express-type address separator (;) and allows multiple spaces after the separator
// "kk@signum.com;    jerry@signum.com" is ok. "kk@sig num.com; jerry@signum.com" is bad
//
function ValidateEmailAddressMulti(email) {

  var emailStr = email.split(';');				// create array of addresses between the split char (';')
		
  for (var i = 0; i < emailStr.length; i++) {  // loop through addresses
  	emailStr[i] = emailStr[i].replace(/^\s+/g,'');	// removes leading white spaces. Modified from http://www.xploredotnet.com/2007/08/remove-white-space-in-string-using.html
    if (!ValidateEmailAddress(emailStr[i])) {
      return false;
    }
  } // for
  return true;
} 

// eof
