﻿$(document).ready(function() {
    $('#divValidationSummary').jqm();
    $("#ctl00_ContentPlaceHolder1_txtZip").mask("99999", { placeholder: " " });
    $("#ctl00_ContentPlaceHolder1_txtTelephone").mask("(999) 999-9999", { placeholder: " " });
    $('#EducationOther').hide();
    $('#ResponsibilityOther').hide();
    $('#EmploymentOther').hide();
    $('#divMembership').hide();
    $('#divApplication').hide();
    $('#divMailAddress').hide();
    $('#divOnlinePayment').hide();
    if ($('#ctl00_ContentPlaceHolder1_hdnUserName').val() != "") {
        $('#divApplicationInstructions').html('\
            Please update your application with the most current information. &nbsp;Once you\'re \
            are finished, click the "Next" button.');
        $('#divPaymentInstructions').html('\
            Thanks for updating your application. &nbsp;Please select how you would like to \
            pay your dues. &nbsp;If you would like to pay right now, select <i>Make Payment</i> \
            Online. &nbsp;If you would rather send a check by mail, select <i>Mail Check</i>. <br/><br/>\
            ' + $('#divPaymentInstructions').html());
        $('#divHeading').hide();
        $('#divApplication').show();
        $('#ctl00_ContentPlaceHolder1_txtFName').focus();
    } else {
        $('#divApplicationInstructions').hide();
        $('#divApplicationInstructions').html('\
            To get start, first create a membership login by completing the form below. \
            Be sure to select a password that is at least seven characters long and does not \
            contain any spaces. &nbsp;Once you\'re done, click the "Next" button.');
        $('#divPaymentInstructions').html('\
            Your account has been successfully created. &nbsp;However you will not be able to \
            log in until payment of your dues can be verified. &nbsp;If you would like to pay \
            right now, select Make Payment Online. &nbsp;If you would rather send a check via \
            mail, select Mail Check. &nbsp;Regardless, you\'ll be notified by email once your \
            account is activated. <br/><br/>' + $('#divPaymentInstructions').html());
    }

    $('#btnRenew').click(function() {
        $('#ctl00_LoginView1_txtUserName').focus();
        $('#divApplicationInstructions').html("\
            In order to continue, you must first login. &nbsp;Please enter your user name \
            and password in the Member Login section to the right. \
            <p> \
                If you can't remember your password click on the Forgot password? link below the \
                Member section. \
            </p> \ ");
        $('#divHeading').slideUp('slow', function() {
            $('#divApplicationInstructions').slideDown('slow');
        });
        return false;
    });

    $('#btnNew').click(function() {
        $('#RightCol').fadeOut('fast');
        $('#divHeading').slideUp('slow', function() {
            $('#divApplicationInstructions').slideDown('slow', function() {
                $('#divMembership').slideDown('slow');
            });
        });
        return false;
    });

    $('#btnNext').click(function() {
        if (Page_ClientValidate('Membership')) {
            $('#divMembership').slideUp('slow', function() {
                $('#divApplicationInstructions').slideUp('slow', function() {
                    $('#divApplicationInstructions').html('\
                        Please complete each question in the application below. &nbsp;Once you\'re \
                        are finished, click the "Next" button.');
                    $('#divApplicationInstructions').slideDown('slow', function() {
                        $('#divApplication').slideDown('slow');
                    });
                });
            });
        } else
            $('#divValidationSummary').jqmShow();
    });

    $('#ctl00_ContentPlaceHolder1_btnSubmitApplication').click(function() {
        if (!Page_ClientValidate('Application'))
            $('#divValidationSummary').jqmShow();
    });

    $('#btnPayOnline').click(function() {
        $('#divPaymentInstructions').slideUp('slow', function() {
            $('#divOnlinePayment').slideDown('slow');
            makePayment('online');
        });
    });

    $('#btnMailCheck').click(function() {
        $('#divPaymentInstructions').slideUp('slow', function() {
            $('#divMailAddress').slideDown('slow');
            makePayment('mail');
        });
    });

    $('#btnBack').click(function() {

    });

    $('#ctl00_ContentPlaceHolder1_lstEducation_7').change(function() {
        $('#EducationOther').toggle();
    });

    $('#ctl00_ContentPlaceHolder1_lstResponsibility_4').change(function() {
        $('#ResponsibilityOther').toggle();
    });

    $('.lstEmployment').change(function() {
        if ($('#ctl00_ContentPlaceHolder1_lstEmployment_6').attr('checked'))
            $('#EmploymentOther').show();
        else
            $('#EmploymentOther').hide();
    });
});

function makePayment(method) {
    var userName = $('#ctl00_ContentPlaceHolder1_hdnUserName').val();
    var year = $('#ctl00_ContentPlaceHolder1_hdnYear').val();
    $.ajax({
        async: false,
        cache: false,
        type: "POST",
        url: "Webservices/BoardMemberService.asmx/MakePayment",
        data: "{ 'userName': '" + userName + "', 'method': '" + method + "', 'year': '" + year + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        proccessData: false,
        success: function(json, success) {
            isValid = eval(json.d);
        }
    });
}

function validateEducation(s, e) {
    e.IsValid = ($('#ctl00_ContentPlaceHolder1_lstEducation').find('input:checked').size() > 0);
}

function validateResponsiblity(s, e) {
    e.IsValid = ($('#ctl00_ContentPlaceHolder1_lstResponsibility').find('input:checked').size() > 0);
}

function validateUserName(s, e) {
    var userName = $('#ctl00_ContentPlaceHolder1_txtUserName').val();
    var isValid = false;
    $.ajax({
        async: false,
        cache: false,
        type: "POST",
        url: "Webservices/BoardMemberService.asmx/IsValidUserName",
        data: "{ 'userName': '" + userName + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        proccessData: false,
        success: function(json, success) {
            isValid = eval(json.d);
        }
    });
    e.IsValid = isValid;
}

function validateEmail(s, e) {
    var email = $('#ctl00_ContentPlaceHolder1_txtEmail').val();
    var isValid = false;
    $.ajax({
        async: false,
        cache: false,
        type: "POST",
        url: "Webservices/BoardMemberService.asmx/IsValidEmail",
        data: "{'email':'" + email + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        proccessData: false,
        success: function(json, success) {
            isValid = eval(json.d);
        }
    });
    e.IsValid = isValid;
}