﻿var Session = { cookie_name: "CH.Session", timeout: null, register: function (a) { this.timeout = a; $.cookie(this.cookie_name, new Date().getTime(), { path: "/" }) }, authenticated: function () { return this.remaining() > 0 }, remaining: function () { var a = this.time() + this.timeout; var b = a - new Date().getTime(); return b > 0 ? b : 0 }, time: function () { var a = $.cookie(this.cookie_name); return a ? new Number(a) : new Date().getTime() }, dialog: function (c, b) { var d = { modal: true, resizable: false, close: function () { a.dialog("destroy").remove() } }; $.extend(d, b); var a = $("<div tabIndex='-1' style='outline-width: 0'></div>").html(c).dialog(d).focus(); return a }, alert: function (c, b) { var a = this.dialog(c, { title: "Alert", close: function () { if (b) { b() } a.dialog("destroy").remove() }, buttons: { Ok: function () { a.dialog("close") } } }).keypress(function (d) { if (d.keyCode == 13) { a.dialog("close") } }) }, confirm: function (c, a) { var b = this.dialog(c, { title: "Confirm", buttons: { Cancel: function () { a(false); b.dialog("close") }, Ok: function () { a(true); b.dialog("close") } } }) } }; Session.Notify = function (q) { var i = this; var e = q.timeout * 60 * 1000; var l = q.warning * 60 * 1000; var f = q.redirect; var k = q.authenticate; var g = q.mode; var b = q.userId; var d = q.base; var j = null; var r = null; var c = Session.Notify.State.hidden; var o = false; function m() { a(); p(e - l) } function a() { Session.register(e) } function p(t) { j = window.setTimeout(function () { if (o) { return } var x = Session.remaining(); if (x > l) { h(true); p(x - l); return } if (x > 0) { if (c != Session.Notify.State.warning) { n("<p>Your session is about to expire.</p><p>Click <strong>continue</strong> to renew your session.</p>", { title: "Warning", close: function () { $.get(k, { cmd: "refresh" }, function () { a() }); s() }, buttons: { Continue: function () { h() } } }, h); c = Session.Notify.State.warning } p(x); return } if (c != Session.Notify.State.expired) { if (g == Session.Notify.Mode.redirect) { var w = []; w.push("<div>"); w.push("<p>Your session has expired</p>"); w.push("<div style='background: #F7F7F7; padding: 5px; border: solid 1px #B8B8B8;'><table width='100%'>"); w.push("<tr><td><input type='radio' id='window-current' checked='true' name='window' value='current' /></td><td><label for='window-current'>login using current window</label></td></tr>"); w.push("<tr><td><input type='radio' id='window-new' name='window' value='new' /></td><td><label for='window-new'>login using new window</label></td></tr>"); w.push("</table></div>"); w.push("</div>"); var v = $(w.join("")); var u = function () { if (v.find(":checked").val() == "current") { location.href = f } else { window.open(f) } }; n(v, { title: "Session Expired", buttons: { Ignore: function () { h(); o = true }, Login: function () { u() } } }, u) } else { var w = []; w.push("<div>"); w.push("<p>Please login using the fields below to continue with your session.</p>"); w.push("<div id='session-notify-fields' style='background: #F7F7F7 no-repeat center center; padding: 10px; border: solid 1px #B8B8B8;'><table width='100%'>"); w.push("<tr><td><label for='session-notify-username'>Username:</label></td><td><input style='width: 99%' type='text' id='session-notify-username' /></td></tr>"); w.push("<tr><td><label for='session-notify-password'>Password:</label></td><td><input style='width: 99%' type='password' id='session-notify-password' /></td></tr>"); w.push("</table></div>"); w.push("<p>If this was not your session, <a href='" + f + "'>login here</a>.</p>"); w.push("</div>"); var v = $(w.join("")); var y = v.find("input:eq(0)"); var u = function () { var C = v.find("#session-notify-fields"); C.find("table").css("visibility", "hidden"); C.css("background-image", "url(" + d + "common/style/images/ajax_loader.gif)"); var z = function () { C.find("table").css("visibility", "visible"); C.css("background-image", "none") }; var D = y.val(); var B = v.find("input:eq(1)").val(); var A = { cmd: "login", username: D, password: B, lastUserId: b }; $.ajax({ url: k, type: "POST", data: A, dataType: "text", success: function (E) { z(); var G = E == "True"; if (!G) { var F = []; F.push("<div style='color: #B32424'>"); if (E == "DiffUser") { F.push("<p>You are attempting to login with a user account that does not match the previously logged in user.</p>"); F.push("<p>Please use <a href='" + f + "'>this link</a> to login with your account.</p>") } else { F.push("<div style='text-align: center'><p>Invalid Login</p></div>") } F.push("</div>"); Session.alert(F.join(""), function () { y.select() }); return } a(); r.dialog("option", "beforeclose", function () { return true }); h() }, error: function () { z(); Session.alert("A problem occurred during authentication. Please try again.", function () { y.select() }) } }) }; n(v, { title: "Session Expired", beforeclose: function () { return false }, buttons: { Login: function () { u() } } }, u); y.focus() } c = Session.Notify.State.expired } p(1000) }, t) } function n(u, t, w) { if (r) { h(true) } var v = { close: function () { s() } }; $.extend(v, t); r = Session.dialog(u, v); if (w) { r.keypress(function (x) { if (x.keyCode == 13) { w() } }) } return r } function h(t) { c = Session.Notify.State.hidden; if (r) { if (t) { r.dialog("option", "beforeclose", function () { return true }); r.dialog("option", "close", function () { s() }) } r.dialog("close"); r = null } } function s() { r.dialog("destroy").remove() } m() }; Session.Notify.State = { hidden: 0, warning: 1, expired: 2 }; Session.Notify.Mode = { redirect: 0, direct: 1 }; Session.Notify.init = function (a) { return new Session.Notify(a) };