$(function()
{
	$("#form_pregunta").submit(function(){
		var envio = true;
		$(".required").each(function () {
			this.value = $.trim(this.value);
			if ( this.value == "" )	{
				$('p.error').show();
				$(this)
					.focus()
					.css("border","1px solid #FFFFFF");
				envio = false;
				return false;
			}
			else {
				$(this).css("border","none");
			}

		});
		if (envio)
		{	// Verificar mail
			if ( !isMail( $('#IDemail').val() ) ) {
				envio = false;
				$('p.error').show();
				$("#IDemail").css("border","1px solid #FFFFFF");
				$('#IDemail').focus();
			}
		}
		if (envio){
			hideAll();
			$.ajax({
				type	 : 'post',
				url		 : $("#form_pregunta").attr("action"),
				data	 : $("#form_pregunta *").serialize(),
				dataType : 'xml',
				success	 : function(xml)
				{
					hideAll();
					error = parseInt($('data',xml).find("error").text());
					if (error) 
					{ 	// error						
						$('p.error').show().html("");
						$('span',xml).each(function(){
							$('p.error').append( $(this).text() + '<br />' );
						});
					}
					else
					{ 	//ok
						$('p.ok').show();
						$("input,textarea").val("");
						setTimeout (function() { 							
							$('h3').click();						
							hideAll();
						}, 1000);
						
					}
				},								
				error		: function()
				{ 
					$('p.error').show(); 
					$('p.infoState').html("").hide();
				},
				beforeSend	: function() { $('p.infoState').show(); }
			});
		}
		return false;
	});

});

function hideAll() {
	$('p.infoState,p.ok,p.error').hide();
}


function isMail(_email) {
	var emailReg = /^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$/i;
	return(emailReg.test(_email));
}