$(function(){
	$("#pwrecoverydialog").dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false
	});
		
	$('#pwrecoverylink').click(function(e) {
		e.preventDefault();
		$('#presend').show();
		$('#pwrecoveryEmail').val('');
		$('#postsend').html('');
		$('#ajaxloadicon').hide();
		$('#pwrecoverySubmitBtn').show();
		$("#pwrecoverydialog").dialog('open');
	});
	
	$('#pwrecoverySubmitBtn').click(function() {
		var email = $('#pwrecoveryEmail').val();
		$('#pwrecoveryEmail').val('');
		$('#postsend').html('');
		
		if (email != '')
		{
			$('#pwrecoverySubmitBtn').hide();
			$('#ajaxloadicon').show();
			
			$.post("./index.php?/ajax/pwrecovery/", { email : email}, function(data){
				if (data == '1')
				{
					$('#postsend').html('<br /><br />Your password has been sent to you by email.');
					$('#presend').hide();
				}
				else if (data == '-1')
				{
					$('#postsend').html('<br /><br />We\'re sorry, your request could not be completed because of a server error. Please try again later.');
					$('#presend').hide();
				}
				else
				{
					$('#postsend').html('We\'re sorry, your request could not be completed. Please make sure you\'ve entered the correct address and try again.');
				}
				$('#ajaxloadicon').hide();
				$('#pwrecoverySubmitBtn').show();
			});
		}
	});
});
