var reWhitespace = /^\s+$/
var reLetter = /^[a-zA-Z]$/
var reAlpha = /^[a-zA-Z]+$/
var reAlphanumeric = /^[a-zA-Z0-9]+$/
var reDigit = /^\d/
var reLetterOrDigit = /^([a-zA-Z]|\d)$/
var reInteger = /^\d+$/
var reSignedInteger = /^(\+|\-)?\d+$/
var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
var reSignedFloat = /^(((\+|\-)?\d+(\.\d*)?)|((\+|\-)?(\d*\.)?\d+))$/
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = " \t\n\r"
var defaultEmptyOK = false
var reEmail = /^.+\@.+\..+$/

function isEmpty(s)
{ return ((s == null) || (s.length == 0)) }

function isWhitespace (s)
{ return (isEmpty(s) || reWhitespace.test(s)) }

function stripCharsInRE (s, bag)
{ return s.replace(bag, "") }

function stripCharsInBag (s, bag)
{   var i
    var returnString = ""

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i)
        if (bag.indexOf(c) == -1)	{ returnString += c }
    }

    return returnString
}

function stripCharsNotInBag (s, bag)
{   var i
    var returnString = ""

    for (i = 0; i < s.length; i++)
	{
	    var c = s.charAt(i)
		if (c.match(bag) != null)
		{returnString += c }
        }

    return returnString
}

function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") {
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") {
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) {
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue;
}

function stripWhitespace (s)
{ return stripCharsInBag (s, whitespace) }

function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) { return true }
    }
    return false
}

function stripInitialWhitespace (s)
{   var i = 0

    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       { i++ }

    return s.substring (i, s.length)
}

function isLetter (c)
{ return reLetter.test(c) }

function isDigit (c)
{ return reDigit.test(c) }

function isLetterOrDigit (c)
{ return reLetterOrDigit.test(c) }

function isInteger (s)
{   var i
    if (isEmpty(s))
       if (isInteger.arguments.length == 1) { return defaultEmptyOK }
       else { return (isInteger.arguments[1] == true) }

    return reInteger.test(s)
}

function isSignedInteger (s)
{  if (isEmpty(s))
	{
		if (isSignedInteger.arguments.length == 1)
			{ return defaultEmptyOK }
      else
      	{ return (isSignedInteger.arguments[1] == true) }
	}
	else
	{	return reSignedInteger.test(s) }
}

function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK

    if (isPositiveInteger.arguments.length > 1)
       { secondArg = isPositiveInteger.arguments[1] }

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) )
}

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK

    if (isNonnegativeInteger.arguments.length > 1)
       { secondArg = isNonnegativeInteger.arguments[1] }

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) )
}

function isNegativeInteger (s)
{   var secondArg = defaultEmptyOK

    if (isNegativeInteger.arguments.length > 1)
       { secondArg = isNegativeInteger.arguments[1] }

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) < 0) ) )
}

function isNonpositiveInteger (s)
{   var secondArg = defaultEmptyOK

    if (isNonpositiveInteger.arguments.length > 1)
       { secondArg = isNonpositiveInteger.arguments[1] }

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) <= 0) ) )
}

function isFloat (s)
{  if (isEmpty(s))
	{
		if (isFloat.arguments.length == 1) { return defaultEmptyOK }
      else { return (isFloat.arguments[1] == true) }
   }
   else
   {
    	return reFloat.test(s)
   }
}

function isSignedFloat (s)
{  if (isEmpty(s))
	{
   	if (isSignedFloat.arguments.length == 1) { return defaultEmptyOK }
   	else { return (isSignedFloat.arguments[1] == true) }
   }
   else
   {
       return reSignedFloat.test(s)
   }
}

function isAlpha (s)
{   var i

    if (isEmpty(s))
    {
       if (isAlpha.arguments.length == 1) { return defaultEmptyOK }
       else { return (isAlpha.arguments[1] == true) }
	 }
    else {
       return reAlpha.test(s)
    }
}

function isAlphanumeric (s)
{   var i

    if (isEmpty(s))
    {
       if (isAlphanumeric.arguments.length == 1) { return defaultEmptyOK }
       else { return (isAlphanumeric.arguments[1] == true) }
	 }
    else {
       return reAlphanumeric.test(s)
    }
}

function reformat (s)
{   var arg
    var sPos = 0
    var resultString = ""

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i]
       if (i % 2 == 1) { resultString += arg }
       else {
           resultString += s.substring(sPos, sPos + arg)
           sPos += arg
       }
    }
    return resultString
}

function warnEmpty (theField, s)
{   theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}

function warnInvalid (theField, s)
{
   if (!(theField == "")) {
     theField.focus()

     if (!((theField.type == "select") || (theField.type == "textarea")  || (theField.type == "hidden") || (theField.type == "select-one") || (theField.type == "checkbox")))
     {
       	theField.select()
     }
   }
   alert(s)
   return false
}

function checkString (theField, s, emptyOK)
{
    if (checkString.arguments.length == 2) { emptyOK = defaultEmptyOK }
    if ((emptyOK == true) && (isEmpty(theField.value))) { return true }
    if (isWhitespace(theField.value))
       { return warnEmpty (theField, s) }
    else { return true }
}

function isSSN (s) {
	 if (isEmpty(s))
       if (isSSN.arguments.length == 1) return defaultEmptyOK;
       else return (isSSN.arguments[1] == true);
    return (isInteger(s) && s.length == 9)
}

function isEmail (s) {
    if (isEmpty(s))
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    return reEmail.test(s)
}

function isURL(s) {
	 if (isEmpty(s))
       if (isURL.arguments.length == 1) return defaultEmptyOK;
       else return (isURL.arguments[1] == true);
    return (s.substr(0,7) == "http://") && (s.indexOf(" ") < 0)
}
