/* --------------------------------------------------------------------------------------- */
/* */
/* _ _ _____ _ _ */
/* | | | | / ____(_) (_) */
/* | |_ _ ___| |_ | | __ ___ ___ _ __ __ _ */
/* _ | | | | / __| __| | | |_ | \ \ / / | '_ \ / _` | */
/* | |__| | |_| \__ \ |_ | |__| | |\ V /| | | | | (_| | */
/* \____/ \__,_|___/\__| \_____|_| \_/ |_|_| |_|\__, | */
/* __/ | */
/* |___/ */
/* */
/* --------------------------------------------------------------------------------------- */
/* */
/* JustGiving - Donate Box */
/* */
/* --------------------------------------------------------------------------------------- */
/* */
/* Mandatory Attributes: */
/* */
/* data-charity: [integer] charity id */
/* */
/* Optional Attributes: */
/* */
/* data-width: [integer px - eg. 450px]. */
/* If not supplied, a default of 330px is used. */
/* There are min and max widths of 300px and 600px. */
/* Additionally, percentage widths are not supported, due to odd */
/* behaviour on mobile devices. */
/* */
/* data-env: string - uri. Internal attribute for JustGiving to switch between */
/* environments. Although, harmless, this should probably not be */
/* published. example values for different servers are shown below */
/* (notice trailing slash) : */
/* */
/* - testing : 'https://v3-testing.staging.justgiving.com' */
/* - staging : 'https://v3-integration.staging.justgiving.com' */
/* - release candidate : 'https://v3-rc.staging.justgiving.com' */
/* - live (default) : 'https://www.justgiving.com' */
/* */
/* data-reference: string. Reference to show in the donation reports which */
/* donation was made from the donatebox with the given reference. */
/* */
/* --------------------------------------------------------------------------------------- */
/* */
/* examples: */
/* */
/* */
/* --------------------------------------------------------------------------------------- */
(function() {
var root = document.getElementById('jgGive'),
charityId = root.getAttribute('data-charity'),
width = root.getAttribute('data-width'),
env = root.getAttribute('data-env'),
reference = root.getAttribute('data-reference'),
iframe = document.createElement('iframe'),
minWidth = '331px',
maxWidth = '600px';
var pageUrl = window.location.toString();
var hasDonated = pageUrl.indexOf('#Donated') > -1;
var fullReference = getFullReference(reference);
var src = getEnv(env) + '/donatebox/' + charityId + '?u=' + encodeURIComponent(pageUrl) + '&d=' + hasDonated + '&r=' + fullReference;
var request = new XMLHttpRequest();
request.open('GET', src, true);
request.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 400) {
iframe.setAttribute('src', src);
iframe.setAttribute('height', '275');
iframe.setAttribute('width', getWidth(width));
iframe.setAttribute('frameborder', '0');
iframe.setAttribute('style', 'border:0;min-width:' + minWidth + ' !important;max-width:' + maxWidth);
iframe.setAttribute('scrolling', 'no');
root.parentNode.insertBefore(iframe, root);
root.parentNode.removeChild(root);
} else {
console.log('Donate box error: not loading frame');
var url = getEnv(env) + '/4w350m3/donation/direct/charity/' + charityId;
var text = 'This feature is temporarily unavailable. To donate to this charity directly on JustGiving please click here';
var div = document.createElement('div');
div.innerHTML = text;
root.parentNode.insertBefore(div, root);
root.parentNode.removeChild(root);
}
}
};
request.send();
request = null;
function getWidth(/* int or percent */ width) {
var defaultValue = '330';
if (width !== null) {
width = width.replace(' ', '');
if (width.indexOf('%') > 0) {
width = defaultValue;
if (window.console) {
console.log('JustGiving Give : Percentage widths are not supported. The default value, ' +
defaultValue + ' has been used instead');
}
}
}
else {
width = defaultValue;
}
return width;
}
function getEnv(/* string - alt uri to use */ env) {
return (env !== null) ? env : 'https://www.justgiving.com/';
}
function getFullReference(reference) {
var referenceSufix = 'donatebox_tj21_buttonv1';
if (!reference)
return referenceSufix;
return reference + '_' + referenceSufix;
};
}() );