//
// general purpose replacement scripts for commonly used vbscript functions
//

function vbInstr(strsearch, charsearchfor) {
	if (typeof(strsearch) == 'undefined') {
		return(0); 
	} else {
		// 1 added to index from 1 as in vb...
		return(strsearch.indexOf(charsearchfor) + 1);
	}
} 

//======================================================================

function vbSplit(s, delimiter) {
	var t = new Array();

	// spare delimiter added in front to make array index from 1 as in vb...      
	t = (delimiter + s).split( delimiter );
		
	return(t);	
}

//======================================================================

function trim(str) {
     if(!str || typeof str != 'string')
         return(null);
     return(str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ')); 
}

//======================================================================

//Usage
// alert(rand(3,6));
 function rand(l,u) // lower bound and upper bound
 {
     return(Math.floor((Math.random() * (u-l+1))+l));
 }
 
//======================================================================

function vbUbound(arr) {
	return(arr.length - 1);
}

//======================================================================

function vbReplace(str, findStr, withStr) {
	var rExp = eval("/" + findStr + "/gi");

	return(str.replace(rExp, withStr));
}

//======================================================================

function vbLeft(str, length) {
	return(str.substr(0, length));
}

//======================================================================

function vbMid(str, start, length) {
    if (length) {
        return (str.substr(start - 1, length));
    } else {
        return (str.substr(start - 1));
    }
}

//======================================================================

function vbRight(str, length) {
	return(str.substr(str.length - length, length));
}

//======================================================================

function vbIsNull(o) {
	try {
	    if ( o ) {
			// not null
			return(false);
		}
	} catch(err) {
		// null
		return(true);
	}
}

   //create 2d array
   //e.g. aMedals = Split2D( "0,Select Medal;1,Gold;2,Silver;3,Bronze" , ";" , "," )
   //     It'll be ordered the same as a getRows array, ( cols,rows ) / ( x,y ) so aMedals[1][2] = "Silver"
   //
   //s = string to split
   //r = row delimiter
   //c = column delimiter
   function Split2D( s , r , c )
   {
      var a,b,t,y,x;
      t = s.split( r );
      u = "";
      a = new Array( t.length );
      for( y = 0 ; y < t.length ; y++ )      //rows
      {
         a[y] = t[y].split( c );
      }

      return a;

      //optional: flip array into form [x][y]
      //b = new Array( a[0].length );
      //for( x = 0 ; x < a[0].length ; x++ )
      //{
      //   b[x] = new Array( a.length );
      //   for( y = 0 ; y < a.length ; y++ )
      //   {
      //      b[x][y] = a[y][x];
      //   }
      //}
      //return b;
   }
