<!-- ASSESSALL Form Validation script //-->
<!-- Version 2.15 //-->
<!-- Copyright GTP iCommerce Pty. Ltd. 2000 All Rights Reserved //-->
<!-- Coded by James Nicholls, overworked keyboard monkey //-->


<!--	Examples of use
//
//			01:
//			if (AssessAll(document.Form.Email,"01@.","You have not entered a valid email address") == false) return false;
//				Returns false and displays error message if the field doesn't contain "@" and "." 
//
//			02:
//			if (AssessAll(document.Form.Price,"02$","Please do not enter a dollar sign in the price field") == false) return false;
//				Returns false and displays error message if the field contains "$" 
//
//			03:
//			if (AssessAll(document.Form.name,"0312","Please do not enter numbers in the name field (except 1 or 2)") == false) return false;
//				Returns false and displays error message if the field contains any numeric characters except for "1" and "2"
//
//			04:
//			if (AssessAll(document.Form.Price2,"04.-","Only numbers are allowed in the price field") == false) return false;
//				Returns false and displays error message if the field contains any non numeric except for "." and "-"
//
//			05:
//			AssessAll(document.Form.First_Name,"05","")
//				Returns the contents of field with alpha characters capitalised
//
//			06:
//			AssessAll(document.Form.First_Name,"06","")
//				Returns the contents of field with alpha characters changed to lower case
//
//			07:
//			AssessAll(document.Form.Data,"07*@","")
//				Returns the contents of field with all "*" and "@" characters removed
//
//			08:
//			AssessAll(document.Form.Data,"08at^@^&^and","")
//				Returns the contents of field with all "at" changed to "@" and "&" changed to "and"
//
//			09:
//			if (AssessAll(document.Form.Address,"09","Please enter your address") == false) return false;
//				Returns false and displays error message if the field is empty
//
//			10:
//			if (AssessAll(document.Form.String,"10a^bat^c","Field 'String' must contain 'a' 'bat' and 'c'") == false) return false;
//				Returns false and displays error message if the field doesn't contain "a" "bat" and "c"
//
//			11:
//			if (AssessAll(document.Form.String,"11a^bat^c","Field 'String' cannot contain 'a' 'bat' or 'c'") == false) return false;
//				Returns false and displays error message if the field contains "a" "bat" or "c"
//
//			12:
//			if (AssessAll(document.Form.ccNumber,"12","") == false) return false;
//				Returns false and displays special error messages if the field contains an invalid credit card number
//
//			13:
//			if (AssessAll(document.Form.Name,"1310","Name must be longer than 10 characters.") == false) return false;
//				Returns false and displays error message if the length of the field's content is lower than argument
//
//			14:
//			if (AssessAll(document.Form.Name,"1420","Name cannot be longer than 20 characters.") == false) return false;
//				Returns false and displays error message if the length of the field's content is higher than argument
//
//-->

<!--	Specification String 
//			First two characters are Specification Number. All other characters are
//			the Arguments.
//
//		Specification Number Details		* Indicates not yet implimented
//			01 - Must contain Arguments
//			02 - Must not contain arguments
//			03 - No numbers allowed, arguments are exceptions
//			04 - Only numbers allowed, arguments are exceptions
//			05 - Returns contents of Assess_Field as capaitalised string (No Arguments
//				 required)
//			06 - Returns contents of Assess_Field as a lower case string (No Arguments
//				 required)
//			07 - Returns contents of Assess_Field with Arguments removed
//			08 - Returns contents of Assess_Field with instances of Arguments replaced
//					(Special ^ delimited Argument Format)
//					Argument format consists of A^B^C^D where string A will be replaced
//					by string B and string C replaced by string D. Leaving out the first
//					argument of a pair (ie: ^B) is illegal. Leaving out the second
//					argument of a pair (ie: A^) will result in the first argument
//					being eliminated from the string, as if specification 07 had been
//					called.
//					Note - 08 is now completly implimented.
//			09 - Returns false if Assess_Field is empty (No Arguments Required)
//			10 - Returns false if Assess_string does not contain Argument strings
//				  (Special ^ delimited Argument Format)
//				  Allows strings as opposed to characters to be checked for, ie:
//       		  10one^two^three - The string must contain "one" and "two" and "three"
//				  Note that this is an *and* comparison, the string must contain *all* of
//				  the argument strings for a positive return
//			11 - Returns false if Assess_string *does* contain Argument strings
//				  (Special ^ delimited Argument Format)
//				  Allows strings as opposed to characters to be checked for, ie:
//                11one^two^three - The string must not contain "one" or "two" or "three"
//				  Note that this is an *or* comparison, *any* one of the strings will
//				  cause a negative return. This could be a good way to stop juvenile
//				  would be phreakers submitting forms full of obscenities :)
//			12 - LUHN Test. No arguments required
//			13 - Returns false if length of Assess_String is smaller than the Argument 
//				 (Numeric Arguments only)
//			14 - Returns false if length of Assess_String is larger than the Argument 
//				 (Numeric Arguments only)
//			15 - Returns false if function CheckEmail(emailAddress) doesn't exist and if Assess_String length is zero return false
//				 (email address)
//-->

