//The following are Global Variables that gets initiated with when the window is loaded. var patientCell; var patientEmail; var ecnt; var initiated = false; var demoNo; var fid; //On load, initiate the EmailTextEngine variables window.addEventListener("load",function(){ startETEngine(); }); function openConsentForm(){ openFid(fid); } function openFid(fid, destination){ var pathArray = window.location.pathname.split( '/' ); var newURL = window.location.protocol + "//" + window.location.host +"/"+pathArray[1]+"/eform/efmformadd_data.jsp?fid="+fid+"&demographic_no="+demoNo; return window.open(newURL, destination); } //Open eForm by formName. This is a redundant method that should probably be replaced by findFID and openFID in the future function openForm(formName){ //Since we already found the FID for "Email Text Consent Form", no need to go through the rest if that's what the user wants. if(formName =="Email Text Consent Form"){ openFid(fid); return; } //1) open the eForm list window var pathArray = window.location.pathname.split( '/' ); var newURL = window.location.protocol + "//" + window.location.host +"/"+pathArray[1]+"/eform/efmformslistadd.jsp?demographic_no="+demoNo; var eFormListWindow = window.open(newURL); eFormListWindow.addEventListener("load", function(){ //2) find all elements var a_array = this.document.links; for(var i=0;i element that has the formName in its innerHTML if(a.innerHTML.indexOf(formName)>-1){ //4) parse the FID and open the eForm var fidRe = /efmformadd_data\.jsp\?fid=(\d*)&/; var actionScript = a.getAttribute("onclick"); var myArray; if((myArray = fidRe.exec(actionScript))!== null){ fid = myArray[1]; newURL = window.location.protocol + "//" + window.location.host +"/"+pathArray[1]+"/eform/efmformadd_data.jsp?fid="+fid+"&demographic_no="+demoNo; this.close(); window.open(newURL); } } } }, false); } //Finding the FID of the eForm with formName function findFID(formName){ //1) open the eForm List window in a hidden iframe so that the user does not see a window being opened and closed quickly var pathArray = window.location.pathname.split( '/' ); var newURL = window.location.protocol + "//" + window.location.host +"/"+pathArray[1]+"/eform/efmformslistadd.jsp?group_view=&demographic_no="+demoNo; var eFormListWindow = window.open(newURL, "hiddenWin"); document.getElementById("hiddenWin").onload = function(){ //2) find all elements in the hiddenWin iFrame containing the eFormListWindow var a_array = document.getElementById("hiddenWin").contentDocument.links; for(var i=0;i element that has the formName in its innerHTML console.log("Looking for '"+formName+"' in:"+a.innerHTML); if(a.innerHTML.indexOf(formName)>-1){ console.log('Found it!') //4) parse out the fid from this element's action script var fidRe = /efmformadd_data\.jsp\?fid=(\d*)&/; var actionScript = a.getAttribute("onclick"); var myArray; if((myArray = fidRe.exec(actionScript))!== null){ fid = myArray[1]; //5) this is a lazy way to go look for the ECNT measurement using the recently found fid. if(ecnt==null){ getECNTMeasurement(); } } break; } } }; } // sending an email through Mandrill, needs an API key from www.mandrill.com function sendEmail(newsubject, newbody){ // if the Engine has not started, start it if(!initiated) startETEngine(); // if there is no email consent, do NOT continue if(!emailConsented()){ alert("No email consent"); return; } // confirm that the user really wants to send an email to the patient var confirmSend = confirm("Sending: \""+newbody+"\" to "+patientEmail); if(!confirmSend){ return; } var m = new mandrill.Mandrill(get_mandrill_api_key()); var params = { "message": { "from_email":get_sender_email(), "to": [{"email": patientEmail}], "subject": newsubject, "text": newbody } }; m.messages.send(params, function(res){ if (res[0]["status"]=="sent"){ //email successfully sent messageSent("Message: "+newbody+" emailed to "+patientEmail); } }, function(err){ //email could not be sent console.log(err); alert("Email send error:"+err); }); } function sendText(body){ //same logic as sendEmail() if(!initiated) startETEngine(); if(!textConsented()){ alert("No text consent"); return; } var confirmSend = confirm("Sending: \""+body+"\" to "+patientCell); if(!confirmSend){ return; } var twilio_id = get_twilio_id(); var twilio_auth = get_twilio_auth(); var url = "https://"+twilio_id+":"+twilio_auth+"@api.twilio.com/2010-04-01/Accounts/"+twilio_id+"/Messages"; //using a form in a hidden iframe to send a POST to Twilio Server. Please suggest improvement if you have a simpler way to send to twilio. var form = document.createElement("form"); form.setAttribute("method", "POST"); form.setAttribute("action", url); form.target = "hiddenFrame"; var fromField = document.createElement("input"); fromField.type = "hidden"; fromField.name = "From"; fromField.value = get_twilio_number(); form.appendChild(fromField); var toField = document.createElement("input"); toField.type = "hidden"; toField.name = "To"; toField.value = patientCell; form.appendChild(toField); var bodyField = document.createElement("input"); bodyField.type = "hidden"; bodyField.name = "Body"; bodyField.value = body; form.appendChild(bodyField); document.body.appendChild(form); var hiddenFrame = document.createElement("iframe"); hiddenFrame.name = "hiddenFrame"; hiddenFrame.setAttribute("hidden",true); document.body.appendChild(hiddenFrame); form.submit(); messageSent("Message: \""+body+"\" texted to "+patientCell); } function messageSent(message){ var copyToEncounter = confirm(message+"\nCopy to encounter note?"); if(copyToEncounter){ var activeNote = document.getElementsByName("caseNote_note")[0]; if(activeNote!==null&&activeNote!==undefined){ activeNote.value += message; return; } var pathArray = window.location.pathname.split( '/' ); var newURL = window.location.protocol + "//" + window.location.host +"/"+pathArray[1]+"/casemgmt/forward.jsp?action=view&demographicNo="+demoNo; var encounterWindow = window.open(newURL); encounterWindow.addEventListener('load', function(){ setTimeout(function(){ var activeNote = encounterWindow.document.getElementsByName("caseNote_note")[0]; activeNote.value += message; },3000); }, false); } } // getting the email and cellphone from the patient's demographic data page https://___/___//demographic/demographiccontrol.jsp?displaymode=edit&dboperation=search_detail&demographic_no=____" // assigning the email and cellphone to global variables for the other methods to use function getPatientEmailAndText(){ var xmlhttp= new XMLHttpRequest(); var pathArray = window.location.pathname.split( '/' ); var newURL = window.location.protocol + "//" + window.location.host +"/"+pathArray[1]+"/demographic/demographiccontrol.jsp?displaymode=edit&dboperation=search_detail&demographic_no="+demoNo; xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ var str=xmlhttp.responseText; if (!str) { return; } var myRe = /Email:<\/span>\n\s*(.*)<\/span>/i; var myArray; if((myArray = myRe.exec(str))!== null){ patientEmail = myArray[1]; } //myRe = /Cell Phone:<\/span>\n\s*(.*)<\/span>/i; myRe = /Cell Phone.*\n\s*(.*)<\/span>/i; if((myArray= myRe.exec(str))!==null){ patientCell = makeTwilioFriendly(myArray[1]); } } }; xmlhttp.open("GET",newURL,false); xmlhttp.send(); } // finding the demographic number in three ways: 1) finding demographic_no in URL, 2) finding demographicNo in URL, 3) finding demo= in the whole webpage // return the value found by 1); if 1) returned nothing, go to 2)'s value; if 1) and 2) returned nothing, go to 3)'s value. function getDemoNo(){ var myRe0 = /demographic_no=(\d*)[&$]/g; var myRe1 = /demographicNo=(\d*)[&$]/g; var myRe2 = /demo=(\d*)[&$]/g; var results0 = myRe0.exec(document.URL); var results1 = myRe1.exec(document.URL); var results2 = myRe2.exec(document.body.innerHTML); var myArray = results0 || results1 || results2; demoNo = myArray[1]; return demoNo; } // Initiating the EmailTextEngine. function startETEngine(){ //If it has been iniated already, do nothing further if(initiated) return; // Insert iFrame to hide the windows var myframe = document.createElement("iframe"); myframe.name = "hiddenWin"; myframe.id = "hiddenWin"; myframe.height="0px"; myframe.width="0px"; myframe.setAttribute("hidden",true); var myframe2 = document.createElement("iframe"); myframe2.name = "hiddenWin2"; myframe2.id = "hiddenWin2"; myframe2.height="0px"; myframe2.width="0px"; myframe2.setAttribute("hidden",true); document.getElementsByTagName("body")[0].appendChild(myframe); document.getElementsByTagName("body")[0].appendChild(myframe2); //Inserting javascripts from the Image Library to the head of the page that is loading the EmailTextEngine. var head = document.getElementsByTagName("head")[0]; var pathArray = window.location.pathname.split( '/' ); var newURL = window.location.protocol + "//" + window.location.host +"/"+pathArray[1]+"/eform/displayImage.do?imagefile=mandrill_nostringify.js"; var mandrill_script = document.createElement('script'); mandrill_script.type = 'text/javascript'; mandrill_script.src = newURL; head.appendChild(mandrill_script); newURL = window.location.protocol + "//" + window.location.host +"/"+pathArray[1]+"/eform/displayImage.do?imagefile=emailtextengine_credentials.js"; var credential = document.createElement('script'); credential.type = 'text/javascript'; credential.src = newURL; head.appendChild(credential); //Initiating patient's contact information and consent information getDemoNo(); findFID("Email Text Consent Form"); getPatientEmailAndText(); initiated = true; } // if the ecnt value is "email" or "both", then the patient has consented to receive emails function emailConsented(){ if(ecnt==null||ecnt.length==0) return false; if(ecnt.indexOf("email")>-1||ecnt.indexOf("both")>-1) return true; else return false; } // if the ecnt value is "text" or "both", then the patient has consented to receive texts function textConsented(){ if(ecnt==null||ecnt.length==0) return false; if(ecnt.indexOf("text")>-1||ecnt.indexOf("both")>-1) return true; else return false; } //getting the ecnt measurement value by going to the Email Text Consent Form which used eForm magic to get the value into: function getECNTMeasurement(){ var consentWindow = openFid(fid, "hiddenWin2"); document.getElementById("hiddenWin2").onload = function(){ ecnt = consentWindow.document.getElementById("consent_measurement").value; // after getting the ECNT measurement, we can change the EmailTextEngine buttons changeButtons(); }; } function changeButtons(){ //Finding all EmailTextEngine buttons on the page var emailButtons = document.getElementsByName("EmailButton"); var textButtons = document.getElementsByName("TextButton"); var consentButtons = document.getElementsByName("ConsentButton"); //For the EmailButtons for(var i=0;i