﻿$(function () {
    $("#topnav_home").addClass("selected"); // Set the highlighted top nav item

    // Add the "Please choose an option" to the Sources select list
    $("#ddlAccountSource").append($('<option></option>').
        attr("value", "-1").
        text("Please choose an option"));
    $("#ddlAccountSource").val("-1");

    // Handles the contact submit buttion click
    $('#btnSignUpSubmit').click(function () {
        CreateAccount();
    });

    // Creates the progress dialog
    $('#dlgProgress').dialog({
        autoOpen: false,
        modal: true,
        height: 150,
        width: 300,
        resizable: true
    });

    $("#progressbar").progressbar({
        value: 100
    });
});

function CreateAccount() {
    if (Validate()) {
        // Show the progress indicator
        ShowProgress();

        // Get all of the values that where entered
        var churchName = $("#txtbxChurchName").val();
        var email = $("#txtbxEmailAddress").val();
        var password = $("#txtbxPassword").val();
        var contactFirstName = $("#txtbxFirstName").val();
        var contactLastName = $("#txtbxLastName").val();
        var streetAddress = $("#txtbxStreetAddress").val();
        var city = $("#txtbxCity").val();
        var stateProvinceId = $("#StatesProvinces").val();
        var postalCode = $("#txtbxPostalCode").val();
        var phone = $("#txtbxPhone").val();
        var denomination = 1;
        var source = $("#ddlAccountSource").val();

        // Call the server to create the account
        $('#lblResponse').hide();
        $('#AccountSuccess').hide();

        $.ajax({
            type: "POST",
            url: "/SignUp/CreateAccount",
            data: "churchName=" + churchName + "&email=" + email + "&password=" + password + "&contactFirstName=" + contactFirstName +
            "&contactLastName=" + contactLastName + "&streetAddress=" + streetAddress + "&city=" + city + "&stateProvinceId=" + stateProvinceId +
            "&postalCode=" + postalCode + "&phone=" + phone + "&denomination=" + denomination + "&source=" + source,
            dataType: "json",
            error: function (xhr, status, error) {
                // Show the error
                $('#lblResponse').attr('class', 'errormsg');
                $('#lblResponse').text(xhr.responseText);
                $('#lblResponse').attr('style', 'display:block;');
                HideProgress();
            },
            success: function (data, textSuccess) {
                HideProgress();

                // hide the fields
                $('#signUpForm').hide();

                // show the success message
                $('#lblResponse').attr('class', 'successmsg');
                $('#lblResponse').text("Account created successfully.");
                $('#lblResponse').show();
                $('#AccountSuccess').show();

                window.location = "/SignUp/ThankYou";
            }
        });
    }
}

function Validate() {
    $.validity.start();

    $("#txtbxChurchName").require();
    $(".emailaddress")
        .require()
        .match("email")
        .equal("Email addresses do not match");
    $("#txtbxPassword").require();
    $("#txtbxConfirmPassword").require();
    $("#txtbxFirstName").require();
    $("#txtbxLastName").require();
    $("#txtbxStreetAddress").require();
    $("#txtbxCity").require();
    $("#txtbxPostalCode").require();
    $("#txtbxPhone").require();

    $("input[type='password']")
        .match(/^.{8,20}$/, "Passwords must be at least 8 characters.")
        .equal("Passwords do not match.");

    var result = $.validity.end();

    // Make sure the terms of service were accepted
    var accepted = $('#chkbxTermsOfServiceAccepted').attr('checked');
    if (!accepted) {
        result.valid = false;
        $('#lblTermsOfServiceValidationMsg').show();
    }
    else
        $('#lblTermsOfServiceValidationMsg').hide();

    var sourceSelected = $("#ddlAccountSource").val();
    if (sourceSelected == "-1") {
        result.valid = false;
        $('#lblSourceValidationMsg').show();
    }
    else
        $('#lblSourceValidationMsg').hide();

    return result.valid;
}

function ShowProgress() {
    $('#dlgProgress').dialog('open');
}

function HideProgress() {
    $('#dlgProgress').dialog('close');
}
