
function Forms()
{
}

Forms.prototype._ResultType = new ResultType( 0 );
Forms.prototype._DefaultReturn = true;
Forms.prototype._Results = null;

Forms.prototype.getDefaultReturn = function()
{
	return this._DefaultReturn;
}

Forms.prototype.setDefaultReturn = function( value )
{
	this._DefaultReturn = value;
}

Forms.prototype.getResults = function()
{
	var copy = this._Results;
	this._Results = null;
	return copy;
}

Forms.prototype.checkForm = function( formObj )
{
	if( formObj == null )
		return this._DefaultReturn;
	
	var formElements = formObj.elements;
	var element = null;
	var result = null;
	var error = false;
	
	this._Results = new Array();
	//this._Results[0] = formObj;
	
	var index = 0;
	
	for( var i = 0; i < formElements.length; i++ )
	{
		element = formElements[i];
		
		if( element == null )
			continue;
		
		result = this.checkElement( element );
		
		if( result.getResult().getValue() != this._ResultType.OK )
		{
			this._Results[index] = result;
			index++;
			error = true;
		}
	}
	
	if( error == true )
		return false;
	else
		return true;
}

Forms.prototype.checkElement = function( element )
{
	if( element == null )
		return this._DefaultReturn;
	
	return this.checkRequired( element );
}

Forms.prototype.checkMinLength = function( element )
{
	var result = new ElementResult( element, new ResultType( ResultType.None ) );
	
	if( element == null )
		return result;
	
	if( element.getAttribute( "minlength" ) )
	{
		var minLength = element.getAttribute( "minlength" );
		
		if( element.value.length < minLength )
			return new ElementResult( element, new ResultType( ResultType.TooShort ) );
	}
	
	return new ElementResult( element, new ResultType( ResultType.OK ) );
}

Forms.prototype.checkRequired = function( element )
{
	var result = new ElementResult( element, new ResultType( this._ResultType.None ) );

	if( element == null )
		return result;
	
	if( element.getAttribute( "required" ) )
	{
		if( element.value == "" )
			return new ElementResult( element, new ResultType( this._ResultType.Required ) );
	}
	
	return new ElementResult( element, new ResultType( this._ResultType.OK ) );	
}
