/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var errors = 0;
var countGoodFields = 0;

$(document).ready(function() {

  $('#name').blur(function () { validate(this, 'Name'); });
  $('#city').blur(function () { validate(this, 'City'); });
  $('#phone').blur(function () { validate(this, 'Phone Number'); });
  $('#email').blur(function () { validate(this, 'Email'); });
  $('#email2').blur(function () { validateEmail2(this); });

});

function validate(obj, name)
{
  if ($(obj).val() == "") {
    $(obj).css('background-color', '#ffbbbb');
    $('#' + $(obj).attr('id') + '_error').html(name + " is a required field.");
    errors++;
    $('#submit').attr('disabled', 'disabled');
  } else {
    $(obj).css('background-color', '');
    $('#' + $(obj).attr('id') + '_error').html('');
    errors--;
    countGoodFields++;
    if ((errors == 0)||(countGoodFields==5)) {
      $('#submit').attr('disabled', '');
    }
  }
}

function validateEmail2(obj)
{
  if ($(obj).val() != $('#email').val() || $(obj).val() == '') {
    $(obj).css('background-color', '#ffbbbb');
    $('#' + $(obj).attr('id') + '_error').html('Both e-mail addresses must be identical');
    errors++;
    $('#submit').attr('disabled', 'disabled');
  } else {
    $(obj).css('background-color', '');
    $('#' + $(obj).attr('id') + '_error').html('');
    errors--;
    countGoodFields++;
    if ((errors == 0)||(countGoodFields==5)) {
      $('#submit').attr('disabled', '');
    }
  }
}

function validateForm()
{
  countGoodFields = 0;
  $('#name').blur();
  $('#city').blur();
  $('#phone').blur();
  $('#email').blur();
  $('#email2').blur();
  
  if (countGoodFields!=5) {
    alert('Please correct the fields that are in error');
    return false;
  } else {
    return true;
  }
}
