<commit_before>// Codes for counties n such
var kommunkod={"Upplands-Väsby":114,Vallentuna:115,"Österåker":117,"Värmdö":120,"Järfälla":123,"Ekerö":125,Huddinge:126,Botkyrka:127,Salem:128,Haninge:136,"Tyresö":138,"Upplands-Bro":139,Nykvarn:140,"Täby":160,Danderyd:162,Sollentuna:163,Stockholm:180,"Södertälje":181,Nacka:182,Sundbyberg:183,Solna:184,"Lidingö":186,Vaxholm:187,"Norrtälje":188,Sigtuna:191,"Nynäshamn":192,"Håbo":305,"Älvkarleby":319,Knivsta:330,Heby:331,Tierp:360,Uppsala:380,"Enköping":381,"Östhammar":382,"Vingåker":428,Gnesta:461,"Nyköping":480,"Oxelösund":481,Flen:482,Katrineholm:483,Eskilstuna:484,"Strängnäs":486,Trosa:488,"Ödeshög":509,Ydre:512,Kinda:513,Boxholm:560,"Åtvidaberg":561,"Finspång":562,Valdemarsvik:563,"Linköping":580,"Norrköping":581,"Söderköping":582,Motala:583,Vadstena:584,"Mjölby":586,Aneby:604,"Gnosjö":617,"Mullsjö":642,Habo:643,Gislaved:662,Vaggeryd:665,"Jönköping":680,"Nässjö":682,"Värnamo":683,"Sävsjö":684,Vetlanda:685,"Eksjö":686,"Tranås":687,Uppvidinge:760,Lessebo:761,Tingsryd:763,Alvesta:764,"Älmhult":765,Markaryd:767,"Växjö":780,Ljungby:781,"Högsby":821,"Torsås":834,"Mörbylånga":840,Hultsfred:860,"Mönsterås":861,Emmaboda:862,Kalmar:880,Nybro:881,Oskarshamn:882,"Västervik":883,Vimmerby:884,Borgholm:885,Gotland:980,"Olofström":1060,Karlskrona:1080,Ronneby:1081,Karlshamn:1082,"Sölvesborg":1083,"Svalöv":1214,Staffanstorp:1230,"Burlöv":1231,Vellinge:1233,"Östra-Göinge":1256,"Örkelljunga":1257,Bjuv:1260,"Kävlinge":1261,Lomma:1262,Svedala:1263,Skurup:1264,"Sjöbo":1265,"Hörby":1266,"Höör":1267,Tomelilla:1270,"Bromölla":1272,Osby:1273,Perstorp:1275,Klippan:1276,"Åstorp":1277,"Båstad":1278,"Malmö":1280,Lund:1281,Landskrona:1282,Helsingborg:1283,"Höganäs":1284,"Eslöv":1285,Ystad:1286,Trelleborg:1287,Kristianstad:1290,Simrishamn:1291,"Ängelholm":1292,"Hässleholm":1293,Hylte:1315,Halmstad:1380,Laholm:1381,Falkenberg:1382,Varberg:1383,Kungsbacka:1384,"Härryda":1401,Partille:1402,"Öckerö":1407,Stenungsund:1415,"Tjörn":1419,Orust:1421,"Sotenäs":1427,Munkedal:1430,Tanum:1435,"Dals-Ed":1438,"Färgelanda":1439,Ale:1440,Lerum:1441,"Vårgårda":1442,Bollebygd:1443,"Grästorp":1444,Essunga:1445,Karlsborg:1446,"Gullspång":1447,Tranemo:1452,Bengtsfors:1460,Mellerud:1461,"Lilla-Edet":1462,Mark:1463,Svenljunga:1465,Herrljunga:1466,Vara:1470,"Götene":1471,Tibro:1472,"Töreboda":1473,"Göteborg":1480,"Mölndal":1481,"Kungälv":1482,Lysekil:1484,Uddevalla:1485,"Strömstad":1486,"Vänersborg":1487,"Trollhättan":1488,"Alingsås":1489,"Borås":1490,Ulricehamn:1491,"Åmål":1492,Mariestad:1493,"Lidköping":1494,Skara:1495,"Skövde":1496,Hjo:1497,Tidaholm:1498,"Falköping":1499,Kil:1715,Eda:1730,Torsby:1737,Storfors:1760,"Hammarö":1761,Munkfors:1762,Forshaga:1763,Grums:1764,"Årjäng":1765,Sunne:1766,Karlstad:1780,Kristinehamn:1781,Filipstad:1782,Hagfors:1783,Arvika:1784,"Säffle":1785,Lekeberg:1814,"Laxå":1860,Hallsberg:1861,Degerfors:1862,"Hällefors":1863,Ljusnarsberg:1864,"Örebro":1880,Kumla:1881,Askersund:1882,Karlskoga:1883,Nora:1884,Lindesberg:1885,Skinnskatteberg:1904,Surahammar:1907,"Kungsör":1960,Hallstahammar:1961,Norberg:1962,"Västerås":1980,Sala:1981,Fagersta:1982,"Köping":1983,Arboga:1984,Vansbro:2021,"Malung-Sälen":2023,Gagnef:2026,Leksand:2029,"Rättvik":2031,Orsa:2034,"Älvdalen":2039,Smedjebacken:2061,Mora:2062,Falun:2080,"Borlänge":2081,"Säter":2082,Hedemora:2083,Avesta:2084,Ludvika:2085,Ockelbo:2101,Hofors:2104,"Ovanåker":2121,Nordanstig:2132,Ljusdal:2161,"Gävle":2180,Sandviken:2181,"Söderhamn":2182,"Bollnäs":2183,Hudiksvall:2184,"Ånge":2260,"Timrå":2262,"Härnösand":2280,Sundsvall:2281,Kramfors:2282,"Sollefteå":2283,"Örnsköldsvik":2284,Ragunda:2303,"Bräcke":2305,Krokom:2309,"Strömsund":2313,"Åre":2321,Berg:2326,"Härjedalen":2361,"Östersund":2380,Nordmaling:2401,Bjurholm:2403,Vindeln:2404,Robertsfors:2409,"Norsjö":2417,"Malå":2418,Storuman:2421,Sorsele:2422,Dorotea:2425,"Vännäs":2460,Vilhelmina:2462,"Åsele":2463,"Umeå":2480,Lycksele:2481,"Skellefteå":2482,Arvidsjaur:2505,Arjeplog:2506,Jokkmokk:2510,"Överkalix":2513,Kalix:2514,"Övertorneå":2518,Pajala:2521,"Gällivare":2523,"Älvsbyn":2560,"Luleå":2580,"Piteå":2581,Boden:2582,Haparanda:2583,Kiruna:2584}
var lanskod={"Stockholms län":1,"Uppsala län":3,"Södermanlands län":4,"Östergötlands län":5,"Jönköpings län":6,"Kronobergs län":7,"Kalmar län":8,"Gotlands län":9,"Blekinge län":10,"Skåne län":12,"Hallands län":13,"Västra Götalands län":14,"Värmlands län":17,"Örebro län":18,"Västmanlands län":19,"Dalarnas län":20,"Gävleborgs län":21,"Västernorrlands län":22,"Jämtlands län":23,"Västerbottens län":24,"Norrbottens län":25}
var kommuntolan={114:1,115:1,117:1,120:1,123:1,125:1,126:1,127:1,128:1,136:1,138:1,139:1,140:1,160:1,162:1,163:1,180:1,181:1,182:1,183:1,184:1,186:1,187:1,188:1,191:1,192:1,305:3,319:3,330:3,331:3,360:3,380:3,381:3,382:3,428:4,461:4,480:4,481:4,482:4,483:4,484:4,486:4,488:4,509:5,512:5,513:5,560:5,561:5,562:5,563:5,580:5,581:5,582:5,583:5,584:5,586:5,604:6,617:6,642:6,643:6,662:6,665:6,680:6,682:6,683:6,684:6,685:6,686:6,687:6,760:7,761:7,763:7,764:7,765:7,767:7,780:7,781:7,821:8,834:8,840:8,860:8,861:8,862:8,880:8,881:8,882:8,883:8,884:8,885:8,980:9,1060:10,1080:10,1081:10,1082:10,1083:10,1214:12,1230:12,1231:12,1233:12,1256:12,1257:12,1260:12,1261:12,1262:12,1263:12,1264:12,1265:12,1266:12,1267:12,1270:12,1272:12,1273:12,1275:12,1276:12,1277:12,1278:12,1280:12,1281:12,1282:12,1283:12,1284:12,1285:12,1286:12,1287:12,1290:12,1291:12,1292:12,1293:12,1315:13,1380:13,1381:13,1382:13,1383:13,1384:13,1401:14,1402:14,1407:14,1415:14,1419:14,1421:14,1427:14,1430:14,1435:14,1438:14,1439:14,1440:14,1441:14,1442:14,1443:14,1444:14,1445:14,1446:14,1447:14,1452:14,1460:14,1461:14,1462:14,1463:14,1465:14,1466:14,1470:14,1471:14,1472:14,1473:14,1480:14,1481:14,1482:14,1484:14,1485:14,1486:14,1487:14,1488:14,1489:14,1490:14,1491:14,1492:14,1493:14,1494:14,1495:14,1496:14,1497:14,1498:14,1499:14,1715:17,1730:17,1737:17,1760:17,1761:17,1762:17,1763:17,1764:17,1765:17,1766:17,1780:17,1781:17,1782:17,1783:17,1784:17,1785:17,1814:18,1860:18,1861:18,1862:18,1863:18,1864:18,1880:18,1881:18,1882:18,1883:18,1884:18,1885:18,1904:19,1907:19,1960:19,1961:19,1962:19,1980:19,1981:19,1982:19,1983:19,1984:19,2021:20,2023:20,2026:20,2029:20,2031:20,2034:20,2039:20,2061:20,2062:20,2080:20,2081:20,2082:20,2083:20,2084:20,2085:20,2101:21,2104:21,2121:21,2132:21,2161:21,2180:21,2181:21,2182:21,2183:21,2184:21,2260:22,2262:22,2280:22,2281:22,2282:22,2283:22,2284:22,2303:23,2305:23,2309:23,2313:23,2321:23,2326:23,2361:23,2380:23,2401:24,2403:24,2404:24,2409:24,2417:24,2418:24,2421:24,2422:24,2425:24,2460:24,2462:24,2463:24,2480:24,2481:24,2482:24,2505:25,2506:25,2510:25,2513:25,2514:25,2518:25,2521:25,2523:25,2560:25,2580:25,2581:25,2582:25,2583:25,2584:25}
var lankodtoname={1:"Stockholms län",10:"Blekinge län",12:"Skåne län",13:"Hallands län",14:"Västra Götalands län",17:"Värmlands län",18:"Örebro län",19:"Västmanlands län",20:"Dalarnas län",21:"Gävleborgs län",22:"Västernorrlands län",23:"Jämtlands län",24:"Västerbottens län",25:"Norrbottens län",3:"Uppsala län",4:"Södermanlands län",5:"Östergötlands län",6:"Jönköpings län",7:"Kronobergs län",8:"Kalmar län",9:"Gotlands län"};

