MediaWiki:FR2013/Resources/MobileBanner-WP.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
{{MediaWiki:FR2013/Resources/CurrencyMinimums.js}}
<script>
/**
 * Javascript for mobile banners.
 * This version assumes checkboxes for amount, buttons for payment method
 *
 * Dependencies: MediaWiki:FR2013/Resources/Country2Currency.js
 */

var fundraisingBanner = fundraisingBanner || {};

fundraisingBanner.validateForm = function(form) {
    var error = true;

    // Get amount selection
    var amount = null;
    for (var i = 0; i < form.amount.length; i++) {
        if (form.amount[i].checked) {
            amount = form.amount[i].value;
        }
    }
    if (form.amountGiven !== undefined) {
        if (form.amountGiven.value != '') {
            var otherAmount = form.amountGiven.value;
            otherAmount = otherAmount.replace(/[,.](\d)$/, '\:$10');
            otherAmount = otherAmount.replace(/[,.](\d)(\d)$/, '\:$1$2');
            otherAmount = otherAmount.replace(/[\$,.]/g, '');
            otherAmount = otherAmount.replace(/:/, '.');
            form.amountGiven.value = otherAmount;
            amount = otherAmount;
        }
    }
    // Check amount is a real number
    error = ( amount == null || isNaN(amount) || amount.value <= 0 );
    // Check amount is at least the minimum
    var currency = form.currency_code.value;
    if (amount < getMinimum(currency) || error) {
        alert('{{{validation-error-minimum|{{int:fr2013-dropdown-smallamount-error}}}}}'.replace('$1', getMinimum(currency) + ' ' + currency));
        error = true;
    }
    return !error;
}

fundraisingBanner.redirectPayment = function(paymentMethod, paymentSubMethod) {
    if (typeof paymentSubMethod == 'undefined'){
        paymentSubMethod = '';
    }
    var form = document.paypalcontribution; // we should really change this some day
    var language = $("input[name='language']").val();

    var paymentsURL = 'https://payments.wikimedia.org/index.php/Special:GatewayFormChooser';

    var params = {
        'uselang' : language,
        'language' : language,
        'currency' : $("input[name='currency_code']").val(),
        'country' : $("input[name='country']").val(),
        'paymentmethod' : paymentMethod
    };
    if( paymentSubMethod != '' ){
        params['submethod'] = paymentSubMethod;
    }

    // WorldPay override for cc
    if( paymentMethod === 'cc' ) {
        params.gateway = 'worldpay';
        params.ffname = 'worldpay';
    }

    form.action = paymentsURL + '?' + $.param(params);
    form.payment_method.value = paymentMethod;
    if( paymentSubMethod != '' ) {
        form.payment_method.value = form.payment_method.value + '.' + paymentSubMethod;
    }
    form.utm_source.value = '{{{banner}}}.no-LP' + '.' + form.payment_method.value;

    if (fundraisingBanner.validateForm(form)) {
        form.submit();
    }
}


$(document).ready( function () {

    mw.loader.using(['mediawiki.util']).then(function() {

        // Allow overriding the geolocation for testing different countries
        if(mw.util.getParamValue('country') !== null){
            Geo.country = mw.util.getParamValue('country');
        }

        var currency = getCurrency();
        var language = mw.config.get('wgUserLanguage');

        // set the form fields
        $("input[name='country']").val(Geo.country);
        $("input[name='currency_code']").val(currency);
        $("input[name='language']").val(mw.config.get('wgUserLanguage'));
        $("input[name='return_to']").val("Thank_You/" + mw.config.get('wgUserLanguage'));

        // append the banner count in utm-key
        var cookieName = 'centralnotice_bannercount_fr12';
        var count = $.cookie(cookieName);
        $('[name="paypalcontribution"]').append(
            $('<input type="hidden" name="utm_key" />').attr('value', count)
        );

        // Localize links in the smallprint
        $('a.localize').each(function(index) {
            // Add language and country
            var url = $(this).attr("href");
            if( url.indexOf("?") === -1 ) {
                url = url + "?";
            } else {
                url = url + "&";
            }
            $(this).attr("href", url + "country=" + Geo.country + "&language=" + language + "&uselang=" + language);
        });

        // Show the correct legal text variant
        if (Geo.country === 'US') {
            $('.informationsharing-US').show();
            $('.informationsharing-nonUS').hide();
        }

    });

});
</script>