 /*
 * jQuery email validation plugin 0.1
 *
 * http://jhg.me.uk/jquery/mail-validation-plugin
 * http://docs.jquery.com/Plugins/Validation
 *
 * Copyright (c) 2009 James Greenwood
 *
 * This work is licensed under the Creative Commons Attribution 3.0 Unported License.
 * To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/
 *
 */

(function($) {

$.fn.validmail = function(options) {

  var defaults = {
    output : '#validation-errors',
    invalid_domains: { }
  };
  
  var settings = $.extend({}, defaults, options);
  set = this;
  
  if (this.length == 2) {
    return this.each(function(index) {
      if (index == 0) {
        
        $(this).keyup(function() {
          if(typeof(firstFieldtimeout) != 'undefined') {
            clearTimeout(firstFieldtimeout);
          }
          firstFieldtimeout = setTimeout(function() {
            firstFieldtimeout = undefined;
            firstFieldChecks(settings['invalid_domains'], settings['output']);
          }, 500);
        });
      }
      else if (index == 1) {        
        $(this).keyup(function() {
          if(typeof(secondFieldtimeout) != 'undefined') {
            clearTimeout(secondFieldtimeout);
          }
          secondFieldtimeout = setTimeout(function() {
            secondFieldtimeout = undefined;
            secondFieldChecks(settings['output']);
          }, 500);
        });
      }
    });
  }
};

})(jQuery);

function firstFieldChecks(invalid_domains, output_area) {
  current = set[0];
  other = set[1];
  
  var typed = $.trim($(current).val());
  
  if  (typed !== null && typed !== '') {
    var output = false;
    
    // Validate email address syntax
    var message = "Invalid email address";
    if (!typed.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
      output = true;
      $(output_area).text(message);
    }
    
    // Check for common email domain mispellings
    if (!output) {
      var matches = typed.match(/^.*@(.*)$/);
      if (matches) {
        if (invalid_domains[matches[1]]) {
          var message = "You put '" + matches[1] + "', did you mean '" + invalid_domains[matches[1]] + "'?";
          
          if ($(output_area).length > 0) {
            if ($(output_area).text() !== message) {
              output = true;
              $(output_area).text(message);
            }
          }
          else if ($(output_area).length == 0) {
            output = true;
            $('body').append('<div id="validation-error">' + message + '</div>');
          }
        }
        else {
          $(output_area).hide().text('');
        }
      }
    }
    
    if (output && $(output_area).is(':hidden')) $(output_area).fadeIn();
  }
}

function secondFieldChecks(output_area) {
  current = set[1];
  other = set[0];
  
  var message = "Email addresses don't match";
  var output = false;
  
  if ($(current).val() !== '' && $(other).val() !== '') {
    if ($(current).val() !== $(other).val()) {
      if ($(output_area).text() !== message) {
        $(output_area).text(message);
        output = true;
      }
    }
    
    if (output && $(output_area).is(':hidden')) $(output_area).fadeIn();
  }
}
