function validateEmail(email){
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(email.match(emailRegEx)){
		return true;
	}else{
		return false;
	}
}

function onSuccess(url, msg){
	if(!msg.substring(0,7).match("Error") && !msg.substring(0,7).match("Success") && !msg.substring(0,7).match("GetMail") ){
		$("#enterVoteEmail, #responseMod").fadeOut(500);
		$('#responseMod .moduleContent').html('<h4>Error</h4><p>There was an unidentified error with your vote. Please try again later.</p>');
		$('#responseMod').fadeIn(700);
	} else if(msg.substring(0,7).match("GetMail")) {
		// double check the get mail substring.
		$("#voteEmailForm").attr("action", url );
		$("#enterVoteEmail").fadeIn(700);
	} else {
		var message= msg.match("Error")? msg.substring(7):"Your vote has been placed!";
		$("#enterVoteEmail, #responseMod").fadeOut(500);
		$('#responseMod .moduleContent').html('<h4>'+msg.substring(0,7)+'</h4><p>'+message+'</p>');
		$('#responseMod').fadeIn(700);
	}
}

$(document).ready(function(){
	
	/*
	$("#browse_left").click(function(){ 
		$(".browseBox").not(".lhs").blockLeft();
	});
	$("#browse_right").click(function(){ 
		$(".browseBox").not(".rhs").blockRight();
	});*/
	$("#cancelButton").click(function(){ 
		$("#enterVoteEmail, #responseMod").fadeOut(500);
	});
	
	$('.voteModule a').click(function(){
		
		var url = $(this).attr('href');
		$.ajax({
			type: "GET",
			url: url,
			success: function(msg){
				onSuccess(url, msg);
			}
		});
		
		return false;
	});
	$('#voteEmailForm').submit(function(){
		
		if( validateEmail( $("#voterEmailInput").val() ) ){
			var url = $(this).attr('action');
			$.ajax({
				type: "POST",
				url: url,
				data: $("#voteEmailForm").serialize(),
				success: function(msg){
					onSuccess(url, msg);
				}
			});
		} else {
			alert('Please enter a valid email address.');
		}
		
		return false;
	});
	
	
});