/**
* Validator
* Author	: Plesnicute Marian
* Version	: 1.0
*/

function Validator(errorHandler) 
{
	this.errorHandler = errorHandler;
	this.fields = [];
	this.field_types = [];
	this.error_fields = [];
	
	if (typeof Validator._initialized == "undefined") 
	{
		Validator.prototype.addField = function (field_id, field_name, field_types) {
			field_id = jQuery.trim(field_id);
			if(!(typeof field_id == "string" && field_id != ''))
				return false;
			field_types = jQuery.trim(field_types);
			if(!(typeof field_types == "string" && field_types != ''))
				return false;
				
			var types = field_types.split('|');
			
			for(var i=0; i < types.length; i++)
			{
				field_type = jQuery.trim(types[i]);
				if(field_type == '')
					continue;
				this.fields[this.fields.length] = {
					id: field_id
					,name: field_name
					,type: field_type.toLowerCase()
				};
			}
		}
		
		Validator.prototype.addFieldType = function (name, fnValidation, errorCode, errorMsg) {
			name = jQuery.trim(name);
			if(!(typeof name == "string" && name != ''))
				return false;
				
			if(typeof fnValidation != "function")
				return false;
				
			this.field_types[this.field_types.length] = {
				name: name.toLowerCase()
				,fnValidation: fnValidation
				,errorCode: errorCode
				,errorMsg: errorMsg
			};
		}
		
		Validator.prototype.validate = function () {
			var global_this = this;
			var type = false;
			var ret = false;
			var inv = 0;
			
			this.error_fields = [];
			for(var i=0;i<this.fields.length;i++)
			{
				if(!$('#'+this.fields[i].id).length)
					continue;
					
				type = false;
				for(var index=0;index<this.field_types.length;index++)
					if(this.field_types[index].name == this.fields[i].type)
					{
						type = this.field_types[index];
						break;
					}
				if(!type)
					continue;
					
				var dom = $('#'+this.fields[i].id).get(0); 
				dom.fnValidation = type.fnValidation;
				ret = dom.fnValidation();
				if ($('#'+this.fields[i].id).siblings('a.invalid').length == 1) {
					$('#'+this.fields[i].id).siblings().remove('a.invalid');
				}
				if(!ret)
				{
					if ($('#'+this.fields[i].id).siblings('a.invalid').length == 0) {
						$('#'+this.fields[i].id).siblings('label').after('<a name="invalid_'+i+'" class="invalid" style="padding:0;"></a>');
					}
					this.error_fields[this.error_fields.length] = { 
						dom: $('#'+this.fields[i].id).get(0)
						,errorCode: type.errorCode
						,errorMsg: type.errorMsg
						,field_name: this.fields[i].name
					};
				}
			}

			if(this.error_fields.length > 0)
			{
				this.errorHandler(this.error_fields);
				window.location.href =  '#'+$('a.invalid:first').attr('name');
				return false;
			}
			else
				return true;
		}
	
		Validator._initialized = true;
	}
	
	this.addFieldType(
		'required'
		, function(){ 
			if(jQuery.trim($(this).val()) == '')
				return false;
			else
				return true;
		}
		, 'E1'
		, 'This is a required field.'
	);
	
	this.addFieldType(
		'float'
		, function(){
			if(jQuery.trim($(this).val()) == '')
				return true;
			if(jQuery.trim($(this).val()).match(/^[-+]?\d+\d*(\.\d*)?$/))
				return true;
			else
				return false;
		}
		, 'E2'
		, 'Numbers only, no commas or spaces'
	);
	
	this.addFieldType(
		'integer'
		, function(){
			if(jQuery.trim($(this).val()) == '')
				return true;
			if(jQuery.trim($(this).val()).match(/^[-+]?\d+\d*$/))
				return true;
			else
				return false;
		}
		, 'E3'
		, 'Numbers only, no commas or spaces'
	);
	
	this.addFieldType(
		'email'
		, function(){
			if(jQuery.trim($(this).val()) == '')
				return true;
			if(jQuery.trim($(this).val()).match(/^[a-z0-9._-]+@[a-z0-9._-]+.[a-z]+$/i))
				return true;
			else
				return false;
		}
		, 'E4'
		, 'Please fill in a valid email address'
	);
	
	this.addFieldType(
		'phone'
		, function(){
			if(jQuery.trim($(this).val()) == '')
				return true;
			if(jQuery.trim($(this).val()).match(/^(\+)?( )*\d+([- ]?\d*)*$/))
				return true;
			else
				return false;
		}
		, 'E5'
		, 'This is not a valid phone number'
	);
	
	this.addFieldType(
		'postcode'
		, function(){
			if(jQuery.trim($(this).val()) == '')
				return true;
			if(jQuery.trim($(this).val()).match(/^[a-z0-9 ]+$/i))
				return true;
			else
				return false;
		}
		, 'E6'
		, 'Please fill in a valid postcode'
	);
	
	this.addFieldType(
		'date'
		, function(){
		var now = new Date();
			if(jQuery.trim($(this).val()) == '')
				return true;
			var val_date = jQuery.trim($(this).val()).match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
			if(!val_date)
				val_date = jQuery.trim($(this).val()).match(/^(\d{1,2})\/(\d{1,2})\/(\d{2})$/);
			if(val_date)
			{
				if( val_date[3]>1900 && val_date[3]< now.getFullYear() && 1 <= val_date[1] && val_date[1] <= 31 && 1 <= val_date[2] && val_date[2] <= 12 )
					return true;
				else
					return false;
			}
			else
				return false;
		}
		, 'E7'
		, 'Please fill in a valid date'
	);
}