//requires jquery to already be present on the page.
    var nameCoachCallbacks = [];
    
    var origOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url) {
        this.addEventListener('load', function() {
            console.log('xhr finished loading', method, url);
            if(url.includes('name-coach.com')){
                if (this.responseText.includes('success')){
                    nameCoachCallbacks.forEach(function(element) {
                        if(typeof element.method === "function"){
                            element.method(element.args);
                        } else {
                            console.log('name-coach success');
                        }
                    });
                }
            }
        });

        origOpen.apply(this, arguments);
    };

function nameCoachUD(el, firstName, lastName, email, callback, accessCode="615FBB"){
    
    $.ajax({
	    method: "GET",
	    url: "https://www.name-coach.com/api/private/v2/names/?access_code="+accessCode+"&icon=2&email="+email
    }).done(function( result ) { 
	    if(result.data[0] && "embed_image" in result.data[0]){
	        //this stuff is kind of porches specific and would probably need changed
	        //to make this a bit more generic
	        $(el).append("<span class='pull-right'>" + result.data[0]["embed_image"] + "</span>");
	        
	        var attrs = { };

            $.each($(el)[0].attributes, function(idx, attr) {
                attrs[attr.nodeName] = attr.nodeValue;
            });
            
            
            $(el).replaceWith(function () {
                return $("<span />", attrs).append($(this).contents());
            });
            
	    } else {
	        if(!jQuery().nameCoachWidget){
                $('body').append('<scri'+'pt type="text/javascript" src="https://www.udayton.edu/_udayton/scripts/namecoach/widget-min.js"></scr'+'ipt>');
            }
            
            //wait for namecoach 
            waitForNamecoach(el, firstName, lastName, email);
            
            var cb = {
              'method'  : callback,
              'args': [firstName, lastName, email]
            };
            nameCoachCallbacks.push(cb);
	    }
	    
    });
    
    

}

//The access_code is orientation specific and if we want to save it to a different kiosk we
//will need to update this to send the access_code to use.
function waitForNamecoach(el, firstName, lastName, email){
    if(jQuery().nameCoachWidget){
        $(el).nameCoachWidget(
            function(a){
            a.accessCode="615FBB";
            a.auth_token="oN_tkzREMPF_rkoyS-HV";
            a.fields={
                first_name: firstName,
                last_name: lastName,
                email:{value: email,type:"readonly"},
                middle_name:{value:"",type:"hidden"}
                }
            });
    } else {
        setTimeout(function() { waitForNamecoach(el, firstName, lastName, email) }, 1000);
    }
}
    

