// MYO Club Core

$(document).ready(function() {  
	
	// terms & conditions
	$("#termsConditions").click(function(){
	    $.modal('<div><h2>Terms and Conditions</h2><p>One voucher per person</p><p>Offer applies to regular sandwich, roll, wrap or salad</p><p>Not valid with any other offer.</p></div>');
	});
	
	// - Member Login	
	var hmMemberLogin = "Your email address"
	$('.yourusername').focus(function() {
		if($(this).val() == hmMemberLogin) {
			$(this).val("");
		}
	}).blur(function () {
		if($(this).val() == "") {
			$(this).val(hmMemberLogin);
		}
	});
	
	// - Register Now Form Validation
	$(".f_name").focus(function() {
		if($(".f_name").val() == "Please enter your name") {
			$(this).val("").removeAttr("style");
		}
	});
	$(".pass").focus(function() {
		if($(".pass").val() == "pass") {
			$(this).val("").removeAttr("style");
		}
	});
	$(".day, .month, .local").focus(function() {
		$(this).removeAttr("style");
	});
	$(".f_email").focus(function() {
		if($(".f_email").val() == "Please enter your email address") {
			$(this).val("").removeAttr("style");
		}
	});
	$(".joinsubmit, #update_submit").click(function() {
		
		var aErrors = new Array(0);

		if($(".f_name").val().length > 0)
		{
		    if ($(".f_name").val() == "" || $(".f_name").val() == "Please enter your name") {
			aErrors.push("- Please enter your name.");
			$(".f_name").css("color", "#d02a2a").val("Please enter your name");
		    }
    		
		    if($(".f_email").val() == "" || $(".f_email").val() == "Please enter your email address") {
			    $(".f_email").css("color","#d02a2a").val("Please enter your email address");
			    aErrors.push("- Please enter your email address");
		    } else {
    		
		        if(!/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test($('.f_email').val())) {
	                $(".f_email").css("color","#d02a2a");
			        aErrors.push("- Your email address is invalid.");
                }
    			
		    }
    		
		    if ($(".pass").val() == "" || $(".pass").val() == "pass") {
			    aErrors.push("- Please enter a password.");
			    $(".pass").css("color", "#d02a2a").val("pass");
		    }
    		
		    if ($(".day").val() == "Day" && $(".month").val() == "Month") {
			    aErrors.push("- Please select your date of birth.");
			    $(".day, .month").css("color", "#d02a2a");
		    } else if ($(".day").val() == "Day") {
			    aErrors.push("- Please select your birth day.");
			    $(".day").css("color", "#d02a2a");
		    } else if ($(".month").val() == "Month") {
			    aErrors.push("- Please select your birth month.");
			    $(".day").css("color", "#d02a2a");
		    }
    		
		    if($(".local option:selected").hasClass("first")) {
			    aErrors.push("- Please choose your local MYO store.");
			    $(".local").css("color", "#d02a2a");
		    }
    		
		    if($(".sitecontainer").attr("id") == "join") {
			    if (!$(".f_tac").is(":checked")) {
				    aErrors.push("- You must agree to the MYO terms and conditions.");
			    }
		    }

		    if (aErrors.length) {
			    displayErrorsAlert(aErrors);
			    return false;
		    }
    		
		    var postUrl = "/Views/ClubMyo/SignUp.aspx";
		    var formType = $(".submitform").val();
    		
		    if (formType == "confirm")
		    {
		        postUrl = "/Views/ClubMyo/Confirm.aspx";
		    }
		    else if(formType == "update")
		    {
		        postUrl = "/Views/ClubMyo/Update.aspx";
		    }
    		
		    $.ajax({
              type: "POST",
              url: postUrl,
              data: {fullName:$('.f_name').val(),PrevEmailAddress:$('#ctl00_cphRHSColumn_ctl00_f_prevemail').val(),EmailAddress:$('.f_email').val(),Password:$('.pass').val(),BirthDay:$('.day').val(),BirthMonth:$('.month').val(),Local:$('.local :selected').val(),key:$('.key').val()},
              dataType: "html",
		       timeout: 50000,
		       error: function(XMLHttpRequest, textStatus, errorThrown) {
		   		    //console.log("Status: " + textStatus + " \nError Thrown: " + errorThrown);
		       },
              success: function(data) {
            
                if($.trim(data) == "")
                {
                    
                    if (formType == "confirm")
                    {
                         window.location = "/member-profile.aspx";
                    }
                    else if (formType == "update")
                    {
                        $(".joinForm").show();
                        $("#msg").html(data); 
                    }
                    else
                    {
                        $("#join-thanks").show("fast");    
                    }
                }
                else
                {
                    if ($.trim(data) == "logout") {
                        $("#join-form").html('');
                        window.location = window.location;
                    } else {
                        $(".joinForm").show();
                        $("#msg").html(data); 
                    }
                }
          }
        });
		
		return false;
		}
		else
        {

            if ($(".f_name").val() == "") {
            aErrors.push("- Please enter your name.");
            $(".f_name").css("color", "#d02a2a").val("Please enter your name");
            }
            
            if($(".f_email").val() == "" || $(".f_email").val() == "Please enter your email address") {
			    $(".f_email").css("color","#d02a2a").val("Please enter your email address");
			    aErrors.push("- Please enter your email address");
		    } else {
    		
		        if(!/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test($('.f_email').val())) {
	                $(".f_email").css("color","#d02a2a");
			        aErrors.push("- Your email address is invalid.");
                }
    			
		    }
    		
		    if ($(".pass").val() == "" || $(".pass").val() == "pass") {
			    aErrors.push("- Please enter a password.");
			    $(".pass").css("color", "#d02a2a").val("pass");
		    }
    		
		    if ($(".day").val() == "Day" && $(".month").val() == "Month") {
			    aErrors.push("- Please select your date of birth.");
			    $(".day, .month").css("color", "#d02a2a");
		    } else if ($(".day").val() == "Day") {
			    aErrors.push("- Please select your birth day.");
			    $(".day").css("color", "#d02a2a");
		    } else if ($(".month").val() == "Month") {
			    aErrors.push("- Please select your birth month.");
			    $(".day").css("color", "#d02a2a");
		    }
    		
		    if($(".local option:selected").hasClass("first")) {
			    aErrors.push("- Please choose your local MYO store.");
			    $(".local").css("color", "#d02a2a");
		    }
    		
		    if($(".sitecontainer").attr("id") == "join") {
			    if (!$(".f_tac").is(":checked")) {
				    aErrors.push("- You must agree to the MYO terms and conditions.");
			    }
		    }

            if (aErrors.length) {
            displayErrorsAlert(aErrors);
            return false;
            }

        }
	});
	
	$("#join-process").bind("ajaxSend", function(){
		
		if($(".submitform").val() == "update" || $(".submitform").val() == "forgotpassword")
		{
			$("#loginForm").hide();			
			$("#login-process").show();
		}
		else
		{
			$(".joinForm").hide();
		    $(this).show();
		}        
			
     }).bind("ajaxComplete", function(){
		   if($(".submitform").val() == "update" || $(".submitform").val() == "forgotpassword")
			{
				$("#login-process").hide();
				$("#loginForm").show();
			}
			else
			{
				$(this).hide();
			} 
     })
	
	// forgot password functionality
	$("#forgotPassword").click(function() {
		$(".submitform").val("forgotpassword");
		$(".login_submit").val("Send it");
		$("#passwordLabel").html("");
		$("#emailLabel").html("Please enter your email address");
		$(".f_login_passwordtxt").hide("fast");
		return false;
	});

	$(".login_submit").click(function() {
	    var aErrors = new Array(0);
		var isForgotPasswordMode = false;	    

	    if($(".yourusername").val() == "" || $(".yourusername").val() == "Please enter your email address") {
			$(".yourusername").css("color","#d02a2a").val("Please enter your email address");
			aErrors.push("- Please enter your email address");
		} else {
		
		    if(!/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test($('.yourusername').val())) {
	            $(".yourusername").css("color","#d02a2a");
			    aErrors.push("- Your email address is invalid.");
            }
			
		}

		// login mode
		if($(".submitform").val() == "update" || $(".submitform").val() == "true")
		{
			if ($(".f_login_passwordtxt").val() == "" || $(".f_login_passwordtxt").val() == "****") {
				aErrors.push("- Please enter a password.");
				$(".f_login_passwordtxt").css("color", "#d02a2a").val("****");
			}
		}
		else
		{
			$.ajax({
		          type: "POST",
		          url: "/Views/ClubMyo/SignIn.aspx",
		          data: "EmailAddress=" + $('.yourusername').val(),
		          dataType: "html",
				   timeout: 50000,
				   error: function(XMLHttpRequest, textStatus, errorThrown) {
			   		    //console.log("Status: " + textStatus + " \nError Thrown: " + errorThrown);
				   },
		          success: function(data) {
					$(".submitform").val("update");
					$(".login_submit").val("Login");
					$("#passwordLabel").html("Your password");
		            $("#emailLabel").html("Your email address");
		          	$(".f_login_passwordtxt").show("fast");
					$("#loginStatusMsg").html(data); 
		      	  }
		    });
			
			return false;

		}
		
		if (aErrors.length) {
			displayErrorsAlert(aErrors);
			return false;
		}
	});
	
	// forgot password functionality
	$("#confirmationForgotPassword").click(function() {
		$(".confirmed-submit").val("Send it");
		$("#confirmIt").val("forgotpassword");
		$("#emailLabel").html("Please enter your email address");
		$("#loginStatusMsg").hide();
		$("#passwordLabel").hide("fast");
		$(".pass").hide("fast");
		return false;
	});

	$(".confirmed-submit").click(function() {
	    
	    var aErrors = new Array(0);
		var isForgotPasswordMode = false;	    

	    if($(".user").val() == "" || $(".user").val() == "Please enter your email address") {
			$(".user").css("color","#d02a2a").val("Please enter your email address");
			aErrors.push("- Please enter your email address");
		} else {
		
		    if(!/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test($('.user').val())) {
	            $(".user").css("color","#d02a2a");
			    aErrors.push("- Your email address is invalid.");
            }
			
		}

		if($("#confirmExisting").val() == "existing")
		{
			
			if ($(".name").val() == "" || $(".name").val() == "Please enter your name") 
			{
				aErrors.push("- Please enter your name.");
				$(".name").css("color", "#d02a2a").val("Please enter your name");
		    }	
			
			if($(".local :selected").val() == "Select location---" || $(".local :selected").val() == "") 
			{
			    aErrors.push("- Please choose your local MYO store.");
			    $(".local").css("color", "#d02a2a");
		    }

			if ($(".day").val() == "Day" && $(".month").val() == "Month") 
			{
			    aErrors.push("- Please select your date of birth.");
			    $(".day, .month").css("color", "#d02a2a");
		    } 
			else if ($(".day").val() == "Day") 
			{
			    aErrors.push("- Please select your birth day.");
			    $(".day").css("color", "#d02a2a");
		    } 
			else if ($(".month").val() == "Month") 
			{
			    aErrors.push("- Please select your birth month.");
			    $(".day").css("color", "#d02a2a");
		    }

		}

		// login mode
		if($("#confirmIt").val() == "login")
		{
			if ($(".pass").val() == "" || $(".pass").val() == "pass") {
				aErrors.push("- Please enter a password.");
				$(".pass").css("color", "#d02a2a").val("pass");
			}
		}
		else
		{
			
			$.ajax({
		          type: "POST",
		          url: "/Views/ClubMyo/SignIn.aspx",
		          data: "EmailAddress=" + $('.user').val(),
		          dataType: "html",
				   timeout: 50000,
				   error: function(XMLHttpRequest, textStatus, errorThrown) {
			   		    //console.log("Status: " + textStatus + " \nError Thrown: " + errorThrown);
				   },
		          success: function(data) {
					$("#confirmIt").val("login");
					$(".confirmed-submit").val("Login");
		          	$(".pass").show("fast");
		          	$("#passwordLabel").show("fast");
		            $("#emailLabel").html("Your email address");
		          	$("#loginStatusMsg").show();
					$("#loginStatusMsg").html(data);
		      	  }
		    });
			
			return false;

		}
		
		if (aErrors.length) {
			displayErrorsAlert(aErrors);
			return false;
		}
	});
	
	$(".local").change(function() {
		
		if($("option:selected", this).attr("disabled")) {
			$(".local .first").attr('selected', 'selected');
		}
		
	});
	
	
	// - Balance Columns
	var lColHeight = $(".main-copy-inner").height();
	var rColHeight = $(".right-column").height();
	
	if(rColHeight > lColHeight) {
		$(".main-copy-inner").height(rColHeight - 50);
	}
	
	// - New Window Link
	$(".new-window").attr("target","_blank");

});

function displayErrorsAlert(aErrors) {

    if (aErrors.length) {
        if (aErrors.length > 1) {
            var prefix = 'Some errors occurred:\n\n';
            var suffix = '\nPlease correct these errors and try again.';
        }
        else {
            var prefix = 'An error has occurred:\n\n';
            var suffix = '\nPlease correct this error and try again.';
        }

        var errorMessage = prefix;

        for (var i = 0; i < aErrors.length; i++) {
            errorMessage += aErrors[i] + "\n";
        }

        errorMessage += suffix;

        alert(errorMessage);
    }

}
	

