// ------------------------------------------------------------------

// Advanced field validator

// ------------------------------------------------------------------

/*

  	Usage example:
  
  		validateField(theForm.Thing,"Thing","1234567890","digits",3,6) 

*/


function validateField(elementValue,elementName,validChars,charType,minLen,maxLen)
{
	var allValid = true ;
	
	// alert("Validating [" + elementName + "]");
	
	if ( minLen != maxLen )
	{
		alertSizeAdvice = "it should consist of " + minLen + " to " + maxLen + " " + charType ;
	}
	else
	{
		alertSizeAdvice = "it should consist of exactly " + minLen + " " + charType ;	
	}
		
	
	// is it completely missing?
	if ( minLen > 0 && elementValue.length == 0 )
	{
		alert( "Please enter the " + elementName + ": " + alertSizeAdvice );
		allValid = false; 
	}
	else
	{
		// is it too small?
		if ( elementValue.length < minLen )
		{
			alert( "Too few characters in " + elementName + ": " + alertSizeAdvice ) ;
			allValid = false; 
		}
		else
		{
			// is it too big?
			if ( elementValue.length > maxLen )
			{
				alert( "Too many characters in " + elementName + ": " + alertSizeAdvice ) ;
				allValid = false; 
			}	
			else
			{
				for ( i = 0; i < elementValue.length; i++ )
				{
					ch = elementValue.charAt( i );

					for ( j = 0; j < validChars.length; j++ )
						if ( ch == validChars.charAt( j ) )
							break;

					if ( j == validChars.length )
					{
						allValid = false;
						break;
					}
				}
				
				if ( allValid == false ) 
				{
					alert("Incorrect typing for" + elementName + ": " + alertSizeAdvice ) ;
				}
			
			}
				
		}
	
	}
		
	
	return allValid ;

}


// ------------------------------------------------------------------



