function FormValidator(f, isEnglish) {
	this.form = f;
	this.isEnglish = 0;
	if (isEnglish) this.isEnglish = isEnglish;
	this.errors = [];
	
	if (this.isEnglish) {
		this.errorMessage = 'The following fields are missing or incorrect: \t\n';
	} else {
		this.errorMessage = 'De volgende velden zijn niet ingevuld of onjuist: \t\n';
	}
	
	this.alphabet = 'abcdefghijklmnopqrstuvwxyz'
	this.numberString = '0123456789';
	this.alphaString = 'abcdefghijklmnopqrstuvwxyz \'-';
	this.alphaNummericString = this.alphaString + this.numberString;
}

FormValidator.prototype.isEmpty = function(name) {
	// check if element has a value
	var result = false;
	var element = this.form.elements[name];
	switch (element.type) {
		case 'radio':
			result = this.isChecked(name);
			break;
		case 'checkbox':
			result = this.isChecked(name);
			break;
		case 'select':
			result = this.isSelected(name);
			break;
		default:
			if (element.value == '') result = true;
	} 
	return result;
}

FormValidator.prototype.checkChars = function(str, validCharStr) {
	// checks if a string contains of only chars of another string
	var result = true;
	for (var i=0; i<str.length; i++) {
		if (validCharStr.indexOf(str.charAt(i)) == -1) result = false;
	}
	return result;
}

FormValidator.prototype.isChecked = function(name) {
	// checks if a radiobutton or checkbox is checked
	var result = false;
	var element = this.form.elements[name];
	if (element.length) {
		for (var i=0; i<element.length; i++) {
			if (element[i].checked) result = true;
		}
	} else {
		if (element.checked) result = true;
	}
	return result;
}

FormValidator.prototype.isSelected = function(name) {
	var result = true;
	var element = this.form.elements[name];
	for (var i=0; i<element.options.length; i++) {
		if (element.options[i].selected) {
			if (element.options[i].value == '') result = false;
		}
	}
	return result;
}

FormValidator.prototype.isEmail = function(name) {
	var result = true;
	var str = this.form.elements[name].value;
	var emailFilter=/^.+@.+\..{2,3}$/;
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	if (!(emailFilter.test(str))) result = false;
	if (name.match(str)) result = false;
	return result;
}

FormValidator.prototype.isDutchPostalCode = function(name) {
	// checks if a string is a valid dutch postal code
	var result = true;
	var str = this.form.elements[name].value;
	var numbers, letters;
	if (!((str.length == 6) || (str.length == 7))) {
		result = false;
	} else {
		var numbers, letters;
		numbers = str.substring(0,4);
		(str.length == 6) ? letters = str.substring(4,6) : letters = str.substring(5,7);
		if (!this.checkChars(numbers, this.numberString)) result = false;
		if (!this.checkChars(letters.toLowerCase(), this.alphabet)) result = false;
	}
	return result;
}

FormValidator.prototype.addError = function(str) {
	// check for duplicates
	var isDuplicate = false;
	for (var i=0; i<this.errors.length; i++) {
		if (this.errors[i].toLowerCase() == str.toLowerCase()) isDuplicate = true;
	}
	if (!isDuplicate) this.errors[this.errors.length] = str;
}

FormValidator.prototype.validate = function() {
	if (this.errors.length == 0) {
		return true;
	} else {
		for (var i=0; i<this.errors.length; i++) {
			this.errorMessage += '\t- ' + this.errors[i] + '\t\n';
		}
		this.errorMessage += '\n';
		alert(this.errorMessage);
		return false;
	}
}


