﻿/// <reference path="jquery-1.4.2.js" />
/// <reference path="NMA.utility.js" />

function cuvAttendanceOptions_ClientValidate(source, arguments) {


    // Check if at least one conference attendance option is selected
    if ($('[id$=chkCommunityPowerConferenceDelegate]').is(':checked') !== false ||
                $('[id$=chkCommunityPowerBanquet]').is(':checked') !== false ||
                $('[id$=chkCommunityPowerLaunch]').is(':checked') !== false ||
                $('[id$=chkPowerNetworkingCentreVisitor]').is(':checked') !== false) {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }


}


/// <summary>
/// Validation of Delegate price options
/// </summary>
function cuvCPCDPriceOptions_ClientValidate(source, arguments) {
    // Check if delegate attendance option is selected
    if ($('[id$=chkCommunityPowerConferenceDelegate]').is(':checked') == true) {

        // Check that only one price option was checked
        arguments.IsValid = NMA.utility.xor(
                            $('[id$=chkCPCDPriceOptionsDiscountCode]').is(':checked'),
                            $('[id$=chkCPCDPriceOptionsOSEAMember]').is(':checked'),
                            $('[id$=chkCPCDPriceOptionsNonMember]').is(':checked'));

    }
}

/// <summary>
/// Validation of Delegate price options
/// </summary>
function cuvCPCDConferenceOptions_ClientValidate(source, arguments) {
    // Check if delegate attendance option is selected
    if ($('[id$=chkCommunityPowerConferenceDelegate]').is(':checked') == true) {

        // Check that one price package was selected
        arguments.IsValid = NMA.utility.xor(
                            $('[id$=chkConferenceDelegatePackageBasic]').is(':checked'),
                            $('[id$=chkConferenceDelegatePackageFull]').is(':checked'),
                            $('[id$=chkConferenceDelegatePackagePriceGold]').is(':checked'));
    }
}

/// <summary>
/// Validation of Delegate price options for cms user
/// </summary>
function cuvCPCDConferenceOptions_CMSUser_ClientValidate(source, arguments) {
    // Check if delegate attendance option is selected
    if ($('[id$=chkCommunityPowerConferenceDelegate]').is(':checked') == true) {

        // Check that one price package was selected
        var selected = NMA.utility.xor(
                            $('[id$=chkConferenceDelegatePackageBasic]').is(':checked'),
                            $('[id$=chkConferenceDelegatePackageFull]').is(':checked'),
                            $('[id$=chkConferenceDelegatePackagePriceGold]').is(':checked'),
                            $('[id$=chkConferenceDelegatePackagePaired]').is(':checked'));

        var price = '';

        if ($('[id$=chkConferenceDelegatePackageBasic]').is(':checked')) {
            price = $('[id$=txtConferenceDelegatePackagePriceBasic]').val();

        }
        if ($('[id$=chkConferenceDelegatePackageFull]').is(':checked')) {
            price = $('[id$=txtConferenceDelegatePackagePriceFull]').val();

        }
        if ($('[id$=chkConferenceDelegatePackagePriceGold]').is(':checked')) {
            price = $('[id$=txtConferenceDelegatePackagePriceGold]').val();

        }
        if ($('[id$=chkConferenceDelegatePackagePaired]').is(':checked')) {
            price = $('[id$=txtConferenceDelegatePackagePaired]').val();

        }

// Validate 
        arguments.IsValid = NMA.utility.isnumber(price,true);
    }
}


/// <summary>
/// Validation of power centre visitor day
/// </summary>
function cuvPNCVOptions_ClientValidate(source, arguments) {
    // if power centre visitor is checked - one day must be selected
    if ($('[id$=chkPowerNetworkingCentreVisitor]').is(':checked') !== false) {
        if ($('[id$=chkPowerNetworkingCentreVisitorMonday]').is(':checked') !== false
                    || $('[id$=chkPowerNetworkingCentreVisitorTuseday]').is(':checked') !== false
                    || $('[id$=chkPowerNetworkingCentreVisitorWednesday]').is(':checked') !== false) {
            arguments.IsValid = true;
        }
        else {
            arguments.IsValid = false;
        }


    }
}

/// <summary>
/// Validation of discount code
/// </summary>
/// <param name="source"></param>
/// <param name="arguments"></param>
function cuvCPCDPriceOptionDiscountCode_ClientValidate(source, args) {

    args.IsValid = false;

    var discount_code = $('[id$=txtDiscountCode]').val();

    if ($('[id$=chkCPCDPriceOptionsDiscountCode]').is(':checked') !== false) {
        if (discount_code.length > 0) {

            // Verify discount code
            $.ajax({
                type: "POST",
                url: "Registration.aspx/VerifyDiscountCode",
                data: "{'discount_code':'" + discount_code + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function(msg) {

                    if (msg.d === true) {

                        args.IsValid = true;
                        //source.innerHTML = "";
                        update_package_pricing();


                    }
                    else {
                        source.innerHTML = "Invalid discount code";
                        args.IsValid = false;

                    }
                }
            });
        }
        else {
            source.innerHTML = "Invalid discount code";
            args.IsValid = false;

        }


    }
}

/// <summary>
/// Validation of OSEA member credentials
/// </summary>
function cuvCPCDPriceOptionOseaMember_ClientValidate(source, args) {
    var username = $('[id$=txtOseaUsername]').val() + '';
    var password = $('[id$=txtOseaPassword]').val() + '';

    args.IsValid = false;


    if ($('[id$=chkCPCDPriceOptionsOSEAMember]').is(':checked') !== false) {
        if (username.length > 0 && password.length > 0) {

            // Verify credentials
            $.ajax({
                type: "POST",
                url: "Registration.aspx/VerifyMemberLoginCredentials",
                data: "{'username':'" + username + "', 'password':'" + password + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function(msg) {

                    if (msg.d === true) {

                        args.IsValid = true;
                        source.innerHTML = "";
                        update_package_pricing();


                    }
                    else {
                        source.innerHTML = "Invalid OSEA member credentials";
                        args.IsValid = false;
                    }
                }
            });

        }
        else {
            source.innerHTML = "Please enter OSEA member credentials";
            args.IsValid = false;

        }
    }
    else {
        args.IsValid = true;
    }
}
