function formValidator(options)
{
	this.jSONrequired	= options.jSONrequired || {};	
	this.alias			= options.alias || null;
	this.dataType		= options.dataType || 'xml';
	this.method			= options.method || 'post';
	this.formID			= options.formID || '';
	this.errorClass		= options.errorClass || '';
	this.form			= $('form#' + this.formID) || null;
	this.url			= options.url || '';
	this.success		= options.success || null;
	this.errorLabel		= options.errorLabel;
	this.prefixID		= options.prefixID || 'ID';
	this.prefixName		= options.prefixName || '';
	this.sendingMsg		= options.sendingMsg || 'Enviando datos...';
	this.okMsg			= options.okMsg || '¡Gracias!';
	this.initForm();
	this.validate();
}

formValidator.prototype = {
	initForm	: function()
	{
		var that = this;
		var requireds = this.jSONrequired;
		for(name in requireds)
		{	
			for(v in requireds[name].validators)
			{
				if(requireds[name].validators[v].valorOmision)
				{
					$('#'+that.prefixID+name)
						.val(requireds[name].validators[v].valorOmision)            
						.focus(function()
						{
							if ($(this).val() == $(this).attr('title'))
							{
								$(this).val('');
							}
						})
					$('#'+that.prefixID+name).attr('title',requireds[name].validators[v].valorOmision)            
				}
			}
		}
		this.errorLabel.html('');
	},
	validate	: function()
	{
		
		var that = this;
		this.form.submit(function()
		{			
			var error = '';
			that.errorLabel.html('');
			$(this).find('*').removeClass(that.errorClass)
			var requireds = that.jSONrequired;
			for(name in requireds)
			{
				var val = requireds[name].validators;
				for(i in val)
				{
					switch (val[i].type)
					{
						case 'text':
							el = that.form.find('input#'+that.prefixID+name)
							selectorVal = $.trim(that.form.find('input#'+that.prefixID+name).val());
							break;
						case 'textarea':
							el = that.form.find('textarea#'+that.prefixID+name)
							selectorVal = $.trim(that.form.find('textarea#'+that.prefixID+name).val());
							break;
						case 'password':
							el = that.form.find('input#'+that.prefixID+name)
							selectorVal = $.trim(that.form.find('input#'+that.prefixID+name).val());
							break;
						case 'select':
							el = that.form.find('select#'+that.prefixID+name);
							selectorVal = that.form.find('select#'+that.prefixID+name+' :selected').val();
							break;
						case 'checkbox':
							el = that.form.find('input#'+that.prefixID+name);
							selectorVal = that.form.find('input#'+that.prefixID+name+':checked').length;
							break;
					}

					if (val[i].regEx == null)
					{
						if ( selectorVal == "" || selectorVal == el.attr('title') )
						{
							if (error=='') error = val[i].errorMsg;
							el.addClass(val[i].errorClass)
							break;
						}
					}
					else if ( !val[i].regEx.test( selectorVal ) || selectorVal == el.attr('title') )
					{									
						if (error=='') error = val[i].errorMsg;
						el.addClass(val[i].errorClass)
						break;
					}
				}
			}

			if (error != '')
			{
				that.errorLabel.html(error)
				that.errorLabel.parent().show();
				setTimeout(function(){ that.errorLabel.html('') },2000)
			}
			else
			{
				that.errorLabel.text(that.sendingMsg);
				var datapost = '';
				that.form.find('*').each(function(i)
				{										
					if (that.prefixName != "") 
					{
						if ( this.name && this.value )
						{
							if (this.name)														
								datapost += that.prefixName + this.name + '=' + escape(unescape(this.value)) + '&';
						}
					}
					else
					{
						if ( that.alias && that.alias[this.name] )
						{
							if (this.name)	
								datapost += that.alias[this.name] + '=' + escape(unescape(this.value)) + '&';
						}
						else
						{
							if (this.name)	
								datapost += this.name + '=' + escape(unescape(this.value)) + '&';
						}

						//datapost = that.form.serialize();
					}
				});

				if (that.success)
				{					
					that.errorLabel.html(that.sendingMsg)
					that.errorLabel.parent().show();
					$.ajax({
						url		: that.url,
						type	: that.method,
						dataType: that.dataType,
						data	: 'nocache=' + Math.random()+'&'+datapost,
						success	: that.success,
						error	: function(e){
							that.errorLabel.html('ERROR')
							that.errorLabel.parent().show();
						}
					});
				}
				else
				{
					that.errorLabel.text(that.okMsg);
					return true;
				}
			}
			return false;
		});
	}
};