var cena_benz= null;
var zuzycie_benz= null;
var cena_gazu= null;




function updateGasPrice()
{
    var benz_string= cena_benz.value;
   
    if( benz_string == '' )
        return;

    benz_string= benz_string.replace( ',', '.' );
 

    var z_benz_string= zuzycie_benz.value;
    if( z_benz_string == '' )
        return;
   
    z_benz_string= z_benz_string.replace( ',', '.' );
    

    var result= ""+Math.round( parseFloat( z_benz_string )*parseFloat( benz_string ) *100 )/100;

    if( result[ result.length-2 ] == '.' )
       result+= '0'; 
    else if( result[ result.length - 3] != '.' )
       result+= '.00';
   

    document.getElementById( 'kosztBenzynyRes' ).innerHTML= result+' PLN';

    updateProfits();
}





function updateGasPrice2()
{
    var benz_string= cena_gazu.value;
   
    if( benz_string == '' )
        return;

    benz_string= benz_string.replace( ',', '.' );
 

    var z_benz_string= zuzycie_benz.value;
    if( z_benz_string == '' )
        return;
   
    z_benz_string= z_benz_string.replace( ',', '.' );
    

    document.getElementById( 'zuzycieGazu' ).innerHTML= ""+( Math.round( parseFloat( z_benz_string )*120 )/100 )+' litr/100km';


    var result= ""+Math.round( parseFloat( z_benz_string )*parseFloat( benz_string ) *120 )/100;

    if( result[ result.length-2 ] == '.' )
       result+= '0'; 
    else if( result[ result.length - 3] != '.' )
       result+= '.00';
   

    document.getElementById( 'zuzycieGazuRes' ).innerHTML= result+' PLN';

    updateProfits();
}





function updateProfits()
{
    var result= parseFloat( document.getElementById( 'kosztBenzynyRes' ).innerHTML ) - parseFloat( document.getElementById( 'zuzycieGazuRes' ).innerHTML );
    
    result= ""+( Math.round( result*100 )/100 );

    if( result[ result.length-2 ] == '.' )
       result+= '0'; 
    else if( result[ result.length - 3] != '.' )
       result+= '.00';

    if( result > 0 )
        document.getElementById( 'zyskaszKm' ).innerHTML= result+' PLN';

    updateFinalRes();
}



function updateFinalRes()
{
    var result= parseFloat( document.getElementById( 'przebiegMies' ).value )*parseFloat( document.getElementById( 'zyskaszKm' ).innerHTML );
    //console.log( document.getElementById( 'przebiegMies' ).value );
    result= ""+( Math.round( result )/100 );

    //console.log( result );
    if( result[ result.length-2 ] == '.' )
       result+= '0'; 
    else if( result[ result.length - 3] != '.' )
       result+= '.00';

    if( result > 0 )
        document.getElementById( 'finalRes' ).innerHTML= result+' PLN';
    
}




function calculatorMain()
{

 cena_benz= document.getElementById( 'cenaBenzyny' );
 zuzycie_benz= document.getElementById( 'zuzycieBenzyny' );
 cena_gazu= document.getElementById( 'cenaGazu' );

 if( cena_benz )
    {

    addPortableEventListener( cena_benz, 'keyup', updateGasPrice );
    addPortableEventListener( zuzycie_benz, 'keyup', updateGasPrice );

    addPortableEventListener( cena_gazu, 'keyup', updateGasPrice2 );
    addPortableEventListener( zuzycie_benz, 'keyup', updateGasPrice2 );

    addPortableEventListener( document.getElementById( 'przebiegMies' ), 'keyup', updateFinalRes )

    }
}






runOnLoad( calculatorMain );


