/* ====================================================================
                            Funciones para pantalla principal
   ====================================================================
 */

var itemActual;
var flag;

function verMenuPopUp(e,c,menu)
{
        itemActual=c.id;

	var m=document.getElementById("menuHOST");
	m.style.display='none';
	m=document.getElementById("menuVM");
	m.style.display='none';

	var m=document.getElementById(menu);
        m.style.left=(e.clientX-8);
	m.style.top=e.clientY-8+window.pageYOffset;
	m.style.display='';
	flag=true;
}

function botonDerechoHost(e)
{
    if (!e) var e = window.event;
    if (e.button==2) verMenuPopUp(e,this,"menuHOST");
    return false;
}

function botonDerechoVM(e)
{
    if (!e) var e = window.event;
    if (e.button==2) verMenuPopUp(e,this,"menuVM");
    return false;
}

function botonDerechoContextual(e)
{
    var c=this;
    var y=(e.clientY-findPosY(c)+window.pageYOffset);
    var x=(e.clientX-findPosX(c)+window.pageXOffset);
    if (y<=0 || y>=c.offsetHeight || x<=0 || x>=c.offsetWidth) c.style.display='none';
    return true;
}

function colapsar(c)
{
  var li = document.getElementById('menuPpalHijo'+c.id);
  if (li!=null) {if (li.style.display=='')  li.style.display='none'; else li.style.display='';}
  itemActual=c.id;
  admHost('ver');
}

function nuevaVM()
{
    cargarDatos("dispacher/nuevaVM",datos);
}

function admPool()
{
    cargarDatos("dispacher?form=Pool&accion=ver","datos");
}

function cargarArbol()
{
    cargarPaginaSincrona("dispacher?form=General&accion=verArbol","arbol");
    flag=false;
    var n=document.getElementById("nHosts").value;
    var i;
    var lista=document.getElementsByName("host");
    for(i=0;i<lista.length;i++) lista[i].onmousedown=botonDerechoHost;
    lista=document.getElementsByName("hijoshost");
    for(i=0;i<lista.length;i++) lista[i].style.display='none';
    lista=document.getElementsByName("vm");
    for(i=0;i<lista.length;i++) lista[i].onmousedown=botonDerechoVM;
}

function admVM(accion)
{
    var m=document.getElementById("menuVM");
    m.style.display='none';
    cargarDatos("dispacher?form=VM&accion="+accion+"&id="+itemActual.substr(2),"datos");
}

function verVM(c)
{   itemActual=c.id;
    var accion="ver";
    cargarDatos("dispacher?form=VM&accion="+accion+"&id="+itemActual.substr(2),"datos");
}

function cfgMenus()
{
    document.getElementById("menuHOST").onmouseout=botonDerechoContextual;
    document.getElementById("menuVM").onmouseout=botonDerechoContextual;
}

function iniPag()
{
    admPool();
    cargarArbol();
    cfgMenus();
}



/* ====================================================================
                            Funciones para pantalla Host
   ====================================================================
 */


function crearVM(idHost)
{
    cargarDatos("dispacher?form=Host&accion=crearVM","datos");
}

function crearNuevaVM()
{
    enviarFormulario("dispacher?form=VM&accion=nueva","formulario","datos");
}

function gestMenuHost(accion)
{
    var id=document.getElementById("id").value;
    enviarFormulario("dispacher?form=Host&accion="+accion+"&id="+id,"formulario","datos");
}

function gestMenuVM(accion)
{
    var id=document.getElementById("id").value;
    enviarFormulario("dispacher?form=VM&accion="+accion+"&id="+id,"formulario","datos");
}

function gestMenuPool(accion)
{
    enviarFormulario("dispacher?form=Pool&accion="+accion,"formulario","datos");
}

/* ====================================================================
                   Funciones para Menu contextual de Host
   ====================================================================
 */
function admHost(accion)
{
    var m=document.getElementById("menuHOST");
    m.style.display='none';
    var id=itemActual.substr(4);
    cargarDatos("dispacher?form=Host&accion="+accion+"&id="+id,"datos");
}