
 //manipulating build-in objects

 //- Array ------------------------------------------------------------------------------------------------------------

 function array_indexOf(item) {
  for(var i = 0;i < this.length;i++) {
   if(item == this[i]) { return i; }
  }
  return -1;
 }

 function array_last() {
  return this[this.length - 1];
 }

 function array_print() {
  var str = '';
  for(var i = 0;i < this.length;i++) {
   str += '['+i+'] => ';
   if(this[i].constructor == Array) {
    this[i].print().replace(/^([\s\S]*)\n$/, '$1').replace(/\n/g, '\n&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp&nbsp;') + '\n';
   }else{
    str += this[i]+'\n';
   }
  }
  return str;
 }

 function array_push(itm) {
  this[this.length] = itm;
 }

 Array.prototype.indexOf = array_indexOf;
 Array.prototype.last    = array_last;
 Array.prototype.print   = array_print;
 Array.prototype.push    = array_push;

 //- String -----------------------------------------------------------------------------------------------------------

 function string_token_split(len) {
  
  var arr = new Array();

  for(i = 0;i < Math.ceil(this.length/len);i++) {
   arr.push(this.substr((i*len),len));
  }

  return arr;

 }

 function string_repeat(char, num) {
  var str = '';
  for(i = 0;i < num;i++) {
   str += char;
  }
  return str;
 }

 function string_trim() {
  return this.replace (/^\s+/, '').replace (/\s+$/, '');
 }

 String.prototype.token_split = string_token_split;
 String.prototype.tokenSplit  = string_token_split;
 String.repeat                = string_repeat;
 String.prototype.trim        = string_trim;

 //- Number -----------------------------------------------------------------------------------------------------------
 
 function number_zero_prefix(len) {
  len--;
  add = "";
  while(this < Math.pow(10, len) && len > 0) {
   add += "0";
   len--;
  }
  return add + this; 
 }

 function number_from_hex(str) {

  str = str.toLowerCase(str);
  num = 0;

  for(var i = 0;i < str.length;i++) {
   num *= 16;
   switch(str.charAt(i)) {
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
     num += (str.charAt(i)*1); break;
    case 'a':
     num += 10; break;
    case 'b':
     num += 11; break;
    case 'c':
     num += 12; break;
    case 'd':
     num += 13; break;
    case 'e':
     num += 14; break;
    case 'f':
     num += 15; break;
   }
  }

  return num;

 }

 function number_to_hex() {

  num = this;
  dig = 0;
  str = ''; 

  while(num > 0) {
   dig = num % 16;
   num = num - dig;
   num = num / 16;
   switch(dig) {
    case 0: case 1: case 2: case 3: case 4:
    case 5: case 6: case 7: case 8: case 9:
     str = '' + dig + str; break;
    case 10:
     str = 'a' + str; break;
    case 11:
     str = 'b' + str; break;
    case 12:
     str = 'c' + str; break;
    case 13:
     str = 'd' + str; break;
    case 14:
     str = 'e' + str; break;
    case 15:
     str = 'f' + str; break; 
   }
  }

  return str;

 }

 function number_to_string(format) {

  var chr = ''; var str = '';


  if(typeof(format) == 'undefined') { return '' + this + ''; }
  if(format.indexOf('.') != -1) { format = format.split('.'); chr='.'; }
  if(format.indexOf(',') != -1) { format = format.split(','); chr=','; }
  if(typeof(format) != 'object') { format = new Array(format); }

  if(format[0] == '?') {
   str = '' + Math.floor(this) + '';
  }else{
   str = Math.floor(this % Math.pow(10, format[0].length)).add_zeros(format[0].length-1);
  }

  str += chr;

  if(typeof(format[1]) != 'undefined') {
   if(format[1] == '?') {
    str += ('' + (this - Math.floor(this)) + '').substr(2);
   }else{
    tmp  = '' + Math.round((this - Math.floor(this)) * Math.pow(10, format[1].length)) + '';
    tmp += String.repeat('0', format[1].length - tmp.length);
    str += tmp;
   }
  }

  return str;

 }
 
 Number.prototype.zero_prefix      = number_zero_prefix;
 Number.prototype.add_zeros        = number_zero_prefix;
 Number.prototype.leading_zero     = number_zero_prefix;
 Number.prototype.add_leading_zero = number_zero_prefix;
 Number.from_hex                   = number_from_hex;
 Number.fromHex                    = number_from_hex;
 Number.prototype.to_hex           = number_to_hex;
 Number.prototype.toHex            = number_to_hex;
 Number.prototype.to_string_format = number_to_string;
 Number.prototype.toStringFormat   = number_to_string;

 //- Date -------------------------------------------------------------------------------------------------------------

 Date.prototype.incMilliseconds = function(msec) {
  this.setTime(this.getTime()+msec);
 }

 Date.prototype.incSeconds = function(sec) {
  this.incMilliseconds(sec*1000);
 }

 Date.prototype.incMinutes = function(min) {
  this.incSeconds(min*60);
 }

 Date.prototype.incHours = function(h) {
  this.incMinutes(h*60);
 }

 Date.prototype.incDate = function(days) {
  this.incHours(days*24);
 }

 //--------------------------------------------------------------------------------------------------------------------

 //Usefull functions whigh are not build-in

 function chargen(num) {
  var char = new Array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
                       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                       'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                       'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
                       'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
                       'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                       'Y', 'Z'); 
  var str  = '';
  for(var i = 0;i < num;i++) {
   str += char[Math.round(Math.random()*(char.length-1))];
  }
  return str;
 }

 function isGregorianLeapYear(year) {
  return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
 }
 
 function removeAllChilds(node) {
  while(node.hasChildNodes()) {
   node.removeChild(node.lastChild);
  }
 }

 function sleep(ms) {
  var dte = new Date();
  var now = dte.getTime();
  do{ dte = new Date(); }while(dte.getTime() < now+ms);
 }





