function editEnable (hide1,hide2,show1,show2,show3)
{
    hide1.visible= false
    hide2.visible= false
    show1.visible= true
    show2.visible= true
    show3.visible= true
}

function editDisable (hide1,hide2,hide3,show1,show2)
{
    hide1.visible= false
    hide2.visible= false
    hide3.visible= false
    show1.visible= true
    show2.visible= true
}

function getIndexByRowId (Plist,Prowid)
{
    for (var i = 0; i < Plist.count; i++) {
        if (Plist.get(i).rowid === Prowid)
            return i
    }
}

//FUNKCE PRO STATISTIKU
//FUNKCE PRO STATISTIKU
//FUNKCE PRO STATISTIKU
//FUNKCE PRO STATISTIKU

function round (Pvalue)
{
    return Math.round(Pvalue*10)/10
}

function months (Plist)
{
    var lastM= Number(Date.fromLocaleString(Qt.locale(),Plist.get(0).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"M"))
    var lastY= Number(Date.fromLocaleString(Qt.locale(),Plist.get(0).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"yyyy"))
    var firstM= Number(Date.fromLocaleString(Qt.locale(),Plist.get(Plist.count-1).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"M"))
    var firstY= Number(Date.fromLocaleString(Qt.locale(),Plist.get(Plist.count-1).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"yyyy"))
    return (lastY-firstY)*12+lastM-firstM
}

function getTotalVolumeRefuel (Plist)
{
    var total=0
    for (var i = 0; i < Plist.count; i++) {
        total= total +Plist.get(i).objem
    }
    return total
}

function getTotalCost (Plist)
{
    var total=0
    for (var i = 0; i < Plist.count; i++) {
        total= total +Plist.get(i).cena
    }
    return total
}

function consumption (Plist)
{
    var total= 0,count= 0, min= 0, max= 0, avr= 0

    for (var i = Plist.count-1; i > 0; i--) {
        if(Plist.get(i).doplna) {
            avr= 100*Plist.get(i).objem/(Plist.get(i-1).tachometr-Plist.get(i).tachometr)
            if(min=== 0 || min > avr)
                min= avr
            if(max=== 0 || max < avr)
                max= avr
            total= total+avr
            count= count + 1
        }
    }
    avr= total/count
    return {average: avr,minimum: min,maximum: max}
}

function getVolumeCostByMonth (Plist,Pdate)
{
    var volume= 0, cost= 0
    for(var i = 0; i < Plist.count; i++) {
        if(Date.fromLocaleString(Qt.locale(),Plist.get(i).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"M.yyyy")===Pdate) {
            volume= volume+Plist.get(i).objem
            cost= cost+Plist.get(i).cena
        }
    }
    return {volume: volume,cost: cost}
}

function getVolumeCostByYear (Plist,Pdate)
{
    var volume= 0, cost= 0
    for(var i = 0; i < Plist.count; i++) {
        if(Date.fromLocaleString(Qt.locale(),Plist.get(i).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"yyyy")===Pdate) {
            volume= volume+Plist.get(i).objem
            cost= cost+Plist.get(i).cena
        }
    }
    return {volume: volume,cost: cost}
}

function getCostByMonth (Plist,Pdate)
{
    var cost= 0
    for(var i = 0; i < Plist.count; i++) {
        if(Date.fromLocaleString(Qt.locale(),Plist.get(i).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"M.yyyy")===Pdate)
            cost= cost+Plist.get(i).cena
    }
    return cost
}

function getCostByYear (Plist,Pdate)
{
    var cost= 0
    for(var i = 0; i < Plist.count; i++) {
        if(Date.fromLocaleString(Qt.locale(),Plist.get(i).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"yyyy")===Pdate)
            cost= cost+Plist.get(i).cena
    }
    return cost
}

//FUNKCE PRO GRAFY
//FUNKCE PRO GRAFY
//FUNKCE PRO GRAFY
//FUNKCE PRO GRAFY

function lineSeriesMilage (Plist,Pseries,Pseries2)
{
    var first= Plist.get(Plist.count-1).tachometr, minX, maxX, minY, maxY,firstBool= true

    for (var i = Plist.count-1; i >= 0; i--) {
        maxX= Date.fromLocaleString(Qt.locale(),Plist.get(i).datum,"dd.MM.yyyy")
        maxY= Plist.get(i).tachometr-first
        Pseries.append(toMsecsSinceEpoch(maxX),maxY)
        Pseries2.append(toMsecsSinceEpoch(maxX),maxY)
        if (firstBool) {
            firstBool= false
            minX= maxX
            minY= maxY
        }
    }
    return {minX: minX,
        maxX: maxX,
        minY: minY,
        maxY: maxY}
}

function toMsecsSinceEpoch(date) {
    var msecs = date.getTime();
    return msecs;
}

