// JavaScript Signup Validation

document.observe("dom:loaded", function() {
	
	Event.observe('signUpForm','submit',function(event) {
	//$("signUpForm").submit(function(){
		
		Event.stop(event);
		if ($('nameForm').getValue() == '') {
			alert('Please enter a name');
			return false;	
		}
		if ($('emailForm').getValue() == '') {
			alert('Please enter an e-mail address');
			return false;
		}
		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);
		if (!pattern.test($('emailForm').getValue())) {
			alert('Please enter a valid e-mail address');
			return false;
		}
		
		// 'this' refers to the current submitted form
		var str = $(this).serialize();
		
		new Ajax.Request($(this).readAttribute('action'),{
			method: 'post',
			parameters: str,
			onSuccess: function(transport){
				$("signUpForm").fade({
					duration:0.5,
					afterFinish:function(){
						$('ajaxReturn').update(transport.responseText);
						$('ajaxReturn').appear({duration:0.5});
					}
				});
			}
		});
		
		/*
		$.ajax({
		type: "POST",
		url: "/lists/newsletteradduser.php",
		data: str,
		success: function(msg){
		
			$('ajaxReturn').ajaxComplete(function(event, request, settings){
				
				$("signUpForm").fadeOut('500', function() {
					$('ajaxReturn').html(msg);
					$('ajaxReturn').fadeIn('500');
				});
			
			});
		
		}
	
		});
		*/
	return false;
		
	});

});

