function validateLife(form) {
    var name = $(form).parents("form").attr("name");
    $('#' + name).validate({
        ignoreTitle: true,
        errorLabelContainer: '#summary',
        showErrors: function (errorMap, errorList) {
            this.defaultShowErrors();
            $('#summary').hide();
        }
    });
}

function validAge(dateText) {
    var age = 0;
    var now = new Date();
    var dob = dateText.split('/');

    if (dob.length == 3) {
        var born = new Date(dob[2], dob[1] * 1 - 1, dob[0]);
        var years = new Date(now.getTime() - born.getTime());
        var base = new Date(0);
        age = years.getFullYear() - base.getFullYear();
    }

    if (parseInt(age) < 20 || parseInt(age) > 40) {
        dialog({ id: '#dialog-message', title: 'Alert', width: 300, height: 150, modal: true, autoOpen: true, draggable: false, resizable: false, showButtons: true, html: "<p>You need to call Member Services<br/> on <b>1800 128 268</b> to discuss your needs before we proceed with your application</p>" });
    }
}

var memberID;
var campaignID;
var responseDate;

function lifePopUp(aMemberID, aCampaignID) {
    memberID = aMemberID
    campaignID = aCampaignID

    dialog({ id: '#dialog-message', modal: true, height: 305, width: 600, autoOpen: true, draggable: false, resizable: false, showButtons: false, hideTitleBar: false, titleCloseBtnOnly: true, closeBlur: true, loadHtml: '/UserControls/Avant/text/life-insurance-popup.htm' });
}

function updateCampaignMember(response) {
    if (response == "yes") {
        $.ajax({
            type: "POST",
            async: false,
            url: "/marketing/campaigns.aspx",
            data: "memberID=" + memberID + "&response=Yes" + "&campaignID=" + campaignID,
            success: function () {
                window.location = "/Become-a-Member/Insurance-Products/Quick-Quote---Income-Protection,-Life-and-TPD-Insurance/"
            },
            error: function () {
                $("#dialog-message").dialog("close");
            }
        });
    }
    else if (response == "later") {
        $.ajax({
            type: "POST",
            async: false,
            url: "/marketing/campaigns.aspx",
            data: "memberID=" + memberID + "&response=Later" + "&campaignID=" + campaignID,
            success: function () {
                $("#dialog-message").dialog("close");
            },
            error: function () {
                $("#dialog-message").dialog("close");
            }
        });
    }
    else if (response == "dont-ask-again") {
        $.ajax({
            type: "POST",
            async: false,
            url: "/marketing/campaigns.aspx",
            data: "memberID=" + memberID + "&response=Dont Ask Again" + "&campaignID=" + campaignID,
            success: function () {
                $("#dialog-message").dialog("close");
            },
            error: function () {
                $("#dialog-message").dialog("close");
            }
        });
    }
    else {
        $("#dialog-message").dialog("close");
    }
}

