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);
}


$(document).ready(function() {
	$('#new_event').hide();
	$('#partners_overlay').hide();

	$('.potnas').click(function() {
		$('#partners_overlay').fadeIn('slow');
		$('#new_event').fadeIn('normal');
		$('.adding2').fadeIn('normal');
		$('#just_events').hide();
		$('.underlay').fadeOut('normal');
	});
	
	$('.grey_close2').click(function() {
		$('#partners_overlay').fadeOut('normal');
		$('#new_event').fadeOut('normal');
		$('.underlay').fadeIn('normal');
	});
	
	$('.closer').click(function() {
		$('#new_event').fadeOut('slow');
		$('.underlay').fadeIn('fast');
	});
	
	$('.adding2').click(function() {
		$('#new_event').fadeIn('slow').show();
		$('#just_events').show();
		$('#column1').fadeIn('slow');
		$('#column3').hide();
		$('#column2').hide();
		$('.underlay').fadeOut('slow');
		$("#step2").animate({
		left: "350px"
		}, 500);
		$("#step3").animate({
		right: "450px"
		}, 500);
	});
	
	$('.add_again').click(function() {
		$('#new_event').fadeIn('slow').show();
		$('#just_events').show();
		$('#column1').fadeIn('slow');
		$('.adding2').hide();
		$('#column3').hide();
		$('#column2').hide();
		$('.underlay').fadeOut('slow');
		$("#step2").animate({
		left: "350px"
		}, 500);
		$("#step3").animate({
		right: "450px"
		}, 500);
	});
	
	$('.adding2').click(function() {
		$('#just_events').fadeIn('normal');
		$('#partners_overlay').fadeOut('slow');
		$('#column1').fadeIn('slow');
		$('#column3').hide();
		$('#column2').hide();
		$('.underlay').fadeOut('slow');
		$("#step2").animate({
		left: "350px"
		}, 500);
		$("#step3").animate({
		right: "450px"
		}, 500);
	});
	
	$('#back_btn').click(function() {
		$('#column1').slideDown('slow');
		$('#column2').slideUp('slow');
		$("#step2").animate({
		left: "350px"
		}, 500);
		$("#step3").animate({
		right: "450px"
		}, 500);
	});
	
	$('#back_btn2').click(function() {
		$('#column2').slideDown('slow');
		$('#column3').slideUp('normal');
		$("#step2").animate({
		left: "700px"
		}, 500);
		$("#step3").animate({
		right: "50px"
		}, 500);
	});


	// Zip Code (numbers only)
	
		
		/*$('#zip').keypress(function(event) {
		return ((event.which >= 48) && (event.which <= 57));
		}
		});*/
	
	
	//Email Validation
	var emailvalid = 0;
	
	function emailChecker() {
	var email = $("#email").val();
		if(email != 0){
			if(isValidEmailAddress(email)){
				$("#validEmail").css({ "background-image": "url('/i/validyes.png')" });
			} else {
				$("#validEmail").css({ "background-image": "url('/i/validno.png')" });
			}
		} else {
			$("#validEmail").css({ "background-image": "none" });
		}
	};
	
	
	function emailReChecker() {
	 	var email = $("#email").val();
	 	var email2 = $("#email-confirm").val();
	 	if(email2 != 0){
			if(email == email2){
				$("#validEmailDup").css({ "background-image": "url('/i/validyes.png')" });
				emailvalid=1;
			} else {
				$("#validEmailDup").css({ "background-image": "url('/i/validno.png')" });
				emailvalid=0;
			}
		} else {
			$("#validEmailDup").css({ "background-image": "none" });
		}
	};
	
	$("#email").keyup(function(){
		emailChecker();
		emailReChecker();
	});
	
	$("#email-confirm").keyup(function(){
		emailChecker();
		emailReChecker();
	});
	
	$("#email").click(function(){
		emailChecker();
		emailReChecker();;
	});
	
	$("#email-confirm").click(function(){
		emailChecker();
		emailReChecker();
	});
	 	
		
	//Form Validation Panel 1
	function focusclear(x) {
		if(x.value == x.defaultValue) {
			x.value = "";
		}
	};
	
	$("#phone").focus(function() {
		focusclear(this);
	});
	
	$("#nxt_btn").click(function() {
		emailChecker();	
		emailReChecker();	
		var firstname = $("input#first_name").val();
		var lastname = $("input#last_name").val();
		var organization = $("input#organization").val();
		
		if (firstname == "" || lastname=="" || organization=="" || emailvalid!=1) {
			alert ("Please complete all of the required fields.");
			return false;
		} else {
			$('#column3').hide();
			$('#column2').slideDown('slow');
			$("#step2").animate({
			left: "700px"
			}, 500);
			$("#step3").animate({
			right: "50px"
			}, 500);
			$("#column1").slideUp('slow');
		}
	});

	//Form Validation Panel 2
		
	$('textarea[maxlength]').keyup(function(){
var max = parseInt($(this).attr('maxlength'));
if($(this).val().length > max){
$(this).val($(this).val().substr(0, $(this).attr('maxlength')));
}
$(this).parent().find('.charsRemaining').html('You have ' + (max - $(this).val().length) + ' characters remaining');
});
	
	$("#nxt_location").focus(function() {
		focusclear(this);
	});
	
	$("#nxt_address").focus(function() {
		focusclear(this);
	});
	
	$("#nxt_address2").focus(function() {
		focusclear(this);
	});
	
	$("#city").focus(function() {
		focusclear(this);
	});
	
	$("#zip").focus(function() {
		focusclear(this);
	});
	
	$("#message_input").focus(function() {
		focusclear(this);
	});
	
	$("#nxt_btn2").click(function() {
		var event = $("input#nxt_event").val();
		var starttime = $("#nxt_start").val();
		var endtime = $("#nxt_end").val();
		var address = $("input#nxt_address").val();
		var city = $("input#city").val();
		var state = $("#state").val();
		var zip = $("input#zip").val();
		var message = $("#message_input").val();
		
		
		var ext = $('#nxt_image').val().split('.').pop().toLowerCase();
		var allow = new Array('gif','png','jpg','jpeg');
		
		if(starttime >= endtime || endtime == "") {
			alert("Please make sure your event's end time is later than your event's start time.");
			return false;
		}
		
		
		if(isNaN(zip)) {
		alert("your zip code must be a numeric value.")
		return false;
	}	
		
		
				
		if (event == "" || starttime=="" || endtime=="" || address=="" || address=="address line 1" || city=="" ||  city=="city" || state=="" || zip=="" || message=="" || message=="(max 500 characters)") {
			alert ("All fields are required.");
		
		}else if($('#nxt_image').val()!="" && jQuery.inArray(ext, allow) == -1) {
		    alert('You may only upload the following formats: jpg, gif, png.');
		
		} else {
			$('#column3').slideDown('normal');
			$('#column2').slideUp('slow');
			$("#step3").animate({
			right: "30px"
			}, 500);
			$("#step2").animate({
			left: "320px"
			}, 500);
		}
	});

	//Form Validation Panel 3
	$("#admission").focus(function() {
		focusclear(this);
	});
	
	$("#partof").focus(function() {
		focusclear(this);
	});
	
	$("#presenter").focus(function() {
		focusclear(this);
	});
	
	$("#tags").focus(function() {
		focusclear(this);
	});
	
	$("#form1").submit(function() {
		var links = $("input#link").val();
		var admission = $("#admission").val();
		var tags = $("#tags").val();
		
		if (links == "http://" || admission == "" || links == "" || tags == "" || tags == "(separated by spaces)") {
			alert ("Please provide Event Links, Admission and Tags.");
			return false;
		} 
		
		if($('input[@name=category]:checked').size() == 0) {
			alert('Please select at least one Category.');
			return false;
		}
		
		if($('input[@name=category]:checked').size() > 5) {
			alert('Please select a maximum of 5 Categories.');
			return false;
		}
	});

});
