/***********************************************************************\
* File:		MoJoForm.js													*
* Date:		August 17, 2007												*
* Author:	James Carpenter												*
* Purpose:	To create a form system that prevents bot spam submission 	*
\***********************************************************************/

// function to execite
var enterFunc = "";
var tempFunc = "";
var elem ;

// function to setup correct actions for the form
function makeForm(  action, method, validateFunc )
{
	// if a validation function was passed and is true or if no function passed
	if ( validateFunc == '' || (typeof validateFunc == 'function' && validateFunc() == true) )
	{
		// get a handle to the existing form
		var mojoForm = document.getElementById('mojoForm');
		
		// set up the attributes of the form
		mojoForm.action = action;
		mojoForm.method = method;
			
		// submit the form 
		mojoForm.submit();
	}
}

// capture event keypress and submit form 
function kH(e) 
{
	var pK = e ? e.which : window.event.keyCode;
	if (pK == 13) {
		if (window.event) {
			target = window.event.srcElement;
		} else if (e) {
			target = e.target;
		}
		fieldArray = document.getElementById('mojoForm').elements;
		for (i=0; i < fieldArray.length; i++)
		{
			if ((fieldArray[i].id == target.id) && (i < fieldArray.length-1))
			{
				$(fieldArray[i+1]).focus();
				break;
			}
		}
	}
	return true;
}
document.onkeypress = kH;
if (document.layers) document.captureEvents(Event.KEYPRESS);

function setElements()
{
	elem = document.getElementById('mojoForm').elements;
	for(var i = 0; i < elem.length; i++)
	{
		if( elem[i].type == "textarea" )
		{
			elem[i].onfocus = function()
			{
				tempFunc = enterFunc;
				enterFunc = "";
			}
			elem[i].onblur = function()
			{
				enterFunc = tempFunc;
				tempFunc = "";
			}
		}		
	} 	
}

