function CheckForm (formAttr) {
	if (formAttr == null) {
	} else if (formAttr == "[object]") { // its the actual form
		// if the form is passed by reference save it, otherwise get the reference to it
		this.form = formAttr;
	} else {
		this.form = eval("document." + formAttr);
	}
	if (this.form) {
		//this.form.onsubmit = this.validate;
		// this isn't "implemented" yet, they (the JS PEOPLE) suck
	}
	this.fields = new Array();

	this.addField 		= __addField;	
	this.addFieldCheck 		= __add_field_standard;
	this.addFieldCheckByValue 	= __add_field_by_value;
	this.addFieldCheckByMatch 	= __add_field_by_match;
	this.addFieldCheckByLength 	= __add_field_by_length;
	this.message 			= __message;
	
	// psuedo constants, hey its JavaScript, what do you want? ^-^
	this.NotEmpty = 1;
	this.CheckValue = 2;
	this.MatchValue = 3;
	this.CheckLength = 4;
	
	// need this for sure
	this.validate = __validateForm;
	
	return this;
}


// this function is internal, this never needs to be called from the page.
// it creates a new object for the array for keeping track of all the fields.

function Field (name, type, msg, val, match, len) {
	this.name = name;
	this.type = type;
	this.msg  = msg;
	this.val = val;
	this.match = match;
	this.len = len;
}

function __addField (type) {
	a = arguments;
	switch (type) {
	case this.NotEmpty:
		this.addFieldCheck (a[1], a[2]);
		break;
	case this.CheckValue:
		this.addFieldCheckByValue (a[1], a[2], a[3], a[4]);
		break;
	case this.MatchValue:
		this.addFieldCheckByMatch (a[1], a[2], a[3]);
		break;
	case this.CheckLength:
		this.addFieldCheckByLength (a[1], a[2], a[3]);
		break;
	default:
		// do nothing
	}
}

function __add_field_standard (name, msg) {  
	this.fields[this.fields.length] = new Field(name, 1, msg, "", "", "");
}

function __add_field_by_value (
	name, 	// name of first field
	value,	// name of value to match to field
	msg,	// message to display
	cond	// whether to match this value, or exclude this value
	) {
	
	if (cond == 1) cond = 3;
	else cond = 4;
	this.fields[this.fields.length] = new Field(name, cond, msg, value, "", "");
}

function __add_field_by_match (
	name, 	// name of first field
	match,	// name of value to match to field
	msg		// message to display
	) {
	this.fields[this.fields.length] = new Field(name, 2, msg, "", match, "");
}

function __add_field_by_length (
	name, 	// name of first field
	len,	// name of value to match to field
	msg		// message to display
	) {
	this.fields[this.fields.length] = new Field(name, 5, msg, "", "", len);
}

function __validateForm (fName) {
	// go through list and check to see if all the conditions are met
	if (fName != null) {
		if (fName == "[object]") {
			this.form = fName;
		} else {
			this.form = eval("document." + fName);
		}
	}
	if (this.form == "") return false;
	
	for (var x = 0 ; x < this.fields.length ; x++) {
		currentField = this.form.elements[this.fields[x].name];
		if (this.fields[x].type == 1) {
			if (currentField.value == "") {
				if (this.fields[x].msg == null) this.message (this.fields[x].name);
				else {
					alert (this.fields[x].msg);
				}
				currentField.focus();
				return false;
			}
		}
		if (this.fields[x].type == 2) {
			currentField2 = this.form.elements[this.fields[x].match];
			if (currentField.value != currentField2.value) {
				if (this.fields[x].msg == null) this.message (this.fields[x].name);
				else {
					alert (this.fields[x].msg);
				}
				currentField.focus();
				return false;
			}		
		}
		if (this.fields[x].type == 3) {
			if (currentField.value != this.fields[x].val) {
				if (this.fields[x].msg == null) this.message (this.fields[x].name);
				else {
					alert (this.fields[x].msg);
				}
				currentField.focus();
				return false;
			}		
		}
		if (this.fields[x].type == 4) {
			if (currentField.value == this.fields[x].val) {
				if (this.fields[x].msg == null) this.message (this.fields[x].name);
				else {
					alert (this.fields[x].msg);
				}
				currentField.focus();
				return false;
			}		
		}
		if (this.fields[x].type == 5) {
			if ((!currentField.length) || (currentField.length < this.fields[x].len)) {
				if (this.fields[x].msg == null) this.message (this.fields[x].name);
				else {
					alert (this.fields[x].msg);
				}
				currentField.focus();
				return false;
			}		
		}		
	}
	return true;
}

function __message (mesg) {
	alert ("Please fill in the field '" + mesg + "'");
}
