/**
 * Metoda usuwa z początku i końca białe znaki ze zmiennej _str i taki wynik zwraca
 */
function trimString(_str){
  return _str.replace(/^[\s\n\r]*|[\s\n\r]*$/g,'');
}


function size(obj,number){
  if(obj.value.length>number){
    obj.value=obj.value.substring(0,number-1);
  }
}


function cutTextByWBR(text, maxLengthaWithoutSpace){
  var patternChangedToWBR = ' [::] ';
  text = cutText(text, maxLengthaWithoutSpace, patternChangedToWBR);
  return replaceAll(text, patternChangedToWBR, '<wbr/>');
}

function cutText(text, maxLengthaWithoutSpace, spaceChars){
  var spacePattern = new RegExp('[^ \t]{'+(maxLengthaWithoutSpace+1)+',}', 'i');
  var delimiterPattern = new RegExp('([.,-/])([^.,-/])', 'i');
  while( spacePattern.test(text) ){
    var match = text.match(spacePattern);
    var toLongText = match[0];
    var textTmp = match[0];

    if( delimiterPattern.test(textTmp) ){
      textTmp = textTmp.replace(delimiterPattern, '$1'+spaceChars+'$2');
      textTmp = cutText(textTmp, maxLengthaWithoutSpace, spaceChars);
    }
    else{
      var textTmp2 = '';
      for( var i=textTmp.length; i>=0; i=i-maxLengthaWithoutSpace){
        if( textTmp.substring(i-maxLengthaWithoutSpace<0 ? 0 : i-maxLengthaWithoutSpace, i)!='' ){
          if( textTmp2!='' ){
            textTmp2 = spaceChars + textTmp2;
          }
          textTmp2 = textTmp.substring(i-maxLengthaWithoutSpace<0 ? 0 : i-maxLengthaWithoutSpace, i)+textTmp2;
        }
      }
      textTmp = trimString(textTmp2);
    }
    text = text.replace(toLongText, textTmp);
  }

  return text;
}

function replaceAll( str, pattern, replacment) {
  var idx = str.indexOf( pattern );

  while ( idx > -1 ) {
    str = str.replace( pattern, replacment );
    idx = str.indexOf( pattern );
  }

  return str;
}