(function ($) { var methods = { init: function (options, tcxWebinarOptions) { return this.each(function () { var dialog, form, // From http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29 emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/, name = '', email = '', meetingid = '', session = '', allFields = [], tips = '', webinarElement, timezone = jstz.determine().name(); function updateTips(t) { tips .text(t) .addClass("ui-state-highlight"); } function checkLength(o, n, min, max) { console.log("checkLength", o); if (o.val().length > max || o.val().length < min) { o.addClass("ui-state-error"); updateTips("Length of " + n + " must be between " + min + " and " + max + "."); return false; } else { return true; } } function webinarDateFormat(dt, timezone) { // input: webinar Date() and detected browser timezone // output: string var lang = navigator.language || navigator.userLanguage; return dt.toLocaleDateString(lang) + " " + dt.toTimeString().substring(0,5) + " (" + timezone + ")"; } function checkRegexp(o, regexp, n) { if (!(regexp.test(o.val()))) { o.addClass("ui-state-error"); updateTips(n); return false; } else { return true; } } function subscribeWebinar(elem) { allFields.removeClass("ui-state-error"); var valid = true; console.log("name", name); console.log("email", email); valid = valid && checkLength(name, "name", 3, 64); valid = valid && checkLength(email, "email", 6, 150); valid = valid && checkRegexp(name, new RegExp("[^0-9]"), "Please enter your name without numbers or special characters"); valid = valid && checkRegexp(email, emailRegex, "Invalid email address"); if (valid) { var postdata = { subscribe: meetingid, email: email.val(), name: name.val() }; console.log(postdata); $.post('https://' + settings.webmeetingFqdn + "/api/webinarsubscribe?wmapisession=" + settings.webmeetingSession, postdata) .done(function (data) { if (data && data.result) { $("body").remove('#tcxdialogmessage'); var dlg = $.templates("#tcxwebinardialogmessage"); var html = dlg.render(); $("body").append(html); $("#tcxdialogmessage").dialog({ modal: true, open: function () { $('.ui-dialog').css('z-index',999999); $('.ui-widget-overlay').css('z-index',999998); }, buttons: { Ok: function () { $(this).dialog("close"); dialog.dialog('close'); console.log("after dialog", elem); updateWebinarElement(webinarElement); return true; } } }); } else { updateTips(data.error); } console.log(data); }) .fail(function (data) { updateTips("Request failed"); }); } return valid; } function updateWebinarElement(elem) { var postdata = {timezone: timezone, extension: settings.webmeetingExtension}; $.post('https://' + settings.webmeetingFqdn + "/api/webinar?wmapisession=" + settings.webmeetingSession, postdata) .done(function (data) { if (data && data.result) { // format date for (var i = 0; i < data.reply.webinars.length; ++i) { data.reply.webinars[i].date = webinarDateFormat(new Date(data.reply.webinars[i].date),timezone); // use helper to format date, allow customization } var tmpl = $.templates("#tcxwebinarlist"); var html = tmpl.render(data.reply); $(elem).html(html); $(elem).find('.tcxwebinartitle a').click(function () { var elem = $(this).parent().parent(); meetingid = $(this).attr("tcxmeetingid"); var api = $($(this).closest('ul')[0]); var ext = api.attr("tcxwmextension"); session = api.attr("tcxwmapisession"); console.log(meetingid, ext, session); $(this).remove('#tcxdialog'); var dlg = $.templates("#tcxwebinardialog"); var html = dlg.render({title: $(this).text()}); $(this).append(html); form = $("#tcxform"); name = $("#tcxname"); email = $("#tcxemail"); tips = $("#tcxtips"); allFields = $([]).add(name).add(email); console.log(name, email); dialog = $("#tcxdialog").dialog({ modal: true, open: function (event, ui) { $('.ui-dialog').css('z-index',999991); $('.ui-widget-overlay').css('z-index',999990); }, height: 480, width: 800, buttons: { Cancel: function () { dialog.dialog("close"); }, "Subscribe": function () { subscribeWebinar(elem); } }, close: function () { form[0].reset(); allFields.removeClass("ui-state-error"); } }); $("#tcxdialog").keydown(function (event) { if (event.keyCode == $.ui.keyCode.ENTER) { $(this).siblings('.ui-dialog-buttonpane').find('button:eq(1)').trigger("click"); return false; } }); return false; }) } else { $(elem).text(data.error); } }) .fail(function (data) { $(elem).text("Webinar API request failed"); }); } // Setup the default options var settings = $.extend({ webmeetingFqdn: '', webmeetingExtension: '', webmeetingSession: '', onFormCancel: null, onFormError: null, inputerrorClass: 'inputtexterror' }, options); // Merge the user-defined options with the defaults if (tcxWebinarOptions) { settings = $.extend(settings, tcxWebinarOptions); } webinarElement = this; updateWebinarElement(webinarElement); }) }, clear: function () { return this.each(function () { $('.' + $(this).attr('errorFieldClass')).text(''); $(this).find('input').removeClass($(this).attr('inputerrorClass')); }) } } $.fn.tcxWebinar = function (method) { if (methods[method]) { return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); } else if (typeof method === 'object' || !method) { return methods.init.apply(this, arguments); } else { $.error('The method ' + method + ' does not exist in $.tcxWebinar'); } } } (jQuery)); /* * Polyfill for Internet Explorer * See https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent */ ( function () { if ( typeof window.CustomEvent === "function" ) return false; function CustomEvent ( event, params ) { params = params || { bubbles: false, cancelable: false, detail: undefined }; var evt = document.createEvent( 'CustomEvent' ); evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail ); return evt; } CustomEvent.prototype = window.Event.prototype; window.CustomEvent = CustomEvent; } )();