var arrPrivilegiosSucursales = new Array; 

function TipoInstitucion(tipo)
{
  if (tipo.toLowerCase() == 'municipalidad')
  {
    document.getElementById('td_comuna').style.display = 'block';
    document.getElementById('td_nombre').style.display = 'none';    
    
    document.getElementById('td_comuna').style.width = "500px";    
    document.getElementById('td_nombre').style.width = "0px";
    
  }
  else
  {
    document.getElementById('td_comuna').style.display = 'none';
    document.getElementById('td_nombre').style.display = 'block';    
    
    document.getElementById('td_comuna').style.width = "0px";    
    document.getElementById('td_nombre').style.width = "500px";
    
  
  }
}

function PrivilegioSucursal()
{
  var i; 
  var privilegio = document.getElementById("persona_privilegio"); 
  var sucursal = document.getElementById("persona_sucursal"); 
  var capa = document.getElementById("sucursalObligatoria");
  if (privilegio.selectedIndex==0)
  {
    capa.style.display = "display";
    sucursal.disabled = false; 
  }
  else
  {
    for (i in arrPrivilegiosSucursales)
    {
      if (privilegio[privilegio.selectedIndex].value == arrPrivilegiosSucursales[i]["id"])
      {
        sucursal.selectedIndex = 0; 
        sucursal.disabled = !arrPrivilegiosSucursales[i]["sucursal"]; 
        if (sucursal.disabled)
        {
          capa.style.display = "none";
        }
        else
        {
          capa.style.display = "block";
        }
      }
    }
  }
}

function OtorgarPrivilegioInstitucion()
{
  var error = false; 
  var rut = document.getElementById('persona_rut'); 
  var rut_inst = document.getElementById('persona_rut_inst'); 
  var privilegio = document.getElementById('persona_privilegio'); 
  var sucursal = document.getElementById('persona_sucursal'); 
  if (Trim(rut.value)=="" && !error)
  {
    error = true;
    alert("Debe ingresar el rut de la persona"); 
    rut.focus();
  }
  if (!(ValidaRUTPersona("persona_rut", undefined)) && !error)
  {
    error = true;
    rut.focus();
  }
  if (privilegio.value==0 && !error)  
  {
    error = true; 
    alert("Debe seleccionar un privilegio para otorgar"); 
    privilegio.focus(); 
  }
  if (!sucursal.disabled)
  {
    if (sucursal.value==0 && !error)
    {
      error = true;
      alert("Debe seleccionar una sucursal"); 
      sucursal.focus(); 
    }
  }
  if (!error)
  {
    var URL = "/misinstituciones/privilegiosinstitucion";
    var Vars = "otorgar=true&rut="+rut.value+"&rut_inst="+rut_inst.value+"&privilegio="+privilegio[privilegio.selectedIndex].value+"&sucursal="+sucursal[sucursal.selectedIndex].value; 
    document.getElementById("btnOtorgarPrivilegio").disabled = true; 
    loadXMLDoc(URL, "POST", Vars, "PrivilegioInstitucionOtorgado"); 
  }
}

function PrivilegioInstitucionOtorgado(root)
{
  var rut = document.getElementById('persona_rut'); 
  var privilegio = document.getElementById('persona_privilegio'); 
  var sucursal = document.getElementById('persona_sucursal'); 
  if (Mozilla)
  {
    var inicio = 1; 
    var incremento = 2; 
  }
  else
  {
    var inicio = 0; 
    var incremento = 1; 
  }
  var status = root.childNodes[inicio].getAttribute("status"); 
  var reason = root.childNodes[inicio].getAttribute("reason"); 
  var exists = root.childNodes[inicio].getAttribute("exists");
  if (status=="true")
  {
    alert("El privilegio fue otorgado a la persona indicada");
    self.location.href="/misinstituciones/privilegios?rut="+root.childNodes[inicio].getAttribute("rut"); 
  }
  else
  {
    document.getElementById("btnOtorgarPrivilegio").disabled = false; 
    if (exists == "true")
    {
      rut.value = ""; 
      privilegio.selectedIndex = 0; 
      sucursal.selectedIndex = 0; 
      alert(reason); 
      rut.focus(); 
    }
    else
    {
      if (confirm("No existen personas registradas en el sistema con el rut indicado.\n\n\u00BFDesea registrar una nueva persona?"))
      {
        window.open('/registrese/nuevapersona?rut='+rut.value, '_Blank', 'width=640,height=480,resizable,scrollbars'); 
      }
      else
      {
	/*
        rut.value = ""; 
        privilegio.selectedIndex = 0; 
        sucursal.selectedIndex = 0; 
	*/
        rut.focus(); 
      }
    }
  }
  return true;
}

