function json_decode(str_json) {
    // http://kevin.vanzonneveld.net
    // +      original by: Public Domain (http://www.json.org/json2.js)
    // + reimplemented by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // + improved by: T.J. Leahy
    // *     example 1: json_decode('[\n    "e",\n    {\n    "pluribus": "unum"\n}\n]');
    // *     returns 1: ['e', {pluribus: 'unum'}]
 
    /*
        http://www.JSON.org/json2.js
        2008-11-19
        Public Domain.
        NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
        See http://www.JSON.org/js.html
    */
 
    var json = this.window.JSON;
    if (typeof json === 'object' && typeof json.parse === 'function') {
        return json.parse(str_json);
    }
 
    var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
    var j;
    var text = str_json;
 
    // Parsing happens in four stages. In the first stage, we replace certain
    // Unicode characters with escape sequences. JavaScript handles many characters
    // incorrectly, either silently deleting them, or treating them as line endings.
    cx.lastIndex = 0;
    if (cx.test(text)) {
        text = text.replace(cx, function (a) {
            return '\\u' +
            ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
        });
    }
 
    // In the second stage, we run the text against regular expressions that look
    // for non-JSON patterns. We are especially concerned with '()' and 'new'
    // because they can cause invocation, and '=' because it can cause mutation.
    // But just to be safe, we want to reject all unexpected forms.
 
    // We split the second stage into 4 regexp operations in order to work around
    // crippling inefficiencies in IE's and Safari's regexp engines. First we
    // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
    // replace all simple value tokens with ']' characters. Third, we delete all
    // open brackets that follow a colon or comma or that begin the text. Finally,
    // we look to see that the remaining characters are only whitespace or ']' or
    // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
    if (/^[\],:{}\s]*$/.
        test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
            replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
            replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
 
        // In the third stage we use the eval function to compile the text into a
        // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
        // in JavaScript: it can begin a block or an object literal. We wrap the text
        // in parens to eliminate the ambiguity.
 
        j = eval('(' + text + ')');
        //alert( text );
 
        return j;
    }
 
    // If the text is not JSON parseable, then a SyntaxError is thrown.
    throw new SyntaxError('json_decode');
}

function calculate(){

  /*INPUTS normal*/
  var NormalLifeTime=document.getElementById('normalLifeTime').value;
  var NormalPrice=document.getElementById('normalPrice').value;
  var NormalPower=document.getElementById('normalPower').value;
  var NormalQuantity=document.getElementById('normalQuantity').value;
  /* INPUTS led */
  var LedLifeTime=document.getElementById('ledLifeTime').value;
  var LedPrice=document.getElementById('ledPrice').value;
  var LedPower=document.getElementById('ledPower').value;
  
  /* INPUTS global */
  var DayTime=document.getElementById('dayTime').value;
  var KwhPrice=document.getElementById('kwhPrice').value;

  /* czas pracy */
  var WorkYear=document.getElementById('WorkYear').value;
  var WorkTime=parseInt(WorkYear*365*DayTime);

  /* ile potrzeba żarówek led */
  var LedQuantity=parseInt((NormalQuantity*NormalPower)/(LedPower*8));
  document.getElementById('ledQuantity').innerHTML=LedQuantity;

  /* cena żarówek */
  var NormalLampPrice=WorkTime/NormalLifeTime*NormalQuantity*NormalPrice;
  document.getElementById('normalLampPrice').innerHTML=parseFloat(NormalLampPrice.toFixed(2))+' zł';
  
  var LedLampPrice=parseInt(WorkTime)/parseInt(LedLifeTime)*parseInt(LedQuantity)*parseInt(LedPrice);
  document.getElementById('ledLampPrice').innerHTML=parseFloat(LedLampPrice.toFixed(2))+' zł';
  
  /*cena energii */
  var NormalEnergyPrice=parseInt((NormalPower*NormalQuantity*WorkTime*KwhPrice)/1000);
  document.getElementById('normalEnergyPrice').innerHTML=parseFloat(NormalEnergyPrice.toFixed(2))+' zł';
  
  var LedEnergyPrice=parseInt((LedPower*LedQuantity*WorkTime*KwhPrice)/1000);
  document.getElementById('ledEnergyPrice').innerHTML=parseFloat(LedEnergyPrice.toFixed(2))+' zł';
 
  /*cena całkowita */
  var NormalSumPrice=NormalEnergyPrice+NormalLampPrice;
  document.getElementById('normalSumPrice').innerHTML=parseFloat(NormalSumPrice.toFixed(2))+' zł';
  
  var LedSumPrice=LedEnergyPrice+LedLampPrice;
  document.getElementById('ledSumPrice').innerHTML=parseFloat(LedSumPrice.toFixed(2))+' zł';
  
  /*zysk po czasie życia żarówki zwykłej*/
  var LedSumProfit=NormalSumPrice-LedSumPrice;
  document.getElementById('ledSumProfit').innerHTML=parseFloat(LedSumProfit.toFixed(2))+' zł';
}

function setLed()
{
    data = json_decode( getLedData( document.getElementById('led_id').value) );
    
	document.getElementById('ledLifeTime').value = data.DEF_LED_LIFE_TIME;
	document.getElementById('ledPrice').value = data.DEF_LED_PRICE;
    document.getElementById('ledPower').value = data.DEF_LED_POWER; 
    document.getElementById('ledLink').innerHTML = '<br /><a href="' + data.Link + '" target="_blank">Kup ' + data.Name + '</a>';
}
