var formdata;
var survey;

$(document).ready(function () {
    PayPal();
    MyAccount();
    SetupInputsWithLabelText();
});

function MyAccount() {
    if ($("#myaccountpage").length > 0) {
        if ($('#dob').length > 0) {
            $("#dob").datepicker({
                showOn: "button",
                yearRange: '-80:-5',
                changeMonth: true,
                changeYear: true,
                buttonImage: "/images/calendar.gif",
                buttonImageOnly: true
            });
        }

        $('#btnsavedetails').click(function () {
            saveuserdetails();
        });
        $('#btnchangepassword').click(function () {
            changepassword();
        });

    }
}

function changepassword() {
    if ($("#security").length > 0) {
        var oldpassword = $('#oldpassword').val();
        var password = $('#password').val();
        var password2 = $('#password2').val();
        var memid = $('#memid').val();

        formdata = '{ "formdata": [{ '
            + 'memid: ' + memid + ', '
            + 'oldpassword: "' + oldpassword + '",'
            + 'password: "' + password + '",'
            + 'password2: "' + password2 + '"'
            + '}]}';

        var url = '/pages/ws.aspx/ChangePassword';
        $.ajax({
            url: url,
            data: formdata,
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json',
            success: function (data) {
                var res = jQuery.parseJSON(data.d);
                if (res.status == 'true') {
                    $('#securityerror').html('');
                    alert('password changed');
                }
                else {
                    $('#securityerror').html(res.html);
                }
            }
        });
    }
}

function PayPal() {
    if ($("#paypalpage").length > 0) {

        if ($('#dob').length > 0) {
            $("#dob").datepicker({
                showOn: "button",
                yearRange: '-80:-5',
                changeMonth: true,
                changeYear: true,
                buttonImage: "/images/calendar.gif",
                buttonImageOnly: true
            });
        }

        $('#details').show();
        $('#survey').hide();
        $('#formPaypal').hide();
        $('#sending').hide();

        $('#next').click(function () {
            validatedetails();
            $('#sending').hide();
        });

        $('#back').click(function () {
            $('#details').show();
            $('#survey').hide();
            $('#formPaypal').hide();
            $('#sending').hide();
        });

        $('#btnSubmitMembershipRegistration').click(function () {
            processform();
        });
    }
}

function loadform() {

    var username = $('#username').val();
    var password = $('#password').val();
    var password2 = $('#password2').val();
    var securityquestion = $('#securityquestion').val();
    var securityanswer = $('#securityanswer').val();
    var email = $('#email').val();

    var title = $('select#title option:selected').val();
    var firstname = $('#firstname').val();
    var surname = $('#surname').val();
    var dob = $('#dob').val();
    var gender = $('select#gender option:selected').val();
    var address = $('#address').val();
    var town = $('#town').val();
    var county = $('#county').val();
    var postcode = $('#postcode').val();
    var homephone = $('#homephone').val();
    var mob = $('#mobile').val();
    var fax = $('#fax').val();
    var privacy = $('select#privacy option:selected').val();


    var q1 = false, q2 = false, q3 = false, q4 = false, q5 = false, q6 = false, q6 = false, q7 = false, q8 = false;
    var waiver = false;
    var commission = false;

    if ($('#q1').is(':checked'))
        q1 = true;
    if ($('#q2').is(':checked'))
        q2 = true;
    if ($('#q3').is(':checked'))
        q3 = true;
    if ($('#q4').is(':checked'))
        q4 = true;
    if ($('#q5').is(':checked'))
        q5 = true;
    if ($('#q6').is(':checked'))
        q6 = true;
    if ($('#q7').is(':checked'))
        q7 = true;
    if ($('#q8').is(':checked'))
        q8 = true;
    if ($('#waiver').is(':checked'))
        waiver = true;
    if ($('#commission').is(':checked'))
        commission = true;

    var coname = $('#coname').val();
    var coaddress = $('#coaddress').val();
    var codetails = $('#codetails').val();
    var eo = $('select#eo option:selected').val();
    var eoother = $('#eoother').val();
    var isdisabled = false;
    if ($('#disabled').is(':checked'))
        isdisabled = true;
    var agegroup = $('select#agegroup option:selected').val();

    survey = '{'
            + 'q1: "' + q1 + '",'
            + 'q2: "' + q2 + '",'
            + 'q3: "' + q3 + '",'
            + 'q4: "' + q4 + '",'
            + 'q5: "' + q5 + '",'
            + 'q6: "' + q6 + '",'
            + 'q7: "' + q7 + '",'
            + 'q8: "' + q8 + '",'
            + 'waiver: "' + waiver + '",'
            + 'commission: "' + commission + '",'
            + 'coname: "' + coname + '",'
            + 'coaddress: "' + coaddress + '",'
            + 'codetails: "' + codetails + '",'
            + 'eo: "' + eo + '",'
            + 'eoother: "' + eoother + '",'
            + 'disabled: "' + isdisabled + '",'
            + 'agegroup: "' + agegroup + '"'
            + '}';

    formdata = '{ "formdata": [{"username": "' + username + '", '
            + 'password: "' + password + '",'
            + 'password2: "' + password2 + '",'
            + 'securityquestion: "' + securityquestion + '",'
            + 'securityanswer: "' + securityanswer + '",'
            + 'email: "' + email + '",'
            + 'firstname: "' + firstname + '",'
            + 'surname: "' + surname + '",'
            + 'title: "' + title + '",'
            + 'dob: "' + dob + '",'
            + 'gender: "' + gender + '",'
            + 'address: "' + address + '",'
            + 'town: "' + town + '",'
            + 'county: "' + county + '",'
            + 'postcode: "' + postcode + '",'
            + 'homephone: "' + homephone + '",'
            + 'mob: "' + mob + '",'
            + 'fax: "' + fax + '",'
            + 'privacy: "' + privacy + '",'
            + 'survey: [' + survey + ']'
           + '}]}';
}

