/*
<!--
NAME: formvalidation_quick.js
DESC: this checks only the following fields of a form to speedily check if there are any errors in user input:
	stripBlanks
	isEmptyString
	isHTML
	isInvalidEmail
	isInvalidString
	listErrors
USAGE NOTES: normally, use the full formvalidation.js script -- use the formvalidation_quick.js when speed is highly important.
*/
var errorListEmpty = "";			//	function isEmptyString(s, friendlyName)
var errorListHTML = "";				//	function isHTML(s, friendlyName)
var errorInvalidEmail = "";			//	function isInvalidEmail(s, friendlyName)
var errorListInvalid  = "";			//	function isInvalidString(s, friendlyName)
var passedTest = "passed";			//	Local Variable
var showAlert = false;				//	Local Variable

/*
Names of Functions included in this file are the following:
FUNCTION: stripBlanks - removes blank characters at end of string (used by itself or with other fxns) -- function stripBlanks(aString)
FUNCTION: isEmptyString - checks for empty fields -- function isEmptyString(s, friendlyName)
FUNCTION: isHTML - returns if html is present in the string -- function isHTML(s, friendlyName)
FUNCTION: isInvalidEmail - checks to see if it appears that this is a valid email type of string -- function isInvalidEmail(s, friendlyName)
FUNCTION: isInvalidString - checks if there is an invalid character in the string -- function isInvalidString(s, friendlyName)
FUNCTION: listErrors - this will compile the list of errors present, if any -- function listErrors()
*/


/*
FUNCTION: stripBlanks - removes blank characters at end of string
INPUT:		s - string from form field that gets tested and modified
*/	
function stripBlanks(aString)
	{aString=aString + "";
		var newString;
		var i;
		var j;
		var blank;
		blank = " ";
		newString = "";
        
		if (aString.indexOf(blank) >= 0)
			{
				for (i=0; i<aString.length; i++)
					{
						if (aString.charAt(i) != blank)
							{
								break;
							}
					}
				for (j=aString.length-1; j>=0; j--)
					{
						if (aString.charAt(j) != blank)
							{
								break;
							}
					}
				if (i == aString.length && j == -1)
					{
						newString = "";
					}
				else if (i != 0 || j != aString.length-1)
					{
						newString = stripBlanks(aString.substring(i, j+1));
					}
				else 
				    {
						newString = aString;
					}
				return newString;
			}
		return aString;	
	}


/*
FUNCTION: 	isEmptyString - checks for empty fields
INPUT:		s - string you are checking
RETURNS:		false - if empty field IS NOT found
				true - if empty field IS found
*/
function isEmptyString(s, friendlyName)
	{s.value=s.value+"";
		if (s.value == "")
			{
				errorListEmpty += "    - " + friendlyName + "\n";
         }
	}


/*
FUNCTION:	isHTML - returns if html is present in the string
INPUT:		s - string you are checking
RETURNS:	false - if HTML IS NOT found
					true - if HTML IS found
*/
function isHTML(s, friendlyName)
{s=s+"";
	s = s.toUpperCase( );
	if( (s.indexOf("<IMG") != -1) )
		errorListHTML += "    - " + friendlyName + "\n";
	else if( (s.indexOf("<A") != -1) || (s.indexOf("</A>") != -1) )
		errorListHTML += "    - " + friendlyName + "\n";
	else if( (s.indexOf("<HTML") != -1) || (s.indexOf("</HTML>") != -1) )
		errorListHTML += "    - " + friendlyName + "\n";
	else if( (s.indexOf("<HEAD") != -1) || (s.indexOf("</head>") != -1) )
		errorListHTML += "    - " + friendlyName + "\n";
	else if( (s.indexOf("<BODY") != -1) || (s.indexOf("</BODY>") != -1) )
		errorListHTML += "    - " + friendlyName + "\n";
	else if( (s.indexOf("<FORM") != -1) || (s.indexOf("</FORM>") != -1) )
		errorListHTML += "    - " + friendlyName + "\n";
	else if( (s.indexOf("<INPUT") != -1) )
		errorListHTML += "    - " + friendlyName + "\n";
	else if( (s.indexOf("<TABLE") != -1) || (s.indexOf("</TABLE>") != -1) )
		errorListHTML += "    - " + friendlyName + "\n";
	else if( (s.indexOf("<TR") != -1) || (s.indexOf("</TR>") != -1) )
		errorListHTML += "    - " + friendlyName + "\n";
	else if( (s.indexOf("<TD") != -1) || (s.indexOf("</TD>") != -1) )
		errorListHTML += "    - " + friendlyName + "\n";
	else if( (s.indexOf("<!--") != -1) || (s.indexOf("-->") != -1) )
		errorListHTML += "    - " + friendlyName + "\n";		
	else if(s.indexOf("<!") != -1)
		errorListHTML += "    - " + friendlyName + "\n";		
}


