// Avoid conflict with MooTools
jQuery.noConflict();

/**
  * Sends the AJAX Request with the entered data to the PHP List tool and returns the result
  * 
  *
  */
jQuery(document).ready(function($){
	// Form submit
	$('#submit').click(function() {
		// Get entered values
		var attribute1 = $('#attribute1').val();
		var attribute2 = $('#attribute2').val();
		var email = $('#email').val();
		var htmlemail = $('#htmlemail').val();
		var list = $('#list').val();
		
		// Do ajax request
		$.ajax({
			url: 'newsletter/index.php?p=subscribe',
			type: 'POST',
			data: 'subscribe=Ausgewählte(n) Newsletter abonieren&attribute1=' + attribute1 + '&attribute2=' + attribute2 + '&email=' + email + '&emailconfirm=' + email + '&htmlemail=' + htmlemail + '&list[1]=' + list,
			
			success: function(res) {
				// Request successful
				if(String(res).indexOf("Vielen Dank") != -1) {
					$('#newsletter').fadeOut("slow", function() {
						$('#newsletter').html("<div>Vielen Dank! Wir haben die Adresse " + email + " in unseren Verteiler aufgenommen. Bitte prüfen Sie Ihren Posteingang.</div>");
						$('#newsletter').fadeIn("slow");
					});
				}
				// Request not successful
				else {
					alert("Es ist ein Fehler aufgetreten. Bitte prüfen Sie Ihre Eingaben.");
				}	
			}
		
		});
		// Disable function of submit button
		return false;
	});
});
