$(document).ready(function() { 
	
	//make sure the toggle on the membership for default is checked.
	$('#create-regular-account').attr('checked', 'checked');
	
    // validate signup form on keyup and submit 
    var validator = $("#create_account").validate({ 
        rules: { 
           first_name: "required", 
            last_name: "required", 
 			email_field: { 
                required: true, 
                email: true 
            },
 			email_confirm: {
				required: true,
				email: true
				//equalTo: ".email_regular_field"
			},
            password_field: { 
                required: true, 
                minlength: 5 
            }, 
            confirm_field: { 
                required: true, 
                minlength: 5, 
                equalTo: ".password_field" 
            },
            item_name: {
            	required: true
            }, 
            terms_conditions: "required"
        }, 
        messages: { 
            first_name: "Enter your firstname<br />", 
            last_name: "Enter your lastname<br />", 
 			email_field: { 
                required: "Please enter a valid email address<br />", 
                minlength: "Please enter a valid email address<br />" 
            },
			email_confirm: {
				required: "Repeat your email<br />",
				minLength: "Please enter a valid email address<br />",
				equalTo: "Enter the same email as above<br />"
			},
            password_field: { 
                required: "Provide a password<br />", 
                rangelength: jQuery.format("Enter at least {0} characters<br />") 
            }, 
            confirm_field: { 
                required: "Repeat your password<br />", 
                minlength: jQuery.format("Enter at least {0} characters<br />"), 
                equalTo: "Enter the same password as above<br />" 
            },
            item_name:{
            	required: "Please select a Membership Option<br />"
            }, 
            terms_conditions: "You must agree to the terms and conditions<br />" 
        }, 
        // the errorPlacement has to take the table layout into account 
        errorPlacement: function(error, element) { 
            if ( element.is(":radio") ) 
                error.prependTo( element.parent().parent() ); 
            else if ( element.is(":checkbox") ) 
                error.prependTo( element.parent()); 
            else 
                error.prependTo( element.parent()); 
        }
        
	});
	
	// validate signup form on keyup and submit 
    var validator2 = $("#create_gift_account").validate({ 
        rules: { 
           	first_name: "required", 
            last_name: "required", 
 			email_field: { 
                required: true, 
                email: true 
            },
 			email_confirm: {
				required: true,
				email: true,
				equalTo: '.email_gift_field'
			},
			recepient_name: {
				required: true
			},
			item_name: {
            	required: true
            }, 
            terms_conditions: "required"
        }, 
        messages: { 
            first_name: "Enter your firstname<br />", 
            last_name: "Enter your lastname<br />", 
 			email_field: { 
                required: "Please enter a valid email address<br />", 
                minlength: "Please enter a valid email address<br />" 
            },
			email_confirm: {
				required: "Repeat your email<br />",
				minLength: "Please enter a valid email address<br />",
				equalTo: "Enter the same email as above<br />"
			},
			recepient_name: {
				required: "Please enter the recepients name<br />"
			},
            item_name:{
            	required: "Please select a Membership Option<br />"
            }, 
            terms_conditions: "You must agree to the terms and conditions<br />" 
        }, 
        // the errorPlacement has to take the table layout into account 
        errorPlacement: function(error, element) { 
            if ( element.is(":radio") ) 
                error.prependTo( element.parent().parent() ); 
            else if ( element.is(":checkbox") ) 
                error.prependTo( element.parent()); 
            else 
                error.prependTo( element.parent()); 
        }
        
	});
	
	// validate signup form on keyup and submit 
    var validator2 = $("#create_free_account").validate({ 
        rules: { 
           	first_name: "required", 
            last_name: "required", 
 			email_field: { 
                required: true, 
                email: true 
            }
        }, 
        messages: { 
            first_name: "Enter your firstname<br />", 
            last_name: "Enter your lastname<br />", 
 			email_field: { 
                required: "Please enter a valid email address<br />", 
                minlength: "Please enter a valid email address<br />" 
            } 
        }, 
        // the errorPlacement has to take the table layout into account 
        errorPlacement: function(error, element) { 
            if ( element.is(":radio") ) 
                error.prependTo( element.parent().parent() ); 
            else if ( element.is(":checkbox") ) 
                error.prependTo( element.parent()); 
            else 
                error.prependTo( element.parent()); 
        }
        
	});
	
	

	//function to hide regular account form
	$('#create-regular-account').click(function(){
		$("#create_gift_account").hide("slow");
		$("#create_free_account").hide("slow");
		$("#create_account").show("slow");
	});
	
	//function to show regular account form
	$('#create-gift-account').click(function(){
		$("#create_account").hide("slow");
		$("#create_free_account").hide("slow");
		$("#create_gift_account").show("slow");
	});
	
	//function to show gift account
	$('#create-free-account').click(function(){
		$("#create_account").hide("slow");
		$("#create_gift_account").hide("slow");
		$("#create_free_account").show("slow");
	});
	

/*********************************************************************
*	Below is for the Free account
**********************************************************************/	

	// Store the selectors
	var freeAccountForm = $('#create_free_account');
	var submitStatus = $('#submitStatus');
	$('#free-submit').click(function(){
		
		var firstName = $('#first_name').val(); // Get the First name
		var lastName = $('#last_name').val(); // Get the last name
		var emailAddress = $('#email_field').val(); // Get the email address
		
		var datastring = "ajax=true";
		datastring = datastring + "&firstName=" + firstName + "&lastName=" + lastName + "&email=" + emailAddress;
		
		//ajax-loader
		submitStatus.html('<span class="status-text"><img src="/wp-content/themes/mytimecalendars/images/ajax-loader.gif" height="16" width="16" />Subscribing...</span>');
			
		$.ajax({
			type: "POST",
			url:  "/free-membership.php",
			data: datastring,
			success: function(response) {
				if(response == "true"){
					submitStatus.html('<span class="success-text">Subscription Successfull!</span>')
						.fadeIn('slow')
						.animate({opacity: 1.0}, 2000)
						.fadeOut('slow', function() {
							submitStatus.html('<span class="thank-you">Thank You!</span>')
							.fadeIn('slow')
							.animate({opacity: 1.0}, 2000)
							.fadeOut('slow', function(){
								freeAccountForm.submit();
							});
						});				
				}else if(response == "error"){
					submitStatus.html('<span class="success-text">Error: All Fields are required!</span>')
					.fadeIn('slow')
					.animate({opacity: 1.0}, 2000)
					.fadeOut('slow', function(){
						submitStatus.html('<span class="thank-you">Please try again!</span>')
						.fadeIn('slow')
						.animate({opacity: 1.0}, 2000)
						.fadeOut('slow');
					});
				}else{
					submitStatus.html('<span class="success-text">Error: Already Subscribed!</span>')
					.fadeIn('slow')
					.animate({opacity: 1.0}, 2000)
					.fadeOut('slow', function(){
						submitStatus.html('<span class="thank-you">Please try a different email!</span>')
						.fadeIn('slow')
						.animate({opacity: 1.0}, 2000)
						.fadeOut('slow');
					});
				}	
			}
		}); 
		
		return false;
		
	});
/*********************************************************************
*	Below is for the Regular Subscription account
*	
*
**********************************************************************/
	// Store the selectors
	var createAccountForm = $('#create_account');
	var validateCoupon = $('#validateCoupon');
	
	//validate the coupon
	$('#regular-submit').click(function(){
		
		var coupon = $('#coupon').val(); // Get coupon value
		var amount = $('#amount').val(); // Get the Subscription amount
		var membership_option = $('#membership_option').val(); // Get the membership option chosen.
		
		//if coupon field is blank - submit the form
		if(coupon == ""){
			return true;
		}else{
			//is the coupon invalid variable for future storage
			var invalid = "";
			
			//ajax-loader
			validateCoupon.removeClass('error').html('<img src="/wp-content/themes/mytimecalendars/images/ajax-loader.gif" height="16" width="16" /><span class="status-text">Checking coupon...</span>');
			
			//ajax function to check the coupon against the database
			$.ajax({
				url: '/wp-content/includes/check-coupon.php',
				data: 'coupon=' + coupon + '&amount=' + amount + '&membership_option=' + membership_option,
				type: 'POST',
				success: function(results){
					var csv = results.split(",");
					var newAmount = csv[0];
					var discountAmount = csv[1];
					var isGift = csv[2];
					
					//If it comes back as invalid
					if(newAmount == "Invalid Coupon"){
						invalid = "yes";
						validateCoupon.html('');
						$('#validateCoupon').html('<span class="inactive-text">'+newAmount+'</span>');														
					
					//If an error code of 1 comes back	
					}else if(newAmount == "1"){
						invalid = "yes";
						validateCoupon.html('');
						$('#validateCoupon').html('<span class="error-text">'+discountAmount+'</span>');
						
					//If nothing else - submit the form	
					}else{
					
						if(isGift == "yes" || newAmount == "0"){
							//take away the ajax-loader.gif
							validateCoupon.html('');
							
							//set the new amount in the amount field
							$('#amount').attr('value', newAmount);
							
							//set the url to go somewhwere else other than paypal
							createAccountForm.attr('action', '/new-membership.php');
							
							validateCoupon.html('<span class="success-text">Coupon Valid: Your new total is $'+newAmount+'</span>')
							.fadeIn('slow')
							.animate({opacity: 1.0}, 3000)
							.fadeOut('slow', function(){
								validateCoupon.html('<img class="redirect-img" src="/wp-content/themes/mytimecalendars/images/ajax-loader.gif" height="16" width="16" /><span class="redirection">Creating your account...</span>')
								.fadeIn('slow')
								.animate({opacity: 1.0}, 2000, function(){
									validateCoupon.html('<img class="redirect-img" src="/wp-content/themes/mytimecalendars/images/ajax-loader.gif" height="16" width="16" /><span class="redirection">Redirecting...</span>')
									.fadeIn('slow')
									.animate({opacity: 1.0}, 2000, function(){
										createAccountForm.submit();
									});
								});
							});
						}else{
							//take away the ajax-loader.gif
							validateCoupon.html('');
							
							//set the new amount in the amount field
							$('#amount').attr('value', newAmount);
							
							
							validateCoupon.html('<span class="success-text">Coupon Valid: Your new total is $'+newAmount+'</span>')
							.fadeIn('slow')
							.animate({opacity: 1.0}, 3000)
							.fadeOut('slow', function() {
								validateCoupon.html('<img class="redirect-img" src="/wp-content/themes/mytimecalendars/images/ajax-loader.gif" height="16" width="16" /><span class="redirection">Redirecting to paypal...</span>')
								.fadeIn('slow')
								.animate({opacity: 1.0}, 2000, function(){
									createAccountForm.submit();
								});
							});
						}	
					}
				}
			});
			
			//make sure it doesn't submit until after it gets done.
			return false;
		}
		
		//submit only if invalid is no
		if(invalid == "no"){
			return true;
		}else{
			//else don't submit.
			return false;
		}
	});
	
	
	
/*********************************************************************
*	Below is for the Regular Gift Option account
**********************************************************************/

	// Store the selectors
	var createGiftAccountForm = $('#create_gift_account');
	var validateGiftCoupon = $('#validateGiftCoupon');
	
	//validate the coupon
	$('#gift-submit').click(function(){
		
		var coupon = $('#giftCoupon').val(); // Get coupon value
		var amount = $('#create_gift_account #amount').val(); // Get the Subscription amount
		var membership_option = $('#create_gift_account #membership_option').val(); // Get the membership option chosen.
		
		//if coupon field is blank - submit the form
		if(coupon == ""){
			return true;
		}else{
			//is the coupon invalid variable for future storage
			var invalid = "";
			
			//ajax-loader
			validateGiftCoupon.removeClass('error').html('<img src="/wp-content/themes/mytimecalendars/images/ajax-loader.gif" height="16" width="16" /><span class="status-text">Checking coupon...</span>');
			
			//ajax function to check the coupon against the database
			$.ajax({
				url: '/wp-content/includes/check-coupon.php',
				data: 'coupon=' + coupon + '&amount=' + amount + '&membership_option=' + membership_option,
				type: 'POST',
				success: function(results){
					var csv = results.split(",");
					var newAmount = csv[0];
					var discountAmount = csv[1];
					
					//If it comes back as invalid
					if(newAmount == "Invalid Coupon"){
						invalid = "yes";
						validateGiftCoupon.html('');
						$('#validateGiftCoupon').html('<span class="inactive-text">'+newAmount+'</span>');														
					
					//If an error code of 1 comes back	
					}else if(newAmount == "1"){
						invalid = "yes";
						validateGiftCoupon.html('');
						$('#validateGiftCoupon').html('<span class="error-text">'+discountAmount+'</span>');
						
					//If nothing else - submit the form	
					}else{
						if(newAmount == "0"){
							//take away the ajax-loader.gif
							validateGiftCoupon.html('');
							
							//set the new amount in the amount field
							$('#create_gift_account #amount').attr('value', newAmount);
							
							//set the url to go somewhwere else other than paypal
							createGiftAccountForm.attr('action', '/new-membership-gift.php');
							
							validateGiftCoupon.html('<span class="success-text">Coupon Valid: Your new total is $'+newAmount+'</span>')
							.fadeIn('slow')
							.animate({opacity: 1.0}, 3000)
							.fadeOut('slow', function(){
								validateGiftCoupon.html('<img class="redirect-img" src="/wp-content/themes/mytimecalendars/images/ajax-loader.gif" height="16" width="16" /><span class="redirection">Sending Emails...</span>')
								.fadeIn('slow')
								.animate({opacity: 1.0}, 2000, function(){
									validateGiftCoupon.html('<img class="redirect-img" src="/wp-content/themes/mytimecalendars/images/ajax-loader.gif" height="16" width="16" /><span class="redirection">Redirecting...</span>')
									.fadeIn('slow')
									.animate({opacity: 1.0}, 2000, function(){
										createGiftAccountForm.submit();
									});
								});
							});
						}else{
						
							//take away the ajax-loader.gif
							validateGiftCoupon.html('');
							
							//set the new amount in the amount field
							$('#create_gift_account #amount').attr('value', newAmount);
							
							
							validateGiftCoupon.html('<span class="success-text">Coupon Valid: Your new total is $'+newAmount+'</span>')
							.fadeIn('slow')
							.animate({opacity: 1.0}, 3000)
							.fadeOut('slow', function() {
								validateGiftCoupon.html('<img class="redirect-img" src="/wp-content/themes/mytimecalendars/images/ajax-loader.gif" height="16" width="16" /><span class="redirection">Redirecting to paypal...</span>')
								.fadeIn('slow')
								.animate({opacity: 1.0}, 2000, function(){
									createGiftAccountForm.submit();
								});
							});	
						}
					}
				}
			});
			
			//make sure it doesn't submit until after it gets done.
			return false;
		}
		
		//submit only if invalid is no
		if(invalid == "no"){
			return true;
		}else{
			//else don't submit.
			return false;
		}
	});

	 
});


