﻿function ethnicityother(t) {
    if (t.value == "Other")
        $("#ethnicityotherdiv").removeClass('hidden');
    else
        $("#ethnicityotherdiv").addClass('hidden');
}
function wordofmouthother(t) {
    if (t.value == "Other")
        $("#wordofmouthotherdiv").removeClass('hidden');
    else
        $("#wordofmouthotherdiv").addClass('hidden');
}

function addMember() {
    var total = $("#familytotal").attr("value");
    total++;

    $("#familytable tr:last").after('<tr class="familymember_' + total + '"><td><input type="text" name="name_' + total + '"/></td><td><input type="text" name="surname_' + total + '"/></td><td><input type="text" name="mobile_' + total + '"/></td><td><input type="text" name="email_' + total + '"/></td><td align="center"><a href="JavaScript: removeMember(' + total + ');" class="nodeco">-</a></td></tr>');
    
    $("#familytotal").attr("value", total);
}

function removeMember(i) {
    $('#familytable tr:eq(' + i + ')').addClass('hidden');
    $('#familytable tr:eq(' + i + ') td:eq(0) input').val('REMOVED');
}

function GetValidatorId(strid) {
    var count = Page_Validators.length;
    var i = 0; var eleName;
    for (i = 0; i < count; i++) {
        eleName = Page_Validators[i].id;
        pos = eleName.indexOf(strid);
        if (pos >= 0) break;
    }
    return eleName;
}

$.extend({
    clientID: function(id) {
        return $("[id$='" + id + "']");
    }
});

function setupPSA() {
    $('.unionselector').addClass('hidden');
    $('.standardinfo').removeClass('hidden');
    $('.extrainfo1').addClass('hidden');
    $('.extrainfo2').removeClass('hidden');
    $('.extrainfo3').addClass('hidden');

    $(".membershipoption option[value=1]").attr("disabled", "disabled");
    $(".membershipoption option[value=2]").attr("selected", "selected");
    
    $(".membershipfeesNormal").addClass('hidden');
    $('.membershipfeesPSA').removeClass('hidden');
    
    $(".radiobuttons input:[value=8]").attr("checked", "checked");
    $('.paymentoptionsPSA').removeClass('hidden');
    $('.paymentoptionsNormal').addClass('hidden');

    ValidatorEnable($('#' + GetValidatorId("Requiredfieldvalidator7"))[0], false);
}

function setupNormal() {
    $('.unionselector').addClass('hidden');
    $('.standardinfo').removeClass('hidden');
    $('.extrainfo1').removeClass('hidden');
    $('.extrainfo2').removeClass('hidden');
    $('.extrainfo3').removeClass('hidden');

    $(".membershipoption option[value=1]").attr("disabled", "");
    
    $(".membershipfeesNormal").removeClass('hidden');
    $('.membershipfeesPSA').addClass('hidden');

    $(".radiobuttons input:[value=1]").attr("checked", "checked");
    $('.paymentoptionsPSA').addClass('hidden');
    $('.paymentoptionsNormal').removeClass('hidden');
}

function unions(s) {
    if (s.value == 33) {//PSA union selected
        setupPSA();
        $.clientID("union2").val('33'); //using extended function to change the selected value
        
        HideValidationErrors();

        ValidatorEnable(document.getElementById(GetValidatorId("RequiredFieldValidatorJob")), false);
        ValidatorEnable(document.getElementById(GetValidatorId("RequiredFieldValidatorEmployername")), false);
        ValidatorEnable(document.getElementById(GetValidatorId("RequiredFieldValidatorWorkplaceaddress")), false);
        ValidatorEnable(document.getElementById(GetValidatorId("RequiredFieldValidatorContact")), false);

        //ValidatorEnable(document.getElementById(GetValidatorId("CustomValidatorPSA")), true);
        document.getElementById(GetValidatorId("CustomValidatorPSA")).enabled = true;

    } else {
        setupNormal()
        $.clientID('union2').val(s.value); //using extended function to change the selected value
        
        HideValidationErrors();

        if (document.getElementById(GetValidatorId("RequiredFieldValidatorJob")).enabled == false) {
            document.getElementById(GetValidatorId("RequiredFieldValidatorJob")).enabled = true;
        }

        if (document.getElementById(GetValidatorId("RequiredFieldValidatorEmployername")).enabled == false) {
            document.getElementById(GetValidatorId("RequiredFieldValidatorEmployername")).enabled = true;
        }

        if (document.getElementById(GetValidatorId("RequiredFieldValidatorWorkplaceaddress")).enabled == false) {
            document.getElementById(GetValidatorId("RequiredFieldValidatorWorkplaceaddress")).enabled = true;
        }

        if (document.getElementById(GetValidatorId("RequiredFieldValidatorContact")).enabled == false) {
            document.getElementById(GetValidatorId("RequiredFieldValidatorContact")).enabled = true;
        }

        //ValidatorEnable(document.getElementById(GetValidatorId("CustomValidatorPSA")), false);
        document.getElementById(GetValidatorId("CustomValidatorPSA")).enabled = false;
    }
}

