Advertisement
Guest User

greasemonkey tweaked

a guest
Oct 3rd, 2017
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        FPL get team info
  3. // @namespace   nickchild
  4. // @include     https://fantasy.premierleague.com/a/leagues/standings/*
  5. // @version     2.1.1
  6. // @grant    GM_getValue
  7. // @grant    GM_setValue
  8. // ==/UserScript==
  9.  
  10.  
  11. var stIsIE = /*@cc_on!@*/false;
  12. var mytimeout;
  13.  
  14. sorttable = {
  15.   init: function() {
  16.     // quit if this function has already been called
  17.     if (arguments.callee.done) return;
  18.     // flag this function so we don't do the same thing twice
  19.     arguments.callee.done = true;
  20.     // kill the timer
  21.     if (_timer) clearInterval(_timer);
  22.  
  23.     if (!document.createElement || !document.getElementsByTagName) return;
  24.  
  25.     sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
  26.  
  27.     forEach(document.getElementsByTagName('table'), function(table) {
  28.       if (table.className.search(/\bsortable\b/) != -1) {
  29.         sorttable.makeSortable(table);
  30.       }
  31.     });
  32.  
  33.   },
  34.  
  35.   makeSortable: function(table) {
  36.     if (table.getElementsByTagName('thead').length == 0) {
  37.       // table doesn't have a tHead. Since it should have, create one and
  38.       // put the first table row in it.
  39.       the = document.createElement('thead');
  40.       the.appendChild(table.rows[0]);
  41.       table.insertBefore(the,table.firstChild);
  42.     }
  43.     // Safari doesn't support table.tHead, sigh
  44.     if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0];
  45.  
  46.     if (table.tHead.rows.length != 1) return; // can't cope with two header rows
  47.  
  48.     // Sorttable v1 put rows with a class of "sortbottom" at the bottom (as
  49.     // "total" rows, for example). This is B&R, since what you're supposed
  50.     // to do is put them in a tfoot. So, if there are sortbottom rows,
  51.     // for backwards compatibility, move them to tfoot (creating it if needed).
  52.     sortbottomrows = [];
  53.     for (var i=0; i<table.rows.length; i++) {
  54.       if (table.rows[i].className.search(/\bsortbottom\b/) != -1) {
  55.         sortbottomrows[sortbottomrows.length] = table.rows[i];
  56.       }
  57.     }
  58.     if (sortbottomrows) {
  59.       if (table.tFoot == null) {
  60.         // table doesn't have a tfoot. Create one.
  61.         tfo = document.createElement('tfoot');
  62.         table.appendChild(tfo);
  63.       }
  64.       for (var i=0; i<sortbottomrows.length; i++) {
  65.         tfo.appendChild(sortbottomrows[i]);
  66.       }
  67.       delete sortbottomrows;
  68.     }
  69.  
  70.     // work through each column and calculate its type
  71.     headrow = table.tHead.rows[0].cells;
  72.     for (var i=0; i<headrow.length; i++) {
  73.       // manually override the type with a sorttable_type attribute
  74.       if (!headrow[i].className.match(/\bsorttable_nosort\b/)) { // skip this col
  75.         mtch = headrow[i].className.match(/\bsorttable_([a-z0-9]+)\b/);
  76.         if (mtch) { override = mtch[1]; }
  77.           if (mtch && typeof sorttable["sort_"+override] == 'function') {
  78.             headrow[i].sorttable_sortfunction = sorttable["sort_"+override];
  79.           } else {
  80.             headrow[i].sorttable_sortfunction = sorttable.guessType(table,i);
  81.           }
  82.           // make it clickable to sort
  83.           headrow[i].sorttable_columnindex = i;
  84.           headrow[i].sorttable_tbody = table.tBodies[0];
  85.           dean_addEvent(headrow[i],"click", sorttable.innerSortFunction = function(e) {
  86.  
  87.           if (this.className.search(/\bsorttable_sorted\b/) != -1) {
  88.             // if we're already sorted by this column, just
  89.             // reverse the table, which is quicker
  90.             sorttable.reverse(this.sorttable_tbody);
  91.             this.className = this.className.replace('sorttable_sorted',
  92.                                                     'sorttable_sorted_reverse');
  93.             this.removeChild(document.getElementById('sorttable_sortfwdind'));
  94.             sortrevind = document.createElement('span');
  95.             sortrevind.id = "sorttable_sortrevind";
  96.             sortrevind.innerHTML = stIsIE ? '&nbsp<font face="webdings">5</font>' : '&nbsp;&#x25B4;';
  97.             this.appendChild(sortrevind);
  98.             return;
  99.           }
  100.           if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) {
  101.             // if we're already sorted by this column in reverse, just
  102.             // re-reverse the table, which is quicker
  103.             sorttable.reverse(this.sorttable_tbody);
  104.             this.className = this.className.replace('sorttable_sorted_reverse',
  105.                                                     'sorttable_sorted');
  106.             this.removeChild(document.getElementById('sorttable_sortrevind'));
  107.             sortfwdind = document.createElement('span');
  108.             sortfwdind.id = "sorttable_sortfwdind";
  109.             sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
  110.             this.appendChild(sortfwdind);
  111.             return;
  112.           }
  113.  
  114.           // remove sorttable_sorted classes
  115.           theadrow = this.parentNode;
  116.           forEach(theadrow.childNodes, function(cell) {
  117.             if (cell.nodeType == 1) { // an element
  118.               cell.className = cell.className.replace('sorttable_sorted_reverse','');
  119.               cell.className = cell.className.replace('sorttable_sorted','');
  120.             }
  121.           });
  122.           sortfwdind = document.getElementById('sorttable_sortfwdind');
  123.           if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); }
  124.           sortrevind = document.getElementById('sorttable_sortrevind');
  125.           if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); }
  126.  
  127.           this.className += ' sorttable_sorted';
  128.           sortfwdind = document.createElement('span');
  129.           sortfwdind.id = "sorttable_sortfwdind";
  130.           sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
  131.           this.appendChild(sortfwdind);
  132.  
  133.             // build an array to sort. This is a Schwartzian transform thing,
  134.             // i.e., we "decorate" each row with the actual sort key,
  135.             // sort based on the sort keys, and then put the rows back in order
  136.             // which is a lot faster because you only do getInnerText once per row
  137.             row_array = [];
  138.             col = this.sorttable_columnindex;
  139.             rows = this.sorttable_tbody.rows;
  140.             for (var j=0; j<rows.length; j++) {
  141.               row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]];
  142.             }
  143.             /* If you want a stable sort, uncomment the following line */
  144.             //sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
  145.             /* and comment out this one */
  146.             row_array.sort(this.sorttable_sortfunction);
  147.  
  148.             tb = this.sorttable_tbody;
  149.             for (var j=0; j<row_array.length; j++) {
  150.               tb.appendChild(row_array[j][1]);
  151.             }
  152.  
  153.             delete row_array;
  154.           });
  155.         }
  156.     }
  157.   },
  158.  
  159.   guessType: function(table, column) {
  160.     // guess the type of a column based on its first non-blank row
  161.     sortfn = sorttable.sort_alpha;
  162.     for (var i=0; i<table.tBodies[0].rows.length; i++) {
  163.       text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]);
  164.       if (text != '') {
  165.         if (text.match(/^-?[£$¤]?[\d,.]+%?$/)) {
  166.           return sorttable.sort_numeric;
  167.         }
  168.         // check for a date: dd/mm/yyyy or dd/mm/yy
  169.         // can have / or . or - as separator
  170.         // can be mm/dd as well
  171.         possdate = text.match(sorttable.DATE_RE);
  172.         if (possdate) {
  173.           // looks like a date
  174.           first = parseInt(possdate[1]);
  175.           second = parseInt(possdate[2]);
  176.           if (first > 12) {
  177.             // definitely dd/mm
  178.             return sorttable.sort_ddmm;
  179.           } else if (second > 12) {
  180.             return sorttable.sort_mmdd;
  181.           } else {
  182.             // looks like a date, but we can't tell which, so assume
  183.             // that it's dd/mm (English imperialism!) and keep looking
  184.             sortfn = sorttable.sort_ddmm;
  185.           }
  186.         }
  187.       }
  188.     }
  189.     return sortfn;
  190.   },
  191.  
  192.   getInnerText: function(node) {
  193.     // gets the text we want to use for sorting for a cell.
  194.     // strips leading and trailing whitespace.
  195.     // this is *not* a generic getInnerText function; it's special to sorttable.
  196.     // for example, you can override the cell text with a customkey attribute.
  197.     // it also gets .value for <input> fields.
  198.  
  199.     if (!node) return "";
  200.  
  201.     hasInputs = (typeof node.getElementsByTagName == 'function') &&
  202.                  node.getElementsByTagName('input').length;
  203.  
  204.     if (node.getAttribute("sorttable_customkey") != null) {
  205.       return node.getAttribute("sorttable_customkey");
  206.     }
  207.     else if (typeof node.textContent != 'undefined' && !hasInputs) {
  208.       return node.textContent.replace(/^\s+|\s+$/g, '');
  209.     }
  210.     else if (typeof node.innerText != 'undefined' && !hasInputs) {
  211.       return node.innerText.replace(/^\s+|\s+$/g, '');
  212.     }
  213.     else if (typeof node.text != 'undefined' && !hasInputs) {
  214.       return node.text.replace(/^\s+|\s+$/g, '');
  215.     }
  216.     else {
  217.       switch (node.nodeType) {
  218.         case 3:
  219.           if (node.nodeName.toLowerCase() == 'input') {
  220.             return node.value.replace(/^\s+|\s+$/g, '');
  221.           }
  222.         case 4:
  223.           return node.nodeValue.replace(/^\s+|\s+$/g, '');
  224.           break;
  225.         case 1:
  226.         case 11:
  227.           var innerText = '';
  228.           for (var i = 0; i < node.childNodes.length; i++) {
  229.             innerText += sorttable.getInnerText(node.childNodes[i]);
  230.           }
  231.           return innerText.replace(/^\s+|\s+$/g, '');
  232.           break;
  233.         default:
  234.           return '';
  235.       }
  236.     }
  237.   },
  238.  
  239.   reverse: function(tbody) {
  240.     // reverse the rows in a tbody
  241.     newrows = [];
  242.     for (var i=0; i<tbody.rows.length; i++) {
  243.       newrows[newrows.length] = tbody.rows[i];
  244.     }
  245.     for (var i=newrows.length-1; i>=0; i--) {
  246.        tbody.appendChild(newrows[i]);
  247.     }
  248.     delete newrows;
  249.   },
  250.  
  251.   /* sort functions
  252.      each sort function takes two parameters, a and b
  253.      you are comparing a[0] and b[0] */
  254.   sort_numeric: function(a,b) {
  255.     aa = parseFloat(a[0].replace(/[^0-9.-]/g,''));
  256.     if (isNaN(aa)) aa = 0;
  257.     bb = parseFloat(b[0].replace(/[^0-9.-]/g,''));
  258.     if (isNaN(bb)) bb = 0;
  259.     return bb-aa;
  260.   },
  261.   sort_alpha: function(a,b) {
  262.     if (a[0]==b[0]) return 0;
  263.     if (a[0]<b[0]) return -1;
  264.     return 1;
  265.   },
  266.   sort_ddmm: function(a,b) {
  267.     mtch = a[0].match(sorttable.DATE_RE);
  268.     y = mtch[3]; m = mtch[2]; d = mtch[1];
  269.     if (m.length == 1) m = '0'+m;
  270.     if (d.length == 1) d = '0'+d;
  271.     dt1 = y+m+d;
  272.     mtch = b[0].match(sorttable.DATE_RE);
  273.     y = mtch[3]; m = mtch[2]; d = mtch[1];
  274.     if (m.length == 1) m = '0'+m;
  275.     if (d.length == 1) d = '0'+d;
  276.     dt2 = y+m+d;
  277.     if (dt1==dt2) return 0;
  278.     if (dt1<dt2) return -1;
  279.     return 1;
  280.   },
  281.   sort_mmdd: function(a,b) {
  282.     mtch = a[0].match(sorttable.DATE_RE);
  283.     y = mtch[3]; d = mtch[2]; m = mtch[1];
  284.     if (m.length == 1) m = '0'+m;
  285.     if (d.length == 1) d = '0'+d;
  286.     dt1 = y+m+d;
  287.     mtch = b[0].match(sorttable.DATE_RE);
  288.     y = mtch[3]; d = mtch[2]; m = mtch[1];
  289.     if (m.length == 1) m = '0'+m;
  290.     if (d.length == 1) d = '0'+d;
  291.     dt2 = y+m+d;
  292.     if (dt1==dt2) return 0;
  293.     if (dt1<dt2) return -1;
  294.     return 1;
  295.   },
  296.  
  297.   shaker_sort: function(list, comp_func) {
  298.     // A stable sort function to allow multi-level sorting of data
  299.     // see: http://en.wikipedia.org/wiki/Cocktail_sort
  300.     // thanks to Joseph Nahmias
  301.     var b = 0;
  302.     var t = list.length - 1;
  303.     var swap = true;
  304.  
  305.     while(swap) {
  306.         swap = false;
  307.         for(var i = b; i < t; ++i) {
  308.             if ( comp_func(list[i], list[i+1]) > 0 ) {
  309.                 var q = list[i]; list[i] = list[i+1]; list[i+1] = q;
  310.                 swap = true;
  311.             }
  312.         } // for
  313.         t--;
  314.  
  315.         if (!swap) break;
  316.  
  317.         for(var i = t; i > b; --i) {
  318.             if ( comp_func(list[i], list[i-1]) < 0 ) {
  319.                 var q = list[i]; list[i] = list[i-1]; list[i-1] = q;
  320.                 swap = true;
  321.             }
  322.         } // for
  323.         b++;
  324.  
  325.     } // while(swap)
  326.   }
  327. };
  328.  
  329. /*
  330.   SortTable
  331.   version 2
  332.   7th April 2007
  333.   Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/
  334.   Instructions:
  335.   Download this file
  336.   Add <script src="sorttable.js"></script> to your HTML
  337.   Add class="sortable" to any table you'd like to make sortable
  338.   Click on the headers to sort
  339.   Thanks to many, many people for contributions and suggestions.
  340.   Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
  341.   This basically means: do what you want with it.
  342. */
  343.  
  344. /* ******************************************************************
  345.    Supporting functions: bundled here to avoid depending on a library
  346.    ****************************************************************** */
  347.  
  348. // Dean Edwards/Matthias Miller/John Resig
  349.  
  350. /* for Mozilla/Opera9 */
  351. if (document.addEventListener) {
  352.     document.addEventListener("DOMContentLoaded", sorttable.init, false);
  353. }
  354.  
  355. /* for Internet Explorer */
  356. /*@cc_on @*/
  357. /*@if (@_win32)
  358.     document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  359.     var script = document.getElementById("__ie_onload");
  360.     script.onreadystatechange = function() {
  361.         if (this.readyState == "complete") {
  362.             sorttable.init(); // call the onload handler
  363.         }
  364.     };
  365. /*@end @*/
  366.  
  367. /* for Safari */
  368. if (/WebKit/i.test(navigator.userAgent)) { // sniff
  369.     var _timer = setInterval(function() {
  370.         if (/loaded|complete/.test(document.readyState)) {
  371.             sorttable.init(); // call the onload handler
  372.         }
  373.     }, 10);
  374. }
  375.  
  376. /* for other browsers */
  377. window.onload = sorttable.init;
  378.  
  379. // written by Dean Edwards, 2005
  380. // with input from Tino Zijdel, Matthias Miller, Diego Perini
  381.  
  382. // http://dean.edwards.name/weblog/2005/10/add-event/
  383.  
  384. function dean_addEvent(element, type, handler) {
  385.     if (element.addEventListener) {
  386.         element.addEventListener(type, handler, false);
  387.     } else {
  388.         // assign each event handler a unique ID
  389.         if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++;
  390.         // create a hash table of event types for the element
  391.         if (!element.events) element.events = {};
  392.         // create a hash table of event handlers for each element/event pair
  393.         var handlers = element.events[type];
  394.         if (!handlers) {
  395.             handlers = element.events[type] = {};
  396.             // store the existing event handler (if there is one)
  397.             if (element["on" + type]) {
  398.                 handlers[0] = element["on" + type];
  399.             }
  400.         }
  401.         // store the event handler in the hash table
  402.         handlers[handler.$$guid] = handler;
  403.         // assign a global event handler to do all the work
  404.         element["on" + type] = handleEvent;
  405.     }
  406. };
  407. // a counter used to create unique IDs
  408. dean_addEvent.guid = 1;
  409.  
  410. function removeEvent(element, type, handler) {
  411.     if (element.removeEventListener) {
  412.         element.removeEventListener(type, handler, false);
  413.     } else {
  414.         // delete the event handler from the hash table
  415.         if (element.events && element.events[type]) {
  416.             delete element.events[type][handler.$$guid];
  417.         }
  418.     }
  419. };
  420.  
  421. function handleEvent(event) {
  422.     var returnValue = true;
  423.     // grab the event object (IE uses a global event object)
  424.     event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
  425.     // get a reference to the hash table of event handlers
  426.     var handlers = this.events[event.type];
  427.     // execute each event handler
  428.     for (var i in handlers) {
  429.         this.$$handleEvent = handlers[i];
  430.         if (this.$$handleEvent(event) === false) {
  431.             returnValue = false;
  432.         }
  433.     }
  434.     return returnValue;
  435. };
  436.  
  437. function fixEvent(event) {
  438.     // add W3C standard event methods
  439.     event.preventDefault = fixEvent.preventDefault;
  440.     event.stopPropagation = fixEvent.stopPropagation;
  441.     return event;
  442. };
  443. fixEvent.preventDefault = function() {
  444.     this.returnValue = false;
  445. };
  446. fixEvent.stopPropagation = function() {
  447.   this.cancelBubble = true;
  448. };
  449.  
  450. // Dean's forEach: http://dean.edwards.name/base/forEach.js
  451. /*
  452.     forEach, version 1.0
  453.     Copyright 2006, Dean Edwards
  454.     License: http://www.opensource.org/licenses/mit-license.php
  455. */
  456.  
  457. // array-like enumeration
  458. if (!Array.forEach) { // mozilla already supports this
  459.     Array.forEach = function(array, block, context) {
  460.         for (var i = 0; i < array.length; i++) {
  461.             block.call(context, array[i], i, array);
  462.         }
  463.     };
  464. }
  465.  
  466. // generic enumeration
  467. Function.prototype.forEach = function(object, block, context) {
  468.     for (var key in object) {
  469.         if (typeof this.prototype[key] == "undefined") {
  470.             block.call(context, object[key], key, object);
  471.         }
  472.     }
  473. };
  474.  
  475. // character enumeration
  476. String.forEach = function(string, block, context) {
  477.     Array.forEach(string.split(""), function(chr, index) {
  478.         block.call(context, chr, index, string);
  479.     });
  480. };
  481.  
  482. // globally resolve forEach enumeration
  483. var forEach = function(object, block, context) {
  484.     if (object) {
  485.         var resolve = Object; // default
  486.         if (object instanceof Function) {
  487.             // functions have a "length" property
  488.             resolve = Function;
  489.         } else if (object.forEach instanceof Function) {
  490.             // the object implements a custom forEach method so use that
  491.             object.forEach(block, context);
  492.             return;
  493.         } else if (typeof object == "string") {
  494.             // the object is a string
  495.             resolve = String;
  496.         } else if (typeof object.length == "number") {
  497.             // the object is array-like
  498.             resolve = Array;
  499.         }
  500.         resolve.forEach(object, block, context);
  501.     }
  502. };
  503.  
  504. //get gameweek id
  505. links = document.getElementsByTagName("a");
  506. gwnum = 0;
  507. for(i=0; i<links.length; i++) {
  508.     if(links[i].innerHTML == "Points") {
  509.         tmpurl = links[i].href;
  510.         tmparr = tmpurl.split("/");
  511.         gwnum = tmparr[tmparr.length-1];
  512.         break;
  513.     }
  514. }
  515.  
  516. config_items = new Array("teamval", "bankval", "totalval", "tt", "gwt", "hitpts", "wc", "chips", "captain", "h2h", "livepoints", "livetotal", "wide", "played", "bonus", "autosubs");
  517. config_values = {};
  518. config_strs = {};
  519. config_strs["teamval"] = "Team value";
  520. config_strs["bankval"] = "Bank value";
  521. config_strs["totalval"] = "Total value";
  522. config_strs["tt"] = "Total transfers";
  523. config_strs["gwt"] = "Gameweek transfers";
  524. config_strs["wc"] = "Wildcard";
  525. config_strs["chips"] = "Chips";
  526. config_strs["captain"] = "Captain";
  527. config_strs["h2h"] = "H2H league position";
  528. config_strs["livepoints"] = "Live gameweek points";
  529. config_strs["livetotal"] = "Live total points";
  530. config_strs["wide"] = "Extra wide";
  531. config_strs["played"] = "Playing stats";
  532. config_strs["hitpts"] = "Transfer points hit";
  533. config_strs["bonus"] = "Bonus points";
  534. config_strs["autosubs"] = "Automatic subs points";
  535. for(i=0; i<config_items.length; i++) {
  536.     c = config_items[i];
  537.     tmpval = GM_getValue(c);
  538.     if(tmpval == undefined) {
  539.         GM_setValue(c, 1);
  540.         tmpval = 1;
  541.     }
  542.     config_values[c] = tmpval;
  543. }
  544.  
  545. optionWidth = "220px";
  546. if(config_values["wide"] == 1) {
  547.     refreshWidth = "1150px";
  548. } else {
  549.     refreshWidth = "1150px";
  550. }
  551.  
  552. function GetXmlHttpObject()
  553. {
  554.     var xmlHttp=null;
  555.     try
  556.       {
  557.       // Firefox, Opera 8.0+, Safari
  558.       xmlHttp=new XMLHttpRequest();
  559.       }
  560.     catch (e)
  561.       {
  562.       // Internet Explorer
  563.       try
  564.         {
  565.         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  566.         }
  567.       catch (e)
  568.         {
  569.         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  570.         }
  571.       }
  572.     return xmlHttp;
  573. }
  574.  
  575. function numberWithCommas(x) {
  576.     var parts = x.toString().split(".");
  577.     parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  578.     return parts.join(".");
  579. }
  580.  
  581. //var window.leagueName;
  582.  
  583. function getData() {
  584.     //alert('go');
  585.     if(!document.getElementById("ismr-classic-standings")) {
  586.         setTimeout(function(){
  587.             getData();
  588.         }, 2000);
  589.         return;
  590.     }
  591.  
  592.     // get data from fixtures
  593.     url = "https://fantasy.premierleague.com/drf/fixtures/?event="+gwnum;
  594.     var xmlHttp=GetXmlHttpObject();
  595.     xmlHttp.onreadystatechange=stateChangedFixturesEvent;
  596.     xmlHttp.open("GET",url,true);
  597.     xmlHttp.send(null);
  598.  
  599.  
  600.     standingsDiv = document.getElementById("ismr-classic-standings");
  601.     //alert(standingsDiv.innerHTML);
  602.     tables = standingsDiv.getElementsByTagName("table");
  603.     //alert(tables.length);
  604.  
  605.     leagueNameDiv = document.getElementById("ismr-main");
  606.     leagueNameH2 = leagueNameDiv.getElementsByTagName("h2");
  607.     window.leagueName = leagueNameH2[0].innerHTML;
  608.  
  609.     table = tables[0];
  610.     trs = table.getElementsByTagName("tr");
  611.     for(j=0; j<trs.length; j++) {
  612.  
  613.         ancs = trs[j].getElementsByTagName("a");
  614.         tds = trs[j].getElementsByTagName("td");
  615.         if(ancs.length > 0) {
  616.             anc = tds[1].getElementsByTagName("a");
  617.             arr1 = anc[0].href.split("team/");
  618.             teamID = arr1[1];
  619.             idpn = "row" + teamID;
  620.             trs[j].id = idpn;
  621.  
  622.         rowstr = "";
  623.         tds[2].style.position = "relative";
  624.         tds[2].innerHTML = tds[2].innerHTML + "<div id='squaddiv"+teamID+"' style='display: none; background-color: #fff; color: #000; padding-left: 20px; font-size: 8pt; position: absolute; left: 0; top: 0; width: "+refreshWidth+"; height: 48px; line-height:48px'><div>";
  625.  
  626.         tds[0].innerHTML = tds[0].innerHTML + "<a style='padding-left: 10px' title='Show Team' href='javascript:void(0)'; onclick='if(document.getElementById(\"squaddiv"+teamID+"\").style.display != \"block\"){document.getElementById(\"squaddiv"+teamID+"\").style.display = \"block\"}else{document.getElementById(\"squaddiv"+teamID+"\").style.display = \"none\"}'>T</a>";
  627.  
  628.         if(config_values["teamval"] == 1) {
  629.             rowstr += "<td id='teamval"+teamID+"' align='right' nowrap>&nbsp;</td>";
  630.         }
  631.         if(config_values["bankval"] == 1) {
  632.             rowstr += "<td id='bankval"+teamID+"' align='right' nowrap>&nbsp;</td>";
  633.         }
  634.         if(config_values["totalval"] == 1) {
  635.             rowstr += "<td id='totalval"+teamID+"' align='right' nowrap>&nbsp;</td>";
  636.         }
  637.         if(config_values["tt"] == 1) {
  638.             rowstr += "<td id='tt"+teamID+"'>&nbsp;</td>";
  639.         }
  640.         if(config_values["gwt"] == 1) {
  641.             rowstr += "<td id='gwt"+teamID+"'>&nbsp;</td>";
  642.         }
  643.         if(config_values["hitpts"] == 1) {
  644.             rowstr += "<td id='hitpts"+teamID+"'>&nbsp;</td>";
  645.         }
  646.         if(config_values["wc"] == 1) {
  647.             rowstr += "<td id='wc"+teamID+"' align='center'>&nbsp;</td>";
  648.         }
  649.         if(config_values["chips"] == 1) {
  650.             rowstr += "<td id='chips"+teamID+"' align='center'>&nbsp;</td>";
  651.         }
  652.         if(config_values["captain"] == 1) {
  653.             rowstr += "<td id='captain"+teamID+"'>&nbsp;</td>";
  654.         }
  655.         if(config_values["h2h"] == 1) {
  656.             rowstr += "<td id='h2h"+teamID+"'><span style='display: none'>-&nbsp;</span>&nbsp;</td>";
  657.         }
  658.         if(config_values["livepoints"] == 1) {
  659.             rowstr += "<td id='livepoints"+teamID+"'>&nbsp;</td>";
  660.         }
  661.         if(config_values["livetotal"] == 1) {
  662.             rowstr += "<td id='livetotal"+teamID+"' nowrap align='right'>&nbsp;</td>";
  663.         }
  664.         if(config_values["played"] == 1) {
  665.             rowstr += "<td id='played_p"+teamID+"' title='&nbsp;' nowrap>&nbsp;</td>";
  666.             rowstr += "<td id='played_tp"+teamID+"' title='&nbsp;' nowrap>&nbsp;</td>";
  667.             rowstr += "<td id='played_dnp"+teamID+"' title='&nbsp;' nowrap>&nbsp;</td>";
  668.         }
  669.         if(config_values["bonus"] == 1) {
  670.             rowstr += "<td id='bonus"+teamID+"' title='&nbsp;' nowrap>&nbsp;</td>";
  671.         }
  672.         if(config_values["autosubs"] == 1) {
  673.             rowstr += "<td id='autosubs"+teamID+"' title='&nbsp;' nowrap>&nbsp;</td>";
  674.         }
  675.         document.getElementById("row"+teamID).innerHTML = document.getElementById("row"+teamID).innerHTML + rowstr;
  676.  
  677.             url = "https://fantasy.premierleague.com/drf/entry/"+teamID;
  678.             var xmlHttp=GetXmlHttpObject();
  679.             xmlHttp.onreadystatechange=stateChangedEntry;
  680.             xmlHttp.open("GET",url,true);
  681.             xmlHttp.send(null);
  682.  
  683.             url = "https://fantasy.premierleague.com/drf/entry/"+teamID+"/event/"+gwnum+"/picks";
  684.             var xmlHttp=GetXmlHttpObject();
  685.             xmlHttp.onreadystatechange=stateChangedEvent;
  686.             xmlHttp.open("GET",url,true);
  687.             xmlHttp.send(null);
  688.  
  689.             url = "https://fantasy.premierleague.com/drf/entry/"+teamID+"/history";
  690.             var xmlHttp=GetXmlHttpObject();
  691.             xmlHttp.onreadystatechange=stateChangedHistory;
  692.             xmlHttp.open("GET",url,true);
  693.             xmlHttp.send(null);
  694.  
  695.             url = "https://fantasy.premierleague.com/drf/entry/"+teamID+"/transfers";
  696.             var xmlHttp=GetXmlHttpObject();
  697.             xmlHttp.onreadystatechange=stateChangedTransfers;
  698.             xmlHttp.open("GET",url,true);
  699.             xmlHttp.send(null);
  700.  
  701.         } else if(j==0) {
  702.             headstr = "";
  703.             if(config_values["teamval"] == 1) {
  704.                 headstr += "<th title='Team value'>Value</th>";
  705.             }
  706.             if(config_values["bankval"] == 1) {
  707.                 headstr += "<th title='Bank value'>Bank</th>";
  708.             }
  709.             if(config_values["totalval"] == 1) {
  710.                 headstr += "<th title='Total value'>Total</th>";
  711.             }
  712.             if(config_values["tt"] == 1) {
  713.                 headstr += "<th><abbr title='Total transfers'>TT</abbr></th>";
  714.             }
  715.             if(config_values["gwt"] == 1) {
  716.                 headstr += "<th><abbr title='Gameweek transfers'>GWT</abbr></th>";
  717.             }
  718.             if(config_values["hitpts"] == 1) {
  719.                 headstr += "<th><abbr title='Transfer points hit'>TPH</abbr></th>";
  720.             }
  721.             if(config_values["wc"] == 1) {
  722.                 headstr += "<th><abbr title='Wildcard available'>WC</abbr></th>";
  723.             }
  724.             if(config_values["chips"] == 1) {
  725.                 headstr += "<th><abbr title='Chips played'>Chips</abbr></th>";
  726.             }
  727.             if(config_values["captain"] == 1) {
  728.                 headstr += "<th>Captain</th>";
  729.             }
  730.             if(config_values["h2h"] == 1) {
  731.                 headstr += "<th><abbr title='Associated H2H league position'>H2H</abbr></th>";
  732.             }
  733.             if(config_values["livepoints"] == 1) {
  734.                 headstr += "<th><abbr title='Live gameweek points'>Live</abbr></th>";
  735.             }
  736.             if(config_values["livetotal"] == 1) {
  737.                 headstr += "<th><abbr title='Live points total'>Total</abbr></th>";
  738.             }
  739.             if(config_values["played"] == 1) {
  740.                 headstr += "<th><abbr title='Players played'>P</abbr></th>";
  741.                 headstr += "<th><abbr title='Players to play'>TP</abbr></th>";
  742.                 headstr += "<th><abbr title='Players who did not play'>DNP</abbr></th>";
  743.             }
  744.             if(config_values["bonus"] == 1) {
  745.                 headstr += "<th><abbr title='Live Bonus points (today&apos;s matches only)'>BAPS</abbr></th>";
  746.             }
  747.             if(config_values["autosubs"] == 1) {
  748.                 headstr += "<th><abbr title='Automatic subs points'>Subs</abbr></th>";
  749.             }
  750.             ths = trs[j].getElementsByTagName("th");
  751.             ths[0].className = "sorttable_nosort";
  752.             ths[0].innerHTML = '<abbr onclick="divs=document.getElementsByTagName(\'div\');onoff=\'\';for(i=0;i<divs.length;i++){tmpid=divs[i].id;if(tmpid.lastIndexOf(\'squaddiv\', 0) === 0){if(onoff==\'\'){if(divs[i].style.display == \'none\'){onoff=\'on\'}else{onoff=\'off\'}}if(onoff==\'on\'){divs[i].style.display=\'\'}else{divs[i].style.display=\'none\'}}}" title="Toggle teams">T</abbr>';
  753.             ths[1].className = "sorttable_nosort";
  754.             trs[j].innerHTML = trs[j].innerHTML + headstr;
  755.         }
  756.     }
  757.         table.innerHTML = table.innerHTML + "<tr><td colspan=21><div id='configrow'></div></td></tr>";
  758.         configrow = document.getElementById("configrow");
  759.  
  760.         for(i=0; i<config_items.length; i++) {
  761.             c = config_items[i];
  762.             //add captain config
  763.             var div = document.createElement('div');
  764.             div.style.display = "inline-block";
  765.             div.style.float = "left";
  766.             div.style.width = optionWidth;
  767.             var span = document.createElement('span');
  768.             span.appendChild(document.createTextNode(config_strs[c]+': '));
  769.             div.appendChild(span);
  770.  
  771.             var a = document.createElement('a');
  772.             if(config_values[c] == 0) {
  773.                 a.style.color = "#bbb";
  774.             }
  775.             a.id = c+'_on';
  776.             a.appendChild(document.createTextNode('On'));
  777.             a.href = 'javascript:null(0)';
  778.             eval("a.addEventListener('click', function(){document.getElementById(\""+c+"_on\").style.color='#000'; document.getElementById(\""+c+"_off\").style.color='#bbb'; GM_setValue(\""+c+"\",1);}, false);");
  779.             div.appendChild(a);
  780.  
  781.             var span = document.createElement('span');
  782.             span.appendChild(document.createTextNode(' | '));
  783.             div.appendChild(span);
  784.  
  785.             var a = document.createElement('a');
  786.             if(config_values[c] == 1) {
  787.                 a.style.color = "#bbb";
  788.             }
  789.             a.id = c+'_off';
  790.             a.appendChild(document.createTextNode('Off'));
  791.             a.href = 'javascript:null(0)';
  792.             eval("a.addEventListener('click', function(){document.getElementById(\""+c+"_off\").style.color='#000'; document.getElementById(\""+c+"_on\").style.color='#bbb'; GM_setValue(\""+c+"\",0);}, false);");
  793.             div.appendChild(a);
  794.             configrow.appendChild(div);
  795.             //end captain config
  796.         }
  797.  
  798.         var div = document.createElement('div');
  799.         div.style.display = "inline-block";
  800.         div.style.float = "left";
  801.         div.style.width = optionWidth;
  802.         var span = document.createElement('span');
  803.         span.appendChild(document.createTextNode('All: '));
  804.         div.appendChild(span);
  805.  
  806.         var a = document.createElement('a');
  807.         a.id = 'all_on';
  808.         a.appendChild(document.createTextNode('On'));
  809.         a.href = 'javascript:null(0)';
  810.         a.addEventListener('click', function(){
  811.             for(i=0; i<config_items.length; i++) {
  812.                 c = config_items[i];
  813.                 document.getElementById(c+"_on").style.color='#000';
  814.                 document.getElementById(c+"_off").style.color='#bbb';
  815.                 GM_setValue(c, 1);
  816.             }
  817.         }, false);
  818.         div.appendChild(a);
  819.  
  820.         var span = document.createElement('span');
  821.         span.appendChild(document.createTextNode(' | '));
  822.         div.appendChild(span);
  823.  
  824.         var a = document.createElement('a');
  825.         a.id = 'all_off';
  826.         a.appendChild(document.createTextNode('Off'));
  827.         a.href = 'javascript:null(0)';
  828.         a.addEventListener('click', function(){
  829.             for(i=0; i<config_items.length; i++) {
  830.                 c = config_items[i];
  831.                 document.getElementById(c+"_off").style.color='#000';
  832.                 document.getElementById(c+"_on").style.color='#bbb';
  833.                 GM_setValue(c, 0);
  834.             }
  835.         }, false);
  836.         div.appendChild(a);
  837.         configrow.appendChild(div);
  838.  
  839.         var div = document.createElement('div');
  840.         div.style.display = "inline-block";
  841.         div.style.float = "left";
  842.         div.style.width = optionWidth;
  843.         var span = document.createElement('span');
  844.         span.appendChild(document.createTextNode('Quick picks: '));
  845.         div.appendChild(span);
  846.  
  847.         var a = document.createElement('a');
  848.         a.id = 'all_on';
  849.         a.appendChild(document.createTextNode('Team Info'));
  850.         a.href = 'javascript:null(0)';
  851.         a.addEventListener('click', function(){
  852.             for(i=0; i<config_items.length; i++) {
  853.                 //live stats
  854.                 if(i<8) {
  855.                     c = config_items[i];
  856.                     document.getElementById(c+"_on").style.color='#000';
  857.                     document.getElementById(c+"_off").style.color='#bbb';
  858.                     GM_setValue(c, 1);
  859.                 } else {
  860.                     c = config_items[i];
  861.                     document.getElementById(c+"_off").style.color='#000';
  862.                     document.getElementById(c+"_on").style.color='#bbb';
  863.                     GM_setValue(c, 0);
  864.                 }
  865.             }
  866.         }, false);
  867.         div.appendChild(a);
  868.  
  869.         var span = document.createElement('span');
  870.         span.appendChild(document.createTextNode(' | '));
  871.         div.appendChild(span);
  872.  
  873.         var a = document.createElement('a');
  874.         a.id = 'all_off';
  875.         a.appendChild(document.createTextNode('Live Data'));
  876.         a.href = 'javascript:null(0)';
  877.         a.addEventListener('click', function(){
  878.             for(i=0; i<config_items.length; i++) {
  879.                 //live stats
  880.                 if(i==7 || i==8 || i==10 || i==11 || i==13) {
  881.                     c = config_items[i];
  882.                     document.getElementById(c+"_on").style.color='#000';
  883.                     document.getElementById(c+"_off").style.color='#bbb';
  884.                     GM_setValue(c, 1);
  885.                 } else {
  886.                     c = config_items[i];
  887.                     document.getElementById(c+"_off").style.color='#000';
  888.                     document.getElementById(c+"_on").style.color='#bbb';
  889.                     GM_setValue(c, 0);
  890.                 }
  891.             }
  892.         }, false);
  893.         div.appendChild(a);
  894.         configrow.appendChild(div);
  895.  
  896.         var div = document.createElement('div');
  897.         div.style.display = "inline-block";
  898.         div.style.width = refreshWidth;
  899.         div.style.float = "left";
  900.         div.style.paddingTop = "10px";
  901.         var span = document.createElement('span');
  902.         var a = document.createElement('a');
  903.         a.appendChild(document.createTextNode('Reload'));
  904.         a.href = 'javascript:location.reload()';
  905.         a.style.border = "1px solid #000";
  906.         a.style.padding = "3px";
  907.         a.style.borderRadius = "6px";
  908.         span.appendChild(a);
  909.         div.appendChild(span);
  910.         configrow.appendChild(div);
  911.  
  912.         var div = document.createElement('div');
  913.         div.style.display = "inline-block";
  914.         div.style.width = refreshWidth;
  915.         div.style.float = "left";
  916.         div.style.paddingTop = "10px";
  917.         var img = document.createElement('img');
  918.         var img2 = document.createElement('img');
  919.         var span = document.createElement('div');
  920.         span.innerHTML = '<br /><b>You can now download my FPL Live app on iOS and see the live data for all your leagues!<br /><br />See live points and positions, full team view, captain and vice-captain, chips and players playing stats for every team...</b><br /><br />';
  921.         span.style.width = refreshWidth;
  922.         img.style.width = "150px";
  923.         img2.style.width = "75px";
  924.         img.src = "http://www.enicma.co.uk/fpl/images/app_store.png";
  925.         img2.src = "http://www.enicma.co.uk/fpl/images/lion_launch.png";
  926.         var a = document.createElement('a');
  927.         var a2 = document.createElement('a');
  928.         a.href = 'https://itunes.apple.com/gb/app/fpl-live/id1039666919';
  929.         a2.href = 'http://www.enicma.co.uk/fpl/';
  930.         a.target = "_blank";
  931.         a2.target = "_blank";
  932.         a.style.padding = "3px";
  933.         a.style.borderRadius = "6px";
  934.         a.appendChild(img);
  935.         a2.style.padding = "3px";
  936.         a2.style.borderRadius = "6px";
  937.         a2.appendChild(img2);
  938.         div.appendChild(span);
  939.         div.appendChild(a);
  940.         div.appendChild(a2);
  941.  
  942.         configrow.appendChild(div);
  943.  
  944. }
  945.  
  946. setTimeout(function(){
  947.     getData();
  948. }, 2000);
  949.  
  950. var playersObj;
  951.  
  952. function stateChangedPlayers() {
  953.     if (this.readyState==4) {
  954.         retval = this.responseText;
  955.         playersObj = JSON.parse(retval);
  956.     }
  957. }
  958.  
  959. url = "https://fantasy.premierleague.com/drf/bootstrap-static";
  960. var xmlHttp=GetXmlHttpObject();
  961. xmlHttp.onreadystatechange=stateChangedPlayers;
  962. xmlHttp.open("GET",url,true);
  963. xmlHttp.send(null);
  964.  
  965. var liveObj;
  966.  
  967. function stateChangedLive() {
  968.     if (this.readyState==4) {
  969.         retval = this.responseText;
  970.         liveObj = JSON.parse(retval);
  971.     }
  972. }
  973.  
  974. url = "https://fantasy.premierleague.com/drf/event/"+gwnum+"/live";
  975. var xmlHttp=GetXmlHttpObject();
  976. xmlHttp.onreadystatechange=stateChangedLive;
  977. xmlHttp.open("GET",url,true);
  978. xmlHttp.send(null);
  979.  
  980. function stateChangedHistory() {
  981.     if (this.readyState==4) {
  982.         retval = this.responseText;
  983.         teamObj = JSON.parse(retval);
  984.         team_id = teamObj.entry.id;
  985.         lastGW = teamObj.entry.current_event;
  986.  
  987.         numChips = teamObj.chips.length;
  988.         chipsdetail = "";
  989.         chipname = "";
  990.         livechip = 0;
  991.  
  992.         wildcardplayed = false;
  993.  
  994.         for(i=0; i<teamObj.chips.length; i++) {
  995.             chip = teamObj.chips[i];
  996.             if(chip.name == "3xc") {
  997.                 chipname = "Triple Captain";
  998.             } else if(chip.name == "attack") {
  999.                 chipname = "All Out Attack";
  1000.             } else if(chip.name == "bboost") {
  1001.                 chipname = "Bench Boost";
  1002.             } else if(chip.name == "freehit") {
  1003.                 chipname = "Free Hit";
  1004.             } else if(chip.name == "wildcard") {
  1005.                 chipname = "Wildcard";
  1006.                 wildcardplayed = true;
  1007.             } else {
  1008.                 chipname = chip.name;
  1009.             }
  1010.             chipsdetail += chipname + " (GW" + chip.event + ")\n";
  1011.             if(chip.event == lastGW) {
  1012.                 livechip = 1;
  1013.             }
  1014.         }
  1015.  
  1016.         document.getElementById("chips"+team_id).innerHTML = numChips;
  1017.         document.getElementById("chips"+team_id).title = chipsdetail;
  1018.         if(livechip) {
  1019.             document.getElementById("chips"+team_id).style.backgroundColor = "pink";
  1020.         }
  1021.         if(!wildcardplayed) {
  1022.             wcimg = "http://upload.wikimedia.org/wikipedia/commons/thumb/0/03/Green_check.svg/13px-Green_check.svg.png";
  1023.             wctxt = "Available";
  1024.         } else {
  1025.             wcimg = "http://upload.wikimedia.org/wikipedia/en/thumb/b/ba/Red_x.svg/13px-Red_x.svg.png";
  1026.             wctxt = "Played";
  1027.         }
  1028.         cell = document.getElementById("wc"+team_id);
  1029.         cell.innerHTML = "<span id='wcs"+teamID+"' style='display: none;'>" + wctxt + "</span><img id='wci"+teamID+"' title='" + wctxt + "' src='" + wcimg + "'>";
  1030.  
  1031.     }
  1032.     standingsDiv = document.getElementById("ismr-classic-standings");
  1033.     //alert(standingsDiv.innerHTML);
  1034.     tables = standingsDiv.getElementsByTagName("table");
  1035.     //alert(tables.length);
  1036.  
  1037.     table = tables[0];
  1038.  
  1039.     clearTimeout(mytimeout);
  1040.     mytimeout = setTimeout(function() {sorttable.makeSortable(table)}, 1000);
  1041. }
  1042.  
  1043. var transfersObj;
  1044.  
  1045. function stateChangedTransfers() {
  1046.     if (this.readyState==4) {
  1047.         retval = this.responseText;
  1048.         transfersObj = JSON.parse(retval);
  1049.         team_id = transfersObj.entry.id;
  1050.         lastGW = transfersObj.entry.current_event;
  1051.         transfersHistory = transfersObj.history;
  1052.         numOfTransfers = transfersHistory.length;
  1053.  
  1054.         playersIN = "GW" + lastGW + " transfers \nIN: ";
  1055.         playersOUT= "OUT: ";
  1056.  
  1057.         for(i=0; i<numOfTransfers; i++) {
  1058.             transfer = transfersObj.history[i];
  1059.             if(transfer.event == lastGW){
  1060.                     if(playersIN != "GW" + lastGW + " transfers \nIN: ") {
  1061.                     playersIN += ", ";
  1062.                 }
  1063.  
  1064.                 if(playersOUT != "OUT: ") {
  1065.                     playersOUT += ", ";
  1066.                 }
  1067.                 playersIN += playersObj.elements[transfer.element_in-1].web_name ;
  1068.                 playersOUT += playersObj.elements[transfer.element_out-1].web_name;
  1069.             }
  1070.         }
  1071.         document.getElementById("gwt"+team_id).title = playersIN + "\n" + playersOUT;
  1072.  
  1073.     }
  1074.  
  1075.     standingsDiv = document.getElementById("ismr-classic-standings");
  1076.     //alert(standingsDiv.innerHTML);
  1077.     tables = standingsDiv.getElementsByTagName("table");
  1078.     //alert(tables.length);
  1079.  
  1080.     table = tables[0];
  1081.  
  1082.     clearTimeout(mytimeout);
  1083.     mytimeout = setTimeout(function() {sorttable.makeSortable(table)}, 1000);
  1084. }
  1085. //bonus var
  1086. var bonusArr = [];
  1087.  
  1088.  
  1089. function stateChangedFixturesEvent() {
  1090.     if (this.readyState==4) {
  1091.         retval = this.responseText;
  1092.         fixturesArr = JSON.parse(retval);
  1093.         event_day = 0;
  1094.  
  1095.         indy = 0;
  1096.  
  1097.         for(k=0;k<fixturesArr.length;k++) {
  1098.             if (fixturesArr[k].started && !fixturesArr[k].finished) {
  1099.                 bpsArr = [];
  1100.                 indx = 0;
  1101.                 stats = fixturesArr[k].stats;
  1102.                 event_day = fixturesArr[k].event_day;
  1103.                 for(i=0; i<stats.length; i++) {
  1104.                     if (Object.keys(stats[i]) == "bps") {
  1105.                         // away team
  1106.                         bps_a = stats[i].bps.a;
  1107.                         for(j=0; j<bps_a.length; j++){
  1108.                             if (bps_a[j].value > 10) {
  1109.                                 bpsArr[indx] = {playerID: bps_a[j].element, bpsPTS: bps_a[j].value};
  1110.                                 indx++;
  1111.                             }
  1112.                         }
  1113.                         // home team
  1114.                         bps_h = stats[i].bps.h;
  1115.                         for(j=0; j<bps_h.length; j++){
  1116.                             if (bps_h[j].value > 10) {
  1117.                                 bpsArr[indx] = {playerID: bps_h[j].element, bpsPTS: bps_h[j].value};
  1118.                                 indx++;
  1119.                             }
  1120.                         }
  1121.                     }
  1122.                 }
  1123.                 // sorting
  1124.                 bpsArr.sort(function(a, b){return b.bpsPTS - a.bpsPTS});
  1125.                 // add bonuses
  1126.                 minBP = 3;
  1127.                 bonus_pt = 3;
  1128.                 empty_pt = 0;
  1129.                 for(i=0; i<minBP; i++) {
  1130.                     if (bpsArr[i].bpsPTS == bpsArr[i+1].bpsPTS) {
  1131.                         bonusArr[indy] = {playerID: bpsArr[i].playerID, bonusPTS: bonus_pt+empty_pt-i};
  1132.                         empty_pt++;
  1133.                         minBP++;
  1134.                     } else {
  1135.                         bonusArr[indy] = {playerID: bpsArr[i].playerID, bonusPTS: bonus_pt+empty_pt-i};
  1136.                         empty_pt = 0;
  1137.                     }
  1138.                     indy++;
  1139.                 }
  1140.             }
  1141.         }
  1142.     }
  1143. }
  1144.  
  1145. function stateChangedEntry() {
  1146.     if (this.readyState==4) {
  1147.         retval = this.responseText;
  1148.         teamObj = JSON.parse(retval);
  1149.  
  1150.         entry = teamObj.entry;
  1151.         value = (entry.value*100)/1000;
  1152.         bank = (entry.bank*100)/1000;
  1153.         total_value = value + bank;
  1154.         value = value.toFixed(1);
  1155.         bank = bank.toFixed(1);
  1156.         total_value = total_value.toFixed(1);
  1157.  
  1158.         total_transfers = entry.total_transfers;
  1159.         gw_transfers = entry.event_transfers;
  1160.         transfers_hit = entry.event_transfers_cost;
  1161.  
  1162.  
  1163.         lastGW = entry.current_event;
  1164.  
  1165.         current_points = entry.summary_overall_points;
  1166.         week_points = entry.summary_event_points;
  1167.  
  1168.  
  1169.     }
  1170. }
  1171.  
  1172. function stateChangedEvent() {
  1173.     if (this.readyState==4) {
  1174.         retval = this.responseText;
  1175.         teamObj = JSON.parse(retval);
  1176.  
  1177.         entry_history = teamObj.entry_history;
  1178.  
  1179.         team_id = entry_history.entry;
  1180.         transfers_cost = entry_history.event_transfers_cost;
  1181.         picks = teamObj.picks;
  1182.         event = teamObj.event;
  1183.  
  1184.         live_score = 0;
  1185.         bonus_pts = 0;
  1186.         bench_pts = 0;
  1187.         bench_bonus_pts = 0;
  1188.         autosubs_pts = 0;
  1189.         autosubs_bonus_pts = 0;
  1190.         autosubs_str = "";
  1191.         bonus_str = "";
  1192.  
  1193.  
  1194.         playedstr = "";
  1195.         playednum = 0;
  1196.         toplaystr = "";
  1197.         toplaynum = 0;
  1198.         didntplaystr = "";
  1199.         didntplaynum = 0;
  1200.  
  1201.         firstnum = 0;
  1202.         subsnum = 0;
  1203.  
  1204.         captain_dnp = false;
  1205.  
  1206.         firstxistr = "";
  1207.         subsstr = "";
  1208.  
  1209.         numfirstxicount = 0;
  1210.         numfirstxidefcount = 0;
  1211.         numnonplayingdefcount = 0;
  1212.         numplayingdefcount = 0;
  1213.         numsubscount = 0;
  1214.         dnpArr = [];
  1215.         subsArr = [];
  1216.  
  1217.  
  1218.  
  1219.         player_minutes = 0;
  1220.         player_fixture = "";
  1221.         player_type = 0;
  1222.  
  1223.         for(i=0; i<picks.length; i++) {
  1224.             is_captain = false;
  1225.             is_vice = false;
  1226.             pick_points = playersObj.elements[picks[i].element-1].event_points;
  1227.             player_type = playersObj.elements[picks[i].element-1].element_type;
  1228.  
  1229.             if(picks[i].is_captain) {
  1230.                 captain_id = picks[i].element;
  1231.                 captain = playersObj.elements[captain_id-1].web_name;
  1232.                 is_captain = true;
  1233.                 captain_points = pick_points;
  1234.             }
  1235.             if(picks[i].is_vice_captain) {
  1236.                 vice_captain_id = picks[i].element;
  1237.                 vice_captain = playersObj.elements[vice_captain_id-1].web_name;
  1238.                 is_vice = true;
  1239.                 vice_points = pick_points;
  1240.             }
  1241.             if(i<11) {
  1242.                 numfirstxicount++;
  1243.  
  1244.                 if (player_type == 2) {
  1245.                    numfirstxidefcount++;
  1246.                 }
  1247.  
  1248.                 firstxistr += "<player title=\"" + pick_points + "\">";
  1249.                 firstxistr += playersObj.elements[picks[i].element-1].web_name;
  1250.  
  1251.                 if(is_captain) {
  1252.                     firstxistr += "(C)";
  1253.                 } else if(is_vice) {
  1254.                     firstxistr += "(vc)";
  1255.                 }
  1256.  
  1257.                 if(numfirstxicount !== 11) {
  1258.                     firstxistr += ", ";
  1259.                 }
  1260.                 firstxistr += "</player>" ;
  1261.  
  1262.  
  1263.  
  1264.                 pts = pick_points * picks[i].multiplier;
  1265.                 live_score = live_score + pts;
  1266.                 //bonus
  1267.                 for (j=0; j<bonusArr.length; j++) {
  1268.                     //bonus for finished matche
  1269.                     if(bonusArr[j].playerID == picks[i].element) {
  1270.                         bonus_pts = bonus_pts + bonusArr[j].bonusPTS * picks[i].multiplier;
  1271.                         bonus_str += playersObj.elements[picks[i].element-1].web_name + "(" + bonusArr[j].bonusPTS * picks[i].multiplier + ")\n";
  1272.                     }
  1273.                 }
  1274.              player_minutes = liveObj.elements[picks[i].element].stats.minutes;
  1275.              player_fixture = liveObj.elements[picks[i].element].explain[0][1];
  1276.              fixtures = liveObj.fixtures;
  1277.              has_played = 0;
  1278.  
  1279.  
  1280.                 for(fix=0; fix<fixtures.length; fix++) {
  1281.                     if(fixtures[fix].id == player_fixture){
  1282.                        if(fixtures[fix].started) {
  1283.                           has_played = 1;
  1284.                        }
  1285.                     }
  1286.                 }
  1287.  
  1288.  
  1289.                 if(has_played == 1) {
  1290.                     if(player_minutes > 0) {
  1291.                         //has played
  1292.                         if(playedstr != "") {
  1293.                             playedstr += ", ";
  1294.                         }
  1295.                         playednum++;
  1296.                         playedstr += playersObj.elements[picks[i].element-1].web_name;
  1297.                     } else {
  1298.                         //didnt play
  1299.                         if(didntplaystr != "") {
  1300.                             didntplaystr += ", ";
  1301.                         }
  1302.  
  1303.                         dnpArr[didntplaynum] = {playerID: picks[i].element-1, playerType: player_type, autosubbedOut: false};
  1304.        
  1305.  
  1306.                         didntplaynum++;
  1307.                         didntplaystr += playersObj.elements[picks[i].element-1].web_name;
  1308.  
  1309.  
  1310.  
  1311.                         if(is_captain) {
  1312.                             captain_dnp = true;
  1313.                         }
  1314.                     }
  1315.                 } else {
  1316.                     //to play
  1317.                     if(toplaystr != "") {
  1318.                         toplaystr += ", ";
  1319.                     }
  1320.                     toplaynum++;
  1321.                     toplaystr += playersObj.elements[picks[i].element-1].web_name;
  1322.                 }
  1323.             } else {
  1324.                 subsArr[numsubscount] = {playerID: picks[i].element-1, playerType: player_type, autosubbedIn: false};
  1325.  
  1326.                 if (player_type == 2){
  1327.                    numnonplayingdefcount++;
  1328.                 }
  1329.  
  1330.                 numsubscount++;
  1331.                 subsstr += "<player title=\"" + pick_points + "\">";
  1332.                 subsstr += playersObj.elements[picks[i].element-1].web_name;
  1333.  
  1334.                 pts = pick_points * picks[i].multiplier;
  1335.                 bench_pts = bench_pts + pts;
  1336.  
  1337.                 for (j=0; j<bonusArr.length; j++) {
  1338.                     //bonus for finished matches
  1339.                     if(bonusArr[j].playerID == picks[i].element) {
  1340.                         bench_bonus_pts = bench_bonus_pts + bonusArr[j].bonusPTS * picks[i].multiplier;
  1341.                     }
  1342.                 }
  1343.  
  1344.                 if(is_captain) {
  1345.                     subsstr += "(C)";
  1346.                 } else if(is_vice) {
  1347.                     subsstr += "(vc)";
  1348.                 }
  1349.  
  1350.                 if(numsubscount !== 4) {
  1351.                     subsstr += ", ";
  1352.                 }
  1353.  
  1354.                 subsstr += "</player>";
  1355.  
  1356.             }
  1357.         }
  1358.         rowstr = "";
  1359.  
  1360.   if (dnpArr.length > 0) {
  1361.         //Sub goalkeepers
  1362.         for (indx=0; indx<dnpArr.length; indx++) {
  1363.             //if its a goalkeeper and isn't already subbed out
  1364.             if (dnpArr[indx].playerType==1 && !dnpArr[indx].autosubbedOut) {
  1365.                 //and isn't already subbed in and has played minutes already
  1366.                if (!subsArr[0].autosubbedIn && liveObj.elements[subsArr[0].playerID+1].stats.minutes>0) {
  1367.                    //sub player
  1368.                    //autosubs_pts += liveObj.elements[subsArr[0].playerID+1].stats.total_points;
  1369.                    autosubs_str += playersObj.elements[subsArr[0].playerID].web_name + "(" + liveObj.elements[subsArr[0].playerID+1].stats.total_points + ")\n";
  1370.                    subsArr[0].autosubbedIn = true;
  1371.                    dnpArr[indx].autosubbedOut = true;
  1372.                }
  1373.             }
  1374.         }
  1375.  
  1376.         //Sub outfield players
  1377.         numplayingdefcount = numfirstxidefcount - numnonplayingdefcount;
  1378.         numofdefssubstituted = 0;
  1379.  
  1380.         if  (numplayingdefcount < 3) {
  1381.         //do {
  1382.             //do only 2 changes because 2 is max places on bench for defenders
  1383.             for (z=1; z<3; z++) {
  1384.             index_subin = -1;
  1385.             index_subout = -1;
  1386.  
  1387.                 for (x=0; x<dnpArr.length; x++) {
  1388.                  //if its a defender and isn't already subbed out
  1389.                  if (dnpArr[x].playerType==2 && !dnpArr[x].autosubbedOut) {
  1390.                      index_subout = x;
  1391.                      break;
  1392.                  }
  1393.                 }
  1394.  
  1395.                 for (y=1; y<=3; y++) {
  1396.                     if (subsArr[y].playerType==2 && !subsArr[y].autosubbedIn && liveObj.elements[subsArr[y].playerID+1].stats.minutes>0) {
  1397.                         index_subin = y;
  1398.                         break;
  1399.                     }
  1400.                 }
  1401.  
  1402.             if(index_subout>=0 && index_subin>0) {
  1403.  
  1404.                subsArr[index_subin].autosubbedIn = true;
  1405.                dnpArr[index_subout].autosubbedOut = true;
  1406.                autosubs_str += playersObj.elements[subsArr[index_subin].playerID].web_name + "(" + liveObj.elements[subsArr[index_subin].playerID+1].stats.total_points + ")\n";
  1407.                numplayingdefcount++;
  1408.  
  1409.             }
  1410.              numofdefssubstituted++;
  1411.              if (numplayingdefcount == 3) {
  1412.                  break;
  1413.              }
  1414.           }
  1415.         }
  1416.       //do other subs in order
  1417.        for (w=0; w<dnpArr.length; w++) {
  1418.            //if already subsituted 2 defenders then skip defenders
  1419.              if ((!dnpArr[w].autosubbedOut) && ((dnpArr[w].playerType!=2) || ((dnpArr[w].playerType==2) && (numofdefssubstituted !=2)))) {
  1420.                  for (v=1; v<=3; v++) {
  1421.                     if (!subsArr[v].autosubbedIn && liveObj.elements[subsArr[v].playerID+1].stats.minutes>0) {
  1422.                          subsArr[v].autosubbedIn = true;
  1423.                          dnpArr[w].autosubbedOut = true;
  1424.                          autosubs_str += playersObj.elements[subsArr[v].playerID].web_name + "(" + liveObj.elements[subsArr[v].playerID+1].stats.total_points + ")\n";
  1425.                          break;
  1426.                          }
  1427.                  }
  1428.                 }
  1429.        }
  1430.  
  1431.       //calculate autosubs points
  1432.       for (m=0; m<subsArr.length; m++) {
  1433.           if (subsArr[m].autosubbedIn) {
  1434.               autosubs_pts += liveObj.elements[subsArr[m].playerID+1].stats.total_points;
  1435.               for (n=0; n<bonusArr.length; n++) {
  1436.                     //add bonus for finished matches
  1437.                     if(bonusArr[n].playerID == subsArr[m].playerID) {
  1438.                         autosubs_pts += bonusArr[n].bonusPTS;
  1439.                     }
  1440.                 }
  1441.           }
  1442.       }
  1443.  
  1444.  
  1445.  
  1446.   }
  1447.         if (captain_dnp){
  1448.             if (teamObj.active_chip == "3xc"){
  1449.             autosubs_pts += vice_points*2;
  1450.             autosubs_str += vice_captain + " ("+ vice_points*2 + ")(vc)";
  1451.         } else {
  1452.             autosubs_pts += vice_points;
  1453.             autosubs_str += vice_captain + " ("+ vice_points + ")(vc)";}
  1454.         }
  1455.  
  1456.         if(teamObj.active_chip == "bboost") {
  1457.                     live_score = live_score + bench_pts + bench_bonus_pts;
  1458.                 } else {
  1459.                 live_score += autosubs_pts + bonus_pts;
  1460.                 }
  1461.  
  1462.  
  1463.  
  1464.         live_total = (current_points - week_points) + live_score;
  1465.  
  1466.         if (event_day == 1){
  1467.                     live_total -= transfers_cost;
  1468.                 }
  1469.  
  1470.  
  1471.         h2hpos = 0;
  1472.  
  1473.         if(config_values["teamval"] == 1) {
  1474.             document.getElementById("teamval"+team_id).innerHTML = value;
  1475.         }
  1476.         if(config_values["bankval"] == 1) {
  1477.             document.getElementById("bankval"+team_id).innerHTML = bank;
  1478.         }
  1479.         if(config_values["totalval"] == 1) {
  1480.             document.getElementById("totalval"+team_id).innerHTML = total_value;
  1481.         }
  1482.         if(config_values["tt"] == 1) {
  1483.             document.getElementById("tt"+team_id).innerHTML = total_transfers;
  1484.         }
  1485.         if(config_values["gwt"] == 1) {
  1486.             document.getElementById("gwt"+team_id).innerHTML = gw_transfers;
  1487.         }
  1488.         if(config_values["hitpts"] == 1) {
  1489.             document.getElementById("hitpts"+team_id).innerHTML = transfers_hit;
  1490.         }
  1491.         if(config_values["captain"] == 1) {
  1492.             document.getElementById("captain"+team_id).innerHTML = captain;
  1493.             document.getElementById("captain"+team_id).title = vice_captain;
  1494.         }
  1495.         if(config_values["livepoints"] == 1) {
  1496.             if (transfers_cost>0 && event_day == 1){
  1497.                 document.getElementById("livepoints"+team_id).innerHTML = live_score + "(-" +transfers_cost + ")" ;
  1498.             } else {
  1499.                 document.getElementById("livepoints"+team_id).innerHTML = live_score;
  1500.             }
  1501.         }
  1502.         if(config_values["livetotal"] == 1) {
  1503.             document.getElementById("livetotal"+team_id).innerHTML = numberWithCommas(live_total);
  1504.         }
  1505.         if(config_values["h2h"] == 1) {
  1506.             h2hpos = "-";
  1507.             /*for(li=0; li<teamObj.leagues.h2h.length; li++) {
  1508.                 ln = teamObj.leagues.h2h[li].name;
  1509.                 if(ln.indexOf(window.leagueName) == 0) {
  1510.                     h2hpos = teamObj.leagues.h2h[li].entry_rank;
  1511.                 }
  1512.             }*/
  1513.             document.getElementById("h2h"+team_id).innerHTML = h2hpos;
  1514.         }
  1515.         if(config_values["played"] == 1) {
  1516.             document.getElementById("played_p"+team_id).innerHTML = playednum;
  1517.             document.getElementById("played_p"+team_id).title = playedstr;
  1518.             document.getElementById("played_tp"+team_id).innerHTML = toplaynum;
  1519.             document.getElementById("played_tp"+team_id).title = toplaystr;
  1520.             document.getElementById("played_dnp"+team_id).innerHTML = didntplaynum;
  1521.             document.getElementById("played_dnp"+team_id).title = didntplaystr;
  1522.         }
  1523.         if(config_values["bonus"] == 1) {
  1524.             document.getElementById("bonus"+team_id).innerHTML = bonus_pts;
  1525.             document.getElementById("bonus"+team_id).title = bonus_str;
  1526.  
  1527.         }
  1528.         if(config_values["autosubs"] == 1) {
  1529.             document.getElementById("autosubs"+team_id).innerHTML = autosubs_pts;
  1530.             document.getElementById("autosubs"+team_id).title = autosubs_str;
  1531.         }
  1532.         document.getElementById("squaddiv"+team_id).innerHTML = firstxistr + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>Subs: "+subsstr;
  1533.  
  1534.     }
  1535.  
  1536.     standingsDiv = document.getElementById("ismr-classic-standings");
  1537.     //alert(standingsDiv.innerHTML);
  1538.     tables = standingsDiv.getElementsByTagName("table");
  1539.     //alert(tables.length);
  1540.  
  1541.     table = tables[0];
  1542.  
  1543.     clearTimeout(mytimeout);
  1544.     mytimeout = setTimeout(function() {sorttable.makeSortable(table)}, 1000);
  1545.  
  1546. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement