
function isblank(s){
  // Returns true if the string parameter contains only space, tab or EOL chars
  var _isblank = true;
  for(var i = 0; (i<s.length) && _isblank; i++) {
    var c = s.charAt(i);
    if ((c!=' ') && (c!='\n') && (c!='\t')) _isblank = false;
  }
  return _isblank;
}

function hasValue(v) {
  // Returns true if the string parameter equals null, an empty string, a tab, EOL or space character. 
  if ((v==null) || (v=="") || isblank(v)) return false;
  else return true;
}

function countWords(e) {
  var charCount = e.value.length;
  var fullStr = e.value + " ";
  var whiteSpaceRegExp = /^[^A-Za-z0-9]+/gi;
  var nonAlphanumericRegExp = /[^A-Za-z0-9]+/gi;
  var leftTrimmedStr = fullStr.replace(whiteSpaceRegExp, "");
  var cleanedStr = leftTrimmedStr.replace(nonAlphanumericRegExp, " ");
  var splitString = cleanedStr.split(" ");
  var wordCount = splitString.length - 1;
  if (fullStr.length < 2) wordCount = 0;
  return wordCount;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function lTrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}

function rTrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


	
function isValidIpAddress(ip) {
  // Matches valid IP addresses. Quads must be in the range 0..255. No backreference is created (?:).
  if (ip.match(/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/)) return true
  else return false
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   }
   return temp;
}

/*
Alternate syntax. Updates the Javascript String proptype. 

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
Usage example of using trim, ltrim, and rtrim
var myString = " hello my name is ";
alert("*"+myString.trim()+"*");
alert("*"+myString.ltrim()+"*");
alert("*"+myString.rtrim()+"*");
*/

