	
	$(function() {
		$('#submit_friend').bind('click',ShowDialog);
		$('#submit_friend2').bind('click',ShowDialog);
		$('#close').bind('click',function(event) {
			event.preventDefault();
			$("#modal").dialog('close');
			$('#blockrandom').show();
			$('#fake_form').hide();
		});
		$('#signup_btn').bind('click',Signup);
		
		$('#show_widget').bind('click',ShowDialog2);
		$('#close_widget').bind('click',function(event) {
			event.preventDefault();
			$("#modal2").dialog('close');
			$('#blockrandom').show();
			$('#fake_form').hide();
		});
		
		$('.text').bind('focus', function(event) {
			var id = $(this).attr('id');
			var last = $(this).val();
			
			if (id == 'from_fname' && last == 'Your first name') {
				$(this).val('');
			}
			
			if (id == 'from_lname' && last == "Your last name") {
				$(this).val('');
			}
			
			if (id == 'from_email' && last == 'Your email') {
				$(this).val('');
			}
			
			if (id == 'to_email' && last == "Your friend's email") {
				$(this).val('');
			}
		});
		
		$('.text').bind('blur', function(event) {
			var id = $(this).attr('id');
			
			if ($(this).val() == '')
			{
				if (id == 'from_fname') {
					$(this).val('Your first name');
				}
				
				if (id == 'from_email') {
					$(this).val('Your email');
				}
				
				if (id == 'to_email') {
					$(this).val("Your friend's email");
				}
				
				if (id == 'from_lname') {
					$(this).val("Your last name");
				}
			}
		});
		var btns = {}
		$('#modal').dialog({autoOpen:false,modal:true,width:'530px',height:'580px',resizable:false,buttons:btns,title:''});
		$('#modal2').dialog({autoOpen:false,modal:true,width:'506px',height:'530px',resizable:false});
	});

	var templateMessage = "Dear friend,\n\n\As you know, the upcoming presidential election will be one of the most important of our lives. Deadlines are quickly approaching. Please make sure you're registered to vote.\n\nVisit http://www.naacp.org/vote to register to vote today.\n\nMake sure your voice is heard on November 4th, and visit http://www.naacp.org/vote to get started.\n\nThanks,\n[From_Name]";
	
function ShowDialog(event) {
	$('#blockrandom').hide();
	$('#fake_form').show();
	var message = templateMessage.replace('[From_Name]',($('#from_fname').val() == 'Your first name') ? '' : $('#from_fname').val());
	$("#fname").val(($('#from_fname').val() == 'Your first name') ? '' : $('#from_fname').val());
	$("#lname").val(($('#from_lname').val() == 'Your last name') ? '' : $('#from_lname').val());
	$("#friend_email").val(($("#from_email").val() == 'Your email') ? '' : $("#from_email").val());
	
	$("#subject").val("Register to Vote!");
	$("#message").val(message);
	$('#modal').dialog('open');
	
	if ($("#to_email").val() != '' && $("#to_email").val() != "Your friend's email") {
		AddRecipient($("#to_email").val());
		RenderRecipients();
	}
	event.preventDefault();
}

function ShowDialog2(event) {
	$('#modal2').dialog('open');
	$('#blockrandom').hide();
	$('#fake_form').show();
	event.preventDefault();
}

function Signup(event) {
	if (!VerifyEmail($('#signup_email').val())) {
		alert('Enter a valid email');
		event.preventDefault();	
	}
}

function VerifyEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email);
}