/*
FUNCTION: isInvalidString - checks if there is an invalid character in the string
INPUT:		s - string you are checking
RETURNS:	false - if string IS NOT found
					true - if string IS found
*/
function isInvalidString(s, friendlyName)
	{s=s+"";
		if ( (s.indexOf("\n") != -1) || (s.indexOf("\t") != -1) || 
				(s.indexOf("<") != -1) || (s.indexOf(">") != -1) || (s.indexOf("&") != -1) || (s.indexOf("'") != -1) || 
				(s.indexOf("*") != -1) || (s.indexOf("\|") != -1) || (s.indexOf("\!") != -1) || 
				(s.indexOf("\#") != -1) || (s.indexOf("\$") != -1) || (s.indexOf("\"") != -1) || 
				(s.indexOf("\%") != -1) || (s.indexOf("\^") != -1) || (s.indexOf("\(") != -1) || 
				(s.indexOf("\)") != -1) || (s.indexOf("\+") != -1) || (s.indexOf("\=") != -1) || 
				(s.indexOf("\?") != -1) || (s.indexOf("\:") != -1) ||
				(s.indexOf("\~") != -1) || (s.indexOf("\`") != -1) || 
				(s.indexOf("\;") != -1) )
			{
				errorListInvalid += "    - " + friendlyName + "\n";
         }
	}


/*
FUNCTION: isInvalidEmail - checks to see if it appears that this is a valid email type of string
INPUT:		s - string you are checking
RETURNS:		error - 	if no "." or "@" symbol is found
							if the "." is found to be one of the last two chars of the string
							if the "@" is found to be one of the last 4 characters of the string
							if "@" is found to be the first char of the string
*/
function isInvalidEmail(s, friendlyName)
	{s=s+"";
		var errorFound = 0;
		var foundAt = 0;
		var foundDot = 0;
		var numAts = 0;
		var numDots = 0;
		var email;
		email = s;		

email = stripBlanks(email);
		
//checks for regular invalid chars		
		if ( (email.indexOf(" ") != -1) || (email.indexOf("<") != -1) || (email.indexOf(">") != -1) || 
			(email.indexOf("&") != -1) || (email.indexOf("*") != -1) || (email.indexOf("\|") != -1) || 
			(email.indexOf("\!") != -1) || (email.indexOf("\#") != -1) || (email.indexOf("\$") != -1) || 
			(email.indexOf("\%") != -1) || (email.indexOf("\^") != -1) || (email.indexOf("\(") != -1) || 
			(email.indexOf("\)") != -1) || (email.indexOf("\+") != -1) || (email.indexOf("\=") != -1) || 
			(email.indexOf("\?") != -1) || (email.indexOf("\,") != -1) || (email.indexOf("\~") != -1) || 
			(email.indexOf("\`") != -1) || (email.indexOf("\'") != -1) || (email.indexOf("\"") != -1) || 
			(email.indexOf("\;") != -1) || (email.indexOf("\:") != -1) )
			{
				errorFound += 1;
         }
//checks the location of the "@" or "." symbols, and records how many it finds of each
      for ( var i=0; i<=email.length-1; i++ )
      	{
      		if ( email.charAt(i) == "\@" )
      			{
      				foundAt = i;
      				numAts = numAts + 1;
      			}
      		if ( email.charAt(i) == "\." )
      			{
      				foundDot = i;
      				numDots = numDots + 1;
      			}
      	}
//if the number of "@" symbols does NOT equal 1, error recorded
      if ( numAts != 1 )
      	{
				errorFound += 1;
      	}
//if the number of "." symbols equals 0, error recorded.
      if ( numDots == 0 )
      	{
				errorFound += 1;
      	}
//if "@" symbol is first in string or one of the last 4 chars in the string, error recorded
      if ( (foundAt < 1) || (foundAt > email.length-5) )
      	{
				errorFound += 1;
      	}
//if "." symbol is one of the last 2 chars of the string, error recorded
      if ( foundDot > email.length-3 )
      	{
				errorFound += 1;
      	}
//checks to see that "@" and "." are not next to each other
		if ( Math.abs(foundDot - foundAt) == 1 )
			{
				errorFound += 1;
			}
//checks to make sure that the "@" symbol comes BEFORE the "."
		if ( foundAt > foundDot )
			{
				errorFound += 1;
			}
		if ( errorFound != 0 )
			{
				errorInvalidEmail += friendlyName + "\n";
   		}
	}


/*
FUNCTION:	listErrors - this will compile the list of errors present, if any.
INPUT:		nothing
RETURNS:		a list of any errors user has made on the form it is checking
 */
