User:Pcoombe/donate-now



//--- Utility Functions--- function arrayToObject(variable) // helper function: converts an array to object literal with keys // then it can be used for quick "in" tests { var obj = {};

for (var i=0; i<variable.length; i++) {   obj[variable[i]]=''; }

return obj; }

function getQuerystring( key ) { key = key.replace( /[\[]/, '\\\[' ).replace( /[\]]/, '\\\]' ); var regex = new RegExp( '[\\?&]' + key + '=([a-zA-Z0-9\_\-]*)' ); var qs = regex.exec( window.location.search ); return qs == null ? '' : qs[1]; }

function getQuerystring2(key, default_) // a function to parse the information passed by central notice { if (default_==null) default_="";

key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regex = new RegExp("[\\?&]"+key+"=([^&#]*)"); var queryed = regex.exec(window.location.href);

if(queryed == null) return default_; else return queryed[1]; }

function getLandingPage { var path = "  ";

if ( path == "default" ) {   pathArray = window.location.pathname.split( '/' ); return pathArray[2]; } else return path; }

// Template defined Arrays var currencyTypes = new Array ; var currencyDonationAmounts = { }; var currencyDisplay = new Array ;

//--constant Arrays--- var countryCurrency = { } var currencyPositions = { }; var currencySymbols = { }; var currencyMinimums = { }; var acceptedCC = arrayToObject([ ]); var acceptedPayPal = arrayToObject([ ]); var acceptedBankTransfer = arrayToObject([ ]);

//-Variables var countryCode = getQuerystring ( 'country' ); var currency = countryCurrency[countryCode]; if(currency == undefined) currency = 'USD';

//- Body functions --- function loadLocalValues { currencyDonationAmounts = {  }; }

function loadGeneralValues { currencyDonationAmounts = {  }; }

function updateButtons( currency ) { document.getElementById('cc-donate-button').style.display = "none"; document.getElementById('paypal-donate-button').style.display = "none"; document.getElementById('banktransfer-donate-button').style.display = "none"; if ( currency in acceptedCC ) {   //is this template still needed? document.getElementById('cc-donate-button').style.display = " "; } if (currency in acceptedPayPal ) {   //why not this? document.getElementById('paypal-donate-button').style.display = "block"; } if (currency in acceptedBankTransfer ) {   //or this? document.getElementById('banktransfer-donate-button').style.display = "block"; } }

function updateSymbol(currency) { $('.amount-label-symbol').remove;

if ( currencySymbols[currency] == undefined ) return;

if ( currencyPositions[currency] == 'after' ) {   $('.amount-label').append(' ' + currencySymbols[currency] + ' '); } else {   $('.amount-label').prepend(' ' + currencySymbols[currency] + ' '); } }

function updateValue(currency) { for(i=0; i<10; i++) {   if(currencyDonationAmounts[currency][i] == undefined) {     break; }

if(currencyDisplay[i] != 0) {     document.getElementById("input_amount_" + i).value = currencyDonationAmounts[currency][i]; document.getElementById("input_amount_label_" + i).innerHTML = currencyDonationAmounts[currency][i]; } } } //--form junk function validateForm( 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.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[form.currency_code.selectedIndex].value; if ( typeof( currencyMinimums[currency] ) == 'undefined' ) { currencyMinimums[currency] = 1; } if ( amount < currencyMinimums[currency] || error ) { alert( ' '.replace('$1', currencyMinimums[currency] + ' ' + currency ) ); error = true; } return !error; }

function DefaultSubmit(formfield, Action) { var keycode; if (window.event) {   keycode = window.event.keyCode; } else if (Action) {   keycode = Action.which; } else return true;

if (keycode == 13) {   if (document.getElementById('cc-donate-button').style.display == "block") {     redirectPayment('cc'); }   else if (document.getElementById('paypal-donate-button').style.display == "block") {     redirectPayment('pp'); }   return false; } else return true; }

function redirectPayment(paymentType) { if (paymentType == 'cc') {   var paymentType = 'cc'; var action_url = "https://payments.wikimedia.org/index.php/Special:PayflowProGateway?uselang= \x26masthead=\x26form_name=\x26text_template=\x26language= " ; } if (paymentType == 'pp') {   action_url="http://wikimediafoundation.org/wiki/Special:ContributionTracking/  "; } document.paypalcontribution.action = action_url; document.paypalcontribution.utm_source.value = getQuerystring( 'utm_source' ) + '.' + getLandingPage + '.' + paymentType; if(validateForm(document.paypalcontribution)) {   if(typeof(OWA) !== 'undefined') {     OWATracker.shareStateByPost( document.paypalcontribution ); }   document.paypalcontribution.submit; } }

          





var items = 1; for(i=0; i<10; i++) {     if(items%4 == 0) {       document.getElementById("amount-box").innerHTML += " "; }

if(currencyDonationAmounts[currency][i] == undefined ) {       break; }

if(currencyDisplay[i] != 0) {       document.getElementById("amount-box").innerHTML += " " + " " + currencyDonationAmounts[currency][i] + " ";

items++; }     //var inp = document.createElement("input"); //inp.value = currencyTypes[i]; //document.getElementById("amount-box").addChild(opt); }

<input type="radio" name="amount" id="input_amount_other" value="" /><label for="input_amount_other"> <input type="text" name="amountGiven" size="3" style="color:#666666;" onclick="this.value=''; toggleGreyout(this);" onfocus="this.form.input_amount_other.checked=true;" onKeyPress="return DefaultSubmit(this,event)" />

<select name="currency_code" id="input_currency_code" size="1" style="width:8em; font-size:80%;" onchange="setOtherBox; loadGeneralValues; updateValue(this.value); updateSymbol(this.value); updateButtons(this.value);">

<script type="text/javascript" language="javascript"> var opt = document.createElement("option"); opt.selected = true; opt.text = currency + " – " + currencySymbols[currency]; opt.value = currency; document.getElementById("input_currency_code").options.add(opt); //       var opt = document.createElement("option"); opt.text = "---"; opt.value = "USD"; document.getElementById("input_currency_code").options.add(opt)

for(i=0; i<currencyTypes.length; i++) {         opt = document.createElement("option"); opt.text = currencyTypes[i] + " – " + currencySymbols[currencyTypes[i]]; opt.value = currencyTypes[i];

document.getElementById("input_currency_code").options.add(opt); }

<input type="button" value=" " class="button" onclick="redirectPayment('cc');" /> <input type="button" value=" " class="button" onclick="redirectPayment('pp');" /> <input type="button" value=" " class="button" onclick="redirectPayment('bt');" />

    *  *  *  <p style="margin-top: 1em;">

<script type="text/javascript"> function setOtherBox { //document.getElementsByName("amountGiven")[0].style.color = "#666666"; //document.getElementsByName("amountGiven")[0].value = "Other"; }

function toggleGreyout(elemID) { var currentColor = document.getElementsByName("amountGiven")[0].style.color;

if(currentColor == '#000000') document.getElementsByName("amountGiven")[0].style.color = "#666666"; else document.getElementsByName("amountGiven")[0].style.color = "#000000"; }

document.paypalcontribution.utm_medium.value = getQuerystring( 'utm_medium' ); document.paypalcontribution.utm_campaign.value = getQuerystring( 'utm_campaign' ); document.paypalcontribution.country.value = getQuerystring ( 'country_code' ); document.paypalcontribution.referrer.value = document.referrer;

( function( $ ) { $(document).ready(function{ //currency = $("#input_currency_code").val; updateButtons(currency); updateValue(currency); updateSymbol(currency); setOtherBox; }) } )( jQuery );