// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

Event.observe(window, 'load', function () {
  // Find the button that we want to disable
  if($('submit_button') != null){
  var button = $('submit_button');
  // Disable it!s
              var name_not_empty = $F('contact_name') != '';
            var email_not_empty = $F('contact_email') != '';
            var phone_not_empty = $F('contact_phone') != '';

  if(!(name_not_empty && email_not_empty && phone_not_empty)){

   button.disabled = true;

  }

  // Set up an observer to monitor this field

    new Form.Observer('contact_form', 0.3, function(){
          // If field == '' then button disabled = true

            var name_not_empty = $F('contact_name') != '';
            var email_not_empty = $F('contact_email') != '';
            var phone_not_empty = $F('contact_phone') != '';


            button.disabled = !(name_not_empty && email_not_empty && phone_not_empty);
        });
  }

});

function validateForm(){

    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var phoneRegEx = /(^[2-9]\d{2}.\d{3}.\d{4}$)/;

    var alerttxt = '';

    email = $F('contact_email');
    phone = $F('contact_phone');

    var emailvalid = email.match(emailRegEx);
    var phonevalid = phone.match(phoneRegEx);

        if(emailvalid && phonevalid){
                return true;

        }else{
            if(!emailvalid){
              alerttxt = alerttxt.concat('Please enter a valid email address');
            }
            if(!phonevalid){
                if(alerttxt == ''){
                    alerttxt = alerttxt.concat('Please enter a valid phone number. (format: 123-456-7890)');
                }else{
                    alerttxt = alerttxt.concat(' and phone number');
                }
            }
            alert(alerttxt);
            return false;
        }
}


