// This file contains the data validation JavaScript functions
// It is included in the asp pages with forms that need these
// data validation routines.

// DEFINE VARIABLES

// whitespace characters
var whitespace = " \t\n\r";

var defaultEmptyOK = false;

/****************************************************************/
/* PURPOSE:  Since we are using the single tick mark as the
	string delimiter to construct our SQL queries, a string with
	a tick mark in it will cause a SQL error.  Therefore we replace
	all "'" with "''", which eliminates the possibility of a SQL error.
*/
function sqlSafe (s)
{
	var new_s = s;
	new_s = replaceAll (new_s, "'", "|");
	new_s = replaceAll (new_s, "|", "''");
	new_s = replaceAll (new_s, "\"", "|");
	new_s = replaceAll (new_s, "|", "''");
	return new_s;
}

/****************************************************************/
function makeSafe (i)
{
	i.value = sqlSafe (i.value);
}

/****************************************************************/
// Check whether string s is empty.
function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

/****************************************************************/
// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s)
{   
    var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
	    // Check that current character isn't whitespace.
	    var c = s.charAt(i);

	    if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

/****************************************************************/
// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

/****************************************************************/

// Right trims the string...  Useful for SQL datatypes of CHAR

function RTrim(strTrim)
{
	var str = new String(strTrim);
	var i = 0;
	var c = "";
	var endpos = 0

	for (i = str.length; i >= 0 && endpos == 0; i = i - 1) {
		c = str.charAt(i);
		if (whitespace.indexOf(c) == -1)
			endpos = i;
	}

	return str.substring(0,endpos+1);
}

/**************************************************************
 LTrim: Removes leading blanks from a string.

 Parameters:
	- str: text string we want to Left Trim

 Returns: left trimmed string
***************************************************************/
function LTrim(str)
{
  var whitespace = new String(" \t\n\r");
  var s = new String(str);

  if (whitespace.indexOf(s.charAt(0)) != -1) {
    // We have a string with leading blank(s)...

    var j=0, i = s.length;

    // Iterate from the far left of string until we
    // don't have any more whitespace...
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
      j++;

    // Get the substring from the first non-whitespace
    // character to the end of the string...
    s = s.substring(j, i);
  }
  return s;
}

/**************************************************************
 Trim: Removes leading and trailing blanks from a string.

 Parameters:
	- str: text string we want to Trim

 Returns: trimmed string
***************************************************************/
function Trim(str)
{
  return RTrim(LTrim(str));
}

/****************************************************************/

// This function determines if the string passed in is a valid
// US zip code.  It accepts either ##### or #####-####.  If the
// string is valid, it returns true, else false.

function isZipcode(strZip)
{
	var s = new String(strZip);

	if (s.length != 5 && s.length != 10)
		// inappropriate length
		return false;


	for (var i=0; i < s.length; i++)
		if ((s.charAt(i) < '0' || s.charAt(s) > '9') && s.charAt(i) != '-')
			return false;

	return true;
}

/****************************************************************/
// This function ensures that a field is less than or equal to the
// Length passed in.  You must call this function with the element
// name in your form (for example: "ForceLength(document.forms[0].txtElement)"
// as opposed to "ForceLength(document.forms[0].txtElement.value)"
// If the field's value is too large, an error message is displayed
// and false is returned, else true is returned.

function ForceLength(objField, nLength, strWarning)
{
	var strField = new String(objField.value);

	if (strField.length > nLength) {
		alert(strWarning);
		return false;
	} else
		return true;
}


/**************************************************************
 FormHasElement: checks a form for the existence of an element

 Parameters:
	- frm: form object to test 
	- ename: element name

 Returns: true if element is found, else falue
***************************************************************/
function FormHasElement(frm, ename)
{
  //determine if form frm has an element on it named ename
  var found = false
  for (var i=0;i<frm.length;i++)
  {
    if (frm.elements[i].name == ename)
    {
      found = true;
      break;
    }
  }
 return found;
}

//-------------------------------------------------------------------------
function allDigits(str)
{
  return inValidCharSet(str,"0123456789");
}

//-------------------------------------------------------------------------
function inValidCharSet(str,charset)
{
  var result = true;
  
  for (var i=0;i<str.length;i++)
    if (charset.indexOf(str.substr(i,1))<0)
    {
      result = false;
      break;
    }
  
  return result;
}
