var validate = function(form, specs) {

  var that = {},
      fields = [];
  
  specs = specs || { all: true };
  
  if (specs.all) {
    fields = form.find('input[type=text], textarea');
  }
  else {
    fields = form.find(specs.required.join(','));
  }
  
  var valid = function(item) {
    return item.value === item.title || item.value === '' ? false : true;
  };
  
  var formIsValid = function() {
    return form.find('.valid').length === fields.length ? true : false;
  };
  
  var fieldIsValid = function(field) {
    $(field).parent().removeClass('error').removeClass('valid');
    if (!valid(field)) {
      $(field).parent().addClass('error');
      return false;
    }
    else {
      $(field).parent().addClass('valid');
      return true;
    }
  };
  
  var validateForm = function() {
    fields.each(function() {
      fieldIsValid(this);
    });
  };
  
  fields.bind('blur', function() {
    
    
    $(this).parent().removeClass('error').removeClass('valid');
    if (!valid(this)) {
      $(this).parent().addClass('error');
    }
    else {
      $(this).parent().addClass('valid');
    }
  });
  
  that.isValid = formIsValid;
  that.validateForm = validateForm;
  
  return that;
  
};
