var Contact_us = Class.create({
  initialize: function(){
    // Error messages
    this.full_name_error = 'The Full Name field is required.';
    this.company_name_error = 'The Company Name field is required.';
    this.address_error      = 'The Street Address field is required.';
    this.city_error         = 'The City field is required.';
    this.state_error        = 'The State field is required.';
    this.zip_error          = 'The Zip field is required.';
    this.phone_error        = 'The Phone field is required.';
    this.email_error        = 'The Email field is required.';
    this.email_notvalid_error = 'The Email field requires a valid email address.';
    this.email_matches_error = 'The Email field does not match the Confirm Email field.';

    this.full_name      = $('full_name') || null;
    this.company_name   = $('company_name') || null;
    this.address        = $('address') || null;
    this.city           = $('city') || null;
    this.state          = $('state') || null;
    this.zip            = $('zip') || null;
    this.phone          = $('phone') || null;
    this.ext            = $('ext') || null;
    this.email          = $('email') || null;
    this.js_message     = $('message_box');
    this.submitbtn      = $('submitbtn');
    this.clearbtn       = $('clearbtn');
    this.init();
  },

  init: function(){
    this.submitbtn.observe('click', this.formHandler.bindAsEventListener(this));
    this.clearbtn.observe('click', this.clearForm.bindAsEventListener(this));
  },

  formHandler: function(evt){
    var msg;
    var regex = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|biz|info|name|aero|info|jobs|museum|name|gov|edu|mil|asia|int)$/i;

    if ((this.full_name) && ($F(this.full_name).blank())) {
      msg = this.full_name_error;
    } else if ((this.company_name) && ($F(this.company_name).blank())){
      msg = this.company_name_error;
    } else if ((this.address) && ($F(this.address).blank())){
      msg = this.address_error;
    } else if ((this.city) && ($F(this.city).blank())){
      msg = this.city_error;
    } else if ((this.state) && ($F(this.state).blank())){
      msg = this.state_error;
    } else if ((this.zip) && ($F(this.zip).blank())){
      msg = this.zip_error;
    } else if ((this.phone) && ($F(this.phone).blank())){
      msg = this.phone_error;
    } else if ((this.email) && ($F(this.email).blank())){
      msg = this.email_error;
    } else if (!regex.test($F(this.email))){
      msg = this.email_notvalid_error;
    } else {
       return true;
    }

    this.js_message.update('<p class="error">' + msg + '</p>');
    this.js_message.scrollTo();

    evt.stop();
    return false;
  },

  clearForm: function (evt){
    evt.stop();
    this.full_name.clear();
    this.company_name.clear();
    this.address.clear();
    this.city.clear();
    this.state.clear();
    this.zip.clear();
    this.phone.clear();
    this.ext.clear();
    this.email.clear();
    this.js_message.update();

    return false;
  }
});

Event.observe(window, 'load', function(){
  new Contact_us();

});