/*
	Multi-purpose form validator
	Requires external element classes
*/

function FormValidator(formEmt){
	this.formEmt = formEmt;
	this.formInputs = null;
	this.formSelects = null;
	this.formTextareas = null;
	
	this.errorString = "Please fill out the following fields:";
	this.requiredPrefix = "- ";
	this.requiredSeparator = "\n";
	this.errorCount = 0;
	
	this.validate = function(){
		
		if(this.formEmt.tagName == "FORM"){
			var errorStr = "";
			
			this.formInputs = this.formEmt.getElementsByTagName("INPUT");
			/*
			this.formSelects = this.formEmt.getElementsByTagName("SELECT");
			this.formTextareas = this.formEmt.getElementsByTagName("TEXTAREA");
			*/
			
			
			for(var i = 0; i < this.formInputs.length; i++){
				if(this.formInputs[i].type == "text"){
					var textInput = new FETextfield(this.formInputs[i]);
					var validationResult = textInput.validate();
					
					if(validationResult == false){
						this.errorCount++;
						this.errorString += this.requiredSeparator + this.requiredPrefix + textInput.getLabel();
					}
				}
				
				if(this.formInputs[i].type == "checkbox"){
					var checkbox = new FECheckbox(this.formInputs[i]);
					var validationResult = checkbox.validate();
					
					if(validationResult == false){
						this.errorCount++;
						this.errorString += this.requiredSeparator + this.requiredPrefix + checkbox.getLabel();
					}
				}
			}
			
			if(this.errorCount > 0){
				alert(this.errorString);
				return false;
			}
			else{
				return true;
			}
		}
		else{
			return false;
		}
	}
}
