
/* Function Prototypes*/

String.prototype.trim        = jf_string_trim;
String.prototype.ltrim       = jf_string_ltrim;
String.prototype.rtrim       = jf_string_rtrim;
String.prototype.right       = jf_string_right;
String.prototype.left        = jf_string_left;
String.prototype.nolf        = jf_string_nolf;
String.prototype.nocr        = jf_string_nocr;
String.prototype.nocrlf      = jf_string_nocrlf;
String.prototype.nolfcr      = jf_string_nolfcr;
String.prototype.nosp        = jf_string_nosp;
String.prototype.nosquotes   = jf_string_nosqts;
String.prototype.crbr        = jf_string_crbr;
String.prototype.brcr        = jf_string_brcr;
String.prototype.lpad        = jf_string_lpad;
String.prototype.rpad        = jf_string_rpad;
String.prototype.flat        = jf_string_flat;
String.prototype.degremlin   = jf_string_degremlin;
String.prototype.format      = jf_string_format;
String.prototype.count       = jf_string_count;
String.prototype.strreverse  = jf_string_reverse;
String.prototype.isnumber    = jf_string_isnumber;
String.prototype.isnotdigit  = jf_string_isnotdigit;

// VB FUNCTIONS EQUIVALENTS
function FORMAT(as,patt)       {return as.format(patt)}         
function CHR(ai)               {return String.fromCharCode(ai)} ; 
function LEN(as)           {return as.length}         ; 
function TRIM(as)          {return as.trim()}         ; 
function LTRIM(as)         {return as.ltrim()}        ; 
function RTRIM(as)         {return as.rtrim()}        ; 
function UCASE(as)         {return as.toUpperCase()}  ;
function LCASE(as)         {return as.toLowerCase()}  ; 
function LPAD(as,fill,cnt) {return as.lpad(fill,cnt)} ; 
function RPAD(as,fill,cnt) {return as.rpad(fill,cnt)} ; 
function FLAT(as)          {return as.flat()}         ; 
function DEGREMLIN(as,ax)  {return as.degremlin(ax)}  ; 
function COUNT(as,b)       {return as.count(b)}       ; 
function INSTR(as,b)       {return as.indexOf(b) + 1} ; 
function INSTR(as,b,i)     {return as.indexOf(b,i)}   ; 
function JOIN(ar)          {return ar.join("")}       ; 
function JOIN(ar,as)       {return ar.join(as)}       ; 
function STRREVERSE(as)    {return as.strreverse()}   ; 
function SPACE(ai)         {return Lpad(' ', ai)}     ; 
function RIGHT(as,ai)      {return as.right(ai)}      ; 
function LEFT(as,ai)       {return as.left(ai)}       ; 
function ISNUMBER(as)      {return as.isnumber()}     ; 
function NOTDIGIT(as)      {return as.isnotdigit()}   ; 
function REPLACE(as,as_rex,as_out) {return jf_string_vbreplace(as,as_rex,as_out)}
///////////////////////////////////////////////////////////////////;
function jf_string_trim()    {return this.replace(/(^ +)|( +$)/g,'')}
function jf_string_ltrim()   {return this.replace(/(^ +)/g,'')}
function jf_string_rtrim()   {return this.replace(/( +$)/g,'')}
function jf_string_right(ai) {return this.substr(this.length - ai)}
function jf_string_left(ai)  {return this.substring(0,ai)}
function jf_string_nolf()    {return this.replace(/(\n+)/g,'')}
function jf_string_nocr()    {return this.replace(/(\r+)/g,'')}
function jf_string_nosp()    {return this.replace(/ /g,'')}
function jf_string_nocrlf()  {return this.replace(/(\r+)|(\n+)/g,'')}
function jf_string_nolfcr()  {return this.replace(/(\n+)|(\r+)/g,'')}
function jf_string_nosqts()  {return this.replace(/(\'+)|(\"+)/g,'')}
function jf_string_brcr()    {return this.replace(/(\<BR\>)/gi,'\r')}
function jf_string_crbr()    {return this.replace(/\r\n|\r|\n\r|\n/gi,'<BR>')}

function jf_string_lpad(as_fill, ai_count)
{
 // lpad: left pads a string with a fill string to a count
 var li;
 var ls_pad, ls_text;

 ls_pad    = '';
 ls_text   = this.replace('','');
 ai_count *= as_fill.length
 for(li=0;li<ai_count;li=(li + as_fill.length)) {ls_pad += as_fill}

 ls_text = ls_pad + ls_text;
 return ls_text;
}

function jf_string_rpad(as_fill, ai_count)
{
 // rpad: right pads a string with a fill string to a count
 //////////////////////////////////////////////////////////
 var li;
 var ls_pad, ls_text;

 ls_pad  = '';
 ls_text = this.replace('','');
 ai_count *= as_fill.length
 for(li=0;li<ai_count;li=(li + as_fill.length)) {ls_pad += as_fill}
 ls_text += ls_pad;

 return ls_text;
}

function jf_string_flat()
{
 var ls_text;

 ls_text = this.replace(/\r\n( )+|\n\r( )+/g,'\r');
 ls_text = ls_text.replace(/( )+\r\n|( )+\n\r/g,'\r');
 ls_text = ls_text.replace(/\r( )+|\n( )+/g,'\r');
 ls_text = ls_text.replace(/( )+\r|( )+\n/g,'\r');
 return ls_text;
}

function jf_string_degremlin(as_gremlins)
{
 // Purpose:    To remove gremlins from a string
 // Parameters: Optional argument - replaces the default gremlins below
 //////////////////////////////////////////////////////////////////////

 var li;
 var ls_text, ls_gremlins, ls_rex;
 var lr;

 ls_text     = this.replace('','');

 // Default gremlins if argument not supplied
 ls_gremlins = '(\r\n)+|(\n\r)+|(\r)+|(\n)+|(\t)+|(\&)+|(\@)+|(\~)+|(\#)+|(\%)+|(\")+|(\!)+|(\\$)+|(\\*)+|(\\^)+'

 li = arguments.length;
 if (li > 0)
 {
  if (arguments[0].trim().length != -1)
  {
   lr = arguments[0].split(',');
   for (li=0;li<lr.length;li++) {lr[li] = '(' + lr[li] + ')'}
   ls_gremlins = lr.join('+|');
  }
 }

 // CREATE Regular Expression object.
 ls_rex = new RegExp(ls_gremlins,"g");

 return this.replace(ls_rex,'')
}

function jf_string_format(as_pattern)
{
 // Purpose: To format a string according to a pattern
 // Usage:   Numeric patterns, date patterns
 /////////////////////////////////////////////////////
 var ls, ls_text, ls_points, ls_num, ls_sign;
 var li, li_len, li_point;
 var lr;

 ls      = '';
 ls_sign = '';
 ls_text = this.replace(/\,/g,'');
 ls_text = ls_text.nosp();
 ls_sign = ls_text.charAt(0);
 if (ls_sign != '+' && ls_sign != '-') {ls_sign = ''}
 else                                  {ls_text = ls_text.substring(1, ls_text.length)}

 as_pattern = as_pattern.toUpperCase();

 li_point = as_pattern.indexOf('.');
 if (li_point > -1 || as_pattern.indexOf(',') > -1)
 {
  // SPLIT decimal into number + decimal
  lr = ls_text.split('.');

  if(as_pattern.indexOf(',XXX') > -1) {lr[0] = jf_string_thousands(lr[0])}
  ls = lr[0]
  if (li_point > -1) {ls = lr[0]}
  {
   as_pattern = as_pattern.substring(li_point + 1);
   li_len = as_pattern.length
   if(lr.length>1)
   {
    if (li_len <= lr[1].length) {lr[1] = lr[1].substring(0,li_len)}
    else if (li_len > lr[1].length && as_pattern.indexOf('0') != -1) {lr[1] = lr[1].rpad('0', li_len - lr[1].length)}
    ls += '.' + lr[1];
   }
  }
 }
 else {ls = ls_text}
 return ls_sign + ls;
}

function jf_string_count(as_pattern)
{
 // Purpose: To count occurences of a pattern in a string
 // Usage:   eg: HTML tag balancing
 /////////////////////////////////////////////////////
 return this.split(as_pattern).length - 1;
}

function jf_string_vbreplace(as,as_rex,as_out)
{
 // Purpose: To simulate VB replace
 //////////////////////////////////
 var ls_rex;

 ls_rex = new RegExp(as_rex,"gi");
 as = as.replace(ls_rex, as_out);
 return as;
}

function jf_string_reverse()
{
 // Purpose: To reverse a string
 ///////////////////////////////
 var  lr;

 lr = this.split('');
 lr = lr.reverse();
 return lr.join('');
}

function jf_string_isnumber()
{
 // Purpose: To check if a number
 /////////////////////////////////
 var  ls_rex, ls_text, ls_msg;

 ls_text = this.replace('','');
 ls_rex  = '[^0-9]';
 lrex    = new RegExp(ls_rex);
 return !lrex.test(ls_text);
}

function jf_string_isnotdigit()
{
 // Purpose: To check if contains non-digit
 //////////////////////////////////////////
 var  ls_rex, ls_text, ls_msg;

 ls_text = this.replace('','');
 ls_rex  = '\D';
 lrex    = new RegExp(ls_rex, "g");
 return lrex.test(ls_text);
}

function jf_string_thousands(as_no)
{
 // Purpose: To format thousands
 ///////////////////////////////
 var li, li_len;
 var ls_no, ls_comma;

 ls_comma = '';
 ls_no       = '';

 li_len = as_no.length;
 if (li_len < 4) {ls_no = as_no}
 else
 {
  for (li=li_len;li>0;li=li-3)
  {
   ls_no       = as_no.substring(as_no.length - 3, as_no.length) + ls_comma + ls_no;
   ls_comma = ',';
   as_no    = as_no.substring(0, as_no.length - 3)
   if (as_no.length < 4)
   {
    ls_no    = as_no + ls_comma + ls_no;
    as_no = '';
    li    = 0;
   }
  }
 }
 return ls_no;
}