function HideValidationErrors() {
    for (var item = 0; item < 12; item++) {
        var vali = Page_Validators[item];
        vali.isvalid = true;
        ValidatorUpdateDisplay(vali);
    }

    Page_ValidationSummaries[0].style.display = "none";
}

function showGift() {
    $('#giftsponsor').show()
}

function setupSponsorLayout() {
    var sel = $.clientID('union').val();

    if (sel != '==Select your union==') {
        if (sel == 33) {//PSA union selected
            setupPSA();
        } else {
            setupNormal()
        }
    }
}

function ValidateChecked(oSrc, args) {
    $(".checkbox input[type='checkbox']").each(function() {
        if (!this.checked) {
            //alert('Has to be checked.');
            args.IsValid = false;
        }
    });
}

// Ajax check
function CheckIfMemberAlreadySignedUp() {
    var n = $.clientID('firstname').val();
    var s = $.clientID('surname').val();
    var e = $.clientID('homeemail').val();
    var y = $.clientID('dobYear').val();
    var m = $.clientID('dobMonth').val();
    var d = $.clientID('dobDay').val();
    
    PageMethods.CheckIfDoubleSignup(n, s, e, y, m, d, CallSuccess, CallFailed);
}

// Popup yes/no
function CallSuccess(result) {
    if (result != "false") {
        var yes = new LertButton('Yes', function() {
            var btn = $.clientID('ButtonRealSubmit');
            if (btn)
                btn.click();
        });

        var no = new LertButton('No', function() {
            window.location = "../../JoinUs/UpdateMemberDetail";
        });

        var message = "<h1>Data entry Alert!</h1><h5>There is already an entry in the database that matches the information " +
                      "you have entered.</h5> If you have previously completed this form, click <b>No</b>.<br/> If you have not " +
                      "captured your information previously and wish to continue and create a new database record click <b>Yes</b>. <br/><br/> " +
                      "<img src='../../JavaScripts/lert-1.0/i/dialog-information.png' alt='' style='float: left; margin: 2px;'>" +
                      "<i>If you wish to change your member details click <b>No</b>. You will be redirected to the Update members detail page " +
                      "where you can enter your Membership number (<b>" + result + "</b>) and Surname to login and change your details.</i>" +
                      "<br/><br/><h4>Do you want to continue creating a new member record?</h4>";

        var myLertBox = new Lert(
        message,
        [yes, no],
        {
            defaultButton: yes,
            icon: '../../JavaScripts/lert-1.0/i/dialog-warning.png'
        });

        $('#lertMessage').html(message);
        
        myLertBox.display();
    }
    else {
        var btn = $.clientID('ButtonRealSubmit');
        if (btn)
            btn.click();
    }

//    if (result != "false") {
//        var answer = confirm("There is already an entry in the database that matches your information. Are you sure you want to continue creating a new entry?")
//        if (answer) {
//            var btn = $.clientID('ButtonRealSubmit');
//            if (btn)
//                btn.click();
//        }
//        else {
//            alert("To modify your details go to the update page (http://together.org.nz/JoinUs/UpdateMemberDetail) and use your member number " + result + " and surname to log in.");
//            window.location = "../../JoinUs/UpdateMemberDetail";
//        }
//    }
//    else {
//        var btn = $.clientID('ButtonRealSubmit');
//        if (btn)
//            btn.click();
//    }
}

function CallFailed(result) {
    alert(result.get_message());
}


$(function() {
    setupSponsorLayout()
    $(".datepicker").datepicker({
        dateFormat: "dd/mm/yy",
        changeMonth: true,
        changeYear: true,
        maxDate: "0",
        defaultDate: "-15Y",
        yearRange: "1950:2010"
    });
    $(".exportdate").datepicker({ dateFormat: "dd/mm/yy" });

    $(".radiobuttons input").bind("click", function() {
        if ($(this).attr("value") == "6") {
            $("#sponsorcode").removeClass('hidden');
            $("#giftcode").addClass('hidden');
        } else if ($(this).attr("value") == "7") {
            $("#giftcode").removeClass('hidden');
            $("#sponsorcode").addClass('hidden');
        } else {
            $("#giftcode").addClass('hidden');
            $("#sponsorcode").addClass('hidden');
        }
    });


    //    $.clientID("Submit").click(function() {
    //        if ($.clientID("union2").val() == '33') {
    //            ValidatorEnable(document.getElementById(GetValidatorId("CustomValidatorPSA")), true);
    //            $(".checkbox input[type='checkbox']").each(function() {
    //                if (!this.checked) {
    //                    alert('test11');
    //                    return false;
    //                }
    //            });
    //        }
    //    });
});



