document.vForms = new Array()
var field_requirements = new Array( "alwaysTrue( this.ref() )", "isEmail( this.ref() )", "isNotEmpty( this.ref() )", "isNumeric( this.ref() )", "isWithin( this.ref(), this.p0, this.p1 )", "isNotZer0( this.ref() )" )


function append_monitored_form( formName )
{
	document.vForms[ formName ] = new vForm( formName )
}

function assign_fieldRequirements( formIDX, elemIDX, requirement_id, p0, p1 )
{
	var frmElement = document.vForms[ formIDX ].child( elemIDX )
	frmElement.p0 = p0
	frmElement.p1 = p1
//	alert( "this.validate = new Function( \"return( \"+fBody+\" );\" );" )
	frmElement.setValidation( field_requirements[ requirement_id ] )
//	frmElement.validate = new Function( "return( "+frmElement.validation+" )" )
}

function vForm( vFormName )
{
	var xObj = new Object()
	xObj.name = vFormName
	xObj.ref = new Function( "return( document."+vFormName+" );" )
	xObj.children = new DiffArray( xObj )
	xObj.append_child = new Function( "idx", "this.children.insert( idx ); this.keys[ this.keys.length ] = idx; return( this.children.objectByKey( idx ) );" )
	xObj.child = new Function( "idx", "return( this.children.objectByKey( idx ) );" )
	xObj.keys = new Array()
	xObj.length = new Function( "return( this.children.length() );" )
	xObj.submit = new Function( "if( validate_form( this ) ) this.ref().submit(); " )
	return( xObj )
}

function FormElem( frm, ElemIndex )
{
	var xObj = new Object()
	xObj.name = ElemIndex
	xObj.p0 = 0
	xObj.p1 = 0
	xObj.parent = frm
	xObj.ref = new Function( "return( this.parent.ref()."+ElemIndex+" );" )
	xObj.refPath = "return( parent.ref()."+ElemIndex+" );"
	xObj.validation = "alwaysTrue( this );"
	xObj.setValidation = new Function( "fBody", "this.validation = fBody; this.validate = new Function( \"return( \"+fBody+\" );\" );" )
	xObj.validate = new Function( "return( eval( this.validation ) );" )
	return( xObj )
}

function DiffArray( frm )
{
	var xObj = new Object()
	xObj.children = new Array()
	xObj.length = new Function( "return( this.children.length );" )
	xObj.frm = frm
	xObj.insert = new Function( "itemName", "this.children[ this.length() ] = new FormElem( this.frm, itemName );" )
	xObj.keys = new Function( "idx", "return( this.children[ idx ].name );" )
	xObj.objectByKey = new Function( "key", "for( var i = 0; i < this.children.length; ++i ) { if( this.children[ i ].name == key ) { return( this.children[ i ] ) } }" )
	return( xObj )
}

function validate_form( frm )
{
	var lr
	var elem
	var blFormValid = true
	for( var i=0; i<frm.length(); ++i )
	{
		elem = frm.child( frm.keys[ i ] )
	//	alert( elem.validation )
		if( elem.validate() )
		{
	//		alert()
			lr = document.getElementById( "c"+frm.keys[ i ] )
			if( lr )
				lr.className = "tc_white_text"
		}
		else
		{
	//		alert()
			lr = document.getElementById( "c"+frm.keys[ i ] )
			if( lr )
				lr.className = "tc_blueText"
			blFormValid = false
		}
	}
	if( !blFormValid )
		alert( "Error: Please review the items in blue." )
	
	return( blFormValid )
}

// validation procedures
function alwaysTrue( elem )
{
	return( true )
}

function isEmail( elem )
{
	if( !isNotEmpty( elem ) )
		return( false )
	var tmp = new String( elem.value )
	if( tmp.indexOf( "@" ) < 1 )
		return( false )
	var atSign = tmp.indexOf( "@" )
	if( tmp.indexOf( ".", atSign ) < atSign )
		return( false )
	
	if( tmp.indexOf( "@", atSign+1 ) > atSign )
		return( false )
	else
		return( true )
}

function isNotEmpty( elem )
{
//	alert( elem.value )
	return( elem.value.length > 0 )
}

function isNumeric( elem )
{
	if( !isNotEmpty( elem ) )
		return( false )
	var ctDecimal = 0
	var nums = new String( " 0123456789," )
//	alert( elem.value )
	var tmp = new String( elem.value )
	var charictar
	for( var i = 0; i < tmp.length; ++i )
	{
		charictar = tmp.substr( i, 1 )
		//alert( charictar )
		if( nums.indexOf( charictar, 0 ) > 0 )
		{
	//		alert( "yes" )
		}
		else if( ( i == 0 ) && charictar == "$" )
		{
		}
		else if( ( i == ( tmp.length - 1 ) ) && charictar == "%" )
		{
		}
		else if( ( charictar == "." ) && ( ctDecimal == 0 ) )
		{
			++ctDecimal
		}
		else
		{
			return( false )
		}
	}
	return( true )
}

function isWithin( elem, x, y )
{
	if( isNumeric( elem ) )
	{
	//	alert( "isNumeric" )
		return( ( elem.value >= x ) && ( elem.value <= y ) )
	}
	else
	{
		return( false )
	}
}

function isNotZero( elem )
{
	return( elem.value != "0" )
}
