/*
	Form element classes for form validator
	All form elements have their own validation methods
*/

function FETextfield(emt){
	this.inputEmt = emt;
	this.valueRequired = null;
	this.emtValue = null;
	
	this.validationType = null;
	this.validTypes = new Array('Email', 'Number', 'Other');
	
	this.validLabelSrcs = new Array('FromNextSibling', 'FromPrevSibling');
	this.emtLabelSrc = null;
	this.emtLabel = "";
	
	this.validate = function(){
		if(this.inputEmt.title == null || this.inputEmt.title == ""){
			for(var i = 0; i < this.validLabelSrcs.length; i++){
				if(this.emtLabel == "" && this.inputEmt.className.indexOf('fvLabel' + this.validLabelSrcs[i]) > -1){
					this.emtLabelSrc = i;
				}
			}
			
			if(this.emtLabelSrc != null){
				switch(this.emtLabelSrc){
					case 0:
						this.emtLabel = this.inputEmt.nextSibling.innerHTML;
						break;
					
					case 1:
						this.emtLabel = this.inputEmt.previousSibling.innerHTML;
						break;
				}
			}
			else{
				this.emtLabel = this.inputEmt.name;
			}
		}
		else{
			this.emtLabel = this.inputEmt.getAttribute("title");
		}
		
		
		
		
		if(this.inputEmt.className.indexOf('fvRequired') > -1){
			this.valueRequired = true;
		}
		else{
			this.valueRequired = false;
		}
		
		if(this.valueRequired == true){
			this.emtValue = this.inputEmt.value;			
			var foundType = null;
			
			for(var i = 0; i < this.validTypes.length; i++){
				if(this.validationType == null && this.inputEmt.className.indexOf('fvType' + this.validTypes[i]) > -1){
					this.validationType = this.validTypes[i];
					foundType = i;
				}
			}
			
			if(this.validationType == null){
				this.validationType = this.validTypes[this.validTypes.length - 1];
			}
			
			var validationRegExp = null;
			
			switch(foundType){
				case 0:
					validationRegExp = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
					break;
				
				case 1:
					validationRegExp = new RegExp("[0-9]+");
					break;
				
				default:
					validationRegExp = new RegExp(".+");
					break;
			}
			
			return validationRegExp.test(this.emtValue);
		}
		else{
			return true;
		}
	}
	
	
	this.getValidationType = function(){
		return this.validationType;
	}
	
	this.getValidTypes = function(){
		return this.validTypes;
	}
	
	this.getLabel = function(){
		return this.emtLabel;
	}
}






function FECheckbox(emt){
	this.inputEmt = emt;
	this.valueRequired = null;
	this.emtValue = null;
	
	this.validLabelSrcs = new Array('FromNextSibling', 'FromPrevSibling');
	this.emtLabelSrc = null;
	this.emtLabel = "";
	
	this.validate = function(){
		if(this.inputEmt.title == null || this.inputEmt.title == ""){
			for(var i = 0; i < this.validLabelSrcs.length; i++){
				if(this.emtLabel == "" && this.inputEmt.className.indexOf('fvLabel' + this.validLabelSrcs[i]) > -1){
					this.emtLabelSrc = i;
				}
			}
			
			if(this.emtLabelSrc != null){
				switch(this.emtLabelSrc){
					case 0:
						this.emtLabel = this.inputEmt.nextSibling.innerHTML;
						break;
					
					case 1:
						this.emtLabel = this.inputEmt.previousSibling.innerHTML;
						break;
				}
			}
			else{
				this.emtLabel = this.inputEmt.name;
			}
		}
		else{
			this.emtLabel = this.inputEmt.getAttribute("title");
		}
		
		if(this.inputEmt.className.indexOf('fvRequired') > -1){
			this.valueRequired = true;
		}
		else{
			this.valueRequired = false;
		}
		
		if(this.valueRequired == true){
			this.emtValue = this.inputEmt.checked;			
			return this.emtValue;
		}
		else{
			return true;
		}
	}
	
	this.getLabel = function(){
		return this.emtLabel;
	}
}
