// JavaScript Document

function ajax(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
	} 
	catch (e){
		// Internet Explorer Browsers
		try{
    		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
    		try{
        		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		catch (e){
          		// Something went wrong
           		alert("Din browser stoppede uventet.");
           		return false;
        	}
		}
	}
	return ajaxRequest;
}

function makeUserDetailFields() {
  var url = "core.php?module=userdetails";
  var ajaxR;
  ajaxR = ajax();
    ajaxR.onreadystatechange = function(){
      if(ajaxR.readyState == 4){ //Making sure the ajax function is ready for action
        document.getElementById('userdetails').innerHTML = ajaxR.responseText;
      }  
    }
  ajaxR.open("GET", url, true);
  ajaxR.send(null);
}

function editedValible(arg) { 
		arg.style.backgroundColor = '#eef451';
		arg.id = 'update';
		if (arg.type=="checkbox" && arg.checked) {arg.value = 1}
		else if (arg.type=="checkbox" && !arg.checked) {arg.value = 0}
}

function submitForm() {
  var i = 0;
  var str = "";
  document.getElementById('result').innerHTML = '';
  for (i=0; i<document.allusers.length-1;i++) {
	if(document.allusers[i].id == 'update') {
	  ajaxFunction(document.allusers[i].name,document.allusers[i].value);
	  document.allusers[i].id = '';
	  document.allusers[i].style.backgroundColor = '#abec7c';
	}
  }
}
	
function ajaxFunction(name,value){
var xmlHttp = new ajax();
xmlHttp.onreadystatechange=function()
{
	if(xmlHttp.readyState==4)
	{
		document.getElementById('result').innerHTML += xmlHttp.responseText;
	document.getElementById(name).innerHTML += xmlHttp.responseText;
	}
	if(xmlHttp.readyState==0)
	{
		document.getElementById('result').innerHTML += 'Kontakter server<br />';
	}
  }
xmlHttp.open('GET','deltagerHandler.php?name=' + name + '&value=' + value ,true);
xmlHttp.send('none');
}

function addmenu(text,icon) {
	
	var xmlHttp = false;
	var xmlHttp = new ajax();
	xmlHttp.onreadystatechange=function()
	{
	if(xmlHttp.readyState==4)
	{
		window.location.reload();
	}
	if(xmlHttp.readyState==0)
	{
		document.getElementById('userDetails_' + uid).innerHTML += 'Kontakter server<br />';
	}
	  }
		
	xmlHttp.open('GET','updatemenu.php?text=' + text + '&icon=' + icon + '&module=addmenu' ,true);
	xmlHttp.send('none');
}

function addsubmenu(arg) {

	id = arg.id.split("_")[1];
	
	var xmlHttp = false;
	var xmlHttp = new ajax();
	xmlHttp.onreadystatechange=function()
	{
	if(xmlHttp.readyState==4)
	{
		document.getElementById('submenu_' + id).innerHTML = xmlHttp.responseText;
	}
	if(xmlHttp.readyState==0)
	{
		document.getElementById('result').innerHTML += 'Kontakter server<br />';
	}
	  }
		
	xmlHttp.open('GET','updatemenu.php?text=' + arg.value + '&id=' + id + '&module=addsubmenu' ,true);
	xmlHttp.send('none');
}

function deletemenu(id) {

	var xmlHttp = false;
	var xmlHttp = new ajax();
	xmlHttp.onreadystatechange=function()
	{
	if(xmlHttp.readyState==4)
	{
		id = id.split("s")[0];
		document.getElementById('submenu_' + id).innerHTML = xmlHttp.responseText;	
	}
	if(xmlHttp.readyState==0)
	{
		document.getElementById('result').innerHTML = 'Kontakter server<br />';
	}
	  }
		
	xmlHttp.open('GET','updatemenu.php?id=' + id + '&module=deletemenu' ,true);
	xmlHttp.send('none');
}

function showSubMenu(id) {
	document.getElementById('menu_' + id).style.display = 'inline';
	document.getElementById('showSubMenu_' + id).innerHTML = "<img src='gfx/icons/foldermove22.png' onclick=\"hideSubMenu('" + id + "');\" onmouseover=\"this.style.cursor= 'pointer';\" />";
}

function hideSubMenu(id) {
	document.getElementById('menu_' + id).style.display = 'none';
	document.getElementById('showSubMenu_' + id).innerHTML = "<img src='gfx/icons/folderopenb22.png' onclick=\"showSubMenu('" + id + "');\" onmouseover=\"this.style.cursor= 'pointer';\" />";
}
	
