﻿$(document).ready(function() {
    $(".RegistrationContainer").hide();
    
    $(".ShowFormButton").click(function() {
        $(".LoginNewUserText").hide();
        $(".RegistrationContainer").show();
        $(".Login").css('height','550px');
        return false;
    });
    
	$(".RegisterButton").click(function() {
	    var isValid;
	    var errorList;
	    errorList = "<ul class='ErrorMessageLabel'>";
	    isValid = true;
	    
	    if($(".RegisterEmailTextBox").val().length == 0){
	        $(".RegisterEmailTextBox").addClass("FormTextBoxError");
	        $(".RegisterEmailLabel").addClass("FormLabelError");
	        errorList += "<li>Please enter an email address.</li>";
	        isValid = false;
	    }
	    if($(".RegisterEmailTextBox").val() != $(".RegisterConfirmEmailTextBox").val()){
	        $(".RegisterConfirmEmailTextBox").addClass("FormTextBoxError");
	        $(".RegisterConfirmEmailLabel").addClass("FormLabelError");
	        $(".RegisterEmailTextBox").addClass("FormTextBoxError");
	        $(".RegisterEmailLabel").addClass("FormLabelError");
	        errorList += "<li>Email Address and Confirm Email Address do no match.</li>";
	        isValid = false;
	    }
	    if($(".RegisterPasswordTextBox").val().length == 0){
	        $(".RegisterPasswordTextBox").addClass("FormTextBoxError");
	        $(".RegisterPasswordLabel").addClass("FormLabelError");
	        errorList += "<li>Please enter your password.</li>";
	        isValid = false;
	    }
	    if($(".RegisterPasswordTextBox").val() != $(".RegisterConfirmPasswordTextBox").val()){
	        $(".RegisterConfirmPasswordTextBox").addClass("FormTextBoxError");
	        $(".RegisterConfirmPasswordLabel").addClass("FormLabelError");
	        $(".RegisterPasswordTextBox").addClass("FormTextBoxError");
	        $(".RegisterPasswordLabel").addClass("FormLabelError");
	        errorList += "<li>Password and Confirm Password do not match.</li>";
	        isValid = false;
	    }
	    if($(".RegisterQuestionsDropdown").val() == "-1") {
	        $(".RegisterQuestionsDropdown").addClass("FormSelectBoxError");
	        errorList += "<li>Please select a password reset question.</li>";
	        isValid = false;
	    }
	    if($(".RegisterPasswordAnswer").val().length == 0){
	        $(".RegisterAnswerLabel").addClass("FormLabelError");
	        errorList += "<li>Please enter a password reset answer</li>";
	        isValid = false;
	    }
        if(!isValid)
        {
            errorList += "</ul>";
            $(".RegistrationErrors").html(errorList);
            $(".Login").css('height','650px');
		    return false;
		}
	});
	
	 $(".MultShipToLinkButton").click(function() {
        //enableDisableMultShipToButton(false);
	    isValidBA = true;
	    isValidSH = true;
	        
	    /* we need to make sure we remove any error classes that my exist first, otherwise, they'll never be removed*/	
        removeErrorClasses();
	    
	    isValidBA = validateBillingAddress();
	    
		if(!isValidBA)
		{
		    $(".MultShipToLinkButton:disabled").removeAttr('disabled');
		    scroll(0,0);
		    return false;
		}
		
	});
	
	 $(".PaymentReviewButton").click(function() {
	    isValidBA = true;
	    isValidSH = true;
	        
	    /* we need to make sure we remove any error classes that my exist first, otherwise, they'll never be removed*/	
        removeErrorClasses();
	    
	    isValidBA = validateBillingAddress();
	    
	    // validate the shipping address if we need to
	    // if they are not shipping to their billing address, and they have not select an address from the radio button group, then we need to 
	    // validate the shipping form.
	    if(!$('.SHAlsoShippingAddressCheckBox').hasClass("SHAlsoShippingAddressCheckBoxChecked")){ 
	        if($(".ShippingAddressesRadioButtons input:checked").val() == 'NewAddress' || $(".ShippingAddressesRadioButtons input").val() == undefined){
                isValidSH = validateShippingAddress();
            }
	    }
		if(!isValidBA || !isValidSH)
		{
		    scroll(0,0);
		    return false;
		}
	});
	
	$(".ShippingAddressesRadioButtons").click(function() {
	    if($(".SHAlsoShippingAddressCheckBox").hasClass("SHAlsoShippingAddressCheckBoxChecked"))
	    {
	        toggleShippingAddressForm(false, true);
	        $('.SHAlsoShippingAddressCheckBox input').removeAttr("checked");
	        $(".SHAlsoShippingAddressCheckBox").removeClass("SHAlsoShippingAddressCheckBoxChecked");
	    }
	    
	    if($(".ShippingAddressesRadioButtons input:checked").val() == 'NewAddress' || $(".ShippingAddressesRadioButtons input").val() == undefined){

	        $(this).removeClass("ShippingAddressesRadioButtonsChecked");
	        toggleShippingAddressForm(false, false);
	    } else {
	        $(this).addClass("ShippingAddressesRadioButtonsChecked");
	        toggleShippingAddressForm(true, false);
	    }
	});
	
	$('.SHAlsoShippingAddressCheckBox').click(function(){
	
	    if($(".ShippingAddressesRadioButtons").hasClass("ShippingAddressesRadioButtonsChecked"))
	    {
	        toggleShippingAddressForm(false, false)
	        $(".ShippingAddressesRadioButtons input")[0].checked = true;
	        $(".ShippingAddressesRadioButtons").removeClass("ShippingAddressesRadioButtonsChecked");
	    }
	
	    var isValidSH;
	    removeErrorClasses();
	    if(!$(this).hasClass("SHAlsoShippingAddressCheckBoxChecked")) {
	        isValidSH = validateBillingAddress();
	        if(isValidSH)
	        {
	            $(".MultShipToLinkButton").attr('disabled','disabled');
	            $(this).addClass("SHAlsoShippingAddressCheckBoxChecked");
	            toggleShippingAddressForm(true, true);
	            checkForBillingAddressChange(true);
	        } else {
	            $(".MultShipToLinkButton:disabled").removeAttr('disabled');
	            $('.SHAlsoShippingAddressCheckBox input').removeAttr("SHAlsoShippingAddressCheckBoxChecked");
	        }
	    } else {
	        $(".MultShipToLinkButton:disabled").removeAttr('disabled');
	        $(this).removeClass("SHAlsoShippingAddressCheckBoxChecked");
	        toggleShippingAddressForm(false, true);
	        checkForBillingAddressChange(false);
	    }
	})
	
        
    function toggleShippingAddressForm(enabled, autofill) {
        var arrFields = ["FirstNameTextBox","LastNameTextBox","Address1TextBox","Address2TextBox","CityTextBox","StateDDL","ZipTextBox","ZipExtTextBox","DayTimeTextBox1","DayTimeTextBox2","DayTimeTextBox3","EveningPhoneTextBox1","EveningPhoneTextBox2","EveningPhoneTextBox3"];

        if(enabled)
        {
            $.each(arrFields, function() {
                if(autofill)
                {
                    $(".SH" + this).val($(".BA" + this).val())
                }
                $(".SH" + this).attr("disabled", "disabled");
            })
        }
        else
        {
            $.each(arrFields, function() {
                if(autofill)
                {
                    $(".SH" + this).val('')
                }
                $(".SH" + this).removeAttr("disabled", "disabled");
            })
            $(".SHStateDDL").val("-1")
        }
    }
    
    function checkForBillingAddressChange(checkKeyUp) {
        var arrFields = ["BAFirstNameTextBox","BALastNameTextBox","BAAddress1TextBox","BAAddress2TextBox","BACityTextBox","BAZipTextBox","BAZipExtTextBox","BADayTimeTextBox1","BADayTimeTextBox2","BADayTimeTextBox3","BAEveningPhoneTextBox1","BAEveningPhoneTextBox2","BAEveningPhoneTextBox3"];
        if(checkKeyUp) {
            $.each(arrFields, function() {
                $("." + this).one("keyup", billingChanged);
                $('.BAStateDDL').change(billingChanged);
            })
        } else {
            $.each(arrFields, function() {
                $("." + this).unbind("keyup");
            })
        }
    }
    
    function billingChanged() {
        var arrFields = ["BAFirstNameTextBox","BALastNameTextBox","BAAddress1TextBox","BAAddress2TextBox","BACityTextBox","BAZipTextBox","BAZipExtTextBox","BADayTimeTextBox1","BADayTimeTextBox2","BADayTimeTextBox3","BAEveningPhoneTextBox1","BAEveningPhoneTextBox2","BAEveningPhoneTextBox3"];
        toggleShippingAddressForm(false, true);
        $('.SHAlsoShippingAddressCheckBox input').removeAttr("checked");
        $('.SHAlsoShippingAddressCheckBox').removeClass("SHAlsoShippingAddressCheckBoxChecked");
        
        $.each(arrFields, function() {
            $("." + this).unbind("keyup");
        })
    }
    
    function removeErrorClasses() {
    	$("label").removeClass("FormLabelError");
	    $(":input").removeClass("FormTextBoxError");
    }
    
    function validateBillingAddress() {
        var isValidBA;
        var billingErrorList;
        isValidBA = true;
        billingErrorList = "<ul class='ErrorMessageLabel'>";
        
        if($(".BAFirstNameTextBox").val().length == 0){
	        $(".BAFirstNameTextBox").addClass("FormTextBoxError");
	        $(".BAFirstNameLabel").addClass("FormLabelError");
	        billingErrorList += "<li>Please enter your first name.</li>";
	        isValidBA = false;
	    }
	    if($(".BALastNameTextBox").val().length == 0){
	        $(".BALastNameTextBox").addClass("FormTextBoxError");
	        $(".BALastNameLabel").addClass("FormLabelError");
	        billingErrorList += "<li>Please enter your last name.</li>";
	        isValidBA = false;
	    }	    
	    if($(".BAAddress1TextBox").val().length == 0){
	        $(".BAAddress1TextBox").addClass("FormTextBoxError");
	        $(".BAAddress1Label").addClass("FormLabelError");
	        billingErrorList += "<li>Please enter your address.</li>";
	        isValidBA = false;
	    }	    
	    if($(".BACityTextBox").val().length == 0){
	        $(".BACityTextBox").addClass("FormTextBoxError");
	        $(".BACityLabel").addClass("FormLabelError");
	        billingErrorList += "<li>Please enter your city.</li>";
	        isValidBA = false;
	    }	 	    
	    if($(".BAStateDDL").val() == "Select State"){
	        $(".BAStateDDL").addClass("FormDropDownBoxError");
	        $(".BAStateLabel").addClass("FormLabelError");
	        billingErrorList += "<li>Please enter your state.</li>";
	        isValidBA = false;
	    }
	    if(!$(".BAZipTextBox").val().match(/\d{5}/)){
	        $(".BAZipTextBox").addClass("FormTextBoxError");
	        $(".BAZipLabel").addClass("FormLabelError");
	        billingErrorList += "<li>Please enter your state.</li>";
	        isValidBA = false;
	    }
	    if($(".BAZipExtTextBox").val().length > 0 && !$(".BAZipExtTextBox").val().match(/\d{4}/)){
	        $(".BAZipExtTextBox").addClass("FormTextBoxError");
	        $(".BAZipLabel").addClass("FormLabelError");
	        billingErrorList += "<li>Please enter a valid zip code extension.</li>";
	        isValidBA = false;
	    }
	    if(!$(".BADayTimeTextBox1").val().match(/\d{3}/)){
	        $(".BADayTimeTextBox1").addClass("FormTextBoxError");
	        $(".BADayTimePhoneLabel").addClass("FormLabelError");
	        $(".BADayTimeTextBox2").addClass("FormTextBoxError");
	        $(".BADayTimePhoneLabel").addClass("FormLabelError");
	        $(".BADayTimeTextBox3").addClass("FormTextBoxError");
	        $(".BADayTimePhoneLabel").addClass("FormLabelError");
	        billingErrorList += "<li>Please enter a valid day time phone number.</li>";
	        isValidBA = false;
	    } else {
	        if(!$(".BADayTimeTextBox2").val().match(/\d{3}/)){
	            $(".BADayTimeTextBox1").addClass("FormTextBoxError");
	            $(".BADayTimePhoneLabel").addClass("FormLabelError");
	            $(".BADayTimeTextBox2").addClass("FormTextBoxError");
	            $(".BADayTimePhoneLabel").addClass("FormLabelError");
	            $(".BADayTimeTextBox3").addClass("FormTextBoxError");
	            $(".BADayTimePhoneLabel").addClass("FormLabelError");
	            billingErrorList += "<li>Please enter a valid day time phone number.</li>";
	            isValidBA = false;
	        } else {
	            if(!$(".BADayTimeTextBox3").val().match(/\d{4}/)){
	                $(".BADayTimeTextBox1").addClass("FormTextBoxError");
	                $(".BADayTimePhoneLabel").addClass("FormLabelError");
	                $(".BADayTimeTextBox2").addClass("FormTextBoxError");
	                $(".BADayTimePhoneLabel").addClass("FormLabelError");
	                $(".BADayTimeTextBox3").addClass("FormTextBoxError");
	                $(".BADayTimePhoneLabel").addClass("FormLabelError");
	                billingErrorList += "<li>Please enter a valid day time phone number.</li>";
	                isValidBA = false;
	            }
	        }
	    }
	    	     
	    if(!$(".BAEmailTextBox").val().match(/^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*([A-Za-z0-9]+\-?)@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/)){
	        $(".BAEmailTextBox").addClass("FormTextBoxError");
	        $(".BAEmailLabel").addClass("FormLabelError");
	        billingErrorList += "<li>Please enter a valid email address.</li>";
	        isValid = false;
	    }
	    if($(".BAEmailTextBox").val() != $(".BAConfirmEmailTextBox").val()){
	        $(".BAEmailTextBox").addClass("FormTextBoxError");
	        $(".BAEmailLabel").addClass("FormLabelError");
	        $(".BAConfirmEmailTextBox").addClass("FormTextBoxError");
	        $(".BAConfirmEmailLabel").addClass("FormLabelError");
	        billingErrorList += "<li>Email and Confirm Email does not match.</li>";
	        isValidBA = false;
        }
        
        if(!isValidBA)
        {
            billingErrorList += "</ul>";
            $(".BAErrorMessageLabel").html(billingErrorList);
		} else {
		    $(".BAErrorMessageLabel").html("");
		}
        return isValidBA;
    }
    
    function validateShippingAddress() {
        var isValidSH;
        var shippingErrorList;
        shippingErrorList = "<ul class='ErrorMessageLabel'>";
	    isValidSH = true;
	    
        if($(".SHFirstNameTextBox").val().length == 0){
            $(".SHFirstNameTextBox").addClass("FormTextBoxError");
            $(".SHFirstNameLabel").addClass("FormLabelError");
            shippingErrorList += "<li>Please enter your first name.</li>";
            isValidSH = false;
        }
        if($(".SHLastNameTextBox").val().length == 0){
            $(".SHLastNameTextBox").addClass("FormTextBoxError");
            $(".SHLastNameLabel").addClass("FormLabelError");
            shippingErrorList += "<li>Please enter your last name.</li>";
            isValidSH = false;
        }
        if($(".SHAddress1TextBox").val().length == 0){
            $(".SHAddress1TextBox").addClass("FormTextBoxError");
            $(".SHAddress1Label").addClass("FormLabelError");
            shippingErrorList += "<li>Please enter your address.</li>";
            isValidSH = false;
        }	    
        if($(".SHCityTextBox").val().length == 0){
            $(".SHCityTextBox").addClass("FormTextBoxError");
            $(".SHCityLabel").addClass("FormLabelError");
            shippingErrorList += "<li>Please enter your city.</li>";
            isValidSH = false;
        }	 	    
        if($(".SHStateDDL").val() == "-1"){
            $(".SHStateDDL").addClass("FormTextBoxError");
            $(".SHStateLabel").addClass("FormLabelError");
            shippingErrorList += "<li>Please enter your state.</li>";
            isValidSH = false;
        }
        if(!$(".SHZipTextBox").val().match(/\d{5}/)){
            $(".SHZipTextBox").addClass("FormTextBoxError");
            $(".SHZipLabel").addClass("FormLabelError");
            shippingErrorList += "<li>Please enter your zip code.</li>";
            isValidSH = false;
        }
        if($(".SHZipExtTextBox").val().length > 0 && $(".BAZipExtTextBox").val().match(/\d{4}/)){
            $(".SHZipExtTextBox").addClass("FormTextBoxError");
            $(".SHZipLabel").addClass("FormLabelError");
            shippingErrorList += "<li>Please enter a valid zip code extension.</li>";
            isValidSH = false;
        }
    
    	if(!isValidSH)
		{
		    shippingErrorList += "</ul>";
            $(".SHErrorMessageLabel").html(shippingErrorList);
		} else {
		    $(".SHErrorMessageLabel").html("");
		}
    
        return isValidSH;
    }
    
    function enableDisableMultShipToButton(setToEnabled){
        alert(setToEnabled);
        if(setToEnabled == true){
            $(".MultShipToLinkButton:disabled").removeAttr('disabled');
        } else {
            $(".MultShipToLinkButton").attr('disabled','disabled');
        }
        
    }
    
});

