﻿
$(document).ready(function() {


    $("#frmPrivateTCSignup").submit(function() {

        var isValid = true;

        if ($("#txtFirstName").length) { //use this check to see if user info part of form displayed

            //alert('exists');

            if (GetIfBlankAndAttachErrorMessage('txtFirstName', 'Required'))
                isValid = false;

            if (GetIfBlankAndAttachErrorMessage('txtLastName', 'Required'))
                isValid = false;

            if (GetIfBlankAndAttachErrorMessage('txtEmail', 'Required'))
                isValid = false;
            else {
                if (!IsValidEmail($.trim($("#txtEmail").val()))) {

                    AttachErrorMessage('txtEmail', 'Email does not appear to be a valid email address.');
                    isValid = false;
                }
                else {

                    RemoveErrorMessage('txtEmail');
                }
            }


            if (GetIfBlankAndAttachErrorMessage('txtDisplayName', 'Required'))
                isValid = false;

            if (GetIfBlankAndAttachErrorMessage('txtPassword', 'Required'))
                isValid = false;
            else {

                if ($.trim($("#txtPassword").val()) != $.trim($("#txtPassword2").val())) {
                    AttachErrorMessage('txtPassword', 'Passwords must match.');
                    isValid = false;
                }

            }



            if (!CheckIfCheckBoxChecked('chkAgreeToTerms2', 'Required'))
                isValid = false;



        }

        if ($("#txtSSUsername").length) { //use this is this is an existing SS user signing up

            if (GetIfBlankAndAttachErrorMessage('txtSSUsername', 'Required'))
                isValid = false;


            if (GetIfBlankAndAttachErrorMessage('txtSSDisplayName', 'Required'))
                isValid = false;

            if (!CheckIfCheckBoxChecked('chkAgreeToTerms2', 'Required'))
                isValid = false;


        }


        if ($("#cardOwner").length) {



            $("#cardNumber").val($.trim($("#cardNumber").val().replace(/\s/g, '').replace(/-/g, '')));


            if (GetIfBlankAndAttachErrorMessage('cardOwner', 'Cardholder Name required.'))
                isValid = false;

            if (GetIfBlankAndAttachErrorMessage('cardNumber', 'Card Number required.'))
                isValid = false;
            else {

                if (checkCreditCard($("#cardNumber").val(), 'Visa') == false && checkCreditCard($("#cardNumber").val(), 'MasterCard') == false && checkCreditCard($("#cardNumber").val(), 'AmEx') == false) {
                    AttachErrorMessage('cardNumber', 'Credit card number does not appear to be a valid Visa, MasterCard, Or American Express number.');
                    isValid = false;


                }
                else {
                    RemoveErrorMessage('cardNumber');

                }


            }


            if (!CheckCCExpiryDate('expMonth', 'expYear')) {
                AttachErrorMessage('expYear', 'Credit card expiry date must be greater than today\'s date.');
                isValid = false;

            }
            else {
                RemoveErrorMessage('expYear');



            }


            if (GetIfBlankAndAttachErrorMessage('ccEmail', 'Email Required'))
                isValid = false;
            else {

                if (!IsValidEmail($.trim($("#ccEmail").val()))) {

                    AttachErrorMessage('ccEmail', 'Email does not appear to be a valid email address.');
                    isValid = false;
                }
                else {
                    RemoveErrorMessage('ccEmail');
                }


            }





        }


        //alert(isValid);

        return isValid;

    });


});

function ShowLearnMoreVideo() {

    jQuery.facebox.settings.opacity = 0.35;


    $.facebox({ elementName: 'divLearnMoreVideo', newClass: 'moneyTalksLearnMorePopup' });

}


function ShowLoginFormForPrivateChallenge() {

    jQuery.facebox.settings.opacity = 0.35;
    //$.facebox.settings.faceboxHtml


    $.facebox({ elementName: 'divLoginOrRegister2' });

    $("#divLogin2").show();
    $("#divForgotPassword2").hide();
    
    //$("#divLoginOrRegister2").width(350);

}

function CheckLogin2() {

    //alert('was called');

    var isValid = CheckLogin2Fields();

    if (isValid) {

        var f = $("#frmLogin2");

        var action = "/Account/CheckPrivateChallengeLogin";  //f.attr("action");

        var serializedForm = f.serialize();

        $.post(action,
                serializedForm,
                function(result) {
                    //alert(result);
                    if (result == "notfound") {
                        //$("#spanLoginMessage2").hide();
                        //window.location.href = window.location.href;  //"/My-Trading-Desk";
                        //window.location.reload(true);
                        $("#divLoginOrRegister2").height(130);
                        $("#spanLoginMessage2").show();
                        $("#spanLoginMessage2").html('No user was found with the username and password provided.');
                        return false;



                    }
                    else if (result == "disabled") {
                        $("#divLoginOrRegister2").height(130);
                        $("#spanLoginMessage2").show();
                        $("#spanLoginMessage2").html('Your account has been disabled. Please contact the administrator.');
                        return false;

                    } else { //return url
                        window.location.href = result;


                        return true;
                    }


                    //$("#spanMessage")[0].innerHTML = result;

                }, "text"
            );
        return true;
    } else {
        $("#divLoginOrRegister2").height(120);

        return false;
    }


}


function CheckLogin2Fields() {



    var isValid = true;

    if (GetIfBlankAndAttachErrorMessage('email2', 'Please enter an email address.'))
        isValid = false;
    else {

        if (!IsValidEmail($.trim($("#email2").val()))) {

            AttachErrorMessage('email2', 'email does not appear to be a valid email address.');
            isValid = false;
        }
        else {
            RemoveErrorMessage('email2');
        }


    }


    if (GetIfBlankAndAttachErrorMessage('password2', 'Please enter a password.'))
        isValid = false;


    return isValid;

}




function ShowForgotPasswordForm2() {

    $("#spanOtherMessage2").hide();
   
    $("#divLoginOrRegister2").width(400);

    $("#divLogin2").hide();
    $("#divForgotPasswordForm2").show();
    $("#divForgotPassword2").show();
    $("#spanLostPasswordMessage2").hide();

}





function SendNewPassword2() {
    var isValid = true;

    if (GetIfBlankAndAttachErrorMessage('sendPasswordToThisEmail2', 'Please enter an email address.'))
        isValid = false;
    else {

        if (!IsValidEmail($.trim($("#sendPasswordToThisEmail2").val()))) {
            AttachErrorMessage('sendPasswordToThisEmail2', 'Email does not appear to be a valid email address.');
            isValid = false;
        }
        else {
            RemoveErrorMessage('sendPasswordToThisEmail2');
        }
    }

    if (isValid) {
        var f = $("#frmLogin2");
        var action = "/Account/LostPassword";  //f.attr("action");

        var serializedForm = f.serialize();

        $.post(action,
                serializedForm,
                function(result) {
                    //alert(result);
                    if (result == "passwordchanged") {
                        $("#spanLostPasswordMessage2").html("Your new password has been sent to the email address you provided.");
                        $("#spanLostPasswordMessage2").show();
                        $("#divForgotPasswordForm2").hide();

                    } else if (result == "notfound") {
                        $("#spanLostPasswordMessage2").html("The email you provided was not found in our system.");
                        $("#spanLostPasswordMessage2").show();

                    }
                    else {
                        $("#spanLostPasswordMessage2").html("Something strange has happened.");
                        $("#spanLostPasswordMessage2").show();
                    }

                }, "text"
            );
        } // end if


        return false;
}
