'use strict';
define(function() {
  var ffl2Directive = function() {
    return {
      //          templateUrl:'js/XG/biz/yamoussoukro/modules/form/formfield/directives/textfieldinput.html',
      //            template:'<td>{{theField01.objectField.name}}</td><td style="width:10px;"></td><td><input ng-model="theField01.objectField.value" ></td>',
      //            template:'<input></input>',
      templateUrl: 'js/XG/biz/yamoussoukro/modules/form/ffl2/view/ffl2.html',
      restrict: 'A',
      controller: 'ffl2Ctrl',
      scope: true,

      link: function($scope, elm, attrs, ctrl) {
        if (attrs['myObjects'] != null)
          $scope.objects = JSON.parse(attrs['myObjects']);

        elm.bind('keyup', function(event) {
          if (!$scope.theField01.fieldDesc.fieldTypeIsNumber) return;
          var replacedValue;
          if (
            $scope.theField01.fieldDesc.type != undefined &&
            ($scope.theField01.fieldDesc.type.indexOf('.Double') != -1 ||
              $scope.theField01.fieldDesc.type.indexOf('.Float') != -1)
          )
            replacedValue = event.target.value
              .replace(/[^\d\.]/g, '')
              .replace(/\./, 'x')
              .replace(/\./g, '')
              .replace(/x/, '.');
          else replacedValue = event.target.value.replace(/\D/g, '');
          //var replacedValue = event.target.value.replace(/\D/g, '');
          if (event.target.value !== replacedValue) {
            event.target.value = replacedValue;
          }
          try {
            $scope.$apply();
          } catch(err) {}
        });
        elm.bind('keypress', function(event) {
          if (!$scope.theField01.fieldDesc.fieldTypeIsNumber) return;
          event.keyCode = event.charCode || event.keyCode;
          //if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && event.keyCode !== 46 && event.keyCode !== 8 && event.keyCode !== 9 && event.keyCode !== 27 && event.keyCode !== 13 && event.keyCode !== 39 && event.keyCode !== 40 && !(event.keyCode === 65 && event.ctrlKey === true) && !(event.keyCode === 67 && event.ctrlKey === true) && !(event.keyCode === 80 && event.ctrlKey === true)) {
          if (
            (event.keyCode < 48 || event.keyCode > 57) &&
            event.keyCode !== 46 &&
            event.keyCode !== 8 &&
            event.keyCode !== 9 &&
            event.keyCode !== 27 &&
            event.keyCode !== 13 &&
            event.keyCode !== 39 &&
            event.keyCode !== 40 &&
            !(event.keyCode === 65 && event.ctrlKey === true) &&
            !(event.keyCode === 67 && event.ctrlKey === true) &&
            !(event.keyCode === 80 && event.ctrlKey === true)
          ) {
            event.preventDefault();
            try {
              $scope.$apply();
            } catch(err) {}
          }
        });
      },
    };
  };

  ffl2Directive.$inject = [];
  return ffl2Directive;
});
