$(document).ready(function(){

	$("form#createUser").submit(function(e){
		e.preventDefault();
		
		var files = new Array();
		$("input[name='files[]']:checked").each(function(i){
			files[i] = $(this).val();
		});
		
		errors = '';
		if ((files.length < 1))
		{
			errors += 'Please select one or more documents.';
		}
		if ($("input#new_username").val()=='')
		{
			errors += ' Please provide a new username.';
		}
		if (errors.length != 0)
		{
			alert(errors);
		}
		else {
			$.ajax(
				{
					type:"POST",
					url: "generate_user.php",
					data: ({
						files: JSON.stringify(files),
						username: $("input#new_username").val()
						//formData: $("form#createUser").serialize()
					}),
					success: function(password){
						$(".results").remove();
						$("form#createUser").after("<div class='results'>Username: "
													+"<strong>"+$("input#new_username").val()+"</strong><br />"
													+"Password: <strong>"+password+"</strong><br />"
													+"<p>This username/password will expire <strong>48 hours</strong> after first login.</p>"
													+"</div>");
						$("input[type='checkbox']").removeAttr('checked');
						$("input[type='text']").val('');
					}
				}
			);
			
		}
	});

});
