$(document).ready(function() {

    $('#show_form').toggle(
        function() {
            $('#form_email').animate({
                height: 'show'
            }, 2000, function() {})
        },
        function() {
            $('#form_email').animate({
                height: 'hide'
            }, 1000, function() {})
        }
        );

    $('#close_form_email').click(
        function() {
            $('#form_email').animate({
                height: 'hide'
            }, 1000, function() {})
        }
        );
});

function validateEmail(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test(email) == false) {
        return false;
    }
    else return true;
}


function invia(){        
    var url_article = document.location.href;
    var email = $('#email').val();
    var nome = $('#nome').val();
    var emailmittente = $('#emailmittente').val();
    var commento = $('#commento').val();
    var captcha =  $('#captchaedit').val();
    var errors = '';
    var id_article =$('#id_article').val();
    
	//if( !validateEmail(email) || !validateEmail(emailmittente) || nome == '' || captcha == ''){
	
	
	if(!validateEmail(email))
		//$('<li><label for="email"><strong>"E-mail destinatario"</strong>: Campo obbligatorio in formato e-mail</label></li>').appendTo('#resetfont');
		errors += '<li><label for="email"><strong>"E-mail destinatario"</strong>: Campo obbligatorio in formato e-mail</label></li>';
	if(nome == '')
		errors +='<li><label for="nome"><strong>"Il tuo nome"</strong>: Campo obbligatorio</label></li>'; 
		//$('<li><label for="nome"><strong>"Il tuo nome"</strong>: Campo obbligatorio</label></li>').appendTo('#resetfont');
	if(!validateEmail(emailmittente))
		errors += '<li><label for="emailmittente"><strong>"La tua e-mail"</strong>: Campo obbligatorio in formato e-mail</label></li>';
		//$('<li><label for="emailmittente"><strong>"La tua e-mail"</strong>: Campo obbligatorio in formato e-mail</label></li>').appendTo('#resetfont');
	
	if (captcha == '')
		errors += '<li><label for="captchaedit"><strong>"Captcha"</strong>: Campo obbligatorio</label></li>';	 
	if (errors != '')
	{
        $('.error_fieldset').show();
        $('#resetfont').html(errors);
		return;
	}

	
	$('#send_email_ok').hide('fast');
	$('#send_email_error').hide('fast');
	
    $.post('/sendemail/send/', {
		'email':email,
        'nome':nome,
        'emailmittente':emailmittente,
        'commento':commento,
	'captchaedit':captcha,
        'id_article':id_article
		},
		function(data) {
			var obj = eval(data);  //TODO: utilizzare un jsonizzatore pi� sicuro			
			if(obj[0] == 0)
			{
				$('.error_fieldset').show();   // aggiunto in data 24/08/2010
				$('#send_email_ok').show();
			}
			else if (obj[0] == 1)
			{
				$('#send_email_error').html(obj[1]).show('slow');				
			}
			else
			  alert('JSON non corretto');

			$('#resetfont').html('');
			//$('.error_fieldset').hide();
			//$('.fieldset').hide();			   
		});
}



//function validate_email(field,alerttxt)
//{
//with (field)
//  {
//  apos=value.indexOf("@");
//  dotpos=value.lastIndexOf(".");
//  if (apos<1||dotpos-apos<2)
//    {alert(alerttxt);return false;}
//  else {return true;}
//  }
//}