function consumptionValues (Plist,Pseries,Pseries2)
{
    var first= true, minX, maxX, minY, maxY,Y
    for (var i = Plist.count-1; i > 0; i--) {
        if(Plist.get(i).doplna) {
            maxX= Date.fromLocaleString(Qt.locale(),Plist.get(i).datum,"dd.MM.yyyy")
            Y= 100*Plist.get(i).objem/(Plist.get(i-1).tachometr-Plist.get(i).tachometr)
            Pseries.append(toMsecsSinceEpoch(maxX),Y)
            Pseries2.append(toMsecsSinceEpoch(maxX),Y)
            if (first) {
                minX= maxX
                minY= Y
                maxY= Y
                first= false
            }
            if (minY > Y)
                minY= Y
            if (maxY < Y)
                maxY= Y
        }
    }
    return {minX: minX,
        maxX: maxX,
        minY: minY,
        maxY: maxY}
}

function refuelPrizes (Plist,Pseries,Pseries2)
{
    var first= true, minX, maxX, minY, maxY,Y
    for (var i = Plist.count-1; i >= 0; i--) {
        maxX= Date.fromLocaleString(Qt.locale(),Plist.get(i).datum,"dd.MM.yyyy")
        Y= Plist.get(i).cena/Plist.get(i).objem
        Pseries.append(toMsecsSinceEpoch(maxX),Y)
        Pseries2.append(toMsecsSinceEpoch(maxX),Y)
        if (first) {
            minX= maxX
            minY= Y
            maxY= Y
            first= false
        }
        if (minY > Y)
            minY= Y
        if (maxY < Y)
            maxY= Y

    }
    return {minX: minX,
        maxX: maxX,
        minY: minY,
        maxY: maxY}
}

function costsValues(Plist,Plist2,Plist3,Plist4,Pseries)
{
    var sumP1=0, sumP2=0, sumP3=0, sumP4=0, max=0
    var j=0,k=0,l=0,m=0
    var currentDate= new Date()
    var currentMonth= Number(currentDate.toLocaleString(Qt.locale(),"M"))
    var currentYear= Number(currentDate.toLocaleString(Qt.locale(),"yyyy"))
    var date= currentMonth+"."+currentYear
    var barSet1= Pseries.append("Tankování",[])
    var barSet2= Pseries.append("Servis",[])
    var barSet3= Pseries.append("Technické",[])
    var barSet4= Pseries.append("Výdaje",[])
    var categories= ["1","2","3","4","5","6","7","8","9","10","11","12"]


    while(j<Plist.count && Date.fromLocaleString(Qt.locale(),Plist.get(j).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"M.yyyy")>date){
        j++
    }
    while(k<Plist2.count && Date.fromLocaleString(Qt.locale(),Plist2.get(k).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"M.yyyy")>date){
        k++
    }
    while(l<Plist3.count && Date.fromLocaleString(Qt.locale(),Plist3.get(l).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"M.yyyy")>date){
        l++
    }
    while(m<Plist4.count && Date.fromLocaleString(Qt.locale(),Plist4.get(m).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"M.yyyy")>date){
        m++
    }

    for (var i = 0; i < 12; i++) {
        if(currentMonth===0) {
            currentMonth= 12
            currentYear--
        }
        date= currentMonth+"."+currentYear
        while(j<Plist.count && Date.fromLocaleString(Qt.locale(),Plist.get(j).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"M.yyyy")===date){
            sumP1= sumP1+Plist.get(j).cena
            if(max < sumP1)
                max= sumP1
            j++
        }
        while(k<Plist2.count && Date.fromLocaleString(Qt.locale(),Plist2.get(k).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"M.yyyy")===date){
            sumP2= sumP2+Plist2.get(k).cena
            if(max < sumP2)
                max= sumP2
            k++
        }
        while(l<Plist3.count && Date.fromLocaleString(Qt.locale(),Plist3.get(l).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"M.yyyy")===date){
            sumP3= sumP2+Plist3.get(l).cena
            if(max < sumP3)
                max= sumP3
            l++
        }
        while(m<Plist4.count && Date.fromLocaleString(Qt.locale(),Plist4.get(m).datum,"dd.MM.yyyy").toLocaleString(Qt.locale(),"M.yyyy")===date){
            sumP4= sumP4+Plist4.get(m).cena
            if(max < -sumP4)
                max= -sumP4
            m++
        }
        barSet1.append(sumP1)
        barSet2.append(sumP2)
        barSet3.append(sumP3)
        barSet4.append(-sumP4)
        categories[i]= Date.fromLocaleString(Qt.locale(),date,"M.yyyy").toLocaleString(Qt.locale(),"MMM yyyy")
        sumP1= 0
        sumP2= 0
        sumP3= 0
        sumP4= 0
        currentMonth--
    }
    return {max: max, categories: categories}
}
