<?php
/*
Template Name: New User Registration
*/
?>

<?php get_header(); ?>

	<?php if (have_posts()) : ?>
	<?php while (have_posts()) : the_post(); ?>
	<?php dt_breadcrumb(); ?>
	
	<div class="maincol">
	
		<!--
		#	header
		-->
		<div class="archiveheader">
			<h1><?php single_post_title(); ?></h1>
			<p><?php _e('Sign up for a free account and make a profile that\'s all about you! Comment, rate content, and more!','news'); ?></p>
		</div>
	
		<div class="post">
		
			<div id="result"></div>
		
			<?php if (is_user_logged_in()) { ?>
				<p><?php printf(__('It seems that you are already registered and signed in as <strong>%1$s</strong>. (<a href="%2$s" title="Sign Out">Not you?</a>)','news'), get_the_author_meta('user_login', $current_user->ID), wp_logout_url( 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] )); ?></p>
				<ul><li><a href="<?php echo get_page_link(get_option('myprofile')); ?>"><?php printf(__('View or update your profile','news')); ?></a></li></ul>
			<?php } else { ?>
		
			<!-- Show signup form -->
			<p><?php _e('Please fill in the form below to signup for a free account. All fields marked with an asterisk (<span class="red">*</span>) are required.','news'); ?></p>
			<form class="signup-form">
				<fieldset>
					<label for="firstname"><?php _e('Enter your first name','news'); ?> (<span class="red">*</span>)</label>
					<input type="text" name="firstname" id="firstname" class="signup-field" />
				</fieldset>
				<fieldset>
					<label for="lastname"><?php _e('Enter your last name','news'); ?> (<span class="red">*</span>)</label>
					<input type="text" name="lastname" id="lastname" class="signup-field" />
				</fieldset>
				<fieldset>
					<label for="username"><?php _e('Please choose a username','news'); ?> (<span class="red">*</span>)</label>
					<input type="text" name="username" id="username" class="signup-field" />
				</fieldset>
				<fieldset>
					<label for="email"><?php _e('Enter your e-mail address','news'); ?> (<span class="red">*</span>)</label>
					<input type="text" name="email" id="email" class="signup-field" />
				</fieldset>
				<fieldset>
					<label for="password"><?php _e('Choose a password','news'); ?> (<span class="red">*</span>)</label>
					<input type="password" name="password" id="password" class="signup-field" />
				</fieldset>
				<fieldset>
					<label for="passwordv"><?php _e('Please re-enter your chosen password','news'); ?> (<span class="red">*</span>)</label>
					<input type="password" name="passwordv" id="passwordv" class="signup-field" />
				</fieldset>
				<input type="button" class="signup-do" value="<?php _e('Register','news'); ?>" />
				<span class="load-small"><img src="<?php bloginfo('template_directory'); ?>/images/loading2.gif" alt="" title="" /></span>
			</form>
			<!-- end signup form -->
		
		<?php } ?>

		</div>

	</div>
	
	<?php get_sidebar(); ?><div class="clear"></div>
	
	<?php endwhile; ?>
	<?php else : ?>
	<?php endif; ?>
	
<script type="text/javascript">
(function($) {

	// Initiate
	$(".signup-do").click(function(){
	
		// show load
		$(".load-small img").show();
	
		// simplify values
		var firstname = $("#firstname").val();
		var lastname = $("#lastname").val();
		var username = $("#username").val();
		var email = $("#email").val();
		var password = $("#password").val();
		var passwordv = $("#passwordv").val();
		
		// validation
		if (firstname == '') {
			$("#result").html('<p class="error"><?php _e('Please fill in your first name.','news'); ?></p>');
			$("#firstname").focus();
			$(".load-small img").hide();
		} else if (lastname == '') {
			$("#result").html('<p class="error"><?php _e('Please fill in your last name.','news'); ?></p>');
			$("#lastname").focus();
			$(".load-small img").hide();
		} else if (username == '') {
			$("#result").html('<p class="error"><?php _e('Please select a user name.','news'); ?></p>');
			$("#username").focus();
			$(".load-small img").hide();
		} else if (email == '') {
			$("#result").html('<p class="error"><?php _e('Please enter your e-mail address.','news'); ?></p>');
			$("#email").focus();
			$(".load-small img").hide();
		} else if (password == '') {
			$("#result").html('<p class="error"><?php _e('Please select a password.','news'); ?></p>');
			$("#password").focus();
			$(".load-small img").hide();
		} else if (passwordv == '') {
			$("#result").html('<p class="error"><?php _e('Please enter your password confirmation.','news'); ?></p>');
			$("#passwordv").focus();
			$(".load-small img").hide();
		} else if (password != passwordv) {
			$("#result").html('<p class="error"><?php _e('Your passwords do not match. Please confirm your password.','news'); ?></p>');
			$("#password").focus();
			$(".load-small img").hide();
		} else {
		
			var query = 'firstname=' + encodeURI(firstname) + '&lastname=' + encodeURI(lastname) + '&username=' + encodeURI(username) + '&email=' + encodeURI(email) + '&password=' + password;

			$("#result").load('<?php bloginfo('template_directory'); ?>/NewUser.php?' + query, function(){
				$(".load-small img").hide();
			});
		
		}
	
	});

}) (jQuery);
</script>

<?php get_footer(); ?>