function SFForm_Validator( element )
{
	this.type = type;
	this.validation = new Array;
}

function SFForm( name )
{
	this.name	= name;
	this.elements = new Array();
	
	this._formObjectName = function()
	{
		return this.name + '_SFForm';
	}
	
	this.Element = function( id, options )
	{
		SF_Form_Elements[id] = this;
		this.elements[id] = options;
	}
	
	this.Init = function()
	{
		for( var element in this.elements )
		{
			if( ( this.elements[element]['default_value'] != undefined ) )
			{
				if ( this.elements[element]['type'] == 'checkbox' )
				{ 
					document.getElementById(element).checked = this.elements[element]['default_value'];
				}
				else
				{
					document.getElementById(element).value = this.elements[element]['default_value'];
				}
			}
			
			if( ( this.elements[element]['helper'] != undefined ) && ( $('#'+element).val() == '' ) )
			{
				$('#'+element).addClass('helper').val(this.elements[element]['helper']);
				
				$('#'+element).focus( function(){SF_Form_Elements[this.id].OnFocus(this.id);} );
			}
			
			$('#'+element).blur( function(){SF_Form_Elements[this.id].OnBlur(this.id);} );
		}
	}
	
	this.OnFocus = function(element)
	{	
		if($('#'+element).hasClass('helper'))
		{
			$('#'+element).removeClass('helper').val('');
		}
	}
	
	this.OnBlur = function(element)
	{
		if( this.elements[element]['live_validations'] != undefined )
		{
			this.Validate(element);
		}
		if( ( this.elements[element]['helper'] != undefined ) && ( $('#'+element).val() == '' ) )
		{
			$('#'+element).addClass('helper').val(this.elements[element]['helper']);
		}
	}
	
	this.Validate = function(element){		
		if ( element == undefined )
		{
			var is_valid = 1;			
			
			for( var element in this.elements )
			{
				is_valid *= ( this.Validate(element) ) ? 1 : 0;
			}
			
			return is_valid == 1;
		}
		else
		{	
			if ( this.elements[element]['type'] == 'editor' )
			{
				eval(element + '_Editor.saveHTML();'); 
			}
		
			var elementDOM = document.getElementById(element);
			
			for( var validations in this.elements[element]['live_validations'] )
			{
				if ( this.elements[element]['live_validations'][validations]['type'] == 'REGEXP' )
				{
					if(!(this.elements[element]['live_validations'][validations]['validator']).test(elementDOM.value)){this.Invalidate(elementDOM,this.elements[element]['live_validations'][validations]['hint']);return false;};
				}			
			}
			if	( this.elements[element]['online_validation'] != undefined )
			{
				/*remote_validate(this,' , $element['online_validation'][0] , ',\'' , $element['online_validation'][1] , '\');' , $helper_function , 'return true;
				
				
				$.getJSON('http://www.skg.sk/'+url+'&jsoncallback=?', function(data){
					if ( data.result == 'FAILED' ){
						invalidate(element,hint);
					}
					else{
						revalidate(element);
					}
				});
				
				return false;*/
			}		
			
			this.Revalidate(document.getElementById(element));
			
			return true;
		}
	}
	
	this.Invalidate = function(elementDOM, hint)
	{
		if ( ( $(elementDOM).attr('title') != hint ) ){
			$(elementDOM).attr('title', hint).removeClass('checking').addClass('error').poshytip('destroy');
			$(elementDOM).poshytip({
				content: hint,
				className: 'tip-red',
				alignTo: 'target',
				alignX: 'right',
				alignY: 'center',
				showOn: 'none',
				offsetX: 10,
				offsetY: 0
			}).poshytip('show');
		}
	}
	
	this.Revalidate = function(elementDOM)
	{
		$(elementDOM).removeClass('error').removeClass('checking');
		$(elementDOM).attr('title', '').poshytip('destroy');
	}
}
