//
// FormChecker class
// =================
//
//   (C) 1999/2000 Mark van 't Zet (mark@base.nl)
//   Last updated 27/7/2000: bugfix for NS3
//   NOTE: You may use this code only if you:
//      - do not change it
//      - include these remarks
//
// WHAT IT IS
// ==========
//
//   Class that checks fields in HTML forms with certain constraints.
//   If constraint is violated, a JavaScript alert pops up with an error message
//   and focus is given to the corresponding field. Optionally, the field can be
//   highlighted with a certain color or all constrainted fields can be highlighted.
//   Constraints are sorted on form order when added to the FormChecker object, so
//   fields are always checked from top to bottom (or to be accurate: the order in which
//   they appear in the HTML source).
//
// HOW YOU USE IT
// ==============
//
// - Name the form in HTML, e.g. <form name="checkme" method="POST" action="http etc..">
// - In script in the HEAD-section of HTML write:
//
//   var myFC;
//
//   function initFC() {
//     myFC = new FormChecker(document.forms['checkme']); //form object to be checked!
//     myFC.highlightColor = "#ffe0e0";
//     myFC.highlightAll = false;
//     myFC.addConstraint("email", "mustcontain", "@", ".", "E-mail address doesn't contain at or dot!");
//     myFC.addConstraint("zipcode", "minlength", 6, "Zipcode must have length of at least 6!");
//     myFC.addConstraint("zipcode", "maxlength", 7, "Zipcode must have length of at most 7!");
//     myFC.addConstraint("name", "isalpha", "Name may only contain letters!");
//     myFC.addConstraint("age", "isnum", "Age must be a number!");
//     myFC.addConstraint("password", "isalphanum", "Password may only contain letters or digits!");
//     myFC.addConstraint("password", "maynotcontain", " ", "Password may only contain letters or digits!");
//     myFC.addConstraint("country", "mustselect", " ", "Please select a country from the pulldown list!");
//   }
//
// - Add the initialization function call to the onLoad property in the BODY tag, e.g.
//     <BODY onLoad="initFC()" etc..>
// - In the form, use
//     <input type="button" onClick="myFC.submit()" value="Submit this form">
//
// REMARKS:
// ========
//
// - Use the reset() method to clear all constraints. This can be used to dynamically
//   add constraints to your form.
// - The check() method can be used to check the form without submitting it.
// - If the property highlightColor is empty (i.e. "") nothing is highlighted.
// - The "isalpha" constraint allows spaces in the field.
// - The fieldnames are case sensitive! (At least, in IE5)
// - On the other hand, the constraint names (e.g. "mustcontain" and "isalpha") are.

function FormChecker(f) {
	if (f) {
		this.form = f;
		this.constraintList = new Array();
		this.highlightColor = "";
		this.highlightAll = false;
		this.addConstraint = FC_addConstraint;
		this.comesBefore = FC_comesBefore;
		this.submit = FC_submit;
		this.check = FC_check;
		this.reset = FC_reset;
		this.highlight = FC_highlight;
		return true;
	} else {
		alert("FormChecker: Illegal form");
		return false;
	}
}

function FC_addConstraint(fname, constraint) {
	if (!this.form.elements[fname]) {
		alert("FormChecker: Field name '" + fname + "' does not exist");
		return false;
	}
	var c = new Constraint(fname, constraint, arguments[arguments.length-1]);
	if (c) {
		for (var i = 0; i < arguments.length - 3; i++) {
			c.addParameter(arguments[i+2]);
		}
		// Add constraint in form order
		for (var i = 0; i < this.constraintList.length; i++) {
			if (this.comesBefore(fname, this.constraintList[i].fieldName)) {
				// Move rest from here
				for (var j = this.constraintList.length; j > i; j--) {
					this.constraintList[j] = this.constraintList[j - 1];
				}
				this.constraintList[i] = c;
				return true;
			}
		}
		this.constraintList[this.constraintList.length] = c;
		return true;
	} else {
		alert("Could not create constraint!");
		return false;
	}
}

