////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Form Validation	2.0																//
//Written by Curtis Kipple															//
//April 19, 2005																	//
//																			//
//This has functions that can check fields for different situations.								//
//It is designed to supplement functions written on individual webpages.
//It was compiled from an earlier version of this script that I also wrote.							//
//																			//
//If you came accross this, you have earned your right to use and modify this script as you need.			//
//I do, however, ask that you leave my name on this.											//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//Checks if value is empty
//returns true if empty or whitespace, otherwise returns false
function IsFieldEmpty(s)
{  var i;
   if (s.length < 1) { return true; }
   else
   {  for(i=0; (i < s.length) && (s.charAt(i) < '!'); i++);
	if(s.charAt(i) < '!') { return true; }
	else { return false; }
   }
}

//Validates Email Addresses
//Checks that field is not empty, that @ and . are used with one char before, after and between both
//returns true if invalid
//returns false if valid
function IsEmailInvalid(s)
{  var i;
   var sLength;
   if (IsFieldEmpty(s)) { return true; }
   else
   {  sLength = s.length;
	for(i = 0; (i < sLength) && (s.charAt(i) != "@"); i++);
	if ( (i >= sLength) || (s.charAt(i) != "@") ) { return true; }
 	else { i += 2; }
 	while( (i < sLength) && (s.charAt(i) != ".") ) { i++; }
  	if ( (i >= sLength - 1) || (s.charAt(i) != ".") ) { return true; }
  	else { return false; }
   }
   return true;
}

//Validates Numbers
//checks that only numerical chars and ( ) - or space are used
//returns true if invalid
//returns false if valid
function IsNumberInvalid(s)
{  var i;
   if (IsFieldEmpty(s)) { return true; }
   else
   {  for (i = 0; (i < s.length); i++)
	{ if ( (s.charAt(i) < "0") || (s.charAt(i) > "9") )
	  { if( (s.charAt(i) != "(") && (s.charAt(i) != ")") && (s.charAt(i) != "-") && (s.charAt(i) != " ") )
          { return true; }
	  }
	}
   }
}

//Validates Phone Number format
//checks that Phone Number is formatted: (xxx) xxx-xxxx
//returns true if not correct
//return false if correct
function IsNumberIncorrectFormat(s)
{  if(s.length != 14) { return true; }
   if(s.charAt(0) != "(") { return true; }
   if( (s.charAt(1) < "0") || (s.charAt(1) > "9") ) { return true; }
   if( (s.charAt(2) < "0") || (s.charAt(2) > "9") ) { return true; }
   if( (s.charAt(3) < "0") || (s.charAt(3) > "9") ) { return true; }
   if(s.charAt(4) != ")") { return true; }
   if(s.charAt(5) != " ") { return true; }
   if( (s.charAt(6) < "0") || (s.charAt(6) > "9") ) { return true; }
   if( (s.charAt(7) < "0") || (s.charAt(7) > "9") ) { return true; }
   if( (s.charAt(8) < "0") || (s.charAt(8) > "9") ) { return true; }
   if(s.charAt(9) != "-") { return true; }
   if( (s.charAt(10) < "0") || (s.charAt(10) > "9") ) { return true; }
   if( (s.charAt(11) < "0") || (s.charAt(11) > "9") ) { return true; }
   if( (s.charAt(12) < "0") || (s.charAt(12) > "9") ) { return true; }
   if( (s.charAt(13) < "0") || (s.charAt(13) > "9") ) { return true; }
   return false;
}

//Checks if two fields are equal to each other
//return false if equal
//returns true if not equal
function AreFieldsDifferent(s1, s2)
{  return  (s1.localeCompare(s2) != 0);
}

//Checks if field contains a particular character
//returns true if it contains character
//return false if does not contain
function containsChar(s1, char1)
{  return  (-1 != s1.indexOf(char1));
}
