// Last Updated: 2002-10-06

function ElementChecker(formName) {
	this.formName = formName;
	this.fields = new Array();
	this.debug = false;
	this.errorFunction = 'alert';
	
	this.defaultMessages = new Array();
	this.defaultMessages['text'] = this.defaultMessages['password'] = this.defaultMessages['textarea'] = new String("Please fill in the %description% field to continue.");
	this.defaultMessages['radio'] = new String("Please select a radio button from the %description% set to continue.");
	this.defaultMessages['checkbox'] = new String("Please check the %description% checkbox to continue.");
	this.defaultMessages['select-one'] = this.defaultMessages['select-multiple'] = new String("Please make a selection from the %description% menu to continue.");
	this.defaultMessages['file'] = new String("Please choose a %description% file to submit.");
}
ElementChecker.prototype.add = ecAdd;
ElementChecker.prototype.compile = ecCompile;
ElementChecker.prototype.check = ecCheck;
ElementChecker.prototype.error = ecError;

function ecAdd(field,description,msg) {
	//var i = this.fields.length;
	this.fields[field] = new Array();
	this.fields[field].name = field;
	this.fields[field].regexps = new Array();
	this.fields[field].regexpMessages = new Array();
	if (description) { this.fields[field].description = description; } else { this.fields[field].description = field; }
	if (msg) { this.fields[field].msg = msg; }
}

function ecCompile() {
	var bad = false;
	
	for (field in this.fields) {
		
		if (document.forms[this.formName] && document.forms[this.formName][field]) {
			if (document.forms[this.formName][field][0] && document.forms[this.formName][field][0].type=="radio") {
				this.fields[field].type = "radio";
			} else {
				this.fields[field].type = document.forms[this.formName][field].type;
			}
			
		} else {
			if (this.debug) { alert("Field '"+field+"' doesn't exist"); }
			bad = true;
		}
	}
	return !bad;
}

function ecError(msg) {
	var singleRE = new RegExp("'","g");
	str = this.errorFunction+"('"+msg.replace(singleRE,"\\'")+"')";
	eval(str);
}

function ecCheck() {
	if (document.forms[this.formName]) {
		if (!this.compile()) return false;
	}
	
	if (document.forms[this.formName]) {
		var frm = document.forms[this.formName];
		
		for (field in this.fields) {
			if (frm[field]) {
				var elm = frm[field];
				var type = this.fields[field].type;
				var name = this.fields[field].name;
				var desc = this.fields[field].description;
				var msg = new String(this.fields[field].msg ? this.fields[field].msg : this.defaultMessages[type]);
				msg = msg.replace("%description%",desc);
				
				switch(type) {
					case 'password':
					case 'textarea':
					case 'text':
						if (!elm.value) { this.error(msg); return false; }
						if (this.advanced) {
							if (msg = this.checkConstraint(name,elm.value)) {
								this.error(msg); return false;
							}
						}
						break;
					case 'file':
						if (!elm.value) { this.error(msg); return false; }
						break;
					case 'radio':
						var found = false;
						for (var j=0; j<elm.length; j++) {
							if (elm[j].checked) found = true;
						}
						if (!found) {
							this.error(msg);
							return false;
						}
						break;
					case 'checkbox':
						if (!elm.checked) { this.error(msg); return false; }
						break;
					case 'select-one':
					case 'select-multiple':
						var found = false;
						for (var j=0; j<elm.options.length; j++) {
							if (elm.options[j].selected && elm.options[j].value) found = true;
						}
						if (!found) {
							this.error(msg);
							return false;
						}
						break;
					case 'submit':
					case 'button':
						break;
					default:
						if (this.debug) { alert("Field type '"+type+"' not supported."); }
						break;
				}
			}
		}
		
		return true;
		
	} else {
		if (this.debug) { alert("Form '"+this.formName+"' doesn't exist"); return false; }
	}
}

// ADVANCED FUNCTIONS BELOW
// include or exclude the below portion of the file freely.

ElementChecker.prototype.advanced = true;
ElementChecker.prototype.constrain = ecConstrain;
ElementChecker.prototype.checkConstraint = ecCheckConstraint;

function ecConstrain(field,re,msg) {
	var i = this.fields[field].regexps.length;
	this.fields[field].regexps[i] = re;
	this.fields[field].regexpMessages[i] = msg ? msg : "Incorrect format for %description%";
}

function ecCheckConstraint(field,value) {
	for (var i=0; i<this.fields[field].regexps.length; i++) {
		var re = this.fields[field].regexps[i];
		if (!re.test(value)) { return this.fields[field].regexpMessages[i].replace("%description%",this.fields[field].description); }
	}
	return false;
}
