function CheckForm(name) {
	this.name = name;
	this.fields = new Array();
	this.form = null;
	this.message = "Please insert your :\n";
	this.classRegular = "";
	this.classEmpty = "emptyfield";
	this.init = false
}

CheckForm.prototype.setStyles = function(regular, empty) { this.classRegular = regular; this.classEmpty = empty; }
CheckForm.prototype.setAlertMessage = function(message) { this.message = message; }
CheckForm.prototype.setName = function(name) { this.name = name;}

CheckForm.prototype.setupByName = function() {
	this.form = document.forms[this.name];
	
	for(name in this.fields)
		this.fields[name].init(this.form);
	
	checkform = this;
	//this.form.onsubmit = function() { return checkform.check(); }
}

CheckForm.prototype.setupByDOM = function(dom) {
	this.form = dom;
	
	for(name in this.fields)
		this.fields[name].init(this.form);
	
	checkform = this;
	//this.form.onsubmit = function() { return checkform.check(); }
}

CheckForm.prototype.addField = function(name, label) {
	if(this.form != null) {
		if(!this.form[name])
			return false;
	}
	
	this.fields[name] = new Input(name, this.form, label);
	if(this.addField.arguments.length > 2)
		this.fields[name].setCount(this.addField.arguments[2]);
}

CheckForm.prototype.addDependencie = function(name1, name2, type, value) {
	this.fields[name1].addDependencie(this.fields[name2], type, value);
}

CheckForm.prototype.check = function() {
	if(!this.init) {
		if(this.check.arguments.length > 0)
			this.setupByDOM(this.check.arguments[0]);
		else
			this.setupByName();
		this.init = true;
	}
	var list = '';
	for(name in this.fields) {
		input = this.fields[name];
		if(input.check())
			input.valid(this.classRegular);
		else {
			input.invalid(this.classEmpty);
			if(list.length)
				list += ', '+this.fields[name].getLabel();
			else
				list = this.fields[name].getLabel();
		}
	}
	
	if(list.length > 0) {
		alert(this.message+list)
		return false;
	}
	return true;
}


// FIELD
function Input(name, form, label) {
	this.label = label;
	this.name = name;
	this.form = form
	this.init(form);
	this.count = 1;
	this.dependencies = null;
}

Input.prototype.setCount = function(count) {
	this.count = count;
}

Input.prototype.init = function(form) {
	if(form && form[this.name]) {
		this.input = form[this.name];
		this.style_able = this.input.value != undefined || this.input.tagName == "select";
	}
	else
		this.input = null;
}

Input.prototype.invalid = function(classname) {
	if(this.style_able)
		this.input.className = classname;
}

Input.prototype.valid = function(classname) {
	if(this.style_able)
		this.input.className = classname;
}

Input.prototype.getLabel = function() {
	return this.label;
}

Input.prototype.addDependencie = function(input, type, value) {
	if(!this.dependencies)
		this.dependencies = [];
	this.dependencies.push({input: input, type: type, value: value});
}

Input.prototype.isParent = function() {
	return (input.value == undefined) ? true : false;
}

Input.prototype.check = function() {
	if(this.dependencies) {
		for(i in this.dependencies) {
			if(this.dependencies[i].type == "value") {
				if(this.dependencies[i].input.isParent()) {
					var notfound = true;
					for(var j=0; j < this.dependencies[i].input.input.length; j++) {
						if(this.dependencies[i].input.input[j].value == this.dependencies[i].value)
							if(this.dependencies[i].input.input[j].checked || this.dependencies[i].input.input[j].selected)
								notfound = false;
					}
					if(notfound) return true; // dependencie not respected, we skip this input
				}
				else if(this.dependencies[i].input.input.value != this.dependencies[i].value)
					return true;
			}
			else if(this.dependencies[i].type == "checked" && !this.dependencies[i].input.input.checked != this.dependencies[i].value)
				return true;
			else if(!this.dependencies[i].input.check())
				return true;
		}
	}
	
	this.tmp_count = this.count;
	this.countValidInputs();
	
	return this.tmp_count <= 0;
}

Input.prototype.countValidInputs = function() {
	var input = this.countValidInputs.arguments.length > 0 ? this.countValidInputs.arguments[0] : this.input;
	
	if(input.value == undefined) // multiples fields
		for(var j=0; j<input.length; j++)
			this.countValidInputs(input[j]);
	
	else if(input.type.toLowerCase() == 'checkbox' || input.type.toLowerCase() == 'radio') { // checkbox ou radio
		if(input.checked)
			this.tmp_count--;
	}
	else { // select, input, texarea
		if(input.value.replace(/^\s+/g,'').replace(/\s+$/g,'').length > 0)
			this.tmp_count--;
	}
}