// Database for local storage
var db;

function getStatData(kommun_name,callback) {

  // Set up a little variable that we simply can store important properties.
  var id = {
    kommun: kommun_name,
    kommunid :kommunkod[kommun_name],
    lanid : kommuntolan[kommunkod[kommun_name]],
    lan : lankodtoname[kommuntolan[kommunkod[kommun_name]]]
  };

  var openRequest = indexedDB.open("maklarstats",5);
  openRequest.onsuccess = function(e) {
      console.log("Success!");
      db = e.target.result;
  }
  openRequest.onupgradeneeded = function(e) {
     console.log("Upgrading...");
     var thisDB = e.target.result;

      if(!thisDB.objectStoreNames.contains("kommuner")) {
          thisDB.createObjectStore("kommuner");

      }
      if(!thisDB.objectStoreNames.contains("lan")) {
          thisDB.createObjectStore("lan");

      }
  }
  openRequest.onerror = function(e) {
    console.log("Error");
    console.dir(e);
  }
  var web_load = true;
  //We have to use our local storage.
  if(web_load){
    //Get kommun data
    getMaklarDataWeb({id:id.kommunid, namn:id.kommun},function(data_kommun) {
      /**
      * TODO: All this crap with ids is nonsense we could rewrite and resuse the save function instead. do that.
      */
      var res = {
        kommunid: "kommun_"+id.kommunid,
        kommun: {meta: {id: id.kommunid, timestamp: Date(), kommun : data_kommun}}

      };
      //Get maklardata
      getMaklarDataWeb({id:id.lanid, namn:id.lan},function(data_lan) {

        res.lanid = "lan_id"+id.lanid;
        res.lan = {meta: {id: id.lanid, timestamp: Date(), lan : data_lan}};

        console.log(res);
        saveDataToLocal(res);
        callback(res);
      });

    });
  }

}

