var epc = epc || {}; (function(funcName, baseObj) { // The public function name defaults to window.docReady // but you can pass in your own object and own function name and those will be used // if you want to put them in a different namespace funcName = funcName || "docReady"; baseObj = baseObj || window; var readyList = []; var readyFired = false; var readyEventHandlersInstalled = false; // call this when the document is ready // this function protects itself against being called more than once function ready() { if (!readyFired) { // this must be set to true before we start calling callbacks readyFired = true; for (var i = 0; i < readyList.length; i++) { // if a callback here happens to add new ready handlers, // the docReady() function will see that it already fired // and will schedule the callback to run right after // this event loop finishes so all handlers will still execute // in order and no new ones will be added to the readyList // while we are processing the list readyList[i].fn.call(window, readyList[i].ctx); } // allow any closures held by these functions to free readyList = []; } } function readyStateChange() { if ( document.readyState === "complete" ) { ready(); } } // This is the one public interface // docReady(fn, context); // the context argument is optional - if present, it will be passed // as an argument to the callback baseObj[funcName] = function(callback, context) { if (typeof callback !== "function") { throw new TypeError("callback for docReady(fn) must be a function"); } // if ready has already fired, then just schedule the callback // to fire asynchronously, but right away if (readyFired) { setTimeout(function() {callback(context);}, 1); return; } else { // add the function and context to the list readyList.push({fn: callback, ctx: context}); } // if document already ready to go, schedule the ready function to run if (document.readyState === "complete") { setTimeout(ready, 1); } else if (!readyEventHandlersInstalled) { // otherwise if we don't have event handlers installed, install them if (document.addEventListener) { // first choice is DOMContentLoaded event document.addEventListener("DOMContentLoaded", ready, false); // backup is window load event window.addEventListener("load", ready, false); } else { // must be IE document.attachEvent("onreadystatechange", readyStateChange); window.attachEvent("onload", ready); } readyEventHandlersInstalled = true; } } })("docReady", window); epc.UtilityWidgetStyle = ".epc-utilitywidget{display:flex;flex-wrap:wrap;background-color:#fff;margin:2px 0;border:1px solid #555;border-radius:4px}.epc-utilitywidget .aag-utilitylogo{padding:10px}.epc-utilitywidget img{width:150px;height:50px;object-fit:contain}.epc-utilitywidget .aag-label h4{font-size:16px;font-family:\"PT Sans\";color:#0671b8;font-weight:700;text-align:center;margin:0}.epc-utilitywidget .aag-label{max-width:150px;min-width:100px;margin:4px 0;font-size:16px;font-family:\"PT Sans\";color:#000;font-weight:700;line-height:1.2;text-align:center}.epc-utilitywidget .aag-prices{flex:1;display:flex;justify-content:center}.epc-utilitywidget .aag-prices .aag-price{display:flex;margin:4px 10px;border-style:solid;border-width:1px;border-color:#b1770b;border-radius:4px;padding:6px 8px;background-image:-moz-linear-gradient(90deg,#feae31 0,#efe457 100%);background-image:-webkit-linear-gradient(90deg,#feae31 0,#efe457 100%);background-image:-ms-linear-gradient(90deg,#feae31 0,#efe457 100%);cursor:pointer}.epc-utilitywidget .aag-prices .aag-price:hover{text-decoration:none}.epc-utilitywidget .aag-prices .aag-price .aag-amount{font-size:24px;font-family:\"PT Sans\";color:#000;font-weight:700;line-height:1.2;text-align:left}.epc-utilitywidget .aag-prices .aag-price .aag-amount span{font-size:18px;font-family:\"PT Sans\";color:#000;line-height:1.2;text-align:left;display:block;font-weight:300}.epc-utilitywidget .aag-btn{border:none;background-color:transparent;margin-left:12px;content:\" \";border-left:1px solid #fff}.epc-utilitywidget .aag-btn:after{content:\"Go\";font-family:\"PT Sans\";margin-left:10px;display:block;line-height:47px;font-weight:700;border-style:solid;border-width:1px;border-color:#419b00;border-radius:50%;min-width:47px;min-height:47px;color:#fff;text-decoration:none;background-image:-moz-linear-gradient(90deg,#a1dc74 0,#6bc827 18%,#58c10c 39%,#76cc37 72%,#ade087 100%);background-image:-webkit-linear-gradient(90deg,#a1dc74 0,#6bc827 18%,#58c10c 39%,#76cc37 72%,#ade087 100%);background-image:-ms-linear-gradient(90deg,#a1dc74 0,#6bc827 18%,#58c10c 39%,#76cc37 72%,#ade087 100%)}.epc-utilitywidget .aag-all{display:flex;align-items:center;margin:0 10px}.epc-utilitywidget .aag-all:after{content:\"View All\";color:#000;display:block;font-size:18px;font-weight:700;text-decoration:underline;margin:0 10px}@media (max-width:800px){.epc-utilitywidget{flex-direction:column;align-items:center;justify-content:center}.epc-utilitywidget .aag-prices{flex-direction:column;align-items:center;justify-content:center}}.epc-utilitywidget.epc-shortwidget{border-radius:0;align-items:stretch}.epc-shortwidget:before{display:block;content:'_';color:#0677be;width:12px;background-color:#0677be}.epc-utilitywidget.epc-shortwidget img{border-radius:0;align-items:stretch;width:90px;height:30px;object-fit:contain}.epc-utilitywidget.epc-shortwidget .aag-btn{margin-left:4px}.epc-utilitywidget.epc-shortwidget .aag-btn:after{min-width:35px;min-height:35px;line-height:35px;margin:0;margin-left:2px}.epc-utilitywidget.epc-shortwidget .aag-prices .aag-price{padding:2px;margin:2px}.epc-utilitywidget.epc-shortwidget .aag-prices .aag-price .aag-amount{font-size:18px}.epc-utilitywidget.epc-shortwidget .aag-prices .aag-price .aag-amount span.aag-unit{font-size:12px}.epc-utilitywidget.epc-shortwidget .aag-label{display:none}.epc-utilitywidget.epc-shortwidget .aag-all{display:none}"; new (function() { var g = this; this.Utility = { ID: 1437, Name: "The Illuminating Company", LogoID: 1437 }; this.ActiveUtility = 0; this.Element = document.getElementById("uwidget"); this.Element.setAttribute("class", this.Element.getAttribute("class") + " epc-utilitywidget"); this.Timer = null; this.Modal = null; this.ModalBody = null; this.TimerActive = false; this.CycleUtilities = []; this.InjectCSS = function() { var style = document.createElement("style"); style.type = "text/css"; style.innerHTML = epc.UtilityWidgetStyle; document.getElementsByTagName('head')[0].appendChild(style); } this.GetNextUtility = function() { g.ActiveUtility = 0; g.LoadUtility(); } this.LoadUtility = function() { var activeUtility = g.ActiveUtility; g.GetPricesForUtility().then(function(prices) { if (activeUtility != g.ActiveUtility) return; g.Clear(); // Get top prices for utility. var logo = g.BuildUtilityLogo(); g.Element.appendChild(logo); g.Element.appendChild(g.BuildLabel()); var pc = document.createElement("div"); pc.setAttribute("class", "aag-prices"); if (prices.length > 0) { for (var i = 0; i < 3 && i < prices.length; i++) { pc.appendChild(g.BuildPrice(prices[i])); } } g.Element.appendChild(pc); g.Element.appendChild(g.BuildAllButton()); if (g.TimerActive) { g.EnableTimer(); } }); } this.LoadImmediate = function() {g.Clear(); g.Element.appendChild(g.BuildLabel()); // Get top prices for utility. var logo = g.BuildUtilityLogo(); g.Element.appendChild(logo); g.Element.appendChild(g.BuildAllButton()); } this.EnableTimer = function() { g.Timer = setTimeout(g.GetNextUtility, 7000); } this.StopTimer = function() { if (g.Timer != null) { clearTimeout(g.Timer); g.Timer = null; g.TimerActive = false; } } this.Clear = function() { g.Empty(g.Element); } this.Empty = function(node) { while (node.hasChildNodes()) { node.removeChild(node.lastChild); } } this.GetUtilityLogo = function() { } this.GetPricesForUtility = function() { return new Promise(function(res, rej) { var utilityID = g.Utility.ID; //utilityID = 1409; // Next Month var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" ]; var nextMonth = new Date(); nextMonth = new Date(nextMonth.getFullYear(), nextMonth.getMonth() + 1, 1); var str = monthNames[nextMonth.getMonth()] + "%20" + nextMonth.getFullYear(); var url = "https://energypricechoice.com/prices?Commodity=Electric&Utility=" + utilityID + "&Market=Residential&StartDate=" + str; g.Ajax(url).then(function (r) { var data = JSON.parse(r); if (data.length == 0) { var url = "https://energypricechoice.com/prices?Commodity=Gas&Utility=" + utilityID + "&Market=Residential&StartDate=Apr%202019"; g.Ajax(url).then(function (r) { var data = JSON.parse(r); res(data); }); } else { res(data); } }); }); } this.ShowUtilityModal = function() { g.StopTimer(); var modal = g.CreateUtilityModal(); modal.setAttribute("class", "aag-modal"); // Load all utilities in main tab. g.LoadModalUtilities(epc.utilitiesAAG); } this.LoadAllModalUtilities = function() { g.LoadModalUtilities(epc.utilitiesAAG); } this.LoadModalUtilities = function(utilities) { g.Empty(g.ModalBody); for (var i in utilities) { g.ModalBody.appendChild(g.BuildUtilitySelect(utilities[i])); } } this.SelectStateTab = function(e) { var btn = e.target; var state = btn.getAttribute("data-state"); g.ModalLoading(); g.Ajax("https://energypricechoice.com/UtilitiesForState/" + state).then(function(resp) { var utilities = JSON.parse(resp); var util = []; for (var i in utilities) { util.push(epc.utilitiesAAG[g.GetUtilityIndex(utilities[i].id)]); } g.LoadModalUtilities(util); }); } this.ModalLoading = function() { g.Empty(g.ModalBody); var cont = document.createElement("div"); cont.setAttribute("class", "aag-loading"); cont.append(document.createTextNode("Loading... Please wait.")); g.ModalBody.append(cont); } this.GetUtilityIndex = function(utilityid) { for (var i in epc.utilitiesAAG) { if (epc.utilitiesAAG[i].ID == utilityid) return i; } return -1; } this.BuildUtilitySelect = function(utility) { var c = document.createElement("div"); c.setAttribute("class", "aag-utility-select"); var title = document.createElement("div"); title.setAttribute("class", "aag-utility-title"); title.innerHTML = utility.Name; c.appendChild(title); var logo = document.createElement("img"); logo.setAttribute("src", "https://energypricechoice.com/files/" + utility.LogoID); logo.setAttribute("title", utility.Name); c.appendChild(logo); var select = document.createElement("div"); select.setAttribute("class", "aag-utility-select-btn"); select.setAttribute("data-utilityid", utility.ID); select.appendChild(document.createTextNode("Select")); select.onclick = g.UtilitySelect; c.appendChild(select); return c; } this.CloseModal = function() { g.Modal.setAttribute("class", "aag-modal aag-modal-hidden"); } this.CreateUtilityModal = function() { var states = [ //"CA", "CT", "DE", "DC", //"FL", //"GA", //"IN", "IL", //"KY", //"MA", //"MD", "MI", "NH", "NJ", "NY", "OH", "PA", "RI", //"TX", //"VA", ]; if (g.Modal != null) return g.Modal; g.Modal = document.createElement("div"); g.Modal.setAttribute("class", "aag-modal"); var modalHeader = document.createElement("div"); modalHeader.setAttribute("class", "aag-modal-header"); var s = document.createElement("div"); s.setAttribute("class", "aag-modal-header-tab"); s.appendChild(document.createTextNode("All States")); s.onclick = g.LoadAllModalUtilities; modalHeader.appendChild(s); for (var i in states) { var s = document.createElement("div"); s.setAttribute("class", "aag-modal-header-tab"); s.setAttribute("data-state", states[i]); s.appendChild(document.createTextNode(states[i])); s.onclick = g.SelectStateTab; modalHeader.appendChild(s); } var close = document.createElement("div"); close.setAttribute("class", "aag-close"); close.onclick = g.CloseModal; close.appendChild(document.createTextNode("Close")); modalHeader.appendChild(close); g.Modal.appendChild(modalHeader); g.ModalBody = document.createElement("div"); g.ModalBody.setAttribute("class", "aag-modal-body"); g.Modal.appendChild(g.ModalBody); document.body.appendChild(g.Modal); return g.Modal; } this.LoadPrices = function() { } this.BuildLabel = function() { var lbl = document.createElement("div"); lbl.setAttribute("class", "aag-label"); var t = document.createElement("h4"); var tt = document.createTextNode("SIGNUP NOW"); t.appendChild(tt); lbl.appendChild(t); lbl.appendChild(document.createTextNode("RATES VALID FOR LIMITED TIME!")); return lbl; } this.BuildAllButton = function() { var btn = document.createElement("a"); btn.setAttribute("class", "aag-all"); var n = g.Utility.Name; n = n.replace(/ /g, "_"); btn.setAttribute("href", "https://energypricechoice.com/utilitysearch/" + n); return btn; } this.BuildUtilityLogo = function() { var c = document.createElement("div"); c.setAttribute("class", "aag-utilitylogo"); var logo = document.createElement("img"); logo.setAttribute("src", "https://energypricechoice.com/files/" + g.Utility.LogoID); c.appendChild(logo); c.onclick = g.ShowUtilityModal; return c; } this.BuildPrice = function(price) { var unit = "kWh"; if (price.service_class_id != 3) unit = "mcf"; // Link var c = document.createElement("a"); c.setAttribute("class", "aag-price"); c.setAttribute("href", "https://energypricechoice.com/offer/" + price.id); //http://epc.test/offer/70367086?electricUsage=&gasUsage=&electricRateClass= // Price Body var p = document.createElement("div"); p.setAttribute("class", "aag-amount"); // Price var tp = document.createTextNode("$" + price.TaxPrice); p.appendChild(tp); // Units var t = document.createElement("span"); t.setAttribute("class", "aag-unit"); var tn = document.createTextNode("per " + unit); t.appendChild(tn); p.appendChild(t); c.appendChild(p); var b = document.createElement("button"); b.setAttribute("class", "aag-btn"); c.appendChild(b); return c; } this.Ajax = function(url) { return new Promise(function(res, rej) { var xmlHttp = new XMLHttpRequest(); xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == XMLHttpRequest.DONE) { if (xmlHttp.status == 200) { res(xmlHttp.responseText); } else if (xmlHttp.status == 404) { res("[]"); } else { rej(); } } }; xmlHttp.open("GET", url, true); xmlHttp.send(); }); } docReady(function() { g.InjectCSS(); g.GetNextUtility(); }); })();