//
// join.js - validates Colorado HOME Alliance membership and survey form
//
// Copyright (C) 2005 Colorado HOME Alliance
//

function checkEmail() {
  var email = document.forms[ 0].Email;
  var goodEmail = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[A-Za-z]{2,7}$/;
  var out = document.getElementById( 'warnEmail');
  out.innerHTML = "";
  if ( ! email.value.match( goodEmail)) {
    out.innerHTML = "Email \"" + email.value + "\" is not a valid email address";
    email.focus();
    email.select();
    return false;
  }
  return true;
}

function checkRequired( fieldName) {
  var field = document.forms[ 0][ fieldName];
  if ( ! field.value) {
    return false;
  }
  return true;
}

function checkPasswords() {
  var p1 = document.forms[ 0].Password;
  var p2 = document.forms[ 0].VPassword;
  var out = document.getElementById( "warnPassword");
  out.innerHTML = "";
  if ( p1.value != p2.value ) { 
    out.innerHTML = "Passwords do not match";
    return false;
  }
  return true;
}

function validateForm() {
  var out = document.getElementById( "warnSubmit");
  out.innerHTML = "";
  if ( ! checkEmail()) {
    out.innerHTML = "Email \"" + document.forms[ 0].Email.value + "\" is not a valid email address";
    document.forms[ 0].Email.focus();
    document.forms[ 0].Email.select();
    return false;
  }
  if ( ! checkRequired( 'FirstName')) {
    out.innerHTML = "First Name not specified";
    document.forms[ 0].FirstName.focus();
    document.forms[ 0].FirstName.select();
    return false;
  }
  if ( ! checkRequired( 'LastName')) {
    out.innerHTML = "Last Name not specified";
    document.forms[ 0].LastName.focus();
    document.forms[ 0].LastName.select();
    return false;
  }
  if ( ! checkPasswords()) {
    out.innerHTML = "Passwords do not match";
    document.forms[ 0].Password.focus();
    document.forms[ 0].Password.select();
    return false;
  }
  return true;
}
