﻿/// <reference path="jquery-1.4.2.js" />

var message_box_id = "ctl00_messagebox";


var pricing = { "0": "ConferenceDelegatePackagePriceBasic",
    "3": "ConferenceDelegatePackagePriceFull",
    "4": "ConferenceDelegatePackagePriceGold"
};



// Used to update hidden province field
function update_province(value) {

    var ctl = $('#[id$=ProvinceId]');
    ctl.val(value);
}


function reset_conference_delegate(source_id) {

    NMA.form.reset_fields("CPCDPriceOptions", source_id);
    NMA.form.reset_fields("CPCDConferenceOptions", source_id);
}

function reset_power_networking_visitor(source_id) {
    NMA.form.reset_fields("PowerNetworkingCentreVisitorOptions", source_id);
}


function reset_toggling() {

    if ($("[id$=chkCommunityPowerConferenceDelegate]")[0].checked) {
        $("#CommunityPowerConferenceDelegateOptions").slideToggle();
        $("#PowerNetworkingCentreVisitor").toggle();
    }

    if ($("[id$=chkPowerNetworkingCentreVisitor]")[0].checked) {

        $("#PowerNetworkingCentreVisitorOptions").slideToggle();
        $("#CommunityPowerConferenceDelegate").toggle();

    }


}

function update_package_pricing() {

    var is_member = false;
    var discount_code = "";
    var current_id = "";
    var current_value = "";
    var current_control = {};


    is_member = $("[id$=chkCPCDPriceOptionsOSEAMember]").is(':checked');

    if ($("[id$=chkCPCDPriceOptionsDiscountCode]").is(':checked')) {
        discount_code = $("[id$=txtDiscountCode]").val();

    }
    else {
        discount_code = "";
    }


    $.ajax({
        type: "POST",
        url: "Registration.aspx/UpdatePackagePricing",
        data: "{'is_member':'" + is_member + "','discount_code':'" + discount_code + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {

            if (msg.d.length > 0) {

                var updated_prices = jQuery.parseJSON(msg.d);
                var prefix = $("[id$=Prefix]").val();
                var is_input = prefix == 'txt' ? true : false;

                for (var i = 0; i < updated_prices.length; i++) {

                    current_id = pricing[updated_prices[i].Key];
                    current_value = updated_prices[i].Value + '';

                    current_control = $("[id$=\"" + prefix + current_id + "\"]");

                    if (is_input) {
                        current_value = current_value.replace('$', '');
                        current_value = current_value.replace(',', '');
                        current_control.val(current_value);
                    }
                    else {
                        current_control.text(current_value);
                    }

                    current_value = '';

                }

            }
            else {
                NMA.form.display_message(message_box_id, "Could not update package pricing", "error");
            }
        }
    });
}

function reset_province() {
    $('[id$=ddlCountry]').triggerHandler('change');
}

function province_dropdown_enable(enable) {

    var dropdown = $('[id$=ddlProvince]');
    var textbox = $('[id$=txtProvince]');
    var validator = $('[id$=rfvProvince]')[0];

    if (enable) {
        dropdown.show();
        textbox.hide();


        ValidatorEnable(validator, true);
    }
    else {

        dropdown.html("");
        dropdown.hide();

        textbox.show();

        ValidatorEnable(validator, false);
    }

}





function update_state_province(country, value) {
    country = (country + '').toLowerCase();

    if (typeof value == "undefined") {
        value = "";
    }

    if (country == "canada" || country == "united states") {

        //
        $.ajax({
            type: "POST",
            url: "Registration.aspx/GetProvinceState",
            data: "{'country':'" + country + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function(msg) {
                if (msg.d.length > 0) {
                    var ddl = $('[id$=ddlProvince]');
                    ddl.html("");

                    var obj = jQuery.parseJSON(msg.d);

                    NMA.form.load_drop_down(ddl, obj, "Select Province - State", "");

                    if (value.length > 0) {
                        ddl.val(value);
                    }

                    province_dropdown_enable(true);

                }
                else {
                    NMA.form.display_message(message_box_id, "Could not load state province", "error");

                    province_dropdown_enable(false);
                }
            }
        });
    }
    else {

        province_dropdown_enable(false);

    }

    return true;
}

