
function validateValue(lsValue, lsRegExp, liMinLen, liMaxLen) {
  var lbValid = false;

  //minLen - 0 means no limit
  if((liMinLen == 0) || (lsValue.length >= liMinLen)) {
    //maxLen - 0 means no limit
    if((liMaxLen == 0) || (lsValue.length <= liMaxLen)) {
      //Only check RegExp with content (and meaning)
      if((lsValue != "") && (lsRegExp != "") && (lsRegExp != ".*")) {
        var lsRegurlarExpression = new RegExp(lsRegExp);
        var arrayMatchresult = lsRegurlarExpression.exec(lsValue); //Look for match within input
        try
        {
        	lbValid = (arrayMatchresult[0] == lsValue); //If match matches input...
        }
        catch(ex)
        {
        	lbValid = false; //Null was returned by exec - which is not an Array
        }
      } else {
        lbValid = true;
      }
    }
  }
  return(lbValid);
}

function augmentValue(lsValue, lsWhiteSpace, lsAlphaChars) {
  //whiteSpace: preserve | strip | collapse | replace | trim | ltrim | rtrim
  //alphaChars: preserve | strip | upper | lower | initialCaps

  if(lsValue != "" && typeof(lsValue) == "string") {
    lsValue = alphaChars(lsAlphaChars, lsValue);
    lsValue = whiteSpace(lsWhiteSpace, lsValue);
  }
  return(lsValue);
}

function whiteSpace(lsMode, lsValue) {
  //mode = preserve | strip | collapse | replace | trim | ltrim | rtrim
  var s = "";
  switch(lsMode) {
    case "preserve": //Do nothing
      s = lsValue;
    break;
    case "ltrim" : //Remove leading blanks
      s = lsValue.replace(/^\s+/,'');
    break;
    case "rtrim" : //Remove trailing blanks
      s = lsValue.replace(/\s+$/,'');
    break;
    case "trim" : //Combine ltrim and rtrim
      s = lsValue.replace(/^\s+/,'').replace(/\s+$/,'');
    break;
    case "strip" : //Remove all blanks
      s = lsValue.replace(/\s/g, "");
    break;
    case "collapse": //Remove redundant consecutive blanks and trim
      s = lsValue.replace(/\s+/g, " ").replace(/^\s+/,'').replace(/\s+$/,'');
    break;
    case "replace":  //Replace all blanks with spaces
      s = lsValue.replace(/\s/g, " ");
    break;
  }
  return(s);
}

function alphaChars(lsMode, lsValue) {
  //mode = preserve | strip | upper | lower | initialCaps
  var s = "";
  switch(lsMode) {
    case "preserve": //Do nothing
      s = lsValue;
    break;
    case "strip" : //Remove all alphaChars
      s = lsValue.replace(/[^0-9]/g, "");
    break;
    case "upper": //Any alpha must be uppercase
      s = lsValue.toUpperCase();
    break;
    case "lower": //Any alpha must be lowercase
      s = lsValue.toLowerCase();
    break;
    case "initialCaps": //Only inital letters in uppercase
      s = initialCaps(lsValue);
    break;
  }
  return(s);
}

function initialCaps(lsValue) {
  lsValue = lsValue.toLowerCase();
  var lbInWord = false;
  var s = "";
  var c = "";
  for (i = 0; i < lsValue.length; i++) {
    c = lsValue.charAt(i);
    if((!lbInWord) && (c.match(/[a-zæøå]/))) {
      s += c.toUpperCase();
      lbInWord = true;
    } else {
      s += c;
      if(c.match(/\s/)) {
        lbInWord = false;
      }
    }
  }
  return(s);
}
