var amplyMatchingCompany; var amplyPostMatch; var amplyToggleMatch; var amplyPageSetup; var amplyFormString; var amplyMatchData = {}; var amplyRecieveMessageData; var amplyMessagePostCheck; (function( window, document, req_version, callback, $, script, done, readystate ){ // If jQuery isn't loaded, or is a lower version than specified, load the // specified version and call the callback, otherwise just call the callback. if ( !($ = window.jQuery) || req_version > $.fn.jquery || callback( $ ) ) { // Create a script element. script = document.createElement( 'script' ); script.type = 'text/javascript'; // Load the specified jQuery from the Google AJAX API server (minified). script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/' + req_version + '/jquery.min.js'; // When the script is loaded, remove it, execute jQuery.noConflict( true ) // on the newly-loaded jQuery (thus reverting any previous version to its // original state), and call the callback with the newly-loaded jQuery. script.onload = script.onreadystatechange = function() { if ( !done && ( !( readystate = this.readyState ) || readystate == 'loaded' || readystate == 'complete' ) ) { console.log(window.jQuery); callback( window.jQuery, done = 1 ); window.jQuery( script ).remove(); } }; // Add the script element to either the head or body, it doesn't matter. document.head.appendChild( script ); } })( window, document, '3.5.1', // $ refers to the jQuery object, // and L is a boolean that indicates whether or not an external jQuery file // was just "L"oaded. function( $, L ) { '$:nomunge, L:nomunge'; // Used by YUI compressor. (function( window, document, req_version, callback, $, script, done, readystate ){ // If jQuery UI isn't loaded, or is a lower version than specified, load the // specified version and call the callback, otherwise just call the callback. if ( !(typeof $.ui != 'undefined') || req_version > $.fn.jquery || callback( $ ) ) { // Create a script element. script = document.createElement( 'script' ); script.type = 'text/javascript'; // Load the specified jQuery from the Google AJAX API server (minified). script.src = 'https://code.jquery.com/ui/' + req_version + '/jquery-ui.min.js'; // When the script is loaded, remove it, execute jQuery.noConflict( true ) // on the newly-loaded jQuery (thus reverting any previous version to its // original state), and call the callback with the newly-loaded jQuery. script.onload = script.onreadystatechange = function() { if ( !done && ( !( readystate = this.readyState ) || readystate == 'loaded' || readystate == 'complete' ) ) { callback($.noConflict(1), done = 1 ); var link = $("",{ rel: "stylesheet", type: "text/css", href: "https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" }); $('head').append(link); $( script ).remove(); } }; // Add the script element to either the head or body, it doesn't matter. document.head.appendChild( script ); } })( window, document, // Minimum jQuery UI version required. Change this as-needed. '1.12.1', // $ refers to the jQuery object, // and L is a boolean that indicates whether or not an external jQuery file // was just "L"oaded. function( $, L ) { '$:nomunge, L:nomunge'; // Usxd by YUI compressor. var amplyMatchDefaults = { "retry": true, "instant_match": true, "opt_in": false, "opt_in_target": "amplyCompanySearch", // the id of the element to hide/show when the company search is toggled "opt_in_text": "I would like to be contacted about my company's matching program", "submit_type": "direct", // direct | button | watch | confirmation. direct: function called from parent page | button: button click event/form submit | check: check field for value change "message_submit_map": "transaction.submitted", "message_submit_value": true, "log_messages": false, "watch_field": "payment_success", "watch_value": true, // defaults to true "submit_id": "submit", // id of submit button "donation_form": "donationForm", // id of donation form "donate_button": true, // show donate button "field_id": { "donor_name": "amplyDonorName", "donor_fname": "amplyDonorFirstName", "donor_lname": "amplyDonorLastName", "donor_email": "amplyDonorEmail", "donor_phone": "amplyDonorPhone", "address_street": "amplyDonorStreet", "address_city": "amplyDonorCity", "address_state": "amplyDonorState", "address_zip": "amplyDonorZip", "company_name": "amplyCompanySearch", "amount": "amplyDonationAmount", }, "message_data_map": null } var amplyKeyArray = { 'donor_name': 'donor_name', 'donor_full_name': 'donor_name', 'donorname': 'donor_name', 'donorfullname': 'donor_name', 'donor_fname': 'donor_fname', 'donor_first_name': 'donor_fname', 'donorfirstname': 'donor_fname', 'donor_lname': 'donor_lname', 'donor_last_name': 'donor_lname', 'donorlastname': 'donor_lname', 'full_name': 'donor_name', 'fullname': 'donor_name', 'fname': 'donor_fname', 'first_name': 'donor_fname', 'firstname': 'donor_fname', 'lname': 'donor_lname', 'last_name': 'donor_lname', 'lastname': 'donor_lname', 'donation_amount': 'amount', 'donate_amount': 'amount', 'donationamount': 'amount', 'donateamount': 'amount', 'amount': 'amount', 'gift_amount': 'amount', 'giftamount': 'amount', 'donation': 'amount', 'company': 'company_name', 'company_name': 'company_name', 'match_company': 'company_name', 'matching_company': 'company_name', 'matchcompany': 'company_name', 'matchingcompany': 'company_name', 'companyname': 'company_name', 'employer': 'company_name', 'employer_name': 'company_name', 'employername': 'company_name', 'donor_email': 'donor_email', 'donoremail': 'donor_email', 'donor_email_address': 'donor_email', 'emailaddress': 'donor_email', 'email_address': 'donor_email', 'donoremailaddress': 'donor_email', 'donor_phone': 'donor_phone', 'donorphone': 'donor_phone', 'donor_phone_number': 'donor_phone', 'phonenumber': 'donor_phone', 'phone_number': 'donor_phone', 'donorphonenumber': 'donor_phone', 'donor_cell_phone': 'donor_phone', 'cell_phone': 'donor_phone', 'transaction': 'transaction_id', 'transaction_id': 'transaction_id', } amplyPageSetup = () => { amplyMatchOptions = Object.assign(amplyMatchDefaults, amplyMatchOptions); window.addEventListener('message', function (e) { const key = e.message ? 'message' : 'data'; const data = e[key]; amplyRecieveMessageData(data); }) if (amplyGetQueryVariable('amply_match_id')) { amplyGetInstantMatchId(amplyGetQueryVariable('amply_match_id')); } else { if (amplyMatchOptions.opt_in) { amplyAddCheckbox(amplyMatchOptions.opt_in_target, amplyMatchOptions.opt_in_text) } if (amplyMatchOptions.submit_type == 'policy') { $('head').append(''); $('#amplyPolicySearch').html(''); amplyCreateAutocomplete('amplyCompanySearch', amplyGetCompanyPolicy); } else amplyCreateAutocomplete(amplyMatchOptions.field_id.company_name) if (amplyMatchOptions.submit_type == 'watch') { console.log('watch') setTimeout(amplyCheckDonationCompleted,500); } if (amplyMatchOptions.submit_type == 'button') { let amplyReplaceButton = $("#" + amplyMatchOptions.submit_id).clone(); amplyReplaceButton.attr('id', 'amplySubmitButton'); amplyReplaceButton.attr('type', 'button'); $("#" + amplyMatchOptions.submit_id).hide(); $("#" + amplyMatchOptions.submit_id).after(amplyReplaceButton); document.getElementById('amplySubmitButton').addEventListener('click', function(event) { event.preventDefault() amplyPostMatch(); }); } if (amplyMatchOptions.submit_type !== 'policy') { document.getElementById('amplyRoot').innerHTML = '
'; $('#amplyRoot').show(); } } } function amplyGatherFormData() { if (!document.getElementById(amplyMatchOptions.field_id.company_name)) return amplyMatchingCompany = document.getElementById(amplyMatchOptions.field_id.company_name).value; let donorName = document.getElementById(amplyMatchOptions.field_id.donor_name) ? (document.getElementById(amplyMatchOptions.field_id.donor_name) ? document.getElementById(amplyMatchOptions.field_id.donor_name).value : "") : (document.getElementById(amplyMatchOptions.field_id.donor_fname) ? document.getElementById(amplyMatchOptions.field_id.donor_fname).value : "") + " " + (document.getElementById(amplyMatchOptions.field_id.donor_lname) ? document.getElementById(amplyMatchOptions.field_id.donor_lname).value : ""); let donorEmail = document.getElementById(amplyMatchOptions.field_id.donor_email); let donorPhone = document.getElementById(amplyMatchOptions.field_id.donor_phone); let donorStreet = document.getElementById(amplyMatchOptions.field_id.address_street); let donorCity = document.getElementById(amplyMatchOptions.field_id.address_city); let donorState = document.getElementById(amplyMatchOptions.field_id.address_state); let donorZip = document.getElementById(amplyMatchOptions.field_id.address_zip); let companyName = document.getElementById(amplyMatchOptions.field_id.company_name); let amount = document.getElementById(amplyMatchOptions.field_id.amount); amplyMatchData = { "donor_name": donorName, "donor_email": donorEmail ? donorEmail.value : "", "donor_phone": donorPhone ? donorPhone.value : "", "address_street": donorStreet ? donorStreet.value : "", "address_city": donorCity ? donorCity.value : "", "address_state": donorState ? donorState.value : "", "address_zip": donorZip ? donorZip.value : "", "company_name": amplyMatchData.company_name ? amplyMatchData.company_name : (companyName ? companyName.value : ""), "amount": amount ? amount.value : "", }; console.log(amplyMatchData) } function amplyExtractByProps(obj, props) { return props.reduce( (acc, prop) => typeof acc === 'object' && prop in acc ? acc[prop] : undefined, obj ) } amplyRecieveMessageData = (data, parent = '') => { if (typeof data != 'object'){ try { data = JSON.parse(data); } catch (e) { return false; } } let keys = Object.keys(data); if (amplyMatchOptions.log_messages && parent == '') console.log(data); if (amplyMatchOptions.message_data_map) { keys = Object.keys(amplyMatchOptions.message_data_map); keys.forEach((key) => { let dataKeys = amplyMatchOptions.message_data_map[key]; dataKeys = dataKeys.split('.'); if (typeof amplyExtractByProps(data, dataKeys) != 'undefined') { amplyMatchData[key] = amplyExtractByProps(data, dataKeys); } }) } else { // Keys not supplied, try to find them automatically keys.forEach((key) => { if (typeof data[key] == 'object' && data[key]) { amplyRecieveMessageData(data[key], key) } else if (typeof data[key] != 'array' && data[key] != '') { if (typeof amplyKeyArray[key.toLowerCase()] != 'undefined') { amplyMatchData[amplyKeyArray[key.toLowerCase()]] = data[key]; } else { if (key.toLowerCase() == 'name' && ['', 'donor','user','patron', 'contact'].indexOf(parent) >= 0) { amplyMatchData['donor_name'] = data[key]; return } if (key.toLowerCase() == 'email' && ['', 'donor','user','patron', 'contact'].indexOf(parent) >= 0) { amplyMatchData['donor_email'] = data[key]; return } if (key.toLowerCase() == 'phone' && ['', 'donor','user','patron', 'contact'].indexOf(parent) >= 0) { amplyMatchData['donor_phone'] = data[key]; return } if (key.toLowerCase() == 'name' && ['company','employer','matchingcompany', 'matching_company'].indexOf(parent) >= 0) { amplyMatchData['company_name'] = data[key]; return } if (key.toLowerCase() == 'email' && ['company','employer','matchingcompany', 'matching_company'].indexOf(parent) >= 0) { amplyMatchData['company_name'] = data[key]; return } if (key.toLowerCase() == 'phone' && ['company','employer','matchingcompany', 'matching_company'].indexOf(parent) >= 0) { amplyMatchData['company_name'] = data[key]; return } if (key.toLowerCase() == 'value' && ['ecommerce','transaction'].indexOf(parent) >= 0) { amplyMatchData['amount'] = data[key]; return } } } }) } if (amplyMatchOptions.log_messages && parent === '') console.log(amplyMatchData); if (parent === '') { if (!amplyMatchData.donor_name && amplyMatchData.donor_fname) { amplyMatchData.donor_name = amplyMatchData.donor_fname + ' ' + amplyMatchData.donor_lname; } let dataKeys = amplyMatchOptions.message_submit_map.split('.'); let submit = amplyExtractByProps(data, dataKeys) == amplyMatchOptions.message_submit_value; if (amplyMatchOptions.message_submit_value === true) { submit = !!amplyExtractByProps(data, dataKeys); } if (submit) amplyPostMatch(); } } amplyPostMatch = (callback = null) => { console.log(amplyMatchData) if (!amplyMatchData.donor_name || !amplyMatchData.donor_email || !amplyMatchData.amount) amplyGatherFormData(); if (typeof amplyMatchOptions.btn_class != 'undefined') amplyMatchData.btn_class = amplyMatchOptions.btn_class; $('#amplyLoading').show(); $.ajax({ type: "POST", url: 'https://sandbox.giveamply.com/api/v1/widget/8832/donation', data: amplyMatchData, success: (data) => { console.log(data); if (typeof data.donation != 'undefined' && typeof data.donation.status_id != 'undefined' && data.donation.status_id == 23 && amplyMatchOptions.retry) { $('#companySearch').show(); amplyCreateAutocomplete('amplyMatchRetry', amplyQuickCompany); amplyMatchData.donation_id = data.donation.id; return; } if (callback) callback(); if (amplyMatchOptions.submit_type == 'button') { let tempAction = document.getElementById(amplyMatchOptions.donation_form).action; if (tempAction.indexOf('?') > -1) tempAction += '&'; else tempAction += '?'; if (typeof data.donation !== 'undefined') tempAction += 'amply_match_id=' + data.donation.id; document.getElementById(amplyMatchOptions.donation_form).action = tempAction $('#' + amplyMatchOptions.submit_id).click(); } if (!amplyMatchOptions.instant_match) return; if (typeof data.donation == 'undefined' || typeof data.donation.match_steps.iMatchable == 'undefined' || !data.donation.match_steps.iMatchable) { console.log("No Instant Match Available"); // $('#amplyLoading').hide(); // $('#noInstant').show(); // return; } document.getElementById('amplyRoot').innerHTML = data.donation.match_steps.html; if (typeof amplyMatchOptions.scroll_into_view != 'undefined' && amplyMatchOptions.scroll_into_view) { let scrollOptions = { behavior: "smooth", block: "start", inline: "nearest" }; if (amplyMatchOptions.scroll_into_view == 'end') scrollOptions = { behavior: "smooth", block: "end", inline: "nearest" }; document.getElementById('amplyRoot').scrollIntoView(scrollOptions); } // amplyInstantMatch(data); $('#amplyLoading').hide(); }, error: function (data) { $('#amplyLoading').hide(); console.log(data); if (callback) callback(); if (amplyMatchOptions.submit_type == 'button') { $('#' + amplyMatchOptions.submit_id); } if (amplyMatchOptions.submit_type == 'watch') { setTimeout(amplyCheckDonationCompleted,3000); } } }); } amplyQuickCompany = () => { let amplyId = amplyGetQueryVariable('amply_match_id') ? amplyGetQueryVariable('amply_match_id') : amplyMatchData.donation_id; let data = {}; data._token = "cKiO2WnNEOkc8leeBufr8apqAVJtVVQl6fnQCxxL"; data.company_name = amplyMatchData.company_name; if (typeof amplyId == 'undefined' || !amplyId) return $.ajax({ type: "POST", url: 'https://sandbox.giveamply.com/api/enter-company-name/' + amplyId, data: data, success: (data) => { if (typeof data.donation == 'undefined' || typeof data.donation.match_steps.iMatchable == 'undefined' || !data.donation.match_steps.iMatchable) { console.log("No Instant Match Available"); // $('#amplyLoading').hide(); // $('#noInstant').show(); // return; } document.getElementById('amplyRoot').innerHTML = data.donation.match_steps.html; // amplyInstantMatch(data); $('#companySearch').hide(); }, error: function (data) { console.log(data); } }); } function amplyGetInstantMatchId(id) { let retryUrl = amplyMatchOptions.retry ? '?retry' : ''; let btnClass = amplyMatchOptions.btn_class ? ('?btnClass=' + amplyMatchOptions.btn_class) : ''; if (btnClass && retryUrl) { btnClass += '&retry'; retryUrl = ''; } $.ajax({ type: "GET", url: 'https://sandbox.giveamply.com' + "/donation/" + id + "/instant-match/data" + retryUrl + btnClass, success: (data) => { if (typeof data.donation != 'undefined' && amplyMatchOptions.retry && data.donation.status_id == 23) { $('#companySearch').show(); amplyCreateAutocomplete('amplyMatchRetry', amplyQuickCompany); return; } if (!amplyMatchOptions.instant_match) return; document.getElementById('amplyRoot').innerHTML = data; if (typeof amplyMatchOptions.scroll_into_view != 'undefined' && amplyMatchOptions.scroll_into_view) { let scrollOptions = { behavior: "smooth", block: "start", inline: "nearest" }; if (amplyMatchOptions.scroll_into_view == 'end') scrollOptions = { behavior: "smooth", block: "end", inline: "nearest" }; document.getElementById('amplyRoot').scrollIntoView(scrollOptions); } }, error: function (data) { console.log(data); } }); } function amplyInstantMatch(data) { // $('#step1Button').attr('href','https://sandbox.giveamply.com/donation/' + data.donation.id + '/confirm/donor?web_confirm=true'); // $('#step1Button').html(data.donation.match_steps.step2.text); if (data.donation.status_id == 4 || data.donation.status_id == 3) { $('#isSubmitted').slideToggle(); } else { $('#isNotSubmitted').slideToggle(); if (data.donation.status_id !== 8) $('#step1Confirm').slideToggle(); // $('#donationThanksMsg').html(data.donation.match_steps.thanksMsg); } $('#donationDetails').slideToggle(); if (data.donation.status_id == 8) { $('#coRep').slideToggle(); // $('#contactName').html(data.donation.company.contact_name); // $('#contactEmail').html(data.donation.company.contact_email); // $('#contactPhone').html(data.donation.company.contact_phone); } if (typeof data.donation.match_steps.step1 != 'undefined' && data.donation.match_steps.step1.url) { $('#step1').slideToggle(); // $('#step1Link').html(data.donation.match_steps.step1.text); // $('#step1Link').attr('href', data.donation.match_steps.step1.url); } // $('#donationAmount').html(data.donation.amount); // $('#donationTransactionId').html(data.donation.transaction_id); } function amplyCheckDonationCompleted() { let field = document.getElementById(amplyMatchOptions.watch_field); if (field.value == amplyMatchOptions.watch_value || field.value == amplyMatchOptions.watch_value.toString()) amplyPostMatch(); else setTimeout(amplyCheckDonationCompleted,500); } function amplyAddCheckbox(amplyInputId, value){ let checkbox = $(''); let label = $(''); let container = $('
'); $('#' + amplyInputId).hide(); checkbox.on('click', () => { $('#' + amplyInputId).toggle(); }); container.append(checkbox); container.append(label); $('#' + amplyInputId).before(container); } function amplyGetCompanyPolicy(companyId, companyName) { if (!companyId) companyId = companyName; let btnClass = amplyMatchOptions.btn_class ? ('?btnClass=' + amplyMatchOptions.btn_class) : ''; $.ajax({ type: "GET", url: 'https://sandbox.giveamply.com/api/organization/8832/company/' + companyId + '/policy' + btnClass, success: (data) => { document.getElementById('amplyRoot').innerHTML = data; $('#amplyRoot').show(); // Process page data and adjust as needed // Display donate button if (!amplyMatchOptions.donate_button) $('#amplyDonateBtn').hide(); // Pre-fill Donor Data if (amplyMatchOptions.url_params) { if (amplyMatchOptions.url_params.donor_name) document.getElementById('amplyDonorName').value = urlParams.get(amplyMatchOptions.url_params.donor_name); if (amplyMatchOptions.url_params.donor_fname) document.getElementById('amplyDonorName').value = urlParams.get(amplyMatchOptions.url_params.donor_fname) + ' ' + urlParams.get(amplyMatchOptions.url_params.donor_lname); if (amplyMatchOptions.url_params.donor_email) document.getElementById('amplyDonorEmail').value = urlParams.get(amplyMatchOptions.url_params.donor_email); if (amplyMatchOptions.url_params.amount) document.getElementById('amplyDonationAmount').value = urlParams.get(amplyMatchOptions.url_params.amount); } } }); } function amplyCreateAutocomplete(id = 'amplyCompanySearch', submitFunction = null) { var amplyGlobalTerm = ''; var amplyChoices = {}; var xhr; if (document.getElementById(id) === null) return; $('#' + id).autocomplete({ minLength: 3, source: function (term, response){ try { xhr.abort(); } catch (e){} amplyGlobalTerm = term.term; xhr = $.getJSON('https://sandbox.giveamply.com/search/company-name/' + term.term + '?exclude=false&other=true', [], function (data){ amplyChoices.raw = data['names']; amplyChoices.array = []; for (let i = 0; i < amplyChoices.raw.length; i++) { let choice = amplyChoices.raw[i]; if (choice.name !== "Other") { let alias = choice.name.toLowerCase() !== choice.company.name.toLowerCase(); amplyChoices.array.push({'value': choice.name,'name': choice.name,'company_id': choice.company_id, 'label': choice.name + (alias ? " (alias)" : ''), 'alias': alias, 'company': choice.company}); } else { amplyChoices.array.push({'value': term.term, 'label': choice.name, 'alias': false, 'company': term.term}); } } // Handle: unemployed? retired? student? response(amplyChoices.array); }); }, select: function( event, ui ) { amplyMatchData.company_name = ui.item.value; console.log(ui.item); if (submitFunction) submitFunction(ui.item.company_id, ui.item.value); } }) .autocomplete( "instance" )._renderItem = function( ul, item ) { var searchMask = amplyGlobalTerm; var regEx = new RegExp(searchMask, "ig"); let listElement = $( "
" ) .append( "" + item.label.replace(regEx, function(a,b){ return '' + a + '' }) + "" ) .appendTo( ul ); if(item.alias) { listElement.attr("title", "Alias For " + item.company.name); } return listElement; }; let poweredBy = $('Powered by Amply'); $('#' + id).after(poweredBy); } function amplyGetQueryVariable(variable) { var query = window.location.search.substring(1); var vars = query.split('&'); for (var i = 0; i < vars.length; i++) { var pair = vars[i].split('='); if (decodeURIComponent(pair[0]) == variable) { return decodeURIComponent(pair[1]); } } console.log('Query variable %s not found', variable); return false } amplyToggleMatch = () => { $('#amplyMatchForm').slideToggle(); } amplyPageSetup(); } , $); } );