// PageFlip3D JavaScript Document
// Created by Embark Interactive
///////////////////////////////////

$(document).ready(function() {
	/*
	$("#username").keyup(function(e){
		$('#username-status').hide();
		if(this.value.length > 3){
			$('#username-loader').show();
			$.post("http://www.pageflip3d.com/wp-content/themes/twicet/scripts/pf3d.php", { func: "checkUsername", value:this.value },
			  function(data){
				$('#username-status').html(data.response); 
				if(data.available==1){
					$('#username-status').addClass('available');
					$('#username-status').removeClass('not-available');
				} else {
					$('#username-status').removeClass('available');
					$('#username-status').addClass('not-available');
				}
				
				$('#username-status').show();
				$('#username-loader').hide();
				//alert(data.response);
			  }, "json");
			  
		} else {
			$('#username-loader').hide();
		}
	});		
	*/
	
	$("#email").keyup(function(e){
		$('#email-status').hide();
		if(this.value.length > 3){
			$('#email-loader').show();
			$.post("http://www.pageflip3d.com/wp-content/themes/twicet/scripts/pf3d.php", { func: "checkEmail", value:this.value },
			  function(data){
				$('#email-status').html(data.response); 
				if(data.available==1){
					$('#email-status').addClass('available');
					$('#email-status').removeClass('not-available');
				} else {
					$('#email-status').removeClass('available');
					$('#email-status').addClass('not-available');
				}
				
				$('#email-status').show();
				$('#email-loader').hide();
				//alert(data.response);
			  }, "json");
			  
		} else {
			$('#email-loader').hide();
		}
	});	
	
	$('#signup').submit(function() {
		//console.log('process form');
		$('#form-error').hide();
		var hasError = false;
		// check for valid email
		
		$('#password-status').hide();
		
		if(!isValidEmailAddress($('#email').val())){
			hasError=true;
			$('#email-status').removeClass('available');
			$('#email-status').addClass('not-available');
			$('#email-status').html('Please enter a valid email address.'); 
			$('#email-status').show();
			$('#email-loader').hide();
		}
		
		
		/*
		if($('#password').val()!=$('#password_confirm').val()){
			$('#password-status').removeClass('available');
			$('#password-status').addClass('not-available');
			$('#password-status').html('Password and confrim password do not match.'); 
			$('#password-status').show();
			hasError=true;
		}
		*/
		
		if(!hasError) {
			$('#form-button-group').fadeOut('normal', function() {
				$(this).parent().append('<img src="http://www.pageflip3d.com/wp-content/themes/twicet/js/loader.gif" alt="Loading…" />');
			});
			
			var formInput = $(this).serialize();
			$.post("http://www.pageflip3d.com/wp-content/themes/twicet/scripts/pf3d.php",{ func: "createAccount", value:formInput }, 
			function(data){
				//console.log(data.response);
				if(data.created==1){
					$('#signup_form').slideUp("fast", function() {
						$(this).before('<h2>Thanks for registering with PageFlip3D.</h2><p>Check your email and click on the big Confirmation button.</p><img src="http://www.pageflip3d.com/wp-content/themes/twicet/images/thanks.jpg" /><h2>Next Steps</h2><p>We have sent you an email to confirm your registration. Then you will provide some details so we can get your account all set up. Once reviewed you will be able to log in to your new account and start Publishing!</p>');
					});
				} else {
					$('#signup_form').slideUp("fast", function() {
						$(this).before('<h2>Account Not Created!</h2><p>We were not able to process your request. Your email or username may already be in use. Please check and try again. Thanks for your interest in PageFlip3D.</p><img src="http://www.pageflip3d.com/wp-content/themes/twicet/images/thanks.jpg" /><p>To try again click here: <a class="read-more" href="signup">Try again!</a></p>');
					});
				}
					
			}, "json");
			
		} 
		
		return false;

	});
	
	
	
	
	
	// login form
	$('#login').submit(function() {
		//console.log('process login');
		$('#form-error').hide();
		var hasError = false;
		// check for valid email
		
		
		if($('#loginpassword').val()=='' || $('#loginusername').val()==''){
			$('#form-error').removeClass('available');
			$('#form-error').addClass('not-available');
			$('#form-error').html('You must enter your username and password.'); 
			$('#form-error').show();
			hasError=true;
		}
		
		
		if(!hasError) {
			$('#form-button-group').fadeOut('normal', function() {
				$(this).parent().append('<img src="http://www.pageflip3d.com/wp-content/themes/twicet/js/loader.gif" alt="Loading…" />');
			});
			
			var formInput = $(this).serialize();
			$.post("http://www.pageflip3d.com/wp-content/themes/twicet/scripts/pf3d.php",{ func: "login", value:formInput }, 
			function(data){
				//console.log(data.response);
				if(data.approved==1){
					$('#login').slideUp("fast", function() {
						$(this).before('<h2>Login approved.</h2><p>Redirecting to your account area now.</p><img src="http://www.pageflip3d.com/wp-content/themes/twicet/images/thanks.jpg" />');
					});
					window.location.replace("http://www.pageflip3d.com/account/");

				} else {
					$('#login').slideUp("fast", function() {
						$(this).before('<h2>Login Failed!</h2><p>We were not able to log you in. Please check your username and password and try again.</p><img src="http://www.pageflip3d.com/wp-content/themes/twicet/images/thanks.jpg" /><p>To try again click here: <a class="read-more" href="http://www.pageflip3d.com/login">Try again!</a></p>');
					});
				}
					
			}, "json");
			
		}
		
		return false;

	});
	
	
	
	
	
	
	
	
	



	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}
	
	
});


