/*	<form  method="post" name="posta" action="<?php echo $_SERVER['PHP_SELF'] ?>" onsubmit="return checkIt(document.posta);">
//	<script type="text/javascript" src="../js/checkIt.js"></script>
*/

function checkDomanda(modulo){ 
	//alert(modulo);
	var messaggio="";
	var range_numerico;
	var complete=true;

	if (modulo.author.value=="")
		messaggio+="<li>inserire il nome (campo obbligatorio)</li>";
	
	if (modulo.email.value=="")
		messaggio+="<li>inserire un indirizzo mail (campo obbligatorio)</li>";
	else{
		var indirizzo_mail=modulo.email.value;
		if ((indirizzo_mail.indexOf("@")==-1)||(indirizzo_mail.indexOf(".")==-1))
			messaggio+="<li>la mail inserita non &egrave; valida</li>";
	}
	
	if (modulo.comment.value=="")
		messaggio+="<li>inserire la domanda (campo obbligatorio)</li>";
	
	if (messaggio!=""){
		//alert(messaggio);
		$("#erroreRegistrazione .errore").html(messaggio);
		$("#erroreRegistrazione").show();
		complete=false;
	}
	
	return complete;
}

function checkIt(modulo){ 
	//alert(modulo);
	var messaggio="";
	var range_numerico;
	var complete=true;

	if (modulo.username.value=="")
		messaggio+="<li>inserire il nome (campo obbligatorio)</li>";
	
	if (modulo.password.value=="")
		messaggio+="<li>inserire una password (campo obbligatorio)</li>";
	
	if (modulo.email.value=="")
		messaggio+="<li>inserire un indirizzo mail (campo obbligatorio)</li>";
	else{
		var indirizzo_mail=modulo.email.value;
		if ((indirizzo_mail.indexOf("@")==-1)||(indirizzo_mail.indexOf(".")==-1))
			messaggio+="<li>la mail inserita non &egrave; valida</li>";
	}		
 
	if (!modulo.consenso.checked) messaggio+="<li>autorizzare trattamento dei dati personali (campo obbligatorio)</li>";
	if (!modulo.disclaimer.checked) messaggio+="<li>confermare lettura del disclaimer (campo obbligatorio)</li>";

	if (messaggio!=""){
		//alert(messaggio);
		$("#erroreRegistrazione .errore").html(messaggio);
		$("#erroreRegistrazione").show();
		complete=false;
	}
	
	return complete;
}

/* CONTROLLO LOGIN
-----------------------------------------------*/
    function showError(str){
        $('div#erroreLogin .errore').html('<li>'+str+'</li>');
		$('div#erroreLogin').show();
    }
   
    function checkLogin(page) {
		var f             = document.forms["mylogin"];
        var __filter     = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        //
		
		alert('checkIt');
        $('div#erroreLogin').slideUp(
            function() {
                if( f.email.value == '' ) {
                    showError('Non hai inserito l\'email');
                    f.email.focus();
                    return false;
                }
                if (!__filter.test( f.email.value )) {
                    showError('Il campo email non &egrave; valido');
                    f.email.select();
                    f.email.focus();
                    return false;
                }
                if( f.password.value == '' ) {
                    showError('Non hai inserito la password');
                    f.password.focus();
                    return false;
                }
                //$('div#ajax-loader').show('slow');
               // $('body').css('cursor','wait');
                // Invio dei dati a logon.php in POST tramite Ajax
                $.post(page, { email: f.email.value, password: f.password.value},
                    function(data) {
						//alert(data);
                        //$('div#ajax-loader').hide('slow');
                        //$('body').css('cursor','default');
                        switch(data) {
                            case '0':
                                // accesso consentito
								//alert("loggato");
								//window.location.reload( false );
								history.back();
                                //document.location.href = 'index.php?cat=6';
                                break;
                            case '-1':
                                showError('Accesso Negato. Email o Password errati');
                                break;
                            case '-2':
                                showError('Dati errati: tentativo di Hack!');
                                break;
                        }
                    }
                );
                // cut-off submit
                return false;
            }
        );
        return false;                    
    }
	
/* CONTROLLO LOGIN
-----------------------------------------------*/
    function checkLoginSmall(page, backpage) {
		var f             = document.forms["mylogin"];
        var __filter     = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        //
		alert(page);
		if( f.email.value == '' ) {
			document.location.href = 'index.php?err=3';
			return false;
		}
		if (!__filter.test( f.email.value )) {
			document.location.href = 'index.php?err=3';
			return false;
		}
		if( f.password.value == '' ) {
			document.location.href = 'index.php?err=3';
			return false;
		}
		
		$.post(page, { email: f.email.value, password: f.password.value},
			function(data) {
				alert(data);
				switch(data) {
					case '0':
						document.location.href = backpage;
						break;
					case '-1':
						document.location.href = 'index.php?err=1';
						break;
					case '-2':
						document.location.href = 'index.php?err=2';
						break;
				}
			}
		);

        return false;                    
    }
	
/* CONTROLLO RECUPER
-----------------------------------------------*/
    function showRecupero(str){
        $('div#erroreRecupero .errore').html('<li>'+str+'</li>');
		$('div#erroreRecupero').show();
    }
   
    function checkRecupero(page) {
		var f             = document.forms["myRecupero"];
        var __filter     = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        //
        $('div#erroreLogin').slideUp(
            function() {
                if( f.email.value == '' ) {
                    showRecupero('Non hai inserito l\'email');
                    f.email.focus();
                    return false;
                }
                if (!__filter.test( f.email.value )) {
                    showRecupero('Il campo email non &egrave; valido');
                    f.email.select();
                    f.email.focus();
                    return false;
                }
				alert(page);
                $.post(page, { email: f.email.value},
                    function(data) {
                        switch(data) {
                            case '1':
                                showRecupero('I dati di accesso sono stati inviati al tuo indirizzo email');
                                break;
                            case '0':
                                showRecupero('L\'indirizzo email inserito non risultano nei nostri archivi');
                                break;
                        }
                    }
                );
                return false;
            }
        );
        return false;                    
    }
	
    function showSos(str){
		//alert (str);
        $('div#erroreSos .errore').html('<li>'+str+'</li>');
		$('div#erroreSos').show();
    }
   
    function checkSos(page) {
		var f             = document.forms["domandaSos"];
        var __filter     = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        //
        $('div#erroreSos').slideUp(
            function() {
				if( f.comment.value == '' ) {
                    showSos('Non hai inserito il commento');
                    f.email.focus();
                    return false;
                }
				if( f.author.value == '' ) {
                    showSos('Non hai inserito ul tuo nome');
                    f.email.focus();
                    return false;
                }
                if( f.email.value == '' ) {
                    showSos('Non hai inserito l\'email');
                    f.email.focus();
                    return false;
                }
                if (!__filter.test( f.email.value )) {
                    showSos('Il campo email non &egrave; valido');
                    f.email.select();
                    f.email.focus();
                    return false;
                }
                $.post(page, { email: f.email.value, author: f.author.value, comment: f.comment.value, capchaSum_input: f.capchaSum_input.value, capchaSum_controllo: f.capchaSum_controllo.value},
                    function(data) {
                        switch(data) {
                            case '1':
                                showSos('Grazie, ora la tua domanda &egrave; in coda di autorizzazione da parte di un moderatore');
                                break;
                            case '0':
                                showSos('Si &egrave; verificato un errore, riprovare pił tardi.');
                                break;
							case '2':
                                showSos('La somma di controllo errata');
                                break;
                        }
                    }
                );
                return false;
            }
        );
        return false;                    
    }