function loaduserdetails() {

    //var username = $('#username').val();
    var email = $('#email').val();

    var title = $('select#title option:selected').val();
    var firstname = $('#firstname').val();
    var surname = $('#surname').val();
    var dob = $('#dob').val();
    var gender = $('select#gender option:selected').val();
    var address = $('#address').val();
    var town = $('#town').val();
    var county = $('#county').val();
    var postcode = $('#postcode').val();
    var homephone = $('#homephone').val();
    var mob = $('#mobile').val();
    var fax = $('#fax').val();
    var privacy = $('select#privacy option:selected').val();
    var memid = $('#memid').val();

    formdata = '{ "formdata": [{ '
            + 'memid: ' + memid + ', '
            //+ 'username: "' + username + '", '
            + 'email: "' + email + '",'
            + 'firstname: "' + firstname + '",'
            + 'surname: "' + surname + '",'
            + 'title: "' + title + '",'
            + 'dob: "' + dob + '",'
            + 'gender: "' + gender + '",'
            + 'address: "' + address + '",'
            + 'town: "' + town + '",'
            + 'county: "' + county + '",'
            + 'postcode: "' + postcode + '",'
            + 'homephone: "' + homephone + '",'
            + 'mob: "' + mob + '",'
            + 'fax: "' + fax + '",'
            + 'privacy: "' + privacy + '"'
            + '}]}';
}

function saveuserdetails() {
    loaduserdetails();
    var url = '/pages/ws.aspx/SaveUserDetails';
    $.ajax({
        url: url,
        data: formdata,
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {
            var res = jQuery.parseJSON(data.d);
            if (res.status == 'true') {
                $('#error').html('');
                alert('user details saved');
            }
            else {
                $('#error').html(res.html);
            }
        }
    });
}

function validatedetails() {
    loadform();
    var url = '/pages/ws.aspx/ValidateRegistrationForm';
    $.ajax({
        url: url,
        data: formdata,
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {
            var res = jQuery.parseJSON(data.d);
            if (res.status == 'true') {
                $('#error').html('');
                $('#details').hide();
                $('#survey').show();
                $('#formPaypal').show();
            }
            else {
                $('#error').html(res.html);
            }
        }
    });
}

function processform() {
    loadform();
    var url = '/pages/ws.aspx/ProcessForm';
    $.ajax({
        url: url,
        data: formdata,
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {

            var res = jQuery.parseJSON(data.d);
            if (res.status == 'true') {
                window.location.href = '/thank-you.aspx';
//                $('#paypaldata').html(res.html);
//                $('#formPaypal').attr('action', res.paypalurl);
//                $('#details').hide();
//                $('#survey').hide();
//                $('#formPaypal').hide();
//                $('#sending').show();
//                $('#formPaypal').submit();


            }
            else {
                $('#error').html(res.html);
            }
            return false;
        }
    });
}



function SetupInputsWithLabelText() {
    if ($("label.useLabel").length > 0) {
        $("label.useLabel").addClass("hide");
        var labels = $("label.useLabel");

        for (var ii = 0; ii < labels.length; ii++) {
            var inputId = $(labels[ii]).attr("for");
            if ($("input#" + inputId).val().length == 0) {
                var value = $("label[for='" + inputId + "']").text();
                $("input#" + inputId).val(value);
                $("input#" + inputId).focus(function () { $(this).val(""); });
            }
        }
    }
}



