﻿/*

	Simple JavaScript functionality to determine difference
	between page load time and form submit time.  If less than
	a predefined amount (in seconds) will display message to user.
	
	To implement, place a reference to this javascript file in the head of the calling page
	and apply the following to your command/submit button:
	
	OnClientClick="javascript: NoBotSubmit('IDOfHiddenControl');"
	
	Requirements: 
	
	Must have a hidden form field on your form (it's ID will be passed-into NoBotSubmit) as the
	following code will populate that field with true/false (true if suspected of bot submission)
	Your codebehind will have to check this value (if valid value will be 'NoBotIsValid')

	Author:	Jay Dobson
	Date:	June 26, 2008	
	
*/

// Variables
var loadTime;
var maxDiff = 3 // Maximum difference (seconds) allowed between submit time and load time

// User is submitting form.  Comparing load time to submit time
function NoBotSubmit(hiddenField) {

	var diff = 0;
	var submitTime = new Date();
	var hdnNoBot = document.getElementById(hiddenField);
	
	try {		
		diff = Math.ceil( ( submitTime - loadTime ) / 1000 );
	}
	
	catch (err) {
		hdnNoBot.value = '';
	}

	// Resetting load time
	loadTime = new Date();

	if ( diff < maxDiff ) {
		hdnNoBot.value = '';
		return false;
	}
	
	else {
		hdnNoBot.value = 'NoBotIsValid';
	}	
	
}

// Initializing loadTime
loadTime = new Date();