/***********************************************
* String utilities © Tim Reed  (unless otherwise noted)
* This notice must stay intact for use
* Visit http://www.i4synthesis.com for contact info
***********************************************/

// concise writing function
function dw(str) { document.write(str); }

/**
 * Regular expressions for normalizing white space
**/
// original source: http://www.brainjar.com/dhtml/tablesort/default4.asp
function normalizeString(str) {
  if ((null==str) || ("string"!=typeof(str))) return str;
  var whtSpEnds = new RegExp("^\\s*|\\s*$", "g");
  var whtSpMult = new RegExp("\\s\\s+", "g");
  str = str.replace(whtSpMult, " ");  // Collapse any multiple whites space.
  str = str.replace(whtSpEnds, "");   // Remove leading or trailing white space.
  return str;
}
/**
 * trim whitespace at front/back of string
**/
function trim(str) {
  if ((null==str) || ("string"!=typeof(str))) return str;
  var whtSpEnds = new RegExp("^\\s*|\\s*$", "g");
  str = str.replace(whtSpEnds, "");
  return str;
}
/**
 * removes leading/trailing commas and white space
 * removes multiple commas and white space
**/
function cleanString(str) {
  if ((null==str) || ("string"!=typeof(str))) return str;
  var commaEnds = new RegExp("^,*|,*$", "g");
  str = str.replace(commaEnds, "");   // trim start/end commas
  var whtSpEnds = new RegExp("^\\s*|\\s*$", "g");
  str = str.replace(whtSpEnds, "");   // trim start/end white space
  var commaMult = new RegExp(",,+|,,+", "g");
  str = str.replace(commaMult, ",");  // collapse any multiple commas
  var whtSpMult = new RegExp("\\s\\s+", "g");
  str = str.replace(whtSpMult, " ");      // collapse any multiple white spaces
  return str;
}
/**
 * This method will replace any matching pattern in a string with a substition
 *
 * @param tgtStr      - string to modify
 * @param tgtMatch    - string to find
 * @param changeTo    - string for replacement
 * @param doNormalize - boolean value to determine if whitespace should be trimmed and reduced to 1
**/
function replaceStr(tgtStr,tgtMatch,changeTo,doNormalize) {
  if ((null==tgtMatch) || (null==tgtStr)) return tgtStr;
  if ("boolean"!=typeof(doNormalize)) doNormalize = true;
  if (null==changeTo) changeTo = "";
  var pattern = new RegExp("\\b"+tgtMatch+"\\b", "gi");
  tgtStr = tgtStr.replace(pattern,changeTo);
  if (doNormalize) return normalizeString(tgtStr);
  return tgtStr;
}
/**
 * Escape characters in string in order to use string in Regular Expression methods
**/
function cleanRegExpChar(str) { //no good way to loop through this list. Just hammer it out.
  if ((null==str) || ("string"!=typeof(str))) return str;
  str = str.replace(/\//,"\/"); //esc /
  str = str.replace(/\\/,"\\\\"); //esc \
  str = str.replace(/\./,"\\."); //esc .
  str = str.replace(/\*/,"\\*"); //esc *
  str = str.replace(/\+/,"\\+"); //esc +
  str = str.replace(/\?/,"\\?"); //esc ?
  str = str.replace(/\|/,"\\|"); //esc |
  str = str.replace(/\(/,"\\("); //esc (
  str = str.replace(/\)/,"\\)"); //esc )
  str = str.replace(/\[/,"\\["); //esc [
  str = str.replace(/\]/,"\\]"); //esc ]
  str = str.replace(/\{/,"\\{"); //esc {
  str = str.replace(/\}/,"\\}"); //esc }
  return str;
  }

/**
 * strips out escaped text strings for readability
 * converts \', \" and \n to ', " and <br/> respectively
**/
function convertEscaped(str) {
  if ((null==str) || ("string"!=typeof(str))) return str;
  str = str.replace(/\'/gi,"'");
  str = str.replace(/\"/gi,'"');
  str = str.replace(/\n/gi,"<br/>");
  return str;
}

/**
 * remove String tgt from start of String str
**/
function stripStrFromStart(str,tgt) {
  if (null==tgt) return str;
  tmpStr = str;
  if (tgt == str.substring(0,tgt.length)) {
    str = str.substring(tgt.length,str.length);
  }
  return str;
}

