var popup = IR.GetPopup("Foutmelding");
var popupit = IR.GetPopup("Listitem");
var popitem = popupit.GetItem("ErrorItem");
var poplist = popup.GetItem("Item 1");
var pop2 = IR.GetPopup("Melding");
var pop2list = pop2.GetItem("Item 1");

var ErrCounter = 0;
var MeldingCounter = 0;

var countmel = IR.GetPage("Drivers").GetItem("Item 8");
var counterr = IR.GetPage("Drivers").GetItem("Item 9");

var Id = "";
var Params = "";

function addMelding(params, test)
{
   
   if(params == "Melding")
   {
   var Count2 = pop2list.ItemsCount;
   var id = 1;
      pop2list.CreateItem(Count2, 1, {Text: test, Image: "popup3_item7_state0_image_07E3020F0818211B7.png"});
      pop2list.CreateItem(Count2, 2, {Text: id}); 
      //IR.Log("send " + params + "  "+ id);
      MeldingCounter++;
      countmel.Text = MeldingCounter; 
      Count2 += 1;               
   } 
   else if(params == "Error")
   {
   //IR.Log(test);
   var Count = poplist.ItemsCount;
   var id = 0;
      poplist.CreateItem(Count, 1, {Text: test, Image: "popup3_item6_state0_image_07E3020F0818211A8.png"});
      poplist.CreateItem(Count, 2, {Text: id}); 
      //IR.Log("Send " + params + "  " + id);
      ErrCounter++;
      counterr.Text = ErrCounter;
      Count += 1;        
   }   
   
}


IR.AddListener(IR.EVENT_ITEM_PRESS, IR.GetPage("Landscape").GetItem("Item 13"),function()
{  
   var testmelding = "Modbus has found a warning";
   addMelding("Error", testmelding);			
})



IR.AddListener(IR.EVENT_ITEM_SELECT, IR.GetItem("Foutmelding").GetItem("Item 1"), function(Id) {
  
   ErrCounter--;   
    
   counterr.Text = ErrCounter;
    
   if(poplist.ItemsCount <= 1)
   {
      ErrCounter = 0;
      counterr.Text = ErrCounter;
   }
   
   poplist.DeleteItem(Id);     
})

IR.AddListener(IR.EVENT_ITEM_SELECT, IR.GetItem("Melding").GetItem("Item 1"), function(Id) {
  
   MeldingCounter--; 
   
   countmel.Text = MeldingCounter;
   
  if(pop2list.ItemsCount <= 1)
   { 
   MeldingCounter = 0;
   countmel.Text = MeldingCounter;
   }

   pop2list.DeleteItem(Id);
   
})