// JavaScript Document


$(document).ready(function() {
                // invia mailinglist
	 $('#mailinglist_btn').click(function () {
					$('#legals_mailinglist').fadeIn('slow');
           				$('#box_campo_mailinglist').fadeOut('slow');
                                        $('#footer_mailing').fadeOut('slow');
	});
         $('#confirm_mailinglis_btn').click(function () {
        				$('#legals_mailform').fadeIn('slow');
           				$('#box_campo_mailinglist').fadeOut('slow');
                                        $('#footer_mailing').fadeOut('slow');

	});
        // messaggio inviato --> crea nuovo
	$('.nuovo_utente_btn').click(function () {
					$('#box_campo_mailinglist').fadeIn('slow');
					$('#footer_mailing').fadeIn('slow');
					$('#done_mailinglist').fadeOut('slow');
                                        $('#done_mailinglist').fadeOut('slow');
                                        $('#delete_mailinglist').fadeOut('slow');
                                        $('input[name=email_mailinglist]').val("");
                                        document.getElementById("radio_form").checked = false;
                                       
	});	
                // messaggio annullato --> crea nuovo
        $('.annulla_mailform').click(function () {
					$('#box_campo_mailinglist').fadeIn('slow');
                                        $('#footer_mailing').fadeIn('slow');
                                        $('#legals_mailform').fadeOut('slow');
                                        $('#legals_mailinglist').fadeOut('slow');
                                        $('#done_mailinglist').fadeOut('slow');
                                        $('input[name=email_mailinglist]').val("");
                                        document.getElementById("radio_form").checked = false;
                                         $('input[name=email]').val("");
                                        $('textarea[name=comment]').val("");
                                        
	});

                    // messaggio inviato --> crea nuovo
	$('#nuovo_messaggio_btn').click(function () {
					$('#box_campo_mailinglist').fadeIn('slow');
					$('#footer_mailing').fadeIn('slow');
					$('#done').fadeOut('slow');
                                        $('input[name=email]').val("");
                                        $('textarea[name=comment]').val("");
	});	
                    // invia mailform
        $('#mailform_btn').click(function () {
                                                    

        				$('#legals_mailform').fadeIn('slow');
           				$('#box_campo_mailinglist').fadeOut('slow');
                                        $('#footer_mailing').fadeOut('slow');

	});
                    // conferma mailinglist
	$('.confirm_mailform_btn').click(function () {


                    var btn = this
           
            
            
            if (btn.getAttribute("id") == "accetto_mailform"){
                
                //Get the data from all the fields
	//	var name = $('input[name=name]');
		var email = $('input[name=email]');
	//	var website = $('input[name=website]');
		var comment = $('textarea[name=comment]');
                document.php_url = "assets/process.php"
                var done =  $('#done')
		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		/*if (name.val()=='') {
			name.addClass('hightlight');
			return false;
		} else name.removeClass('hightlight');
		*/
              
		if (email.val()=='') {
			email.addClass('hightlight');
			return false;
		} else email.removeClass('hightlight');
		
		if (comment.val()=='') {
			comment.addClass('hightlight');
			return false;
		} else comment.removeClass('hightlight');
		
		//organize the data properly
		var data =  '&email=' + email.val() + '&comment='  + encodeURIComponent(comment.val());
		
		
                
                
                
            }

            else if (btn.getAttribute("id") == "accetto_mailinglist"){
 
                //Get the data from all the fields
		var email = $('input[name=email_mailinglist]');
        	var cancella = document.getElementById("radio_form")
                var cancella_utente = cancella.checked;
              
                document.php_url = "assets/process_mailinglist.php"

		if (email.val()=='') {
			email.addClass('hightlight');
			return false;
		}

               else {

                if (cancella_utente){

                            		var data =  '&email=' + email.val();
                                        var done =  $('#delete_mailinglist')
                }
                else {

                                    var data = '&email=' + email.val();
                                    var done =  $('#done_mailinglist')
                }

                                 


		//organize the data properly
               }




            }

          

		//disabled all the text fields
		$('.text').attr('disabled','true');
		
		//show the loading sign
		$('.loading').show();
		
	 
	
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: document.php_url,
				
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {	
			
				//if process.php returned 1/true (send mail success)
				if (html==1) {			
				
					//hide the form
					$('#legals_mailform').fadeOut('slow');
                                        $('#legals_mailinglist').fadeOut('slow');
				 

                                    done.fadeIn('slow');
                                 

				//if process.php returned 0/false (send mail failed)
				} else alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
	
	
	
	




       
	
	
	
});	


	
