//Validator class
var validator = new Class ({		   
		initialize:function(element) { 
		// Intialize element
			this.result = false;	
			this.input_value = null;
			this.el_form = $(element);
		},
		checkValue:function() {
		//Check Value
			this.input_value = this.el_form.getProperty('property');
		},
		changeStyle:function(class_name) {
		//Change style if required --> class name string
			this.el_form.toggle_class(class_name);
		},
		setMessage:function(message) {
			this.el_form.value = message;
			},
		validate:function(criteria) {
		// Check Value at first
				this.checkValue();
		//Check criteria		
				if(criteria == 'empty') {
					// If field is empty
									if (this.input_value.length >= 1) {
											return true;
									}
									else {
											return false;
								   }								 
				}
				 else if (criteria == 'number') {
					 // If field is number
									if(this.input_value.test(/^-?\d{1,3}(,?\d{3})*(\.00)?$/) ==  true) { 
											return true;
									}
									else {
											return false;
									}
				}
				else if(criteria == 'e_mail') {
					 // If field is email
									if(this.input_value.test(/[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/) ==  true) { 
											return true;
									}
									else {
											return false;
									}
			}
				else if (criteria == 'range') {							
				//If field matches range
					for	( i=0 ; i < used_values.length ; i++ ) {	
					if	(this.input_value == used_values[i]) {
																		
									this.setMessage('Podana nazwa została już użyta');
									
									return false;
								}
					else {
									return true;
						
								}
						}	
				}
		}		
});

//Check value function

function checkForm() {
	
	if(e_mail.validate('e_mail') &&  e_mail.validate('empty')) {
			
			return true;
	 
	}
	else {
			e_mail.setMessage('Nieprawidłowy adres e-mail');
			return false;
			
	}
}

