var db = screwtopDb; var hasStorage = (function() { try { localStorage.setItem("testitem", "testvalue"); localStorage.removeItem("testitem"); return true; } catch(e) { return false; } }()); function setCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; } function getCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } function deleteCookie(name) { setCookie(name,"",-1); } var username = "unauthenticated"; var password = "bogus"; var SDBURL = "https://shardx.screwtopdb.com/"; var APP = "nopeekvoting"; function saveAuthentication(u, p) { if (hasStorage) { localStorage.setItem(APP + '-username', u); localStorage.setItem(APP + '-password', p); } else { setCookie(APP + '-username', u, 365); setCookie(APP + '-password', p, 365); } } function clearAuthentication() { if (hasStorage) { localStorage.removeItem(APP + '-username'); localStorage.removeItem(APP + '-password'); } else { deleteCookie(APP + '-username'); deleteCookie(APP + '-password'); } } function logout(cb) { db() .url(SDBURL) .deleteCacheFromLocalStorage(username) .logout() .end(function() { clearAuthentication(); username = "unauthenticated"; if (cb) cb(); }); } function passwordreminder() { var uname = prompt("Enter your username below.",""); if (uname && uname.trim() != "") { db().url(SDBURL) .appName(APP) .sendPasswordReminder(uname) .func(function(msg) { alert(msg); }) .end(); } } function usernamereminder() { var email = prompt("Enter your email address below.",""); if (email && email.trim() != "") { db().url(SDBURL) .appName(APP) .sendUsernameReminder(email) .func(function(msg) { alert(JSON.stringify(msg)); }) .end(); } } function deleteAccount() { db().url(SDBURL) .deferredWrite(false) .deleteCacheFromLocalStorage(username) .deleteUser(username) .logout() .end(function() { clearAuthorization(); $('#logindiv').show(); }); } function login(dburl, app, performLoginActions) { APP = app; SDBURL = dburl; var u = (hasStorage ? localStorage.getItem(APP + '-username') : getCookie(APP + '-username')); var p = (hasStorage ? localStorage.getItem(APP + '-password') : getCookie(APP + '-password')); if (!db.isEmpty(u) && !db.isEmpty(p)) { username = u; password = p; db() .url(SDBURL) .authentication(APP, u, p) .onError(clearAuthentication) .end(function() { $('#logindiv').hide(); performLoginActions(); }); } else { $('#logindiv').show(); $('#username').focus(); $('#loginform').submit(function() { u = $('#username').val(); p = $('#password').val(); if (db.isEmpty(u.trim()) || db.isEmpty(p.trim())) return; db() .url(SDBURL) .login(APP, u, p) .each(function(data) { username = data.username[0]; password = p; }) .func(function() { $('#logindiv').hide(); }) .authentication(APP, u, p) .end(function() { saveAuthentication(u,p); $('#logindiv').hide(); performLoginActions(); }); return false; }); $('#registerform').submit(function() { var u = $('#newusername').val().trim(); var p = $('#newpassword').val().trim(); var p2 = $('#newpassword2').val().trim(); var email = $('#newemail').val().trim(); if (db.isEmpty(u) || db.isEmpty(p) || db.isEmpty(p2) || db.isEmpty(email)) { alert('All fields must be filled in.'); return; } if (p != p2) alert('Passwords do not match.'); else { db().url(SDBURL) .appName(APP) .createUser(u, p, email) .end(function(arr) { $('#logindiv').hide(500, function() { if (arr[0] && arr[0].username) { alert("Your account has been created. You may now log in."); } else { alert(JSON.stringify(arr[0], null,' ')); } $('#newusername').val(''); $('#newpassword').val(''); $('#newpassword2').val(''); $('#newemail').val(''); $('#logindiv').show(500); }); }); } return false; }); $('#forgotpasswordbtn').click(function() { passwordreminder(); }); $('#forgotusernamebtn').click(function() { usernamereminder(); }); } }