function AssessAll(Assess_Field,Specification_String,Alert_String) {

var undefined;
if (Assess_Field == undefined) {window.alert("Error: A submitted field does not exist. \nSpecification string = " + Specification_String + ".\nAlert string = " + Alert_String + "."); return false;};

	var Assess_String = Assess_Field.value;
	var Specification = Specification_String.substring(0,2);
	var Arguments = Specification_String.substring(2,Specification_String.length);
	var No_Arguments = Arguments.length;
	var Counter = 0;

var undefined;   // This test courtesy of Bevan Holman's 'two fields with same name' error
if (Assess_String == undefined) {window.alert("Error: A submitted field does not contain valid text. \nSpecification string = " + Specification_String + ".\nAlert string = " + Alert_String + ".\n (You may have defined this field twice)"); return false;};

switch(Specification) {

	case "01": <!-- 01 - Must contain arguments //-->
	Counter = No_Arguments;
	if (Assess_String != "") {
		while (Counter > 0) {
				if (Assess_String.indexOf(Arguments.charAt(Counter-1)) == -1) {window.alert(Alert_String);return false;};
				Counter--;			
				};
			};
	break;


	case "02": <!-- 02 - Must not contain arguments //-->
	Counter = No_Arguments;
	while (Counter > 0) {
			if (Assess_String.indexOf(Arguments.charAt(Counter-1)) > -1) {window.alert(Alert_String);return false;};
			Counter--;			
			};
	break;


	case "03": <!-- 03 - Text Only //-->
	var Invalid = "01234567890";
	Counter = Assess_String.length;
	if (Assess_String != "") {
		while (Counter > 0) {
							var This_Character = Assess_String.charAt(Counter -1);
							if (Invalid.indexOf(This_Character) > -1)	{
																		if (Arguments.indexOf(This_Character) == -1) {
																												window.alert(Alert_String);return false;
																												};
																	};
							Counter--;			
							};
						};
	break;


	case "04": <!-- 04 - Numeric Only //-->
	Arguments = Arguments + "01234567890";
	Counter = Assess_String.length;
	if (Assess_String != "") {
		while (Counter > 0) {
				var This_Character = Assess_String.charAt(Counter -1);
				if (Arguments.indexOf(This_Character) == -1) {window.alert(Alert_String);return false;};
				Counter--;			
		};
	};
	break;


	case "05": <!-- 05 - Returns Capitalised String //-->
	return Assess_String.toUpperCase();
	break;


	case "06": <!-- 06 - Returns lower case String //-->
	return Assess_String.toLowerCase();
	break;


	case "07": <!-- 07 removes all instances of Arguments //-->
	var nString = "";
	var counter = 0;
	while (counter < Assess_String.length) {
			       	if (Arguments.indexOf(Assess_String.charAt(counter)) == -1) {nString = nString + Assess_String.charAt(counter)};
			 		counter++;
					};
	return nString;
	break;
			

	case "08": <!-- 08 - Replaces all instances of odd numbered Arguments with folowing even numbered arguments //-->
					var Argument_Array = Arguments.split("^");
					var nString = "";
					var Swaps = Argument_Array.length;
					if (Swaps%2 > 0) {window.alert("Error: Uneven number of swap arguments. \nSpecification = 08 \n Arguments = " + Arguments); return false;};

					var Array_Counter = 0
					var paircounter = 0;
					while (paircounter <= Swaps) 	{
													while (Assess_String.indexOf(Argument_Array[paircounter]) != -1)
																	{
																	if (Argument_Array[paircounter] == "") {window.alert("Error: Zero length first swap argument. \nSpecification = 08 \n Arguments = " + Arguments); return false;};
																 	var Instance = Assess_String.indexOf(Argument_Array[paircounter]);
																	var NumChars = Argument_Array[paircounter].length;
																	nString = Assess_String.substr(0,Instance) + Argument_Array[paircounter+1] + Assess_String.substr(Instance+NumChars, Assess_String.length);
																	Assess_String = nString;
																	};
													paircounter = paircounter +2;
													};
					if (nString.length == 0) {nString = Assess_String};
					return nString;
	break;

	case "09": <!-- 09 - Required //-->
	if (Assess_String == "") {window.alert(Alert_String);return false;};
	break;

	case "10": <!-- 10 - Must contain all arguments //-->
					var Argument_Array = Arguments.split("^");
					var nString = "";
					var Phrases = Argument_Array.length;

					var OK = true;
					var Array_Counter = 0
					while (Array_Counter < Argument_Array.length)
									{
									if (Argument_Array[Array_Counter] == "") {window.alert("Error: Zero length search argument. \nSpecification = 10 \n Arguments = " + Arguments); return false;};
								 	if (Assess_String.indexOf(Argument_Array[Array_Counter]) == -1) {window.alert(Alert_String);return false;};
									Array_Counter ++
									};
	break;

	case "11": <!-- 11 - Must not contain any of arguments //-->
					var Argument_Array = Arguments.split("^");
					var nString = "";
					var Phrases = Argument_Array.length;

					var OK = true;
					var Array_Counter = 0
					while (Array_Counter < Argument_Array.length)
									{
									if (Argument_Array[Array_Counter] == "") {window.alert("Error: Zero length search argument. \nSpecification = 11 \n Arguments = " + Arguments); return false;};
								 	if (Assess_String.indexOf(Argument_Array[Array_Counter]) > -1) {window.alert(Alert_String);return false;};
									Array_Counter ++
									};
	break;

	case "12": <!-- 12 - LUHN //-->
	  var cardnumber = Assess_String;
          var checkOK = "0123456789";
          var CrValid = true;
          var checksum=0;
          var ddigit=0;
          var kdig = 0;
          if (cardnumber.length < 13) {
                alert ('You have not entered enough digits in Credit Card Number.');
                return false;
                }
          for (i = cardnumber.length-1;  i >= 0;  i=i-1)
          {
            kdig=kdig+1;
            ch = cardnumber.charAt(i);
            if ((kdig % 2) != 0)
               checksum=checksum+parseInt(ch);
            else {
               ddigit=parseInt(ch)*2;
               if (ddigit >= 10)
                  checksum=checksum+1+(ddigit-10);
               else
                 checksum=checksum+ddigit;
        }
        for (j = 0;  j < checkOK.length;  j=j+1)
          if (ch == checkOK.charAt(j))
            break;
        if (j == checkOK.length)
        {
          alert('Please enter only digits. No dashes or non-numeric characters.');
          return false;
        }
        }       
        if ((checksum % 10) != 0){
             alert('You have entered an invalid credit card number. Please check the number for errors.');
             return false;
        }
	break;

	case "13": <!-- 13 - Minimum Length //-->
		if (isNaN(Arguments/2)) {window.alert("Error: Argument is non numeric. \nSpecification = 13 \n Arguments = " + Arguments); return false;};
    	if (Assess_String.length < Arguments) {window.alert(Alert_String);return false;};
	break;

	case "14": <!-- 14 - Maximum Length //-->
		if (isNaN(Arguments/2)) {window.alert("Error: Argument is non numeric. \nSpecification = 14 \n Arguments = " + Arguments); return false;};
    	if (Assess_String.length > Arguments) {window.alert(Alert_String);return false;};
	break;

	case "15":	// 15 -	I'm using a very simple email checking algorythm to keep download times low 
				//		The pages for the site are fairly hefty already without a whole load of format checking script
				//

	{
		if ( typeof( window[ 'checkEmailInclude' ] ) != "undefined" )
		{
			if (Assess_String.length < 1)
			{
				window.alert(Alert_String);
				return false;
			}

			return CheckEmail(Assess_String);
		}
		else
		{
			alert('Please include the check email function for this procedure to work.');
			return false;
		}
		break;
	}
	default:
	{
		window.alert("Error: Undefined Specification number " + Specification + " Danger Will Robinson! Danger!");
		break;
	}

}; <!-- end of switch statement -->

} <!-- end of code //-->


