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

var errors = 0;
var countGoodFields = 0;

$(document).ready(function() {
  $('#fname').blur(function () { validate(this, 'First Name'); });
  $('#zipCode').blur(function () { validate(this, 'Zip Code'); });
  $('#email').blur(function () { validate(this, 'Email'); });
});

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==3)) {
      $('#submit').attr('disabled', '');
    }
  }
}

function validateForm()
{
  countGoodFields = 0;
  $('#fname').blur();
  $('#zipCode').blur();
  $('#email').blur();
  if (countGoodFields!=3) {
    alert('Please correct the fields that are in error');
    return false;
  } else {
    return true;
  }
}
