$.fn.validateForm = function() {
  var args = arguments[0] || {};
  var validate_on_blur = args.validateOnBlur;

  $(this).submit(function(){



    var has_error = false;
    $('.required').css({"border" : "1px solid #E0E0E0"});
    $('#ddd_telefone').css({"border" : "1px solid #E0E0E0"});
    $(".form_validation_error").remove();
    /*validação de campos obrigaórios*/
    $(".required").each(function(){

      if ($(this).attr('id') == 'telefone') {
        if ($(this).val() == '') {
          $(this).css({"border" : "2px solid #F00"});
          $('#ddd_telefone').css({"border" : "2px solid #F00"});
          has_error = true;
        } else if ($('#ddd_telefone').val() == '') {
          $(this).css({"border" : "2px solid #F00"});
          $('#ddd_telefone').css({"border" : "2px solid #F00"});
          has_error = true;
        }
      } else if($('#confirmaremail').val() != $('#email').val()){
		$('#confirmaremail').css({"border" : "2px solid #00F"});
		has_error = true;
		}else {
        if ($(this).val() == '') {
          $(this).css({"border" : "2px solid #F00"});
          has_error = true;
        }
      }
    });
    /*validação de email*/
    $(".email").each(function(){
      if (!$(this).hasClass('required') || $(this).val() != '') {
	  
        if (!validateEmail($(this).val())) {
          $(this).css({"border" : "2px solid #F00"});
          has_error = true;
        }
		
      }
    });
    
    if (has_error) {
      alert("Preencha corretamente os campos obrigatórios!");
      return false;
    }
  });
  
  if (validate_on_blur) {
    $('.required').blur(function(){
      $(this).css({"background-color" : "#FFF"});
      if ($(this).val() == '') {
        $(this).css({"border" : "2px solid #F00"});
      } else {
        $(this).css({"border" : "1px solid #A5ACB2"});
      }
    });
    
    $('.email').blur(function(){
      $(this).css({"background-color" : "#FFF"});
      if (!validateEmail($(this).val())) {
        $(this).css({"border" : "2px solid #F00"});
      } else {
        $(this).css({"border" : "1px solid #A5ACB2"});
      }
    });
  }
}

function validateEmail(email){  
  var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
  return regex.test(email);  
}