function update_text(id, text, clear) {
    var ctl = $("'[id$=" + id + "]'");
    var ctl_text = ctl.text();

    if (typeof clear == 'undefined') {
        clear = false;
    }

    if (clear) {
        ctl.text("");
    }
    else {
        ctl.text(text);

    }


}
function update_community_power_banquet(clear) {

    update_text("spnCommunityPowerBanquet", "additional", clear);

}

function update_community_power_launch(clear) {

    update_text("spnCommunityPowerLaunch", "additional", clear);


}


function update_checkbox(id, flag) {

    var checkbox = $("'[id$=" + id + "]'");
    checkbox.attr('checked', flag);

}

function ViewCart_Click() {
    if (confirm("Any changes you have made will not be saved. Continue to the cart?")) {
        location.href = "Cart.aspx";
    }
}


function reset_text() {
    update_community_power_banquet(true);
    update_community_power_launch(true);
}


function reset_form() {

    if (confirm("Are you sure you want to clear this form?"))
    {
        // Turn off validators
        Page_ValidationActive = false;

        reset_toggling();

        reset_text();

        document.forms[0].reset();

        reset_province();

        // reset pricing
        update_package_pricing();

        // Turn on validators
        Page_ValidationActive = true;
    }
}

$(document).ready(function() {

    var cuvAttendanceOptions = $("[id$=cuvAttendanceOptions]")[0];
    var cuvCPCDConferenceOptions = $("[id$=cuvCPCDConferenceOptions]")[0];

    $("[id$=chkCommunityPowerConferenceDelegate]").change(function() {

        $("#CommunityPowerConferenceDelegateOptions").slideToggle();
        $("#PowerNetworkingCentreVisitor").toggle();

        reset_conference_delegate(this.id);


        ValidatorValidate(cuvAttendanceOptions);

    });

    $("[id$=chkPowerNetworkingCentreVisitor]").change(function() {

        $("#PowerNetworkingCentreVisitorOptions").slideToggle();
        $("#CommunityPowerConferenceDelegate").toggle();

        reset_power_networking_visitor(this.id);

        ValidatorValidate(cuvAttendanceOptions);
    });

    $("[id$=chkCommunityPowerBanquet]").click(function() {

        ValidatorValidate(cuvAttendanceOptions);
    });

    $("[id$=chkCommunityPowerLaunch]").click(function() {
        ValidatorValidate(cuvAttendanceOptions);
    });


    $('[id$=chkDietaryRestrictions]').click(function() {

        var validator = $('[id$=rfvDietaryRestrictions]')[0];

        // Enable disable validator according to click action
        ValidatorEnable(validator, $('[id$=chkDietaryRestrictions]').is(':checked'));

    });

    $('[id$=txtDietaryRestrictions]').keyup(function() {

        update_checkbox('chkDietaryRestrictions', $(this).val().length > 0);

    });


    $('[id$=chkAccessabilityRestrictions]').click(function() {

        var validator = $('[id$=rfvAccessabilityRestrictions]')[0];
        // Enable disable validator according to click action
        ValidatorEnable(validator, $('[id$=chkAccessabilityRestrictions]').is(':checked'));

    });

    $('[id$=txtAccessabilityRestrictions]').keyup(function() {

        update_checkbox('chkAccessabilityRestrictions', $(this).val().length > 0);

    });

    $('[id$=txtDiscountCode]').keyup(function() {

        update_checkbox('chkCPCDPriceOptionsDiscountCode', $(this).val().length > 0);
        update_package_pricing();

    });

    $('[id$=txtOseaUsername]').keyup(function() {

        update_checkbox('chkCPCDPriceOptionsOSEAMember', $(this).val().length > 0 || $('[id$=chkCPCDPriceOptionsOSEAMember]').is(':checked'));

    });

    $('[id$=txtOseaPassword]').keyup(function() {

        update_checkbox('chkCPCDPriceOptionsOSEAMember', $(this).val().length > 0 || $('[id$=chkCPCDPriceOptionsOSEAMember]').is(':checked'));

    });


    // Used to bind drop <select> keyboard event 
    $('[id$=ddlCountry]').keyup(function() {

        update_state_province($(this).val(), $('[id$=ProvinceId]').val());

    });
    // Used to bind country drop down
    $('[id$=ddlCountry]').change(function() {

        update_state_province($(this).val(), $('[id$=ProvinceId]').val());

    });


    $('[id$=ddlCommunityPowerBanquet]').change(function() {

        update_checkbox('chkCommunityPowerBanquet', true);
    });


    $('[id$=ddlCommunityPowerLaunch]').change(function() {

        update_checkbox('chkCommunityPowerLaunch', true);
    });


    // bind attendance option checkboxes
    var AttendanceOption_checkboxes = $('#AttendanceOptions :checkbox').filter(function(index) { return $(this).parent().attr('rel') == 'grpAttendanceOptions' });

    // Create mutually exclusive checkbox groups
    AttendanceOption_checkboxes.bind('change', function() {


        var source = $(this);

        // Add 'activechk' to check box css 
        var active = source.hasClass('activechk');

        if (source.attr('checked')) {

            // Add 'activechk' to check box css to indicate it is the active one 
            // used for clicking on same checkbox again.
            source.addClass('activechk');

            var checkboxes = $('#AttendanceOptions :checkbox' + ":not(#" + this.id + ")").filter(function(index) { return $(this).parent().attr('rel') == 'grpAttendanceOptions' });
            var cur_checkbox;
            checkboxes.each(function() {
                cur_checkbox = $(this);
                if (cur_checkbox.is(':checked')) {
                    cur_checkbox.removeAttr('checked');
                }
            });

        }
        else {

            if (active) {
                source.removeAttr('checked');
            }
            else {
                source.attr('checked', 'checked');
            }
        }


    });


    // Create mutually exclusive checkbox groups for price options
    $('#CPCDPriceOptions :checkbox').bind('change', function() {

        var source = $(this);

        // Add 'activechk' to check box css 
        var active = source.hasClass('activechk');

        if (source.attr('checked')) {

            // Add 'activechk' to check box css to indicate it is the active one 
            // used for clicking on same checkbox again.
            source.addClass('activechk');

            var checkboxes = $('#CPCDPriceOptions :checkbox' + ":not(#" + this.id + ")");
            var cur_checkbox;

            checkboxes.each(function() {
                cur_checkbox = $(this);

                if (cur_checkbox.is(':checked')) {
                    cur_checkbox.removeAttr('checked');
                    cur_checkbox.triggerHandler('click');
                    NMA.form.reset_fields("CPCDPriceOptions", source.attr("id"), true);
                }

                cur_checkbox = null;

            });

        }
        else {

            if (active) {
                source.removeAttr('checked');
            }
            else {
                source.attr('checked', 'checked');
            }
        }
    });

    $("[id$=chkCPCDPriceOptionsDiscountCode]").click(function() {

        // Enable disable validator according to click action
        ValidatorEnable($('[id$=cuvCPCDPriceOptionDiscountCode]')[0], $(this).is(':checked'));

    });

    $("[id$=chkCPCDPriceOptionsOSEAMember]").click(function() {
        var val = $('[id$=cuvCPCDPriceOptionOseaMember]')[0];
        var enable = $(this).is(':checked');
        var txtuser = $('[id$=txtOseaUsername]');
        var txtpass = $('[id$=txtOseaPassword]');

        // Enable disable validator according to click action
        ValidatorEnable(val, enable);

        if (enable) {
            // Attach validator to password field
            ValidatorHookupControl(txtuser[0], val);
            ValidatorHookupControl(txtpass[0], val);
        }
        else {
            ValidatorHookupControl("", val);
        }

    });



    $("[id$=chkCPCDPriceOptionsNonMember]").click(function() {
        update_package_pricing();
    });


    // Create mutually exclusive checkbox groups
    $('#CPCDConferenceOptions :checkbox').bind('change', function() {

        var source = $(this);
        // Active checkbox has 'activechk' added to its css class
        var active = source.hasClass('activechk');

        if (source.attr('checked')) {

            // Add 'activechk' to check box css to indicate it is the active one 
            // used for clicking on same checkbox again.
            source.addClass('activechk');

            var checkboxes = $('#CPCDConferenceOptions :checkbox' + ":not(#" + this.id + ")");

            checkboxes.each(function() {

                if ($(this).is(':checked')) {
                    $(this).removeAttr('checked');
                }

            });

        }
        else {

            if (active) {
                source.removeAttr('checked');
            }
            else {
                source.attr('checked', 'checked');
            }
        }
    });

    $("[id$=chkConferenceDelegatePackagePriceGold]").click(function() {
        var checked = !($("[id$=chkConferenceDelegatePackagePriceGold]").is(":checked"));
        update_community_power_banquet(checked);
        update_community_power_launch(checked);

        //  trigger conference option validation
        ValidatorValidate(cuvCPCDConferenceOptions);
    });

    $("[id$=chkConferenceDelegatePackageFull]").click(function() {
        var checked = !($("[id$=chkConferenceDelegatePackageFull]").is(":checked"));

        update_community_power_banquet(checked);
        update_community_power_launch(checked);

        //  trigger conference option validation
        ValidatorValidate(cuvCPCDConferenceOptions);
    });

    $("[id$=chkConferenceDelegatePackageBasic]").click(function() {

        var checked = !($("[id$=chkConferenceDelegatePackageBasic]").is(":checked"));
        update_community_power_banquet(true);
        update_community_power_launch(checked);

        //  trigger conference option validation
        ValidatorValidate(cuvCPCDConferenceOptions);
    });

    $("[id$=txtProvince]").blur(function() {
        update_province(this.value);
    });

    $("[id$=ddlProvince]").blur(function() {
        update_province(this.value);
    });

    // Init forms fields that have client behaviour
    var country = $('[id$=ddlCountry]');

    if (country.attr('selectedIndex') > 0) {
        var default_value = $('[id$=ProvinceId]').val();
        update_state_province(country.val(), default_value);
    }

    if ($("[id$=chkCommunityPowerConferenceDelegate]").is(":checked")) {
        $("#CommunityPowerConferenceDelegateOptions").slideToggle();
        $("#PowerNetworkingCentreVisitor").slideToggle();

        if ($("[id$=chkConferenceDelegatePackagePriceGold]").is(":checked")) {
            $("[id$=chkConferenceDelegatePackagePriceGold]").triggerHandler('click');
        }

        if ($("[id$=chkConferenceDelegatePackageFull]").is(":checked")) {
            $("[id$=chkConferenceDelegatePackageFull]").triggerHandler('click');
        }

        if ($("[id$=chkConferenceDelegatePackageBasic]").is(":checked")) {
            $("[id$=chkConferenceDelegatePackageBasic]").triggerHandler('click');
        }

    }

    if ($("[id$=chkPowerNetworkingCentreVisitor]").is(":checked")) {
        $("#PowerNetworkingCentreVisitorOptions").slideToggle();
        $("#CommunityPowerConferenceDelegate").slideToggle();


    }

    if ($("[id$=chkCPCDPriceOptionsOSEAMember]").is(":checked")) {

        var edit = $("[id$=Mode]").val().length > 0;

        $("[id$=chkCPCDPriceOptionsOSEAMember]").triggerHandler('click');

        if (edit) {
            $("[id$=cuvCPCDPriceOptionOseaMember]").html("For security reasons , please re-enter password");
        }
    }
    
    update_package_pricing();
    
    if ($("[id$=ctl00_phrContent_ErrorMessage]").val().length > 0)
    {
        NMA.form.display_message(message_box_id, $("[id$=ctl00_phrContent_ErrorMessage]").val(), "error");
    }

});


