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.escape			= options.escape || false;
	this.preSubmit		= options.preSubmit || null;
	this.preSend		= options.preSend || null;
	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('');
							}
						})
					if ( $('#'+that.prefixID+name).val() == "" ) $('#'+that.prefixID+name).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 = '';
			
			if(that.preSubmit){
				error = that.preSubmit();
			}
			
			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 'hidden':
							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].equal) {
						}
					}
					else if ( !val[i].regEx.test( selectorVal ) || selectorVal == el.attr('title') )
					{
						if (error=='') error = val[i].errorMsg;
						el.addClass(val[i].errorClass)
						break;
					}
					else if(val[i].equal) {
						if ( $('#' + that.prefixID + val[i].equal).val() != $('#' + that.prefixID + name).val() )
						{
							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(''); },5000)
			}
			else
			{
				
				if(that.preSend){
					that.preSend();
				}				
				
				that.errorLabel.parent().show();
				that.errorLabel.text(that.sendingMsg);
				var datapost = '';
				that.form.find('*').each(function(i)
				{
					if (that.prefixName != "")
					{
						if ( this.name && this.value )
						{
							if (this.name)
							{
								if ( that.escape )
								{
									datapost += that.prefixName + this.name + '=' + escape(unescape(this.value)) + '&';
								}
								else
								{
									datapost += that.prefixName + this.name + '=' + this.value + '&';
								}
							}
						}
					}
					else
					{
						if ( that.alias && that.alias[this.name] )
						{
							if (this.name)
							{
								if ( that.escape )
								{
									datapost += that.alias[this.name] + '=' + escape(unescape(this.value)) + '&';
								}
								else
								{
									datapost += that.alias[this.name] + '=' + this.value + '&';
								}

							}
						}
						else
						{
							if (this.name)
							{
								if ( that.escape )
								{
									datapost += this.name + '=' + escape(unescape(this.value)) + '&';
								}
								else
								{
									var v = this.value.replace(/\n/g,'<br />');
									v = v.replace(/\r/g,'<br />');
									datapost += this.name + '=' + v + '&';
								}

							}
						}

						//datapost = that.form.serialize();
					}
				});

				if (that.success)
				{
					$.ajax({
						url		: that.url,
						type	: that.type,
						dataType: that.dataType,
						data	: 'nocache=' + Math.random()+'&'+datapost,
						success	: that.success
					});
				}
				else
				{
					that.errorLabel.text(that.okMsg);
					return true;
				}
			}

			return false;
		});
	}
};
