//<script language=JavaScript>	
	/*
		Collection of functions for Form validation and operations
		copyright 2002 Prairie 21 Incorporated
		
		Written by Terry Bailey
	
	*/
	
/* ****************************
    Sets focus to first field element in the form:	SetFocusToFirst(oForm)
 
    INPUT:    oForm = a form object containing the desired element
    
    RETURNS:  none
    ***************************** */

	function SetFocusToFirst(oForm){
		oForm.elements(0).focus();
	}


	/* ****************************
    Display list of errors found if any: ShowError(strValue)
 
    INPUT:    strValue = a sting list of errors separated by \n new line characters
    
    RETURNS:  false if errors exist
							true if no errors exist (strValue is empty)
    ***************************** */

	function ShowError(strValue){
		if (strValue.length > 0){
			alert("The following errors were found\n\n" + strValue + "\n\nPlease correct the errors and resubmit");
			return false
		}
		return true;
	}
	
	
	/* ****************************
    check for desired value in a string:	inString(strValue,strSearchValue)
 
    INPUT:    strValue = a string value to be evaluated
							strSearchValue = the string value to find
    
    RETURNS:  true if search value has been found,
							false if search value has not been found
    ***************************** */
	
	function inString(strValue,strSearchValue){
		strValue = strValue.toLowerCase();
		strSearchValue = strSearchValue.toLowerCase();
		if (strValue.search(strSearchValue) == -1){
			return false;
		}
		return true;
	}


	/* ****************************
    Web extension validation:	hasExtension(strValue)
 
    INPUT:    strValue = a string value to be validated
    
    RETURNS:  true if a valid web extension has been found,
							false if a valid web extension has not been found
    ***************************** */
	
	function hasExtension(strValue){
		if (inString(strValue,".com") == true){
		}else if (inString(strValue,".net") == true){
		}else if (inString(strValue,".org") == true){
		}else if (inString(strValue,".edu") == true){
		}else if (inString(strValue,".biz") == true){
		}else if (inString(strValue,".info") == true){
		}else{
			return false;
		}
		return true;
	}


	/* ****************************
    Email address validation:	isEmail(strValue)
 
    INPUT:    strValue = a string value to be validated
    
    RETURNS:  true if a valid email address has been found,
							false if not a valid email address
    ***************************** */
	
	function isEmail(strValue){
		if (hasExtension(strValue) == false) {
			return false;
		}
		return inString(strValue,"@");
	}


	/* ****************************
    Required form field validation:	hasValue(oFormField)
 
    INPUT:    oFormField = a form field object to be validated
    
    RETURNS:  true if a valid entry has been found,
							false if no valid entry.  In addition, focus is moved to the form
							field and if it is a text field any existing value is selected (highligted). 
    ***************************** */
	
	function hasValue(oFormField){
		if (RightTrim(oFormField.value).length == 0){
			if (oFormField.type.toLowerCase() == "text") {
				oFormField.select();
			}
			oFormField.focus();
			return false;
		}
		return true
	}

	/* ****************************
    String trim functions:	LeftTrim(strValue), RightTrim(strValue), Trim(strValue)
 
    INPUT:    strValue = a string to be trimmed (White space removed)
    
    RETURNS:  input string with leading white space removed, trailing white space removed,
							or both respectively. 
    ***************************** */

	function LeftTrim(strValue){
		for (var i=0;(i < strValue.length) && (strValue.charAt(i)==" ");	i++){
		}
		return strValue.substring (i, strValue.length);
	}

	function RightTrim(strValue){
		for (var i=strValue.length-1;(i >= 0) && (strValue.charAt(i)==" ");	i--){
		}
		return strValue.substring (0, i+1);
	}

	function Trim(strValue){
		strValue = leftTrim(strValue);
		strValue = rightTrim(strValue);
		return strValue;
	}
	
	
	/* ****************************
    FUNCTION:	isDiscover(strValue)
 
    INPUT:    strValue = a string representing a credit card number

    RETURNS:  true, credit card number is a valid Discover card number.
							false, if not a valid Discover Card

    Sample number: 6011000000000012 (16 digits)
    ***************************** */

function isDiscover(strValue){
  strPrefix = strValue.substring(0,4);
  if ((strValue.length == 16) && (strPrefix == "6011")){
    return isCreditCard(strValue);
  }
  return false;

} // END FUNCTION isDiscover()


	/* ****************************
    FUNCTION: isMasterCard(strValue)
 
    INPUT:    strValue = a string representing a credit card number

    RETURNS:  true, credit card number is a valid MasterCard number
							false, if not a valid MasterCard

    Sample number: 5424 0000 0000 0015 (16 digits)
    ***************************** */

function isMasterCard(strValue){
  firstDigit = strValue.substring(0,1);
  nextDigit = strValue.substring(1,2);
  if ((strValue.length == 16) && (firstDigit == 5) &&
      ((nextDigit >= 1) && (nextDigit <= 5))) {
    return isCreditCard(strValue);
  }
  return false;

} // END FUNCTION isMasterCard()


	/* ****************************
    FUNCTION:  isVisa(strValue)
 
    INPUT:     strValue = a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid VISA number.
		    
	      false, otherwise

    Sample numbers: 4111 1111 1111 1111 (16 digits)
										4007 0000 0002 7		(13 digits)
    ***************************** */

function isVisa(strValue){
  if (((strValue.length == 16) || (strValue.length == 13)) &&
      (strValue.substring(0,1) == 4)){
    return isCreditCard(strValue);
  }
  return false;
}  // END FUNCTION isVisa()



	/* ****************************
    FUNCTION:  isCreditCard(strValue)
 
    INPUT:     strValue = a string representing a credit card number

    RETURNS:  true, credit card number passes the Luhn Mod-10 test
				      false, does not pass test
    ***************************** */

function isCreditCard(strValue) {
  // Encoding only works on cards with less than 19 digits
  if (strValue.length > 19)
    return (false);

  sum = 0; mul = 1; l = strValue.length;
  for (i = 0; i < l; i++) {
    digit = strValue.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} // END FUNCTION isCreditCard()

//</script>