function getDataFromLocal(kommun){

}
/*
*Save data locally. possible we can redo this and use only one
*/
function saveDataToLocal(data){
  console.log("This i will save locally");
  console.log(data);
  var transaction = db.transaction(["kommuner"],"readwrite");
  var store = transaction.objectStore("kommuner");

  //Perform the add
  var request = store.put(data.kommun.meta,data.kommun.meta.id);

  request.onerror = function(e) {
      console.log("Error",e.target.error.name);
      //some type of error handler
  }

  request.onsuccess = function(e) {
      console.log("Woot! Did it");
  }
  var transaction = db.transaction(["lan"],"readwrite");
  var store = transaction.objectStore("lan");

  //Perform the add
  var request = store.put(data.lan.meta,data.lan.meta.id);

  request.onerror = function(e) {
      console.log("Error",e.target.error.name);
      //some type of error handler
  }

  request.onsuccess = function(e) {
      console.log("Woot! Did it");
  }
}
function getMaklarDataWeb(q,callback){
  console.log(q);
  // Official realestate statistics
  var statUrl = 'http://www.maklarstatistik.se/usercontrols/statistik/LineChart.aspx?Months=24&LK='+q.id+'&Main='+q.namn+'&Extra1=8888&Extra2=8888&Typ=Boratterkurvdata'
  console.log(statUrl);
  var req = new XMLHttpRequest();

  req.open('GET', statUrl);
  // Make sure chrome parses the xml for us
  req.overrideMimeType('text/xml');

  req.onload = function() {

    var response = req.responseXML;<commit_msg>Made inte load data from local storage before internet<commit_after>// Codes for counties n such
var kommunkod={"Upplands-Väsby":114,Vallentuna:115,"Österåker":117,"Värmdö":120,"Järfälla":123,"Ekerö":125,Huddinge:126,Botkyrka:127,Salem:128,Haninge:136,"Tyresö":138,"Upplands-Bro":139,Nykvarn:140,"Täby":160,Danderyd:162,Sollentuna:163,Stockholm:180,"Södertälje":181,Nacka:182,Sundbyberg:183,Solna:184,"Lidingö":186,Vaxholm:187,"Norrtälje":188,Sigtuna:191,"Nynäshamn":192,"Håbo":305,"Älvkarleby":319,Knivsta:330,Heby:331,Tierp:360,Uppsala:380,"Enköping":381,"Östhammar":382,"Vingåker":428,Gnesta:461,"Nyköping":480,"Oxelösund":481,Flen:482,Katrineholm:483,Eskilstuna:484,"Strängnäs":486,Trosa:488,"Ödeshög":509,Ydre:512,Kinda:513,Boxholm:560,"Åtvidaberg":561,"Finspång":562,Valdemarsvik:563,"Linköping":580,"Norrköping":581,"Söderköping":582,Motala:583,Vadstena:584,"Mjölby":586,Aneby:604,"Gnosjö":617,"Mullsjö":642,Habo:643,Gislaved:662,Vaggeryd:665,"Jönköping":680,"Nässjö":682,"Värnamo":683,"Sävsjö":684,Vetlanda:685,"Eksjö":686,"Tranås":687,Uppvidinge:760,Lessebo:761,Tingsryd:763,Alvesta:764,"Älmhult":765,Markaryd:767,"Växjö":780,Ljungby:781,"Högsby":821,"Torsås":834,"Mörbylånga":840,Hultsfred:860,"Mönsterås":861,Emmaboda:862,Kalmar:880,Nybro:881,Oskarshamn:882,"Västervik":883,Vimmerby:884,Borgholm:885,Gotland:980,"Olofström":1060,Karlskrona:1080,Ronneby:1081,Karlshamn:1082,"Sölvesborg":1083,"Svalöv":1214,Staffanstorp:1230,"Burlöv":1231,Vellinge:1233,"Östra-Göinge":1256,"Örkelljunga":1257,Bjuv:1260,"Kävlinge":1261,Lomma:1262,Svedala:1263,Skurup:1264,"Sjöbo":1265,"Hörby":1266,"Höör":1267,Tomelilla:1270,"Bromölla":1272,Osby:1273,Perstorp:1275,Klippan:1276,"Åstorp":1277,"Båstad":1278,"Malmö":1280,Lund:1281,Landskrona:1282,Helsingborg:1283,"Höganäs":1284,"Eslöv":1285,Ystad:1286,Trelleborg:1287,Kristianstad:1290,Simrishamn:1291,"Ängelholm":1292,"Hässleholm":1293,Hylte:1315,Halmstad:1380,Laholm:1381,Falkenberg:1382,Varberg:1383,Kungsbacka:1384,"Härryda":1401,Partille:1402,"Öckerö":1407,Stenungsund:1415,"Tjörn":1419,Orust:1421,"Sotenäs":1427,Munkedal:1430,Tanum:1435,"Dals-Ed":1438,"Färgelanda":1439,Ale:1440,Lerum:1441,"Vårgårda":1442,Bollebygd:1443,"Grästorp":1444,Essunga:1445,Karlsborg:1446,"Gullspång":1447,Tranemo:1452,Bengtsfors:1460,Mellerud:1461,"Lilla-Edet":1462,Mark:1463,Svenljunga:1465,Herrljunga:1466,Vara:1470,"Götene":1471,Tibro:1472,"Töreboda":1473,"Göteborg":1480,"Mölndal":1481,"Kungälv":1482,Lysekil:1484,Uddevalla:1485,"Strömstad":1486,"Vänersborg":1487,"Trollhättan":1488,"Alingsås":1489,"Borås":1490,Ulricehamn:1491,"Åmål":1492,Mariestad:1493,"Lidköping":1494,Skara:1495,"Skövde":1496,Hjo:1497,Tidaholm:1498,"Falköping":1499,Kil:1715,Eda:1730,Torsby:1737,Storfors:1760,"Hammarö":1761,Munkfors:1762,Forshaga:1763,Grums:1764,"Årjäng":1765,Sunne:1766,Karlstad:1780,Kristinehamn:1781,Filipstad:1782,Hagfors:1783,Arvika:1784,"Säffle":1785,Lekeberg:1814,"Laxå":1860,Hallsberg:1861,Degerfors:1862,"Hällefors":1863,Ljusnarsberg:1864,"Örebro":1880,Kumla:1881,Askersund:1882,Karlskoga:1883,Nora:1884,Lindesberg:1885,Skinnskatteberg:1904,Surahammar:1907,"Kungsör":1960,Hallstahammar:1961,Norberg:1962,"Västerås":1980,Sala:1981,Fagersta:1982,"Köping":1983,Arboga:1984,Vansbro:2021,"Malung-Sälen":2023,Gagnef:2026,Leksand:2029,"Rättvik":2031,Orsa:2034,"Älvdalen":2039,Smedjebacken:2061,Mora:2062,Falun:2080,"Borlänge":2081,"Säter":2082,Hedemora:2083,Avesta:2084,Ludvika:2085,Ockelbo:2101,Hofors:2104,"Ovanåker":2121,Nordanstig:2132,Ljusdal:2161,"Gävle":2180,Sandviken:2181,"Söderhamn":2182,"Bollnäs":2183,Hudiksvall:2184,"Ånge":2260,"Timrå":2262,"Härnösand":2280,Sundsvall:2281,Kramfors:2282,"Sollefteå":2283,"Örnsköldsvik":2284,Ragunda:2303,"Bräcke":2305,Krokom:2309,"Strömsund":2313,"Åre":2321,Berg:2326,"Härjedalen":2361,"Östersund":2380,Nordmaling:2401,Bjurholm:2403,Vindeln:2404,Robertsfors:2409,"Norsjö":2417,"Malå":2418,Storuman:2421,Sorsele:2422,Dorotea:2425,"Vännäs":2460,Vilhelmina:2462,"Åsele":2463,"Umeå":2480,Lycksele:2481,"Skellefteå":2482,Arvidsjaur:2505,Arjeplog:2506,Jokkmokk:2510,"Överkalix":2513,Kalix:2514,"Övertorneå":2518,Pajala:2521,"Gällivare":2523,"Älvsbyn":2560,"Luleå":2580,"Piteå":2581,Boden:2582,Haparanda:2583,Kiruna:2584}
var lanskod={"Stockholms län":1,"Uppsala län":3,"Södermanlands län":4,"Östergötlands län":5,"Jönköpings län":6,"Kronobergs län":7,"Kalmar län":8,"Gotlands län":9,"Blekinge län":10,"Skåne län":12,"Hallands län":13,"Västra Götalands län":14,"Värmlands län":17,"Örebro län":18,"Västmanlands län":19,"Dalarnas län":20,"Gävleborgs län":21,"Västernorrlands län":22,"Jämtlands län":23,"Västerbottens län":24,"Norrbottens län":25}
var kommuntolan={114:1,115:1,117:1,120:1,123:1,125:1,126:1,127:1,128:1,136:1,138:1,139:1,140:1,160:1,162:1,163:1,180:1,181:1,182:1,183:1,184:1,186:1,187:1,188:1,191:1,192:1,305:3,319:3,330:3,331:3,360:3,380:3,381:3,382:3,428:4,461:4,480:4,481:4,482:4,483:4,484:4,486:4,488:4,509:5,512:5,513:5,560:5,561:5,562:5,563:5,580:5,581:5,582:5,583:5,584:5,586:5,604:6,617:6,642:6,643:6,662:6,665:6,680:6,682:6,683:6,684:6,685:6,686:6,687:6,760:7,761:7,763:7,764:7,765:7,767:7,780:7,781:7,821:8,834:8,840:8,860:8,861:8,862:8,880:8,881:8,882:8,883:8,884:8,885:8,980:9,1060:10,1080:10,1081:10,1082:10,1083:10,1214:12,1230:12,1231:12,1233:12,1256:12,1257:12,1260:12,1261:12,1262:12,1263:12,1264:12,1265:12,1266:12,1267:12,1270:12,1272:12,1273:12,1275:12,1276:12,1277:12,1278:12,1280:12,1281:12,1282:12,1283:12,1284:12,1285:12,1286:12,1287:12,1290:12,1291:12,1292:12,1293:12,1315:13,1380:13,1381:13,1382:13,1383:13,1384:13,1401:14,1402:14,1407:14,1415:14,1419:14,1421:14,1427:14,1430:14,1435:14,1438:14,1439:14,1440:14,1441:14,1442:14,1443:14,1444:14,1445:14,1446:14,1447:14,1452:14,1460:14,1461:14,1462:14,1463:14,1465:14,1466:14,1470:14,1471:14,1472:14,1473:14,1480:14,1481:14,1482:14,1484:14,1485:14,1486:14,1487:14,1488:14,1489:14,1490:14,1491:14,1492:14,1493:14,1494:14,1495:14,1496:14,1497:14,1498:14,1499:14,1715:17,1730:17,1737:17,1760:17,1761:17,1762:17,1763:17,1764:17,1765:17,1766:17,1780:17,1781:17,1782:17,1783:17,1784:17,1785:17,1814:18,1860:18,1861:18,1862:18,1863:18,1864:18,1880:18,1881:18,1882:18,1883:18,1884:18,1885:18,1904:19,1907:19,1960:19,1961:19,1962:19,1980:19,1981:19,1982:19,1983:19,1984:19,2021:20,2023:20,2026:20,2029:20,2031:20,2034:20,2039:20,2061:20,2062:20,2080:20,2081:20,2082:20,2083:20,2084:20,2085:20,2101:21,2104:21,2121:21,2132:21,2161:21,2180:21,2181:21,2182:21,2183:21,2184:21,2260:22,2262:22,2280:22,2281:22,2282:22,2283:22,2284:22,2303:23,2305:23,2309:23,2313:23,2321:23,2326:23,2361:23,2380:23,2401:24,2403:24,2404:24,2409:24,2417:24,2418:24,2421:24,2422:24,2425:24,2460:24,2462:24,2463:24,2480:24,2481:24,2482:24,2505:25,2506:25,2510:25,2513:25,2514:25,2518:25,2521:25,2523:25,2560:25,2580:25,2581:25,2582:25,2583:25,2584:25}
var lankodtoname={1:"Stockholms län",10:"Blekinge län",12:"Skåne län",13:"Hallands län",14:"Västra Götalands län",17:"Värmlands län",18:"Örebro län",19:"Västmanlands län",20:"Dalarnas län",21:"Gävleborgs län",22:"Västernorrlands län",23:"Jämtlands län",24:"Västerbottens län",25:"Norrbottens län",3:"Uppsala län",4:"Södermanlands län",5:"Östergötlands län",6:"Jönköpings län",7:"Kronobergs län",8:"Kalmar län",9:"Gotlands län"};

// Database for local storage
var db;

function getStatData(kommun_name,callback) {

  // Set up a little variable that we simply can store important properties.
  var id = {
    kommun: kommun_name,
    kommunid :kommunkod[kommun_name],
    lanid : kommuntolan[kommunkod[kommun_name]],
    lan : lankodtoname[kommuntolan[kommunkod[kommun_name]]]
  };

  var openRequest = indexedDB.open("maklarstats",5);
  openRequest.onsuccess = function(e) {
      console.log("Success!");
      db = e.target.result;

      getDataFromLocal(id, function(res,need_updated){
        if(need_updated){
          console.log("Nu laddar vi från netet");
          //Get kommun data
          getMaklarDataWeb({id:id.kommunid, namn:id.kommun},function(data_kommun) {
            /**
            * TODO: All this crap with ids is nonsense we could rewrite and resuse the save function instead. do that.
            */
            var res = {
              kommunid: "kommun_"+id.kommunid,
              kommun: {meta: {id: id.kommunid, timestamp: Date(), kommun : data_kommun}}

            };
            //Get maklardata
            getMaklarDataWeb({id:id.lanid, namn:id.lan},function(data_lan) {

              res.lanid = "lan_id"+id.lanid;
              res.lan = {meta: {id: id.lanid, timestamp: Date(), lan : data_lan}};

              saveDataToLocal(res);
              callback(res);
            });

          });
        }else{
          console.log("Vi har laddat från lokalt!");
          console.log(res);
          callback(res);
        }
      });
  }
  openRequest.onupgradeneeded = function(e) {
     console.log("Upgrading...");
     var thisDB = e.target.result;

      if(!thisDB.objectStoreNames.contains("kommuner")) {
          thisDB.createObjectStore("kommuner");

      }
      if(!thisDB.objectStoreNames.contains("lan")) {
          thisDB.createObjectStore("lan");

      }
  }
  openRequest.onerror = function(e) {
    console.log("Error");
    console.dir(e);
  }

}

function getDataFromLocal(id,callback){
  console.log("Nu kommer vi börja ladda");
    console.log(id);
  var transaction = db.transaction(["kommuner"],"readonly");
  var store = transaction.objectStore("kommuner");

  var request = store.get(id.kommunid);

  request.onsuccess = function(e) {

      var kommun_result = e.target.result;
      if(kommun_result) {
        var res = {
          kommunid: "kommun_"+id.kommunid,
          kommun: {meta: kommun_result}

        };

        var transaction = db.transaction(["lan"],"readonly");
        var store = transaction.objectStore("lan");

        var request = store.get(id.lanid);

        request.onsuccess = function(e) {

            var lan_result = e.target.result;
            if(lan_result) {
              res.lanid = "lan_id"+id.lanid;
              res.lan = {meta:lan_result};
              callback(res,false);
            }
        }
      }
  }

}
/*
*Save data locally. possible we can redo this and use only one
*/
function saveDataToLocal(data){

  var transaction = db.transaction(["kommuner"],"readwrite");
  var store = transaction.objectStore("kommuner");

  //Perform the add
  var request = store.put(data.kommun.meta,data.kommun.meta.id);

  request.onerror = function(e) {
      console.log("Error",e.target.error.name);
      //some type of error handler
  }

  request.onsuccess = function(e) {
      console.log("Woot! Did it");
  }
  var transaction = db.transaction(["lan"],"readwrite");
  var store = transaction.objectStore("lan");

  //Perform the add
  var request = store.put(data.lan.meta,data.lan.meta.id);

  request.onerror = function(e) {
      console.log("Error",e.target.error.name);
      //some type of error handler
  }

  request.onsuccess = function(e) {
      console.log("Woot! Did it");
  }
}
function getMaklarDataWeb(q,callback){

  // Official realestate statistics
  var statUrl = 'http://www.maklarstatistik.se/usercontrols/statistik/LineChart.aspx?Months=24&LK='+q.id+'&Main='+q.namn+'&Extra1=8888&Extra2=8888&Typ=Boratterkurvdata'

  var req = new XMLHttpRequest();

  req.open('GET', statUrl);
  // Make sure chrome parses the xml for us
  req.overrideMimeType('text/xml');

  req.onload = function() {

    var response = req.responseXML;
{'fraction_non_alphanumeric': 0.19484635524321345, 'fraction_numerical': 0.2490468972486335, 'mean_word_length': 9.482426576793452, 'pattern_counts': {'":': 290, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6549238', 'n_tokens_mistral': 13197, 'n_tokens_neox': 10242, 'n_words': 603}