function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

submitted = false;

function sendForm(form, type) {
	
	if (!submitted) {
		submitted = true;
	
		if (type == 'newsletter') {
			var inputField = $('#email', form);
			var email = inputField.val()
			
			if (!isValidEmailAddress(email)) {
				$('#mailResponse', form).html('"' + email + '" är inte en giltig e-postadress!');
				inputField.addClass("error"); 
			} else {
				$('#submitter', form).val('Vänta...');
				$('#submitter', form).attr('disabled', 'disabled');				
				
				$.post("_ajax/newsletter.php", { email: email },
					function(data){
						
						var response = "";
						if (data == 1) {
							response = "E-postadressen; " + email + ", har blivit <b>tillagd</b>.";
						} else if (data == 2) {
							response = "E-postadressen är nu <b>raderad</b> från sändlistan.";
						} else {
							response = "Det blev nått fel vid uppdateringen, försök igen!";
						}
						
						$('#mailResponse', form).html(response);
						
						$('#submitter', form).val('Skicka');
						$('#submitter', form).removeAttr("disabled");
						inputField.val('');
						inputField.removeClass("error");
						submitted = false;
					
				});		
				
				return false;
				
			}
			
		}
	
	} else {
		;
	}
	
	submitted = false;
	return false;
	
}


function theRotator() {
	
	$('div#rotator ul li').css({opacity: 0.0});
	$('div#rotator ul li:first').css({opacity: 1.0});
	setInterval('rotate()',4000);
}

function rotate() {	

	//Get the first image
	var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
};
