ERROR_KEYUP_REGEXP	= new Array();		// RE's checked on keyup
ERROR_BLUR_REGEXP	= new Array();		// RE's checked on blur (ie- emails, and other complex ones)
ERROR_BLUR_EMPTY	= new Array();		// field checked on blur to be blank
ERROR_BLUR_DEFAULT	= new Array();		// field checked on blur to contain invalid default values

g_errors_keyup	= "";					// stores errors triggered on keyup
g_errors_blur	= "";					// stores errors triggered on blur
g_errors_empty	= "";

g_DefaultField	= new Array();


function updateInfo(){};
function updateDrop(){};


function form_events( strForm )
{
	for ( var i=0; i<document.forms[ strForm ].elements.length; i++ )
	{
		if ( document.forms[ strForm ].elements[i].type == "text" || document.forms[ strForm ].elements[i].type == "textarea" )
		{
			// Default value editing
			if ( g_DefaultField[ document.forms[ strForm ].elements[i].id ] )
			{
				document.forms[ strForm ].elements[i].onblur = function()
				{
					// Set value to default if leaving box empty
					if ( getValue( this.id ) == "" || getValue( this.id ) == "-" )
					{
						setValue( this.id, g_DefaultField[ this.id ] );
						updateInfo( this );
					}

					// check for valid default values
					ERROR_BLUR_test( this );
				}

				// Clearing default values on click for easier editing
				document.forms[ strForm ].elements[i].onclick = function()
				{
					if ( getValue( this.id ) == g_DefaultField[ this.id ] )
						setValue( this.id, "" );
				}
			}

			else
			{
				document.forms[ strForm ].elements[i].onblur = function()
				{
					// check for valid RE match
					ERROR_BLUR_test( this );
				}
			}

			// Enter text event / regular expression
			document.forms[ strForm ].elements[i].onkeyup = function()
			{
				if ( ERROR_KEYUP_test( this ) )
					updateInfo( this );
			}
		}

		else if ( document.forms[ strForm ].elements[i].type == "select-one" )
		{
			// Dropdown event
			document.forms[ strForm ].elements[i].onchange = function()
			{
				updateDrop( this );
			}
		}

		else if ( document.forms[ strForm ].elements[i].type == "checkbox" )
		{
			// Checkbox event
			document.forms[ strForm ].elements[i].onclick = function()
			{
				updateInfo( this );
			}
		}
	}
}

var REGEXP_DEFAULT				= /^[0-9A-Za-z !#$%&\'()+,\-\.:<>?]*$/;
var REGEXP_NUMBERS_ONLY			= /^([0-9]|[1-9][0-9]+)?$/;
var REGEXP_NUMBERS_NEGATIVE		= /^-?([0-9]|[1-9][0-9]+)?$/;
var REGEXP_NUMBERS_FLOAT		= /^([0-9]|[1-9][0-9]+)?(\.[0-9]*)?$/;


function ERROR_KEYUP_test( _this )
{
	var RE = ERROR_KEYUP_REGEXP[ _this.id ] ? ERROR_KEYUP_REGEXP[ _this.id ] : REGEXP_DEFAULT;

	// check blur errors if wrong
	if ( g_errors_blur.indexOf( _this.id ) != -1 || g_errors_empty.indexOf( _this.id ) != -1 )
		ERROR_BLUR_test( _this );

	if ( RE.test( _this.value ) )
	{
		g_errors_keyup = g_errors_keyup.replace( _this.id, "" );
		status_valid( _this.id );

		return ( true );
	}
	else
	{
		if ( g_errors_keyup.indexOf( _this.id ) == -1 )
			g_errors_keyup += _this.id;

		status_invalid( _this.id );

		return ( false );
	}
}

function ERROR_BLUR_test( _this )
{
	if ( ERROR_BLUR_EMPTY[ _this.id ] )
	{
		if ( getValue( _this.id ) == "" )
		{
			if ( g_errors_empty.indexOf( _this.id ) == -1 )
				g_errors_empty += _this.id;

			status_invalid( _this.id );
			return ( false );
		}
		else if ( g_errors_empty.indexOf( _this.id ) != -1 )
		{
			g_errors_empty = g_errors_empty.replace( _this.id, "" );
			status_valid( _this.id );

			return ( true );
		}
	}

	if ( ERROR_BLUR_REGEXP[ _this.id ] )
	{
		var RE = ERROR_BLUR_REGEXP[ _this.id ];

		if ( !RE.test( _this.value ) )
		{
			if ( g_errors_blur.indexOf( _this.id ) == -1 )
				g_errors_blur += _this.id;

			status_invalid( _this.id );
			return ( false );
		}
	}

	if ( !ERROR_BLUR_DEFAULT[ _this.id ] || ( ERROR_BLUR_DEFAULT[ _this.id ] && getValue( _this.id ) != g_DefaultField[ _this.id ] ) )
	{
		g_errors_blur = g_errors_blur.replace( _this.id, "" );
		status_valid( _this.id );

		return ( true );
	}
	else
	{
		if ( g_errors_blur.indexOf( _this.id ) == -1 )
			g_errors_blur += _this.id;

		status_invalid( _this.id );
		return ( false );
	}

	return ( true );
}


function status_valid( iFieldId )
{
	// hide/show error boxes
	if ( g_errors_keyup == "" )
		setDisplay( "status_invalid_keyup", "none" );

	if ( g_errors_blur == "" )
		setDisplay( "status_invalid_blur", "none" );

	if ( g_errors_empty == "" )
		setDisplay( "status_invalid_empty", "none" );

	if ( g_errors_keyup == "" && g_errors_blur == "" && g_errors_empty == "" )
	{
		setDisplay( "status_valid", "block" );
		setDisplay( "status_invalid", "none" );
	}

	_this = document.getElementById( iFieldId );

	_this.style.color = "#000";
	_this.style.backgroundColor = "#fff";
	_this.style.borderColor = "#001825";
}

function status_invalid( iFieldId )
{
	// hide/show error boxes
	if ( g_errors_keyup != "" )
		setDisplay( "status_invalid_keyup", "block" );

	if ( g_errors_blur != "" )
		setDisplay( "status_invalid_blur", "block" );

	if ( g_errors_empty != "" )
		setDisplay( "status_invalid_empty", "block" );

	if ( g_errors_keyup != "" || g_errors_blur != "" || g_errors_empty != "" )
	{
		setDisplay( "status_box", "block" );
		setDisplay( "status_valid", "none" );
		setDisplay( "status_invalid", "block" );
	}

	_this = document.getElementById( iFieldId );

	_this.style.color = "#fff";
	_this.style.backgroundColor = "#b20a10";
	_this.style.borderColor = "#b2bcc2";
}
