var xValues = [];
var yCPUValues = [];
var yMemoryValues = [];
var yDiskValues = [];
var yNetworkInputValues = [];
var yNetworkOutputValues = [];
var g_balancer_menuOBj;
var g_blockchain_menuOBj;
var g_workfoldermenuOBj;
var g_cloud_menuOBj;
var g_api_httpport;

function draw_cpu_menory_disk()
{
	new Chart("CPUChart", {
	  type: "line",
	  data: {
	    labels: xValues,
	    datasets: [{ label: 'CPU (%)',
	      data: yCPUValues,
	      borderColor: "red",
	      fill: false
	    }, { label: 'Memory (%)',
	      data: yMemoryValues,
	      borderColor: "green",
	      fill: false
	    }, { label: 'Disk (%)',
	      data: yDiskValues,
	      borderColor: "blue",
	      fill: false
	    }]
	  },
	  options: {
	    title: {
	        display: true,
	        text: 'System Usage'
	      },
	    legend: {display: true},
    	    scales: {
            xAxes: [{
                afterTickToLabelConversion: function(data){
                    var xLabels = data.ticks;
                    xLabels.forEach(function (labels, i) {
                        if (i % 10 !=0){
                            xLabels[i] = '';
                        }
                    });
                } 
            }],
	            yAxes: [{
	            display: true,
	            ticks: {
	                suggestedMin: 0,    // minimum will be 0, unless there is a lower value.
	                // OR //
	                beginAtZero: true   // minimum value will be 0.
	            }
	            }]      
         }
	  }
	});

}
function fill_cpu_menory_disk()
{
	xValues.length = 0;
	yCPUValues.length = 0;
	yMemoryValues.length = 0;
	yDiskValues.length = 0;
	yNetworkInputValues.length = 0;
	yNetworkOutputValues.length = 0;
	for (x=0;;x++) {
	    try {
	    	//alert(myObj.results[x].memory);
	    	xValues.push(JSONSystemInfo.results[x].timestamp);
	    	yCPUValues.push(JSONSystemInfo.results[x].cpu);
	    	yMemoryValues.push(eval(JSONSystemInfo.results[x].memory));
	    	yDiskValues.push(JSONSystemInfo.results[x].disk);
	    	yNetworkInputValues.push(JSONSystemInfo.results[x].down_bw);
	    	yNetworkOutputValues.push(JSONSystemInfo.results[x].up_bw);
	    	
	    } catch(err)
	    {
	    	break;
	    }
	    
	}
}
function draw_network()
{
	new Chart("NetworkChart", {
	  type: "line",
	  	
	     
	  data: {
	    labels: xValues,
	    
	    datasets: [{ label: 'Output (Mbps)',
	      data: yNetworkInputValues,
	      borderColor: "red",
	      fill: false
	    }, { label: 'Input (Mbps)',
	      data: yNetworkOutputValues,
	      borderColor: "blue",
	      fill: false
	    }]
	  },
	  options: {
	    title: {
	        display: true,
	        text: 'Network Bandwidth'
	      },
	    legend: {display: true},
	    scales: {
	            xAxes: [{
	                afterTickToLabelConversion: function(data){
	                    var xLabels = data.ticks;
	                    xLabels.forEach(function (labels, i) {
	                        if (i % 10 !=0){
	                            xLabels[i] = '';
	                        }
	                    });
	                } 
	            }],
	            yAxes: [{
	            display: true,
	            ticks: {
	                suggestedMin: 0,    // minimum will be 0, unless there is a lower value.
	                // OR //
	                beginAtZero: true   // minimum value will be 0.
	            }
	            }]   
                 }
	  }
	});

}
function CloseMenu() {
	
	 var y=document.getElementById("triplebar");
	 y.classList.toggle("change");
  var x = document.getElementById("ezcontent");
  if (x.style.display === "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
 }
function Mobile_Button_Get_Stream_Provider()
{
	CloseMenu();
	Button_Get_Stream_provider();
}
 function Mobile_callServer_System_Inquery()
{
	CloseMenu();
	callServer_System_Inquery();
}
function Mobile_Button_add_user()
{
	CloseMenu();
	Button_add_user();
}
function Mobile_Button_add_android_user()
{
	CloseMenu();
	Button_add_android_user();
}
function Mobile_Button_add_mag_user()
{
	CloseMenu();
	Button_add_mag_user();
}
function Mobile_Button_add_active_code()
{
	CloseMenu();
	Button_add_active_code();
}
			

function Mobile_Button_get_all_user()
{
	CloseMenu();
	Button_get_all_user();
}
function Mobile_Button_Add_New_Group()
{
	CloseMenu();
	Button_Add_New_Group()();
}
function Mobile_Button_Server_group_Inquery()
{
	CloseMenu();
	Button_Server_group_Inquery();
}
function Mobile_Button_Add_New_Reseller()
{
	CloseMenu();
	Button_Add_New_Reseller();
}
function Mobile_Button_get_reseller()
{
	CloseMenu();
	Button_get_reseller();
}
function Mobile_Button_Add_New_CH()
{
	CloseMenu();
	Button_Add_New_CH();
}

function Mobile_Button_Add_New_Created_CH()
{
	CloseMenu();
	Button_Add_New_Created_CH();
}
function Mobile_Button_Server_CH_Inquery()
{
	CloseMenu();
	Button_Server_CH_Inquery();
}
function Mobile_Button_Server_EPG_Inquery()
{
	CloseMenu();
	Button_Server_EPG_Inquery();
}
function Mobile_Button_Server_XMLTV_Inquery()
{
	CloseMenu();
	Button_Server_XMLTV_Inquery();
}
function Mobile_Button_Add_New_Movie()
{
	CloseMenu();
	Button_Add_New_Movie();
}
function Mobile_Button_Upload_Movies()
{
	CloseMenu();
	Button_Upload_Movies();
}

function Mobile_Button_Server_Movie_Inquery()
{
	CloseMenu();
	Button_Server_Movie_Inquery();
}
function Mobile_Button_Add_New_Series()
{
	CloseMenu();
	Button_Add_New_Series();
}
function Mobile_Button_Server_Series_Inquery()
{
	CloseMenu();
	Button_Server_Series_Inquery();
}
function Mobile_Button_Add_New_balancer()
{
	CloseMenu();
	Button_Add_New_balancer();
}
function Mobile_Button_Server_balancer_Inquery()
{
	CloseMenu();
	Button_Server_balancer_Inquery();
}
function Mobile_Button_Server_blockchain_Inquery()
{
	CloseMenu();
	Button_Server_blockchain_Inquery();
}

function Mobile_Button_player_filter()
{
	CloseMenu();
	Button_player_filter();
}
function Mobile_Button_Server_blacklist_Inquery()
{
	CloseMenu();
	Button_Server_blacklist_Inquery();
}

function Mobile_Button_Get_Work_Folder()
{
	CloseMenu();
	Button_Get_Work_Folder();
}



function Mobile_Button_Get_Channel_Statistics()
{
	CloseMenu();
	Button_Get_Channel_Statistics();
}
function Mobile_Button_Get_User_Log()
{
	CloseMenu();
	Button_Get_User_Log();
}

function Mobile_Button_Setting()
{
	CloseMenu();
	Button_Setting();
}
function Mobile_Button_Manage_Profile()
{
	CloseMenu();
	Button_Manage_Profile();
}
function Mobile_Button_Get_Activity_Log()
{
	CloseMenu();
	Button_Get_Activity_Log();
}


function mySetting(y) {
  y.classList.toggle("change");
  var x = document.getElementById("ezcontent");
  if (x.style.display === "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
 }
function open_submenu(event) {
  event.preventDefault(); // Prevents jumping

  const menuItem = event.target;
  const submenu = menuItem.parentElement.querySelector(".submenu");

  if (submenu) {
    // Close all other submenus
    document.querySelectorAll(".submenu").forEach((menu) => {
      if (menu !== submenu) {
        menu.classList.remove("show");
      }
    });

    // Toggle current submenu
    submenu.classList.toggle("show");
  }
}


function Init_Config()
{
  var login_status = document.getElementById("login_status");
  var iconwidth=25;
  var iconheight=25;

   if (xmlHttp.readyState == 4) {
   	
   if (xmlHttp.status == 200)
   {
	var response= xmlHttp.responseText.split("\r\n");
    	var main_screen= document.getElementById("main_screen");
	var str;
	str='';
	str+='<div id="eztitle">';
	str+='<table align="center" width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">';
	str+='<tr>';
	str+='<td id=version_id style="text-indent: 20;"><font face="Arial" color="#ffffff" size="6">Ezserver v7.0</font>';
	str+='</td>';
	str+='<td id=login_count_text>';
  	str+='</td>';
	
	str+='<td id=menu_title_text>';
  	str+='</td>';
  
	
	if(bMobileDevice){
		
	str+='<td align="right" style="padding-right: 100px;">';
		str+='<div class="menucontainer" id=triplebar onclick="mySetting(this)">';
		  str+='<div class="bar1"></div>';
		  str+='<div class="bar2"></div>';
		  str+='<div class="bar3"></div>';
		str+='</div>';
		str+='<a style="margin-left:100;" href="javascript:logout()"><img border="0"  style="margin-right: 30;" src="/admin/logout.png" width="'+70+'" height="'+60+'"></a>';
		
	}else
	{
		str+='<td align="right">';
	 	str+='<a href="javascript:callServer_System_Inquery()"><img border="0" src="/admin/home.PNG" width="'+iconwidth+'" height="'+iconheight+'"></a>&nbsp; &nbsp;';
		str+='<a href="javascript:Button_shutdown()"><img border="0" src="/admin/shutdown.PNG" width="'+iconwidth+'" height="'+iconheight+'"></a>&nbsp; &nbsp;';
		str+='<a href="javascript:Button_Setting()"><img border="0" src="/admin/setting.PNG" width="'+iconwidth+'" height="'+iconheight+'"></a>&nbsp; &nbsp;';
		str+='<a href="javascript:logout()"><img border="0"  style="margin-right: 30;" src="/admin/logout.png" width="'+iconwidth+'" height="'+iconheight+'"></a>';
	}
 	str+='</td>';
	str+='</tr>';
	str+='</table>';
	str+='</div>';

                    
       
  	str+='<div id="ezcontent">';
	if(bMobileDevice){
		
		
		  str+='<nav class="navigation">';
		  str+='<ul class="mainmenu">';
		    str+='<li><a href="javascript:Mobile_callServer_System_Inquery()">DASHBOARD</a></li>';
	 	 str+='<li><a href="#" onclick="open_submenu(event)">MANAGEMENT</a>';
		      str+='<ul class="submenu">';
      		        str+='<li><a href="javascript:Mobile_Button_Manage_Profile()">Manage Profile</a></li>';
		       str+='<li><a href="javascript:Mobile_Button_player_filter()">USER AGENT</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Server_blacklist_Inquery()">BLACKLIST</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Get_Work_Folder()">Work Folder</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Get_Stream_Provider()">Stream Provider</a></li>';
		        
		        str+='<li><a href="javascript:Mobile_Button_Get_Activity_Log()">Activity LOG</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Get_User_Log()">User LOG</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Get_Channel_Statistics()">STATISTICS</a></li>';
		        
		        str+='<li><a href="javascript:Mobile_Button_Setting()">SETTING</a></li>';
		      str+='</ul>';
		    str+='</li>';   
    		   
		    
		    str+='<li><a href="#" onclick="open_submenu(event)">USERS</a>';
		      str+='<ul class="submenu">';
			str+='<li><a href="javascript:Mobile_Button_add_user()" >ADD USER</a></li>';
			str+='<li><a id=add_android_user_button href="javascript:Mobile_Button_add_android_user()">Add Android Device</a></li>';
			str+='<li><a id=add_mag_user_button href="javascript:Mobile_Button_add_mag_user()">Add MAG STB</a></li>';
			str+='<li><a id=add_active_code_button href="javascript:Mobile_Button_add_active_code()">Add Active Code</a></li>';
			
			str+='<li><a href="javascript:Mobile_Button_get_all_user()">MANAGE USERS</a></li>';
		      str+='</ul>';
		    str+='</li>';
		   
		    str+='<li><a href="#" onclick="open_submenu(event)">PACKAGES</a>';
		      str+='<ul class="submenu">';
		        str+='<li><a href="javascript:Mobile_Button_Add_New_Group()">ADD PACKAGE</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Server_group_Inquery()">MANAGE PACKAGES</a></li>';
		        
		      str+='</ul>';
		    str+='</li>';
		    str+='<li><a href="#" onclick="open_submenu(event)">CHANNELS</a>';
		      str+='<ul class="submenu">';
		        str+='<li><a href="javascript:Mobile_Button_Add_New_CH()">ADD CHANNEL</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Add_New_Created_CH()">ADD CREATED CHANNEL</li></a>';
		      
		        str+='<li><a href="javascript:Mobile_Button_Server_CH_Inquery()">MANAGE CHANNELS</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Server_XMLTV_Inquery()">MANAGE XMLTV</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Server_EPG_Inquery()">MANAGE EPG</a></li>';
		        
		      str+='</ul>';
		    str+='</li>';
		    str+='<li><a href="#" onclick="open_submenu(event)">MOVIES</a>';
		      str+='<ul class="submenu">';
		        str+='<li><a href="javascript:Mobile_Button_Add_New_Movie()">ADD MOVIE</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Upload_Movies()">UPLOAD MOVIES</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Server_Movie_Inquery()">MANAGE MOVIES</a></li>';
		      str+='</ul>';
		    str+='</li>';
		    str+='<li><a href="#" onclick="open_submenu(event)">SERIES</a>';
		      str+='<ul class="submenu">';
		        str+='<li><a href="javascript:Mobile_Button_Add_New_Series()">ADD SERIES</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Server_Series_Inquery()">MANAGE SERIES</a></li>';
		      str+='</ul>';
		    str+='</li>';
		     
		     str+='<li id=balancermenu><a href="#" onclick="open_submenu(event)">BALANCERS</a>';
		      str+='<ul class="submenu">';
		         str+='<li><a href="javascript:Mobile_Button_Server_balancer_Inquery()">MANAGE SERVERS</a></li>';
		      str+='</ul>';
		    str+='</li>';
		    /*
		    str+='<li id=balancermenu><a href="#" onclick="open_submenu(event)">BALANCERS</a>';
		      str+='<ul class="submenu">';
		        str+='<li><a href="javascript:Mobile_Button_Add_New_balancer()">INSTALL SERVER</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Server_balancer_Inquery()">MANAGE SERVERS</a></li>';
		      str+='</ul>';
		    str+='</li>';
		    */
		   /* str+='<li id=blockchainmenu><a >CONNECTION</a>';
		      str+='<ul class="submenu">';
		        str+='<li><a href="javascript:calllist_auto_active_player_info()">MANAGE CONNECTION</a></li>';
		        str+='<li><a href="javascript:Button_alert_player_info()">MANAGE ALERT</a></li>';
		        str+='<li><a href="javascript:Mobile_Button_Server_blockchain_Inquery()">MANAGE BLOCKCHAIN</a></li>';
		      str+='</ul>';
		    str+='</li>';
		    */

	    
		  str+='</ul>';
		str+='</nav>';
		
	}else
	{
		str+='<div class="navbar" style="padding-left: 100px;">';
	
		// Dashboard
		str+='<div class="menudropdown">';
		    str+='<button class="dropbtn" onclick="callServer_System_Inquery()" ><img  src="/admin/home.PNG" align="texttop"  width=25 height=25></img> DashBoard '; 
		    str+='</button>';
		str+='</div>'; 
		// User
		str+='<div class="menudropdown">';
		    str+='<button class="dropbtn"><img src="/admin/user.PNG" align="texttop" width=25 height=25></img> Users '; 
		    str+='<i class="fa fa-caret-down"></i>';
		    str+='</button>';
		    str+='<div class="menudropdown-content">';
		      str+='<a id=add_user_button href="javascript:Button_add_user()">Add User</a>';
		      str+='<a id=add_android_user_button href="javascript:Button_add_android_user()">Add Android Device</a>';
		      str+='<a id=add_mag_user_button href="javascript:Button_add_mag_user()">Add MAG STB</a>';
		      str+='<a id=add_active_code_button href="javascript:Button_add_active_code()">Add Active Code</a>';
		      str+='<a href="javascript:Button_get_all_user()">Manage Users</a>';
		    str+='</div>';
		str+='</div>'; 
		
		// PACKAGEs
		str+='<div class="menudropdown">';
		    str+='<button class="dropbtn"><img src="/admin/user.PNG" align="texttop" width=25 height=25></img> Packages '; 
		    str+='<i class="fa fa-caret-down"></i>';
		    str+='</button>';
		    str+='<div class="menudropdown-content">';
		      
		      str+='<a href="javascript:Button_Add_New_Group()">Add Package</a>';
		      str+='<a href="javascript:Button_Server_group_Inquery()">Manage Packages</a>';
		    str+='</div>';
		str+='</div>'; 
		// Channel
		str+='<div class="menudropdown">';
		    str+='<button class="dropbtn"><img src="/admin/channel.PNG" align="texttop" width=25 height=25></img> Channels '; 
		    str+='<i class="fa fa-caret-down"></i>';
		    str+='</button>';
		    str+='<div class="menudropdown-content">';
		      str+='<a href="javascript:Button_Add_New_CH()">Add Channel</a>';
		      str+='<a href="javascript:Button_Add_New_Created_CH()">Add Created Channel</a>';
		      str+='<a href="javascript:Button_Server_CH_Inquery()">Manage Channels</a>';
		      str+='<a href="javascript:Button_Server_XMLTV_Inquery()">Manage XMLTV</a>';
		      str+='<a href="javascript:Button_Server_EPG_Inquery()">Manage EPG</a>';
		      str+='</div>';
		str+='</div>'; 
		// Movie
		str+='<div class="menudropdown">';
		    str+='<button class="dropbtn"><img src="/admin/movie.PNG" align="texttop" width=25 height=25></img> Movies '; 
		    str+='<i class="fa fa-caret-down"></i>';
		    str+='</button>';
		    str+='<div class="menudropdown-content">';
		      str+='<a href="javascript:Button_Add_New_Movie()">Add Movie</a>';
		      str+='<a href="javascript:Button_Upload_Movies()">Upload Movies</a>';
		      str+='<a href="javascript:Button_Server_Movie_Inquery()">Manage Movies</a>';
		    str+='</div>';
		str+='</div>'; 
		// Series
		str+='<div class="menudropdown">';
		    str+='<button class="dropbtn"><img src="/admin/movie.PNG" align="texttop" width=25 height=25></img> Series '; 
		    str+='<i class="fa fa-caret-down"></i>';
		    str+='</button>';
		    str+='<div class="menudropdown-content">';
		      str+='<a href="javascript:Button_Add_New_Series()">Add Series</a>';
		      str+='<a href="javascript:Button_Server_Series_Inquery()">Manage Series</a>';
		    str+='</div>';
		str+='</div>'; 
		
		// Balancers
		str+='<div id=balancermenu class="menudropdown">';
		    str+='<button id=balancerbutton class="dropbtn"><img src="/admin/balancer.PNG" align="texttop" width=25 height=25></img> Balancers '; 
		    str+='<i class="fa fa-caret-down"></i>';
		    str+='</button>';
		    str+='<div class="menudropdown-content">';
		      str+='<a href="javascript:Button_Add_New_balancer()">Install Server</a>';
		      str+='<a href="javascript:Button_Server_balancer_Inquery()">Manage Servers</a>';
		    str+='</div>';
		str+='</div>'; 
		// Blockchain
		/*str+='<div id=blockchainmenu class="menudropdown">';
		    str+='<button class="dropbtn"><img src="/admin/balancer.PNG" align="texttop" width=25 height=25></img> Connection '; 
		    str+='<i class="fa fa-caret-down"></i>';
		    str+='</button>';
		    str+='<div class="menudropdown-content">';
		      str+='<a href="javascript:calllist_auto_active_player_info()">Manage Connection</a>';
		      str+='<a href="javascript:Button_alert_player_info()">Manage Alert</a>';
		      str+='<a href="javascript:Button_Server_blockchain_Inquery()">Manage Node</a>';
		    str+='</div>';
		str+='</div>'; 
		*/
		
		// Management
		str+='<div class="menudropdown">';
		    str+='<button class="dropbtn"><img src="/admin/setting.PNG" align="texttop" width=25 height=25></img> Management '; 
		    str+='<i class="fa fa-caret-down"></i>';
		    str+='</button>';
		    str+='<div class="menudropdown-content">';
		     str+='<a href="javascript:Button_Manage_Profile()">Profile</a>';
		      str+='<a href="javascript:Button_player_filter()">User Agent</a>';
		      str+='<a href="javascript:Button_Server_blacklist_Inquery()">Blacklist</a>';
		      str+='<a id=workfoldermenu href="javascript:Button_Get_Work_Folder()">Work Folder</a>';
		      str+='<a id=workfoldermenu href="javascript:Button_Get_Stream_provider()">Stream Provider</a>';
		      str+='<a href="javascript:Button_Get_Activity_Log()">Activity Log</a>';
		      str+='<a href="javascript:Button_Get_User_Log()">User Log</a>';
		      str+='<a href="javascript:Button_Get_Channel_Statistics()">Statistics</a>';
		      str+='<a href="javascript:Button_Setting()">Setting</a>';
		    str+='</div>';
		str+='</div>'; 
		
	}
	str+='</div>';	
	str+='<div id="ezhead" style=" padding-left: 120px;background-color:#F5F6F8"><font size=4><b>Dashboard</b></font>';
	str+='</div>';                              	
	str+='<div id="content" style=" padding-left: 120px; padding-right: 120px;background-color:#F5F6F8">';
	str+='</div>';                              
	str+='</div>';    
	main_screen.innerHTML=str;
	if(bMobileDevice){
       		document.getElementById("ezcontent").style.display = "none";
       		document.getElementById("eztitle").style.height="160";
       }
	g_balancer_menuOBj=document.getElementById("balancermenu");
	g_balancer_menuOBj.hidden=true;
	//g_blockchain_menuOBj=document.getElementById("blockchainmenu");
	//g_blockchain_menuOBj.hidden=true;
	g_cloud_menuOBj=document.getElementById("cloudmenu");
	
	g_workfoldermenuOBj=document.getElementById("workfoldermenu");
//	g_workfoldermenuOBj.style.display = "none";

	if (location.protocol == 'https:') {
		g_api_httpport=response[26];
		g_httpport=response[27];
	}else if (location.protocol == 'http:') {
		g_api_httpport=response[1];
		g_httpport=response[2];
		
	}
	call_user_authorization_mode();
   }else
   {
   	login_status.innerHTML='<p style="margin-left: 25; margin-top: 0"><font face="Arial" size="2" color="#ff0000"><b>Illegal administrator user.</b></font>';
   }
 
  }
}
