$(document).ready(function(){
	$("a.subscribe").click( function( event ) {
		event.preventDefault();
		
		$('input:text').each( function() {
			$(this).val('');
		});
		
		if ( $("div.contact2").css('display') == 'none' ) {
			$("div.pane:visible").fadeOut("slow", function() {
				$("div.contact2").fadeIn(1000);
				$("a.back").fadeIn(1000);
			});
		}
	});
	
	$("a.submit").click( function( event ) {
		event.preventDefault();
		
		var valid = true;
		var postdata = new Array();
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		$('input:text').each(function(index){
			if ( ( index == 0 && $(this).val() == '' ) || ( index == 2 && !filter.test($(this).val()) ) ) {
				$("p.msg").text('First Name and Valid Email are required')
						  .addClass('errmsg');
				valid = false;
			}
			postdata[index] = $(this).attr('name') + '=' + $(this).val();
		});
		
		if ( valid ) {
			$.ajax({
				type: "POST",
				url: "storecontact.php",
				data: postdata.join("&")
			});
			
			$("div.contact2").fadeOut("slow", function() {
				$("div.contact3").fadeIn(1000);
			});
		}
	});
	
	$("a.back").click( function( event ) {
		event.preventDefault();
		
		if ( $("div.contact1").css('display') == 'none' ) {
			$(this).fadeOut("slow");
			$("div.pane:visible").fadeOut("slow", function() {
				$("div.contact1").fadeIn(1000);
			});
		}
	});
});
