function createMenu(parent) {
	var y=GetDomOffset(document.getElementById('mkmenu'),'offsetTop');
	var x=GetDomOffset(document.getElementById('mkmenu'),'offsetLeft');
	var zone = document.createElement('div');
	zone.id='newMenu';zone.style.height='126px';zone.style.width='300px';zone.style.overflow='auto';zone.style.display='block';zone.style.position='absolute';zone.style.padding='1px';zone.style.borderTop='1px solid #DCDCDC';zone.style.borderRight='1px solid #A9A9A9';zone.style.borderBottom='1px solid #808080';zone.style.borderLeft='1px solid #A9A9A9';zone.style.backgroundColor='#FFF';zone.style.color="#000000";zone.style.left=(x-5)+'px';zone.style.top=(y-100)+'px';
	zone.innerHTML='<div style="text-align:right;"><img id="exit" alt="X" src="../images/stop.png"/></div><form>&nbsp;Veuillez indiquer le nom du menu a créer :<br/><br/>&nbsp;<input type="text" id="MenuTitreTag" name="titre" maxlength="128" /><br/><br/>&nbsp;&nbsp;&nbsp;<input type="button" id="buttonCreateMenu" value="Créer le menu" /></form>';
	document.body.appendChild(zone);
	document.getElementById('exit').onclick=function() { document.body.removeChild(zone); }
	document.getElementById('buttonCreateMenu').onclick=function() {
		var xhr = getXMLHTTP();
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
				var rep = xhr.responseText;
				alert(rep);
				if (rep=='Menu ajouté avec succès') {
					document.getElementById('waySelector').innerHTML='';
					var xhrq = getXMLHTTP();
					xhrq.onreadystatechange = function(){ if(xhrq.readyState == 4 && xhrq.status == 200){ document.getElementById('waySelector').innerHTML= xhrq.responseText; } }
					xhrq.open("POST","adm/menus/displaymenus.php",true);
					xhrq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					xhrq.send("parent="+parent);
				}
			}
		}
		xhr.open("POST","adm/menus/newmenu.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		var titre = document.getElementById('MenuTitreTag').value;
		xhr.send("titre="+titre+"&parent="+parent);
		document.body.removeChild(zone);
	}
}

function choixMenu(ID) {
	var way = document.getElementById('chemin').value;
	document.getElementById('chemin').value = way+'/'+ID;
	var xhrq = getXMLHTTP();
	xhrq.onreadystatechange = function(){ if(xhrq.readyState == 4 && xhrq.status == 200){ document.getElementById('waySelector').innerHTML= xhrq.responseText; } }
	xhrq.open("POST","adm/menus/displaymenus.php",true);
	xhrq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrq.send("parent="+ID);
}

function upToParent() {
	var way = document.getElementById('chemin').value;
	var parents = way.substr(0,(way.lastIndexOf('/')));
    var nearparent = parents.substr(parents.lastIndexOf('/')+1,(parents.length-(parents.lastIndexOf('/')+1)));
	document.getElementById('chemin').value = parents;
	var xhrq = getXMLHTTP();
	xhrq.onreadystatechange = function(){ if(xhrq.readyState == 4 && xhrq.status == 200){ document.getElementById('waySelector').innerHTML= xhrq.responseText; } }
	xhrq.open("POST","adm/menus/displaymenus.php",true);
	xhrq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrq.send("parent="+nearparent);
}

function downmenu(id) {
    var xhrq = getXMLHTTP();
	xhrq.onreadystatechange = function(){ if(xhrq.readyState == 4 && xhrq.status == 200){ document.getElementById('waySelector').innerHTML= xhrq.responseText; } }
	xhrq.open("POST","adm/menus/movedownmenu.php",true);
	xhrq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrq.send("id="+id);
}

function upmenu(id) {
    var xhrq = getXMLHTTP();
	xhrq.onreadystatechange = function(){ if(xhrq.readyState == 4 && xhrq.status == 200){ document.getElementById('waySelector').innerHTML= xhrq.responseText; } }
	xhrq.open("POST","adm/menus/moveupmenu.php",true);
	xhrq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrq.send("id="+id);
}

function moveparentmenu(id) {
    var xhrq = getXMLHTTP();
	xhrq.onreadystatechange = function(){ if(xhrq.readyState == 4 && xhrq.status == 200){ document.getElementById('waySelector').innerHTML= xhrq.responseText; } }
	xhrq.open("POST","adm/menus/moveparentmenu.php",true);
	xhrq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrq.send("id="+id);
}

function movechildmenu(id) {
    var y=GetDomOffset(document.getElementById('waySelector'),'offsetTop');
	var x=GetDomOffset(document.getElementById('waySelector'),'offsetLeft');
	var zone = document.createElement('div');
	zone.id='mvMenu';zone.style.width='300px';zone.style.overflow='auto';zone.style.display='block';zone.style.position='absolute';zone.style.padding='1px';zone.style.borderTop='1px solid #DCDCDC';zone.style.borderRight='1px solid #A9A9A9';zone.style.borderBottom='1px solid #808080';zone.style.borderLeft='1px solid #A9A9A9';zone.style.backgroundColor='#FFF';zone.style.color="#000000";zone.style.left=x+'px';zone.style.top=y+'px';
    document.body.appendChild(zone);
    var xhr = getXMLHTTP();
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4 && xhr.status == 200){
            var rep = xhr.responseText;
            zone.innerHTML=rep;
            document.getElementById('exit').onclick=function() { document.body.removeChild(zone); }
        }
    }
    xhr.open("POST","adm/menus/lightdisplay.php",true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send("id="+id);
}

function moveaction(id,newparent) {
    var xhrq = getXMLHTTP();
	xhrq.onreadystatechange = function(){ if(xhrq.readyState == 4 && xhrq.status == 200){ document.getElementById('waySelector').innerHTML= xhrq.responseText; } }
	xhrq.open("POST","adm/menus/movechildmenu.php",true);
	xhrq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrq.send("id="+id+"&newparent="+newparent);
}

function deletemenu(id) {
    var r=confirm("Êtes-vous sur de vouloir supprimer ce menu ? Si celui-ci comporte des enfants, ceux-ci sont conservés mais deviendrons caduques.");
    if (r==true) {
        var xhrq = getXMLHTTP();
        xhrq.onreadystatechange = function(){ if(xhrq.readyState == 4 && xhrq.status == 200){ document.getElementById('waySelector').innerHTML= xhrq.responseText; } }
        xhrq.open("POST","adm/menus/deletemenu.php",true);
        xhrq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhrq.send("id="+id);
    }
}

function editmenu(id) {
    var name=prompt("Veuillez saisir le nouveau nom du menu");
    if (name!=null && name!="") {
        var xhrq = getXMLHTTP();
        xhrq.onreadystatechange = function(){ if(xhrq.readyState == 4 && xhrq.status == 200){ document.getElementById('waySelector').innerHTML= xhrq.responseText; } }
        xhrq.open("POST","adm/menus/editmenu.php",true);
        xhrq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhrq.send("id="+id+"&name="+name);
    }
}