$(document).ready(function () {

    //ajax functions that update members response - poup add
    $("#life-no").bind("click", function () { updateCampaignMember('no') });
    $("#life-later").bind("click", function () { updateCampaignMember('later') });
    $("#life-yes").bind("click", function () { updateCampaignMember('yes') });
    $("#life-dont-ask-again").bind("click", function () { updateCampaignMember('dont-ask-again') });


    $("a#duty-of-disclosure").click(function () { return false; });
    $("a#duty-of-disclosure").click(function () {
        dialog({ id: '#dialog-message', title: 'The duty of disclosure', width: 600, height: 600, autoOpen: true, draggable: true, resizable: true, loadHtml: '/Application-Form/lifeinsurance/text/the-duty-of-disclosure.htm' });
    });

    //used for scroll back position quick quote
    if ($('div').hasClass('amount')) {
        $("html").scrollTop(1000);
    }

    var clientID = "LifeInsurance1_"

    if ($("#" + clientID + "A_Other_Income_Protection_Y").attr('checked') == true)
        $("#income_protection_block").show();
    else
        $("#income_protection_block").hide();


    $("#" + clientID + "A_Other_Income_Protection_Y").click(function (event) {
        $("#income_protection_block").show();
        $("#" + clientID + "A_Insurer").focus();
    });

    $("#" + clientID + "A_Other_Income_Protection_N").click(function (event) {
        $("#income_protection_block").hide();

        $("#income_protection_block :text").each(function (index) {
            $(this).val('');
        });
    });


    //-------------------------------------------------------------------

    if ($("#" + clientID + "A_Hazardous_Activities_Y").attr('checked') == true)
        $("#hazardous_activities_block").show();
    else
        $("#hazardous_activities_block").hide();


    $("#" + clientID + "A_Hazardous_Activities_Y").click(function (event) {
        $("#hazardous_activities_block").show();
        $("#" + clientID + "A_Hazardous_Comments").focus();
    });

    $("#" + clientID + "A_Hazardous_Activities_N").click(function (event) {
        $("#hazardous_activities_block").hide();

        $("#hazardous_activities_block textarea").each(function (index) {
            $(this).val('');
        });
    });

    //-------------------------------------------------------------------
    if ($("#" + clientID + "A_Smoke_Y").attr('checked') == true)
        $("#do_you_smoke_block").show();
    else
        $("#do_you_smoke_block").hide();

    $("#" + clientID + "A_Smoke_Y").click(function (event) {
        $("#do_you_smoke_block").show();
        $("#" + clientID + "A_How_Many_Per_Day").focus();
    });

    $("#" + clientID + "A_Smoke_N").click(function (event) {
        $("#do_you_smoke_block").hide();

        $("#do_you_smoke_block select").each(function (index) {
            $(this).get(0).selectedIndex = 0;
        });
    });

    //------------------------------------------------------------------

    if ($("#" + clientID + "A_Alcohol_Y").attr('checked') == true)
        $("#alcohol_block").show();
    else
        $("#alcohol_block").hide();

    $("#" + clientID + "A_Alcohol_Y").click(function (event) {
        $("#alcohol_block").show();
        $("#" + clientID + "A_Weekly_Qty").focus()
    });

    $("#" + clientID + "A_Alcohol_N").click(function (event) {
        $("#alcohol_block").hide();

        $("#alcohol_block  select").each(function (index) {
            $(this).get(0).selectedIndex = 0;
        });
    })

    //------------------------------------------------------------------
    if ($("#" + clientID + "A_Hereditary_Y").attr('checked') == true)
        $("#hereditary_block").show();
    else
        $("#hereditary_block").hide();


    $("#" + clientID + "A_Hereditary_Y").click(function (event) {
        $("#hereditary_block").show();
        $("#" + clientID + "A_Hereditary_Condition_1").focus()
    });

    $("#" + clientID + "A_Hereditary_N").click(function (event) {
        $("#hereditary_block").hide();

        $("#hereditary_block  select").each(function (index) {
            $(this).get(0).selectedIndex = 0;
        });

        $("#hereditary_block :text").each(function (index) {
            $(this).val('');
        });
    });

    //------------------------------------------------------------------
    if ($("#" + clientID + "A_Disorders_Y").attr('checked') == true)
        $("#disorders_block").show();
    else
        $("#disorders_block").hide();


    $("#" + clientID + "A_Disorders_Y").click(function (event) {
        $("#disorders_block").show();
        $("#" + clientID + "A_Disorder_Condition_1").focus()
    });

    $("#" + clientID + "A_Disorders_N").click(function (event) {
        $("#disorders_block").hide();

        $("#disorders_block  select").each(function (index) {
            $(this).get(0).selectedIndex = 0;
        });

        $("#disorders_block :text").each(function (index) {
            $(this).val('');
        });
    });

    //------------------------------------------------------------------ 

    $("#A_Total_Proportion").val('0')

    function setDefaultValues() {
        $("#beneficiary_block :text.proportion").each(function () {
            var temp = parseInt($(this).val());

            //if (isNaN(temp)) {
            //    $(this).val('0');
            //}
        });
    }

    if ($("#" + clientID + "A_Dont_Nominate").attr('checked') == true) {
        $("#beneficiary_block").hide();
        $("#A_Total_Proportion").removeClass("error");
    }
    else {
        $("#beneficiary_block").show();
        setDefaultValues();
    }


    $("#" + clientID + "A_Dont_Nominate").click(function (event) {
        if ($(this).attr('checked')) {
            $("#beneficiary_block").hide();

            $("#beneficiary_block  select").each(function () {
                $(this).get(0).selectedIndex = 0;
            });

            $("#beneficiary_block :text").each(function () {
                $(this).val('');
            });
        }
        else {
            $("#beneficiary_block").show();
        }

        $("#A_Total_Proportion").removeClass("error")
    });


    //-------------------------------------------------------------------

    $("#" + clientID + "A_Purchased_At_Chemist_Y").click(function (event) {
        $("#drugstaken_block").show();
        $("#" + clientID + "A_DrugsTaken").focus();
    });

    if ($("#" + clientID + "A_Purchased_At_Chemist_Y").attr('checked') == true)
        $("#drugstaken_block").show();
    else
        $("#drugstaken_block").hide();


    $("#" + clientID + "A_Purchased_At_Chemist_N").click(function (event) {
        $("#drugstaken_block").hide();

        $("#drugstaken_block textarea").each(function (index) {
            $(this).val('');
        });
    });


    //-------------------------------------------------------------------           

    function calculateProportion() {
        var total = 0
        $("#beneficiary_block :text.proportion").each(function () {
            var temp = parseInt($(this).val());

            if (!isNaN(temp)) {
                total += temp;
                if (total > 100 || total < 100) {
                    $("#A_Total_Proportion").addClass("error")
                    $("#A_Total_Proportion_Msg").addClass("requiredText")
                }
                else {
                    $("#A_Total_Proportion").removeClass("error")
                    $("#A_Total_Proportion_Msg").removeClass("requiredText")
                }
            }
        });
        $("#A_Total_Proportion").val(total);
    }

    $("#beneficiary_block :text.proportion").bind("keyup", calculateProportion);
    $("#beneficiary_block :text.proportion").bind("click", function () {
        this.select();
    });

    setDefaultValues();
    calculateProportion();

    var d = new Date();
    var year = d.getFullYear();
    
    $(function () {
        $(":text.life-date-picker").datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'dd/mm/yy',
            yearRange: '1900:' + year,
            showAnim: 'fold',
            showButtonPanel: false,
            onClose: function (dateText, inst) {
                if (inst.id == "LifeInsurance1_A_Dob" || inst.id == "ctl00_LeftPlaceHolder_uxContentBlock_ctl00_Q_Dob")
                    validAge(dateText);
            }
        });
    });
});


//window.location.hostname