function listErrors()
	{
		passedTest = "passed";
		var msg = "We can't process this form! \n Please correct the following problems:\n\n";
		
		if ( errorInvalidEmail.length > 0 )
			{
				msg += "\n";
				msg += "---------------------------------------------------------------------\n";
				msg += "Please enter a valid email for " + errorInvalidEmail + "\n";
				showAlert = true
			}
		if ( errorListInvalid.length > 0 )
			{
				msg += "\n";
				msg += "There are unusable characters in these fields:\n";
				msg += "---------------------------------------------------------------------\n";
				msg += errorListInvalid;
				showAlert = true
			}
		if ( errorListEmpty.length > 0 )
			{
				msg += "\n";
				msg += "You must fill out the following required field(s):\n";
				msg += "---------------------------------------------------------------------\n";
				msg += errorListEmpty;
				msg += "\n";
				showAlert = true
			}
		if ( errorListHTML.length > 0 )
			{
				msg += "\n";
				msg += "You can't use these HTML tags in these fields:\n";
				msg += "---------------------------------------------------------------------\n";
				msg += errorListHTML;
				msg += "\n";
				showAlert = true
			}
		if (showAlert)
			{
				passedTest = "notPassed";
				alert(msg);
			}
			
		//reset these already existing variables to nothing so that the error message will appear only once in the error alert box.
		errorListEmpty = "";
		errorListHTML = "";
		errorInvalidEmail = "";
		errorListInvalid  = "";
		showAlert = false;		//	Local Variable
	}		

	function validDate(formField,fieldLabel,required)
	{

		var result = true;

		if (required && !validRequired(formField,fieldLabel))
			result = false;
	  
		if (result)
		{
			var elems = formField.value.split("/");
			
			result = (elems.length == 3); // should be three components
			
			if (result)
			{
				var month = parseInt(elems[0]);
				var day = parseInt(elems[1]);
				var year = parseInt(elems[2]);
				result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
						 allDigits(elems[1]) && (day > 0) && (day < 32) &&
						 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
			}
			
			if (!result)
			{
				alert('Please enter a date in the format MM/DD/YY for the "' + fieldLabel +'" field.');
				formField.focus();		
			}
		} 
		
		return result;
	}

	
	function validDate1(formField,fieldLabel,required)
	{

		var result = true;

		if (required && !validRequired(formField,fieldLabel))
			result = false;
	  
		if (result && (formField.value.length > 0))
		{
			var elems = formField.value.split("/");
			
			result = (elems.length == 3); // should be three components
			
			if (result)
			{
				var month = parseInt(parseFloat(elems[0]));
				var day = parseInt(parseFloat(elems[1]));
				var year = parseInt(parseFloat(elems[2]));
				result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
						 allDigits(elems[1]) && (day > 0) && (day < 32) &&
						 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
			}
			
			if (!result)
			{
				alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
				formField.focus();		
			}
		} 
		
		return result;
	}

	function validRequired(formField,fieldLabel)
	{
		var result = true;
		
		if (formField.value == "")
		{
			alert('Please enter a value for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}
		
		return result;
	}

	function isEmailAddr(email)
	{
	  var result = false;
	  var theStr = new String(email);
	  var index = theStr.indexOf("@");
	  if (index > 0)
	  {
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
	  }
	  return result;
	}

	function allDigits(str)
	{
		return inValidCharSet(str,"0123456789");
	}

	function inValidCharSet(str,charset)
	{
		var result = true;

		// Note: doesn't use regular expressions to avoid early Mac browser bugs	
		for (var i=0;i<str.length;i++)
			if (charset.indexOf(str.substr(i,1))<0)
			{
				result = false;
				break;
			}
		
		return result;
	}

	function validEmail(formField,fieldLabel,required)
	{
		var result = true;
		
		if (required && !validRequired(formField,fieldLabel))
			result = false;

		if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
		{
			alert("Please enter a complete email address in the form: yourname@yourdomain.com");
			formField.focus();
			result = false;
		}	   
	  return result;
	}

	function validPhoneNumber(formField,fieldLabel,required) {

		var result = true;

		if (required && !validRequired(formField,fieldLabel))
			result = false;
	  
		if (result)
		{
			var elems = formField.value.split("-");
			
			result = (elems.length == 3); // should be three components
			
			if (result)
			{
				var area = parseInt(elems[0]);
				var prefix = parseInt(elems[1]);

				result = allDigits(elems[0]) && (area > 0) && allDigits(elems[1]) && (prefix > 0) && 
						 allDigits(elems[2]) && ((elems[0].length == 3) && (elems[1].length == 3) && (elems[2].length == 4));
			}
			
			if (!result)
			{
				alert('Please enter the value in the format xxx-xxx-xxxx for the "' + fieldLabel +'" field.');
				formField.focus();		
			}
		} 		
		return result;
	}
// -->
