var constraints = new Array();

function addRegexpConstraint(formId, id, regexp, message) {
	addConstraint(formId, function() {
			if(document.getElementById(id).value.match(regexp))
				return true;
			alert(message);
			document.getElementById(id).focus();
			return false;
		});	
}

function addCheckboxConstraint(formId, id, message, value) {
	if(value == undefined)
		value = true;
		
	addConstraint(formId, function() {
			if(document.getElementById(id).checked == value)
				return true;
			alert(message);
			document.getElementById(id).focus();
			return false;
		});	
}

function addRadioConstraint(formId, ids, message) {
	addConstraint(formId, function() {
			for(var i in ids)
				if(document.getElementById(ids[i]).checked)
					return true;
			alert(message);
			document.getElementById(ids[0]).focus();
			return false;
		});	
}

function addCustomConstraint(formId, f, message) {
	addConstraint(formId, function() {
			if(f())
				return true;
			alert(message);
			return false;
		});
}

function addConstraint(formId, constraint) {
	if(constraints[formId] == undefined)
		constraints[formId] = new Array();
	constraints[formId].push(constraint);
}

function checkFormConstraints(formId) {
	for(var i in constraints[formId])
		if(!constraints[formId][i]())
			return false;
	return true;
}
