// mask form
angular.module('phoneMask', [])
.directive('phoneDir', function(){
  return {
    link : function(scope, element, attrs) {
        var options = {
            onKeyPress: function(val, e, field, options) {
                putMask();
            }
        }

        $(element).mask('(00) 00000-0000', options);

        function putMask() {
            var mask;
            var cleanVal = element[0].value.replace(/\D/g, '');//pega o valor sem mascara
            if(cleanVal.length > 10) {//verifica a quantidade de digitos.
                mask = "(00) 00000-0000";
            } else {
                mask = "(00) 0000-00009";
            }
            $(element).mask(mask, options);//aplica a mascara novamente
        }
    }
  }

})
.directive('cnpjDir', function(){
  return {
    link : function(scope, element, attrs) {
        $(element).mask('00.000.000/0000-00', {reverse:true});
    }
  }
})
.directive('cepDir', function(){
  return {
    link : function(scope, element, attrs) {
        $(element).mask('00000-000');
    }
  }
});
