text: /**
   * (c) jTools Input Mask
   * https://github.com/paulhodel/jtools
   *
   * @author: Paul Hodel <paul.hodel@gmail.com>
   * @description: Input mask
   */
  
  jSuites.mask = (function() {
      var obj = {};
      var index = 0;
      var values = []
      var pieces = [];
  
      obj.run = function(value, mask, decimal) {
          if (value && mask) {
              if (! decimal) {
                  decimal = '.';
              }
              if (value == Number(value)) {
                  var number = (''+value).split('.');
                  var value = number[0];
                  var valueDecimal = number[1];
              } else {
                  value = '' + value;
              }
              index = 0;
              values = [];
              // Create mask token
              obj.prepare(mask);
              // Current value
              var currentValue = value;
              if (currentValue) {
                  // Checking current value
                  for (var i = 0; i < currentValue.length; i++) {
                      if (currentValue[i] != null) {
                          obj.process(currentValue[i]);
                      }
                  }
              }
              if (valueDecimal) {
                  obj.process(decimal);
                  var currentValue = valueDecimal;
                  if (currentValue) {
                      // Checking current value
                      for (var i = 0; i < currentValue.length; i++) {
                          if (currentValue[i] != null) {
                              obj.process(currentValue[i]);
                          }
                      }
                  }
              }
              // Formatted value
              return values.join('');
          } else {
              return '';
          }
      }
  
      obj.apply = function(e) {
          var mask = e.target.getAttribute('data-mask');
          if (mask && e.keyCode > 46) {
              index = 0;
              values = [];
              // Create mask token
              obj.prepare(mask);
              // Current value
              if (e.target.selectionStart < e.target.selectionEnd) {
                  var currentValue = e.target.value.substring(0, e.target.selectionStart); 
              } else {
                  var currentValue = e.target.value;
              }
              if (currentValue) {
                  // Checking current value
                  for (var i = 0; i < currentValue.length; i++) {
                      if (currentValue[i] != null) {
                          obj.process(currentValue[i]);
                      }
                  }
              }
              // New input
              obj.process(obj.fromKeyCode(e));
              // Update value to the element
              e.target.value = values.join('');
              if (pieces.length == values.length && pieces[pieces.length-1].length == values[values.length-1].length) {
                  e.target.setAttribute('data-completed', 'true');
              } else {
                  e.target.setAttribute('data-completed', 'false');
              }
              // Prevent default
              e.preventDefault();
          }
      }
  
      /**
       * Process inputs and save to values
       */
      obj.process = function(input) {
          do {
              if (pieces[index] == 'mm') {
                  if (values[index] == null || values[index] == '') {
                      if (parseInt(input) > 1 && parseInt(input) < 10) {
                          values[index] = '0' + input;
                          index++;
                          return true;
                      } else if (parseInt(input) < 10) {
                          values[index] = input;
                          return true;
                      } else {
                          return false;
                      }
                  } else {
                      if (values[index] == 1 && values[index] < 2 && parseInt(input) < 3) {
                          values[index] += input;
                          index++;
                          return true;
                      } else if (values[index] == 0 && values[index] < 10) {
                          values[index] += input;
                          index++;
                          return true;
                      } else {
                          return false
                      }
                  }
              } else if (pieces[index] == 'dd') {
                  if (values[index] == null || values[index] == '') {
                      if (parseInt(input) > 3 && parseInt(input) < 10) {
                          values[index] = '0' + input;
                          index++;
                          return true;
                      } else if (parseInt(input) < 10) {
                          values[index] = input;
                          return true;
                      } else {
                          return false;
                      }
                  } else {
                      if (values[index] == 3 && parseInt(input) < 2) {
                          values[index] += input;
                          index++;
                          return true;
                      } else if (values[index] < 3 && parseInt(input) < 10) {
                          values[index] += input;
                          index++;
                          return true;
                      } else {
                          return false
                      }
                  }
              } else if (pieces[index] == 'hh24') {
                  if (values[index] == null || values[index] == '') {
                      if (parseInt(input) > 2 && parseInt(input) < 10) {
                          values[index] = '0' + input;
                          index++;
                          return true;
                      } else if (parseInt(input) < 10) {
                          values[index] = input;
                          return true;
                      } else {
                          return false;
                      }
                  } else {
                      if (values[index] == 2 && parseInt(input) < 4) {
                          values[index] += input;
                          index++;
                          return true;
                      } else if (values[index] < 2 && parseInt(input) < 10) {
                          values[index] += input;
                          index++;
                          return true;
                      } else {
                          return false
                      }
                  }
              } else if (pieces[index] == 'hh') {
                  if (values[index] == null || values[index] == '') {
                      if (parseInt(input) > 1 && parseInt(input) < 10) {
                          values[index] = '0' + input;
                          index++;
                          return true;
                      } else if (parseInt(input) < 10) {
                          values[index] = input;
                          return true;
                      } else {
                          return false;
                      }
                  } else {
                      if (values[index] == 1 && parseInt(input) < 3) {
                          values[index] += input;
                          index++;
                          return true;
                      } else if (values[index] < 1 && parseInt(input) < 10) {
                          values[index] += input;
                          index++;
                          return true;
                      } else {
                          return false
                      }
                  }
              } else if (pieces[index] == 'mi' || pieces[index] == 'ss') {
                  if (values[index] == null || values[index] == '') {
                      if (parseInt(input) > 5 && parseInt(input) < 10) {
                          values[index] = '0' + input;
                          index++;
                          return true;
                      } else if (parseInt(input) < 10) {
                          values[index] = input;
                          return true;
                      } else {
                          return false;
                      }
                  } else {
                      if (parseInt(input) < 10) {
                          values[index] += input;
                          index++;
                          return true;
                       } else {
                          return false
                      }
                  }
              } else if (pieces[index] == 'yy' || pieces[index] == 'yyyy') {
                  if (parseInt(input) < 10) {
                      if (values[index] == null || values[index] == '') {
                          values[index] = input;
                      } else {
                          values[index] += input;
                      }
                      
                      if (values[index].length == pieces[index].length) {
                          index++;
                      }
                      return true;
                  } else {
                      return false;
                  }
              } else if (pieces[index] == '#' || pieces[index] == '#.##' || pieces[index] == '#,##' || pieces[index] == '# ##') {
                  if (input.match(/[0-9]/g)) {
                      if (pieces[index] == '#.##') {
                          var separator = '.';
                      } else if (pieces[index] == '#,##') {
                          var separator = ',';
                      } else if (pieces[index] == '# ##') {
                          var separator = ' ';
                      } else {
                          var separator = '';
                      }
                      if (values[index] == null || values[index] == '') {
                          values[index] = input;
                      } else {
                          values[index] += input;
                          if (separator) {
                              values[index] = values[index].match(/[0-9]/g).join('');
                              var t = [];
                              var s = 0;
                              for (var j = values[index].length - 1; j >= 0 ; j--) {
                                  t.push(values[index][j]);
                                  s++;
                                  if (! (s % 3)) {
                                      t.push(separator);
                                  }
                              }
                              t = t.reverse();
                              values[index] = t.join('');
                              if (values[index].substr(0,1) == separator) {
                                  values[index] = values[index].substr(1);
                              } 
                          }
                      }
                      return true;
                  } else {
                      if (pieces[index] == '#.##' && input == '.') {
                          // Do nothing
                      } else if (pieces[index] == '#,##' && input == ',') {
                          // Do nothing
                      } else if (pieces[index] == '# ##' && input == ' ') {
                          // Do nothing
                      } else {
                          if (values[index]) {
                              index++;
                              if (pieces[index]) {
                                  if (pieces[index] == input) {
                                      values[index] = input;
                                      return true;
                                  } else {
                                      if (pieces[index] == '0' && pieces[index+1] == input) {
                                          index++;
                                          values[index] = input;
                                          return true;
                                      }
                                  }
                              }
                          }
                      }
  
                      return false;
                  }
              } else if (pieces[index] == '[-]') {
                  if (input == '-' || input == '+') {
                      values[index] = input;
                  } else {
                      values[index] = ' ';
                  }
                  index++;
                  return true;
              } else if (pieces[index] == '0') {
                  if (input.match(/[0-9]/g)) {
                      values[index] = input;
                      index++;
                      return true;
                  } else {
                      return false;
                  }
              } else if (pieces[index] == 'a') {
                  if (input.match(/[a-zA-Z]/g)) {
                      values[index] = input;
                      index++;
                      return true;
                  } else {
                      return false;
                  }
              } else {
                  if (pieces[index] != null) {
                      if (pieces[index] == '\\a') {
                          var v = 'a';
                      } else if (pieces[index] == '\\0') {
                          var v = '0';
                      } else {
                          var v = pieces[index];
                      }
                      values[index] = v;
                      if (input == v) {
                          index++;
                          return true;
                      }
                  }
              }
  
              index++;
          } while (pieces[index]);
      }
  
      /**
       * Create tokens for the mask
       */
      obj.prepare = function(mask) {
          pieces = [];
          for (var i = 0; i < mask.length; i++) {
              if (mask[i].match(/[0-9]|[a-z]|\\/g)) {
                  if (mask[i] == 'y' && mask[i+1] == 'y' && mask[i+2] == 'y' && mask[i+3] == 'y') {
                      pieces.push('yyyy');
                      i += 3;
                  } else if (mask[i] == 'y' && mask[i+1] == 'y') {
                      pieces.push('yy');
                      i++;
                  } else if (mask[i] == 'm' && mask[i+1] == 'm' && mask[i+2] == 'm' && mask[i+3] == 'm') {
                      pieces.push('mmmm');
                      i += 3;
                  } else if (mask[i] == 'm' && mask[i+1] == 'm' && mask[i+2] == 'm') {
                      pieces.push('mmm');
                      i += 2;
                  } else if (mask[i] == 'm' && mask[i+1] == 'm') {
                      pieces.push('mm');
                      i++;
                  } else if (mask[i] == 'd' && mask[i+1] == 'd') {
                      pieces.push('dd');
                      i++;
                  } else if (mask[i] == 'h' && mask[i+1] == 'h' && mask[i+2] == '2' && mask[i+3] == '4') {
                      pieces.push('hh24');
                      i += 3;
                  } else if (mask[i] == 'h' && mask[i+1] == 'h') {
                      pieces.push('hh');
                      i++;
                  } else if (mask[i] == 'm' && mask[i+1] == 'i') {
                      pieces.push('mi');
                      i++;
                  } else if (mask[i] == 's' && mask[i+1] == 's') {
                      pieces.push('ss');
                      i++;
                  } else if (mask[i] == 'a' && mask[i+1] == 'm') {
                      pieces.push('am');
                      i++;
                  } else if (mask[i] == 'p' && mask[i+1] == 'm') {
                      pieces.push('pm');
                      i++;
                  } else if (mask[i] == '\\' && mask[i+1] == '0') {
                      pieces.push('\\0');
                      i++;
                  } else if (mask[i] == '\\' && mask[i+1] == 'a') {
                      pieces.push('\\a');
                      i++;
                  } else {
                      pieces.push(mask[i]);
                  }
              } else {
                  if (mask[i] == '#' && mask[i+1] == '.' && mask[i+2] == '#' && mask[i+3] == '#') {
                      pieces.push('#.##');
                      i += 3;
                  } else if (mask[i] == '#' && mask[i+1] == ',' && mask[i+2] == '#' && mask[i+3] == '#') {
                      pieces.push('#,##');
                      i += 3;
                  } else if (mask[i] == '#' && mask[i+1] == ' ' && mask[i+2] == '#' && mask[i+3] == '#') {
                      pieces.push('# ##');
                      i += 3;
                  } else if (mask[i] == '[' && mask[i+1] == '-' && mask[i+2] == ']') {
                      pieces.push('[-]');
                      i += 2;
                  } else {
                      pieces.push(mask[i]);
                  }
              }
          }
      }
  
      /** 
       * Thanks for the collaboration
       */
      obj.fromKeyCode = function(e) {
          var _to_ascii = {
              '188': '44',
              '109': '45',
              '190': '46',
              '191': '47',
              '192': '96',
              '220': '92',
              '222': '39',
              '221': '93',
              '219': '91',
              '173': '45',
              '187': '61', //IE Key codes
              '186': '59', //IE Key codes
              '189': '45'  //IE Key codes
          }
  
          var shiftUps = {
              "96": "~",
              "49": "!",
              "50": "@",
              "51": "#",
              "52": "$",
              "53": "%",
              "54": "^",
              "55": "&",
              "56": "*",
              "57": "(",
              "48": ")",
              "45": "_",
              "61": "+",
              "91": "{",
              "93": "}",
              "92": "|",
              "59": ":",
              "39": "\"",
              "44": "<",
              "46": ">",
              "47": "?"
          };
  
          var c = e.which;
  
          if (_to_ascii.hasOwnProperty(c)) {
              c = _to_ascii[c];
          }
  
          if (!e.shiftKey && (c >= 65 && c <= 90)) {
              c = String.fromCharCode(c + 32);
          } else if (e.shiftKey && shiftUps.hasOwnProperty(c)) {
              c = shiftUps[c];
          } else if (96 <= c && c <= 105) {
              c = String.fromCharCode(c - 48);
          } else {
              c = String.fromCharCode(c);
          }
  
          return c;
      }
  
      return obj;
  })();