(function ($) { $.widget("portal.membership", { lang: { submitting: "Submitting" || "Submitting", resetting: "Resetting" || "Resetting", confirming: "Confirming" || "Confirming", sessionexpired: "Session Expired" || "Session Expired", popuptitle: "Disclosure of Information & Data Protection" || "Disclaimer", success: "We have sent you an email with instructions on how to change your password. If you do not receive anything in the next several minutes, please check your junk/spam folder." || "We have sent you an email with instructions on how to change your password. If you do not receive anything in the next several minutes, please check your junk/spam folder." }, options: { parentpageref: null, loginurl: "", popdisclaimer: false, loginwithwebcam: false, webcamdetectintervalseconds: 5, webcamdetectintervalmaxattempts: 15, loginwebcamattemptcount: 1, logincamopencookie: "", logincamtimeoutid: null, loginstream: null }, _create: function () { // bind relevant events this.bind(); }, bind: function() { // reference this plugin var $that = this; // first check to see if we're not at the parent (browser level) node. if not, we need to redirect user back to the login page at the parent. if ($("body.portal-private")[0]) { $.colorbox( { href: "#popup_SessionExpired", inline: true, title: $that.lang.sessionexpired, overlayClose: false, width: 640, maxHeight: "90%", maxWidth: "90%", closeButton: false, onComplete: function () { $("#popup_SessionExpired .button_ok").on("click", function () { var btn = $(this); btn.closest(".portal-popup-content").find(".portal-popup-message").empty().html("Redirecting ..."); btn.addClass("hide"); location.href = $that.options.loginurl + "/SessionExpired"; }); } }); } if (navigator.mediaDevices !== undefined) { $that.options.parentpageref.on("click", ".button-login-cam", function () { $that.options.parentpageref.find("div.portal-login-status").removeClass("invalid show hide").html("Initalizing camera..."); $that.options.parentpageref.find("div.portal-login-submit").addClass("hide"); $that.checkwebcamlogin(this); }); } else { $that.options.parentpageref.find(".button-login-cam").remove(); } // wire the login button $that.options.parentpageref.find("form#env-login").submit(function (e) { var $status = $(this).find("div.portal-login-status"); var $submit = $(this).find("div.portal-login-submit"); $status.html("" + $that.lang.submitting + ""); SwitchElementViews($status, $submit); return $that.checkloginattempt(); }); // wire the reset password button $that.options.parentpageref.find("form#env-resetpassword").submit(function () { var $status = $(this).find("div.portal-resetpassword-status"); var $submit = $(this).find("div.portal-resetpassword-submit"); $status.html("" + $that.lang.resetting + ""); SwitchElementViews($status, $submit); return $that.resetpassword(this); }); // wire the complete invitation button $that.options.parentpageref.find("form#env-completeinvitation").submit(function () { var $status = $(this).find("div.portal-completeinvitation-status"); var $submit = $(this).find("div.portal-completeinvitation-submit"); $status.html("" + $that.lang.confirming + ""); SwitchElementViews($status, $submit); return $that.completeinvitationattempt(this); }); $that.options.parentpageref.on("change", ".country_selection", function () { $that.displaystatefield(this); }); var ddlcountry = $that.options.parentpageref.find(".country_selection"); if (ddlcountry.length > 0) $that.displaystatefield(ddlcountry); // remove the sidemenu buttons for this page $that.options.parentpageref.find("#env-sidemenu-forgotpassword").remove(); // if we need to pop the disclaimer, do it here if ($that.options.popdisclaimer) { $(document).ready(function () { $that.showdisclaimer(); }); } else $("#env-completeinvitation").removeClass("hide"); }, checkwebcamlogin: function (obj) { var $that = this; var $btn = $(obj); var ar = !is_iPad ? { min: 0, ideal: 1, max: 1.7777777778 } : {}; var constraints = { audio: false, video: { facingMode: "user", aspectRatio: ar } }; var promise = navigator.mediaDevices.getUserMedia(constraints).then(function (mediaStream) { var video = document.getElementById("login-camera"); video.srcObject = mediaStream; video.onloadedmetadata = function (e) { video.play(); var $logincampanel = $btn.closest("section").find(".portal-logincam-container"); $logincampanel.addClass('open'); $that.options.loginwithwebcam = true; $that.options.loginwebcamattemptcount = 1; $that.options.loginstream = mediaStream; $("a.portal-logincam-cancel").one("click", function () { $that.removeWebcam(); }); $that.options.parentpageref.find("div.portal-login-status").addClass("hide"); $that.checkloginattempt(); }; }).catch(function (err) { console.log(err.message); $(".portal-login-status").html("Your webcam could not be accessed for the following reason: " + err.message + ". Please sign in using your standard credentials."); $that.options.parentpageref.find("div.portal-login-status").removeClass("hide"); $that.options.parentpageref.find("div.portal-login-submit").removeClass("hide"); return false; }); }, checkloginattempt: function () { var $that = this; // initialize the max count $that.options.parentpageref.find(".webcam-attempt").html($that.options.loginwebcamattemptcount); $that.options.parentpageref.find(".webcam-max").html($that.options.webcamdetectintervalmaxattempts); // get reference to the login form var form = document.getElementById("env-login"); var $form = $that.options.parentpageref.find("form#env-login"); // determine the login url var loginurl = "/Authentication/Membership/CheckLogin"; if ($that.options.loginwithwebcam) { // save base64 bytes to login photo property var canvas = document.createElement('canvas'); var video = document.querySelector("#login-camera"); canvas.width = video.videoWidth; canvas.height = video.videoHeight; canvas.getContext('2d').drawImage(video, 0, 0); var dataURL = canvas.toDataURL("image/png"); var base64data = dataURL.split(',')[1]; $("#hdnLoginPhoto").val(base64data); // update the login url loginurl = "/Authentication/Membership/CheckWebcamLogin"; } // validate login attempt $.ajax({ type: form.method, url: loginurl, data: $form.serialize(), success: function (returndata) { if (returndata.ok) { if ($that.options.loginwithwebcam) { $that.options.parentpageref.find(".animate-pulse").removeClass("started invalid").addClass("success"); $that.options.parentpageref.find(".webcam-status").empty(); $that.options.parentpageref.find(".webcam-message").html("Facial access granted. Logging into the portal..."); $that.options.parentpageref.find(".webcam-attempt-content").addClass("hide"); $that.options.parentpageref.find(".portal-logincam-cancel").addClass("hide"); } // go to the dashboard location.href = returndata.redirect; } else { var returnmessage = returndata.message; var $status = $that.options.parentpageref.find("div.portal-login-status"); var $submit = $that.options.parentpageref.find("div.portal-login-submit"); var $wcstat = $that.options.parentpageref.find(".webcam-status"); if ($that.options.loginwithwebcam) { var rm = function (returnmessage) { switch (returnmessage) { case "Unauthorized login.": return "Face not recognized. Attempting re-scan..."; case "Multiple faces found.": return "Too many faces. Please make sure only one face is in view."; case "Face not detected.": return "No face detected in view. Please adjust your position."; default: return returndata.message + " Attempting re-scan..."; } }; $that.options.parentpageref.find(".animate-pulse").removeClass("started invalid").addClass("invalid"); $that.options.loginwebcamattemptcount++; $wcstat.html(rm); $wcstat.removeClass("animate-blink"); setInterval(function () { $wcstat.addClass("animate-blink"); }, 250); if ($that.options.loginwebcamattemptcount > $that.options.webcamdetectintervalmaxattempts) { $that.removeWebcam(); $status.html("Facial recognition attempts exceeded."); SwitchElementViews($status, null); SwitchElementViews($submit, null); } else { $that.options.parentpageref.find(".webcam-attempt").html($that.options.loginwebcamattemptcount); $that.options.logincamtimeoutid = setTimeout(function () { $that.checkloginattempt(); }, ($that.options.webcamdetectintervalseconds * 1000)); } } else { if (!returndata.message) returnmessage = "Internal portal error. Please contact site administration."; $status.html("" + returnmessage + ""); SwitchElementViews($status, null); SwitchElementViews($submit, null); } } }, error: function (data, errorThrown) { $that.options.parentpageref.find("div.portal-login-status").html("" + errorThrown + ""); var $status = $that.options.parentpageref.find("div.portal-login-status"); var $submit = $that.options.parentpageref.find("div.portal-login-submit"); SwitchElementViews($status, null); SwitchElementViews($submit, null); } }); return false; }, removeWebcam: function () { $that = this; var $logincampanel = $that.options.parentpageref.find(".portal-logincam-container"); clearTimeout($that.options.logincamtimeoutid); $logincampanel.removeClass('open'); $that.options.loginwithwebcam = false; $that.options.loginwebcamattemptcount = 1; $that.options.parentpageref.find(".webcam-attempt").html($that.options.loginwebcamattemptcount); $that.options.parentpageref.find("div.portal-login-submit").removeClass("hide"); $that.options.loginstream.getVideoTracks().forEach(function (tracks) { tracks.stop(); }); $that.options.loginstream = null; }, sendforgotpassword: function (form) { // send forgot password var $that = this; $.ajax({ type: form.method, url: "/Authentication/Membership/SendForgotPassword", data: $(form).serialize(), success: function (returndata) { if (returndata.ok) { // indicate password change instructions $that.options.parentpageref.find("div.portal-forgotpassword-container").addClass("hide"); $that.options.parentpageref.find("div.portal-forgotpassword-status").html("" + $that.lang.success + ""); } else { $that.options.parentpageref.find("div.portal-forgotpassword-status").html("" + returndata.message + ""); var $status = $that.options.parentpageref.find("div.portal-forgotpassword-status"); var $submit = $that.options.parentpageref.find("div.portal-forgotpassword-submit"); SwitchElementViews($status, null); SwitchElementViews($submit, null); } }, error: function (data, errorThrown) { $that.options.parentpageref.find("div.forgotpassword-status-message").html("" + errorThrown + ""); var $status = $that.options.parentpageref.find("div.portal-forgotpassword-status"); var $submit = $that.options.parentpageref.find("div.portal-forgotpassword-submit"); SwitchElementViews($status, null); SwitchElementViews($submit, null); } }); return false; }, resetpassword: function (form) { // reset password var $that = this; $.ajax({ type: form.method, url: "/Authentication/Membership/ChangePassword", data: $(form).serialize(), success: function (returndata) { if (returndata.ok) { // hide the form and show the success screen $that.options.parentpageref.find("div.portal-resetpassword-container").addClass("hide"); $that.options.parentpageref.find("div.portal-resetpassword-success").removeClass("hide"); } else { $that.options.parentpageref.find("div.portal-resetpassword-status").html("" + returndata.message + ""); var $status = $that.options.parentpageref.find("div.portal-resetpassword-status"); var $submit = $that.options.parentpageref.find("div.portal-resetpassword-submit"); SwitchElementViews($status, null); SwitchElementViews($submit, null); } }, error: function (data, errorThrown) { $that.options.parentpageref.find("div.portal-resetpassword-status").html("" + errorThrown + ""); var $status = $that.options.parentpageref.find("div.portal-resetpassword-status"); var $submit = $that.options.parentpageref.find("div.portal-resetpassword-submit"); SwitchElementViews($status, null); SwitchElementViews($submit, null); } }); return false; }, completeinvitationattempt: function (form) { // validate complete invitation attempt var $that = this; var $form = $(form); var completefunction = function(form, msg) { if (msg == "") { $.ajax({ type: "POST", url: "/Authentication/Membership/CompleteInvitation", data: $(form).serialize(), success: function (returndata) { if (returndata.ok) { // go to the dashboard location.href = returndata.redirect + "/invitation"; } else { $that.options.parentpageref.find("div.portal-completeinvitation-status").html("" + returndata.message + ""); var $status = $that.options.parentpageref.find("div.portal-completeinvitation-status"); var $submit = $that.options.parentpageref.find("div.portal-completeinvitation-submit"); SwitchElementViews($status, null); SwitchElementViews($submit, null); } }, error: function (data, errorThrown) { $that.options.parentpageref.find("div.portal-completeinvitation-status").html("" + errorThrown + ""); var $status = $that.options.parentpageref.find("div.portal-completeinvitation-status"); var $submit = $that.options.parentpageref.find("div.portal-completeinvitation-submit"); SwitchElementViews($status, null); SwitchElementViews($submit, null); } }); } else { $that.options.parentpageref.find("div.portal-completeinvitation-status").html("" + msg + ""); var $status = $that.options.parentpageref.find("div.portal-completeinvitation-status"); var $submit = $that.options.parentpageref.find("div.portal-completeinvitation-submit"); SwitchElementViews($status, null); SwitchElementViews($submit, null); } }; completefunction(form, ""); return false; }, showdisclaimer: function() { var $that = this; $.colorbox( { href: "#popup_PortalDisclaimer", inline: true, title: $that.lang.popuptitle, overlayClose: false, width: 640, maxHeight: "90%", maxWidth: "90%", closeButton: false, onComplete: function () { $("#popup_PortalDisclaimer .button_ok").on("click", function () { $("#env-completeinvitation").removeClass("hide"); }) } }); }, showforgotpasswordpanel: function(form) { // Get the submitting HTML code when the page first loads var submitting_code = form.find("div.portal-forgotpassword-status").html(); // wire the forgot password button var $that = this; form.submit(function () { $(this).find("div.portal-forgotpassword-status").html(submitting_code); var $status = $(this).find("div.portal-forgotpassword-status"); var $submit = $(this).find("div.portal-forgotpassword-submit"); SwitchElementViews($status, $submit); return $that.sendforgotpassword(this); }); }, displaystatefield: function (ddl) { // get the elements var $ddlcountry = $(ddl); var $ddlstate = $ddlcountry.closest(".portal-complete-invitation-extension-form").find(".state_selection"); var $txtstate = $ddlcountry.closest(".portal-complete-invitation-extension-form").find(".state_field"); // if country is united states, make sure the state dropdownlist is showing var country = $ddlcountry.children("option").filter(":selected").text() if (country == "United States") { $ddlstate.removeClass("hide"); $txtstate.addClass("hide"); } else { $ddlstate.addClass("hide"); $txtstate.removeClass("hide"); } }, destroy: function () { this.element.removeData(); } }); })(jQuery);