/* Form Validator Javascript */

	Dependancy = function(n1, n2) {
		this.n1 = n1;
		this.n2 = n2;
		this.evaluated = false;
	}

	FormValidator = function(formName,language) {
		this.formObj = document[formName];
		this.specialEnum = new Array("email","phone","zip","digit","customerID");
		this.errorData = new String();
		this.dependancies = new Array();
		this.language = (language) ? language : "de";
		this.errorMessage = (this.language=="de") ? "Bite f\374llen Sie die folgenden Felder aus:" : "The following Fields need to be adjusted: ";

	}
	
	FormValidator.prototype.outputSelectRequired = function(fieldName) {
		return (this.language=="de") ? "Bitte "+fieldName+" ausw\344hlen" : "Please select a "+fieldName;
	}
	
	FormValidator.prototype.outputTextRequired = function(fieldName) {
		return (this.language=="de") ? "Bitte ausf\374llen" : "Required field";
	}
	
	FormValidator.prototype.isEnumType = function(type) {
		for (var i=0; i < this.specialEnum.length; i++) {
			if (this.specialEnum[i]==type) return true;
		}
	 return false;
	}
	
	FormValidator.prototype.getDependancyIndex = function(name) {
		for (var i=0; i<this.dependancies.length; i++) {
			if (this.dependancies[i].n1==name || this.dependancies[i].n2==name) return i;
		}
	}
	
	FormValidator.prototype.setDependantChecked = function(idx) {
		this.dependancies[idx].evaluated=true;	
	}
	
	FormValidator.prototype.clearDependancyChecks = function() {
		for (var i=0; i<this.dependancies.length; i++) {
			this.dependancies[i].evaluated=false;
		}
	}
	
	FormValidator.prototype.isDependantChecked = function(idx) {
		return this.dependancies[idx].evaluated;
	}
	
	FormValidator.prototype.hasDependancyError = function(name) {
		var index = this.getDependancyIndex(name);
		
		if (!this.isDependantChecked(index)) {
			this.setDependantChecked(index);
			return (this.formObj[this.dependancies[index].n1].value=="" && this.formObj[this.dependancies[index].n2].value=="");
		}
	}
	
	FormValidator.prototype.addSpecial = function(name, type) {
		if (this.isEnumType(type)) {
			this.formObj[name].title = "special";
			this.formObj[name].setAttribute("specialType",type);
		}
	}
	
	FormValidator.prototype.removeSpecial = function(name, type) {
		if (this.isEnumType(type)) {
			this.formObj[name].title = "";
		}
	}

	FormValidator.prototype.addDependant = function(n1, n2) {
		this.formObj[n1].title = "dependant";
		this.formObj[n2].title = "dependant";
		this.dependancies[this.dependancies.length] = new Dependancy(n1, n2);
	}
				
	FormValidator.prototype.validateForm = function() {
		this.errorData = new String();
		for (var i=0; i<this.formObj.elements.length; i++) {
			var field = this.formObj.elements[i];
			switch (field.type) {
				case 'text' 		: switch(field.title) {
										case 'required' : if (field.value.length==0) {
															this.errorData+= "\n\t["+field.getAttribute("label")+"]: "+this.outputTextRequired(field.getAttribute("label"));
														  }
														  break;
										case 'special'	: this.errorData+=eval("this.verify_"+field.getAttribute("specialType")+"('"+field.name+"')");
														  break;
										case 'dependant' : if (this.hasDependancyError(field.name)) {
																				var n1 = this.dependancies[this.getDependancyIndex(field.name)].n1;
																				var n2 = this.dependancies[this.getDependancyIndex(field.name)].n2;
																				this.errorData+="\n\t["+this.formObj[n1].getAttribute("label")+"] or ["+this.formObj[n2].getAttribute("label")+"]: At least 1 is required."; 
																			}
																			break;  
									  }
									  break;
				case "select-one"	: if (field.title=='required') {
										if (field.options[field.options.selectedIndex].value=="null" || field.options[field.options.selectedIndex].value.length==0) {
											this.errorData+="\n\t["+field.getAttribute("label")+"]: "+this.outputSelectRequired(field.getAttribute("label"));
										}
									  }
									  break;
			}
		}
		
		if (this.errorData.length>0) {
			alert(this.errorMessage+this.errorData);
			this.clearDependancyChecks();
			return false;
		} else {
			this.clearDependancyChecks();
			return true;
		}
	}			

	FormValidator.prototype.verify_email = function(n) {
		var email_filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var example = (this.language=="de") ? "Beispiel: name@domain.de" : "Example: name@domain.com";
		
	      return (email_filter.test(this.formObj[n].value))  ? "" 
													     : "\n\t["+this.formObj[n].getAttribute("label")+"]: "+example;
	}

	FormValidator.prototype.verify_digit = function(n) {
		var digit_filter  = /^\d+$/;
		
	  	  return (digit_filter.test(this.formObj[n].value))  ? "" 
													     : "\n\t["+this.formObj[n].getAttribute("label")+"]: Numbers Only.";
	}

	FormValidator.prototype.verify_customerID = function(n) {
		var digit_filter  = /^\d{5}$/;
		var error = (this.language=="de") ? "5-stellige Kundennummer angeben." : "5 Digits Required.";
		
	  	  return (digit_filter.test(this.formObj[n].value))  ? "" 
													     : "\n\t["+this.formObj[n].getAttribute("label")+"]: "+error;
	}