function FC_comesBefore(inputName1, inputName2) {
	inputName1 = inputName1.toUpperCase();
	inputName2 = inputName2.toUpperCase();
	for (var i = 0; i < this.form.elements.length; i++) {
		if (this.form.elements[i].name) {
			var currentInputName = this.form.elements[i].name.toUpperCase();
			if (currentInputName == inputName1)
				return true;
			if (currentInputName == inputName2)
				return false;
		}
	}
	return false;
}


function FC_submit() {
	if (this.check()) {
		this.form.submit();
		return false;
	}
	return true;
}

function FC_check() {
	for (var i = 0; i < this.constraintList.length; i++) {
		if (!this.constraintList[i].check(this.form)) {
			var field = this.form.elements[this.constraintList[i].fieldName];
			if (field && field.focus) field.focus();
			if (window.scrollBy) window.scrollBy(0, -60);
			if (this.highlightColor != "" && field.style) {
				if (this.highlightAll) {
					this.highlight();
				} else {
					field.style.backgroundColor = this.highlightColor;
				}
			}
			alert(this.constraintList[i].errorMsg);
			return false;
		}
	}
	return true;
}

function FC_highlight() {
alert('bla');
/*
	for (var i = 0; i < this.constraintList.length; i++) {
		var field = this.form.elements[this.constraintList[i].fieldName];
		if (this.highlight != "" && field.style) {
			field.style.backgroundColor = this.highlightColor;
		}
	}
*/
}

function FC_reset() {
	for (var i = 0; i < this.constraintList.length; i++) {
		var field = this.form.elements[this.constraintList[i].fieldName];
		if (this.highlightColor != "" && field.style) {
			field.style.backgroundColor = "";
		}
	}
	this.constraintList = new Array();
}

function Constraint(fieldname, constraint, errorMsg) {
	this.fieldName = fieldname;
	this.constraintType = constraint.toUpperCase();
	this.errorMsg = errorMsg;
	this.parameters = new Array();
	this.addParameter = C_addParameter;
	this.check = C_check;
	this.isNum = C_isNum;
	this.isAlpha = C_isAlpha;
	this.isAlphaNum = C_isAlphaNum;
}

function C_addParameter(param) {
	this.parameters[this.parameters.length] = param;
}

function C_check(f) {
	var field = f.elements[this.fieldName];
	if (!field)
		return true;
	if (this.constraintType == "MUSTSELECT") {
		if (field.selectedIndex == 0) {
			return false;
		}
	} else if (this.constraintType == "MUSTCONTAIN") {
		for (var i = 0; i < this.parameters.length; i++) {
			if (field.value.indexOf(this.parameters[i]) < 0) {
				return false;
			}
		}
		return true;
	} else if (this.constraintType == "MINLENGTH") {
		return (field.value.length >= parseInt(this.parameters[0]))
	} else if (this.constraintType == "MAXLENGTH") {
		return (field.value.length <= parseInt(this.parameters[0]))
	} else if (this.constraintType == "MAYNOTCONTAIN") {
		for (var i = 0; i < this.parameters.length; i++) {
			if (field.value.indexOf(this.parameters[i]) >= 0) {
				return false;
			}
		}
		return true;
	} else if (this.constraintType == "ISALPHA") {
		return (this.isAlpha(field.value))
	} else if (this.constraintType == "ISNUM") {
		return (this.isNum(field.value))
	} else if (this.constraintType == "ISALPHANUM") {
		return (this.isAlphaNum(field.value))
	} else if (this.constraintType == "EVAL") {
		return (eval(this.parameters[0]))
	}
	return true;
}

function C_isAlphaNum(s) {
	var c;
	for (var i = 0; i < s.length; i++) {
		c = s.substr(i, 1);
		if (!C_isAlpha(c) && !C_isNum(c)) return false;
	}
	return true;
}

function C_isAlpha(s) {
	s = s.toUpperCase();
	var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
	for (var i = 0; i < s.length; i++) {
		if (alphabet.indexOf(s.substr(i, 1)) == -1) return false;
	}
	return true;
}

function C_isNum(s) {
	var digits = "01234567890";
	for (var i = 0; i < s.length; i++) {
		if (digits.indexOf(s.substr(i, 1)) == -1) return false;
	}
	return true;
}


// END OF CLASSES

