/*
*	Checklist object definition
*
*	Copyright(C) 2000, Ong Kian Soon. All rights reserved.
*
*	You are grant a non-exclusive, royalty free, license to use, modify or
*	redistribute this code in any form, provided that this copyright notice
*	and license appear on all copies of the code.
*/

//
//	Checklist constructor
//
function Checklist()
{
	this.Rules = new Array();
}
new Checklist();							// fix for Netscape Navigator 3.x
Checklist.prototype.AddRule = cklAddRule;
Checklist.prototype.RemoveRule = cklRmvRule;
Checklist.prototype.FindRule = cklFindRule;
Checklist.prototype.Validate = cklValidate;

//
//	Rule constructor
//	Parameters:
//		src = source object
//		ctxt = context object
//		func = test function
//		id = identification code (optional)
//
function cklRule( src, ctxt, func, id )
{
	this.Source = src;
	this.Context = ctxt;
	this.Func = func;
	this.ID = id;
}

//
//	Add a rule
//	Parameters:
//		Refer to <cklRule>
//
function cklAddRule( src, ctxt, func, id )
{
	this.Rules[ this.Rules.length ] = new cklRule( src, ctxt, func, id );
}

//
//	Remove a rule
//	Parameters:
//		i = index of rule
//
function cklRmvRule( i )
{
	var rul = this.Rules, len = rul.length - 1;
	while( i < len )						// move trailing rules backward
	{
		rul[i] = rul[i+1];
		i++;
	}
	rul.length--;							// compact rules array
}


//
//	Find a rule
//	Parameters:
//		id = ID of rule
//	Returns:
//		Index of rule if successful, -1 otherwise
//
function cklFindRule( id )
{
	var rul = this.Rules, len = rul.length;
	for( var i = 0; i < len; i++ ) if( rul[i].ID == id ) return i;
	return -1;
}

//
//	Validate input
//	Return:
//		null if successful, failed rule reflection otherwise
//
function cklValidate()
{
	var rul = this.Rules, len = rul.length;
	for( var i = 0; i < len; i++ ) if( !rul[i].Func( rul[i] ) ) return rul[i];
	return null;
}

