
/*
	Title: Turing number generator  version 1.
	
	Author: Ken Jones, Digital Design Consulting

	These functions generate turing numbers for use with input forms.
	Get the user to input a turing number to prevent robots from logging in
	The idea is based on the principle of the Turing test, hence the name...
	
	version 1 implements :-
	
	getTuringNumber( count )
	
	This function generates a random number with "count" number of digits
	
	validateTuring( genTuring, userTuring, alertText )
	
	This function compares genTuring, which is generated by the getTuringNumber function 
	with userTuring which was input by the user.
	
	alertText is displayed in an alert dialog when the input is incorrect.
	
	returns true if the two turing values are the same false otherwise
	
	Usage:
	
	1. call getTuringNumber(...) and show it to the user.
	2. get the user to input the same turing number in a text box.
	3. call validateTuring(...) with both numbers and do what you depending on the outcome.
	
 */	

function getTuringNumber( count ) {	
	//  generate count random numbers from 0 - 9
	// and return then in a string
	var nString = "";
	for ( i=0; i<count; i++ ) {
		nString += Math.floor((Math.random() * 9));
	}		
	return nString;
}
function validateTuring( genTuring, userTuring, alertText ) {	
	// the turing numberis always required
	if( genTuring==null || genTuring=="" ) {
		alert("SCRIPT ERROR: Turing number is NULL");
		return false; 
	} else if ( userTuring==null || userTuring=="" ) {
		alert(alertText);
		return true;
	} else if ( genTuring == userTuring ) {
		return true;
	} else {
		alert(alertText);
		return false; 
	}
}

