/* SUCURSALES */

var arrSucursales = new Array;
var arrRepresentantesLegales = new Array; 
var arrCargos = new Array; 
var arrTipoInstitucion = new Array; 
var soloSucursal = false;

function MuestraOcultaCapaSucursalesInstitucion(Valor)
{
  switch (Valor)
  {
    case "0" :
    {
      document.getElementById("solo_una_casa_matriz").style.display = "block"; 
      document.getElementById("varias_sucursales").style.display = "none"; 
      break;
    }
    case "1" :
    {
      document.getElementById("solo_una_casa_matriz").style.display = "none"; 
      document.getElementById("varias_sucursales").style.display = "block"; 
      break;
    }
    default :
    {
    }
  }
}

function MuestraOcultaCapaEsInteresPub(Objeto)
{
  var es = arrTipoInstitucion[Objeto.value]; 
  if (es)
  {
    document.getElementById("institucionprivada_es_interes_pub_1").checked = true; 
    document.getElementById("capa_es_interes_pub").style.display = "none"; 
  }
  else
  {
    document.getElementById("institucionprivada_es_interes_pub_2").checked = true; 
    document.getElementById("capa_es_interes_pub").style.display = "block"; 
  }
}

function ChequeaEIRL(RUT)
{
  var RUT = document.getElementById(RUT).value; 
  if (Trim(RUT)!="")
  {
    RUT = RUT.split("-"); 
    RUT = RUT[0]; 
    RUT = parseInt(replaceSubstring (RUT, ".", "")); 
		if(RUT <= 50000000)
		{
      document.getElementById("capa_eirl").style.display = "block";
		}
		else
		{
		  document.getElementById("capa_eirl").style.display = "none"; 
		}
  }
  else
  {
    document.getElementById("capa_eirl").style.display = "none"; 
  }  
}

function AgregarNuevaSucursalInstitucion()
{
  var id = document.getElementById("sucursal_id_aux");
  if (soloSucursal && id.value=="")
  {
    alert("No posee privilegios para agregar nuevas sucursales");
  }
  else
  {
    var filter=/^[0-9]+$/;
    var calle = document.getElementById("sucursal_calle_aux"); 
    var numero = document.getElementById("sucursal_numero_aux"); 
    var departamento = document.getElementById("sucursal_departamento_aux"); 
    var id_comuna = document.getElementById("sucursal_id_comuna_aux");     
    var area_fijo = document.getElementById("sucursal_area_fijo_aux"); 
    var fono_fijo = document.getElementById("sucursal_fono_fijo_aux"); 
    var fono_movil = document.getElementById("sucursal_fono_movil_aux"); 
    var area_fax = document.getElementById("sucursal_area_fax_aux"); 
    var fono_fax = document.getElementById("sucursal_fono_fax_aux");
    var error = false; 

    if (!error && Trim(calle.value)=="")
    {
      alert("Debe ingresar la calle de la direcci\u00f3n de la sucursal");
      calle.focus();
      error = true;
    }
    if (!error && id_comuna[id_comuna.selectedIndex].value == 0)
    {
      alert("Debe ingresar la comuna de la direcci\u00f3n de la sucursal");
      id_comuna.focus();
      error = true;
    }
    if (!error && Trim(area_fijo.value)=="" && Trim(fono_fijo.value)=="" && Trim(fono_movil.value)=="" && Trim(area_fax.value)=="" && Trim(fono_fax.value) == "")
    {
      alert("Debe ingresar al menos un tel\u00e9fono");
      area_fijo.focus();
      error = true;
    }
    else
    {
      if (!error && Trim(area_fijo.value)=="" && Trim(fono_fijo.value)!="")
      {
        alert("Debe ingresar el \u00e1rea del tel\u00e9fono fijo de la sucursal");
        area_fijo.focus();
        error = true;
      }
      if (!error && Trim(area_fijo.value)!="" && Trim(fono_fijo.value)=="")
      {
        alert("Debe ingresar el tel\u00e9fono fijo de la sucursal");
        fono_fijo.focus();
        error = true;
      }
      if (!error && Trim(area_fax.value)=="" && Trim(fono_fax.value)!="")
      {
        alert("Debe ingresar el \u00e1rea del tel\u00e9fono fax de la sucursal");
        area_fax.focus();
        error = true;
      }
      if (!error && Trim(area_fijo.value)!="" && Trim(fono_fijo.value)=="")
      {
        alert("Debe ingresar el tel\u00e9fono fax de la sucursal");
        fono_fax.focus();
        error = true;
      }
      if (Trim(area_fijo.value)!="" && !error)
      {   
        if (!filter.test(area_fijo.value))
        {
		      alert("Debe ingresar solo n\u00fameros en el \u00e1rea del tel\u00e9fono fijo de la sucursal");
		      area_fijo.focus();
		      error = true;
		    }
		  }
      if (Trim(fono_fijo.value)!="" && !error)
      {   
		    if (!filter.test(fono_fijo.value))
		    {
		      alert("Debe ingresar solo n\u00fameros en el tel\u00e9fono fijo de la sucursal");
		      fono_fijo.focus();
		      error = true;
		    }
		  }
      if (Trim(fono_movil.value)!="" && !error)
      {   
		    if (!filter.test(fono_movil.value))
		    {
		      alert("Debe ingresar solo n\u00fameros en el tel\u00e9fono m\u00f3vil de la sucursal");
		      fono_movil.focus();
		      error = true;
		    }
		  }
      if (Trim(area_fax.value)!="" && !error)
      {   
		    if (!filter.test(area_fax.value))
		    {
		      alert("Debe ingresar solo n\u00fameros en el \u00e1rea del tel\u00e9fono fax de la sucursal");
		      area_fax.focus();
		      error = true;
		    }
		  }
      if (Trim(fono_fax.value)!="" && !error)
      {   
		    if (!filter.test(fono_fax.value))
		    {
		      alert("Debe ingresar solo n\u00fameros en el tel\u00e9fono fax de la sucursal");
		      fono_fax.focus();
		      error = true;
		    }
		  }
    }
    var i; 
    var comunaDuplicada = false; 
    if (id.value=="")
    {
      for (i in arrSucursales)
      {
        if (arrSucursales[i]["id_comuna"] == id_comuna.value)
        {
          comunaDuplicada = true;
        }
      }  
      if (comunaDuplicada)
      {
        alert("Solo debe haber una sucursal por comuna"); 
        id_comuna.focus(); 
        error = true;
      }
    }
    if (!error)
    {
      var aux = new Array; 
      aux = {
        "editar" : true, 
        "eliminar" : !soloSucursal, 
        "calle" : htmlentities(calle.value), 
        "numero" : numero.value, 
        "departamento" : htmlentities(departamento.value), 
        "id_comuna" : id_comuna[id_comuna.selectedIndex].value, 
        "comuna" : htmlentities(id_comuna[id_comuna.selectedIndex].text), 
        "area_fijo" : area_fijo.value, 
        "fono_fijo" : fono_fijo.value, 
        "fono_movil" : fono_movil.value, 
        "area_fax" : area_fax.value,  
        "fono_fax" : fono_fax.value
      }; 
      if (id.value=="")
      {
        arrSucursales[arrSucursales.length] = aux; 
      }
      else
      {
        arrSucursales[id.value] = aux;
      }
      ActualizaCapaNuevaSucursal(); 
      document.getElementById("btnAgregarSucursal").value = "Agregar";
      document.getElementById("boton_cancelar_sucursal").style.display = "none"; 
      LimpiarFormularioSucursal();
    }
  }
  return true;    
}

function EditarNuevaSucursalInstitucion(i)
{
  var id = document.getElementById("sucursal_id_aux");
  var calle = document.getElementById("sucursal_calle_aux"); 
  var numero = document.getElementById("sucursal_numero_aux"); 
  var departamento = document.getElementById("sucursal_departamento_aux"); 
  var id_comuna = document.getElementById("sucursal_id_comuna_aux");     
  var area_fijo = document.getElementById("sucursal_area_fijo_aux"); 
  var fono_fijo = document.getElementById("sucursal_fono_fijo_aux"); 
  var fono_movil = document.getElementById("sucursal_fono_movil_aux"); 
  var area_fax = document.getElementById("sucursal_area_fax_aux"); 
  var fono_fax = document.getElementById("sucursal_fono_fax_aux");
  var aux = arrSucursales[i]; 
  aux["calle"] = Trim(replaceSubstring(aux["calle"], "&quot;", "\""));
  aux["numero"] = Trim(replaceSubstring(aux["numero"], "&quot;", "\""));
  aux["departamento"] = Trim(replaceSubstring(aux["departamento"], "&quot;", "\""));
  var found = false;   
  var j = 0; 
  id.value = i; 
  calle.value = htmlspecialchars(aux["calle"], false); 
  numero.value = htmlspecialchars(aux["numero"]); 
  departamento.value = htmlspecialchars(aux["departamento"]); 
  area_fijo.value = aux["area_fijo"]; 
  fono_fijo.value = aux["fono_fijo"]; 
  fono_movil.value = aux["fono_movil"]; 
  area_fax.value = aux["area_fax"]; 
  fono_fax.value = aux["fono_fax"]; 
  while (!found && j<id_comuna.length)
  {
    if (id_comuna[j].value == aux["id_comuna"])
    {
      found = true;
      id_comuna.selectedIndex = j; 
    }
    else
    {
      j++;
    }
  }
  document.getElementById("btnAgregarSucursal").value = "Grabar";
  document.getElementById("boton_cancelar_sucursal").style.display = "block"; 
  calle.focus();
  return true;
}

function CancelarEditarSucursal()
{
  document.getElementById("btnAgregarSucursal").value = "Agregar";
  document.getElementById("boton_cancelar_sucursal").style.display = "none"; 
  LimpiarFormularioSucursal(); 
}  

function LimpiarFormularioSucursal()
{
  var id = document.getElementById("sucursal_id_aux");
  var calle = document.getElementById("sucursal_calle_aux"); 
  var numero = document.getElementById("sucursal_numero_aux"); 
  var departamento = document.getElementById("sucursal_departamento_aux"); 
  var id_comuna = document.getElementById("sucursal_id_comuna_aux");     
  var area_fijo = document.getElementById("sucursal_area_fijo_aux"); 
  var fono_fijo = document.getElementById("sucursal_fono_fijo_aux"); 
  var fono_movil = document.getElementById("sucursal_fono_movil_aux"); 
  var area_fax = document.getElementById("sucursal_area_fax_aux"); 
  var fono_fax = document.getElementById("sucursal_fono_fax_aux");
  id.value = "";
  calle.value = "";
  numero.value = "";
  departamento.value = "";
  id_comuna.selectedIndex = 0; 
  area_fijo.value = "";
  fono_fijo.value = "";
  fono_movil.value = "";
  area_fax.value = "";
  fono_fax.value = "";
  calle.focus(); 
  return true;
}

function EliminarNuevaSucursalInstitucion(id)
{
  if (confirm("Realmente desea quitar la sucursal?\n\nNOTA: Puede ingresarla nuevamente"))
  {
    var aux = new Array; 
    var i; 
    for (i in arrSucursales)
    {
      if (i!=id)
      {
        aux[aux.length] = arrSucursales[i]; 
      }
    }
    arrSucursales = aux; 
    LimpiarFormularioSucursal(); 
    ActualizaCapaNuevaSucursal(); 
  }
  return true;
}

function ActualizaCapaNuevaSucursal()
{
  if (arrSucursales.length > 0)
  {
    var HTML = ""; 
    HTML+="<table class=\"tablaMantenedor\" style=\"margin-top: 0px;\" cellspacing=\"0\">\n";
    HTML+="<tr>\n";
    HTML+="<th class=\"tituloMantenedor ancho50\" colspan=\"2\">\n\t#\n</th>\n";
    HTML+="<th class=\"tituloMantenedor\">\n\tCalle\n</th>\n";
    HTML+="<th class=\"tituloMantenedor\">\n\tN&ordm;\n</th>\n";
    HTML+="<th class=\"tituloMantenedor\">\n\tDepto.\n</th>\n";
    HTML+="<th class=\"tituloMantenedorLastChild\">\n\tComuna\n</th>\n";
    HTML+="</tr>\n";  
    var i; 
    for (i in arrSucursales)
    {
      aux = arrSucursales[i]; 
      aux["calle"] = Trim(replaceSubstring(aux["calle"], "\"", "&quot;")); 
      aux["numero"] = Trim(replaceSubstring(aux["numero"], "\"", "&quot;")); 
      aux["departamento"] = Trim(replaceSubstring(aux["departamento"], "\"", "&quot;")); 
      HTML+="<tr>\n";
      HTML+="<td class=\"celdaMantenedor arriba ancho50\" nowrap>\n";
      if (aux['editar'])
      {
        HTML+="\t\t<img src=\"/images/edit_icon.png\" title=\"Editar\" onclick=\"EditarNuevaSucursalInstitucion("+i+");\" onmouseover=\"this.style.cursor='hand'; this.style.cursor='pointer';\">\n";
      }       
      if (aux['eliminar'])
      {
        HTML+="\t\t<img src=\"/images/delete_icon.png\" title=\"Quitar\" onclick=\"EliminarNuevaSucursalInstitucion("+i+");\" onmouseover=\"this.style.cursor='hand'; this.style.cursor='pointer';\">\n";
      }      
      HTML+="\t</td>\n";
      HTML+="<td class=\"celdaMantenedor arriba ancho10\" style=\"border-left: none;\">\n";
      HTML+="\t\t";
      HTML+=(parseInt(i)+1);
      HTML+="<input type=\"hidden\" name=\"sucursal2[editar][]\" value=\""+aux["editar"]+"\">";
      HTML+="<input type=\"hidden\" name=\"sucursal2[eliminar][]\" value=\""+aux["eliminar"]+"\">";
      HTML+="<input type=\"hidden\" name=\"sucursal2[calle][]\" value=\""+aux["calle"]+"\">";
      HTML+="<input type=\"hidden\" name=\"sucursal2[numero][]\" value=\""+aux["numero"]+"\">";
      HTML+="<input type=\"hidden\" name=\"sucursal2[departamento][]\" value=\""+aux["departamento"]+"\">";
      HTML+="<input type=\"hidden\" name=\"sucursal2[id_comuna][]\" value=\""+aux["id_comuna"]+"\">";
      HTML+="<input type=\"hidden\" name=\"sucursal2[comuna][]\" value=\""+aux["comuna"]+"\">";
      HTML+="<input type=\"hidden\" name=\"sucursal2[area_fijo][]\" value=\""+aux["area_fijo"]+"\">";
      HTML+="<input type=\"hidden\" name=\"sucursal2[fono_fijo][]\" value=\""+aux["fono_fijo"]+"\">";
      HTML+="<input type=\"hidden\" name=\"sucursal2[fono_movil][]\" value=\""+aux["fono_movil"]+"\">";
      HTML+="<input type=\"hidden\" name=\"sucursal2[area_fax][]\" value=\""+aux["area_fax"]+"\">";
      HTML+="<input type=\"hidden\" name=\"sucursal2[fono_fax][]\" value=\""+aux["fono_fax"]+"\">";
      HTML+="\n";
      HTML+="\t</td>\n";
      HTML+="<td class=\"celdaMantenedor\" style=\"vertical-align: top;\">\n\t"+aux["calle"]+"\n</td>\n";
      HTML+="<td class=\"celdaMantenedor derecha\" style=\"vertical-align: top;\">\n\t"+aux["numero"]+"\n</td>\n";
      HTML+="<td class=\"celdaMantenedor\" style=\"vertical-align: top;\">\n\t"+aux["departamento"]+"\n</td>\n";
      HTML+="<td class=\"celdaMantenedorLastChild\" style=\"vertical-align: top;\">\n\t"+aux["comuna"]+"\n</td>\n";
      HTML+="</tr>\n";
    }
    HTML+="</table>\n";
  }
  else
  {
    var HTML = "&nbsp;&nbsp;A\u00fan no ha definido sucursales";
  }
  document.getElementById("sucursales_ya_definidas").innerHTML = HTML; 
  return true;
}

/* REPRESENTANTES LEGALES */

function AgregarNuevoRepresentanteLegal()
{
  if (soloSucursal)
  {
    alert("No posee privilegios para agregar nuevos representantes legales");
  }
  else
  {
    if (ValidaRUTPersona("representantelegal_rut"))
    {
      var Rut = document.getElementById("representantelegal_rut"); 
      if (Trim(Rut.value)=="")
      {
        alert("Debe ingresar el Rut de una persona"); 
      }
      else
      {
        var URL = "/inscribir/datospersona"; 
        var Vars = "rut="+Rut.value; 
        loadXMLDoc(URL, "POST", Vars, "ValidarRepresentanteLegal", true); 
      }
      return true;
    }
  }
}

function ValidarRepresentanteLegal(root)
{
  var Rut = document.getElementById("representantelegal_rut"); 
  if (Mozilla)
  {
    var inicio = 1;
    var incremento = 2;
  }
  else
  {
    var inicio = 0; 
    var incremento = 1;
  }
  if (root.childNodes[inicio].getAttribute("found")=="true")
  {
    var root2 = root.childNodes[inicio];
    var aux = new Array; 
    if (Mozilla)
    {
      aux = {
        "rut" : root2.childNodes[1].getAttribute("value"), 
        "rutdv" : root2.childNodes[3].getAttribute("value"), 
        "nombre_completo" : root2.childNodes[5].getAttribute("value") 
      }; 
    }
    else
    {
      aux = {
        "rut" : root2.childNodes[0].getAttribute("value"), 
        "rutdv" : root2.childNodes[1].getAttribute("value"), 
        "nombre_completo" : root2.childNodes[2].getAttribute("value") 
      };     
    }
    arrRepresentantesLegales[arrRepresentantesLegales.length] = aux; 
    ActualizaCapaRepresentanteLegal();     
    LimpiarFormularioRepresentanteLegal(); 
  }
  else
  {
    if (confirm("La persona que quiere ingresar no existe\n\n\u00BFDesea registrar una nueva persona?"))
    {
      window.open('/registrese/nuevapersona?rut='+Rut.value, '_Blank', 'width=640,height=480,resizable,scrollbars'); 
    }
    else
    {
      Rut.focus();
    }
  }
}

function ActualizaCapaRepresentanteLegal()
{
  if (arrRepresentantesLegales.length > 0)
  {
    var HTML = ""; 
    HTML+="<table class=\"tablaMantenedor\" style=\"margin-top: 0px;\" cellspacing=\"0\">\n";
    HTML+="<tr>\n";
    HTML+="<th class=\"tituloMantenedor\" colspan=\"2\" style=\"width: 45px;\">\n\t#\n</th>\n";
    HTML+="<th class=\"tituloMantenedor\" style=\"width: 60px;\">\n\tRUT\n</th>\n";
    HTML+="<th class=\"tituloMantenedorLastChild\">\n\tNombre\n</th>\n";
    HTML+="</tr>\n";  
    var i; 
    for (i in arrRepresentantesLegales)
    {
      aux = arrRepresentantesLegales[i]; 
      aux["nombre_completo"] = Trim(replaceSubstring(aux["nombre_completo"], "\"", "&quot;")); 
      HTML+="<tr>\n";
      HTML+="<td class=\"celdaMantenedor\" style=\"vertical-align: top; width: 30px;\" nowrap>\n";
      if (aux["editar"])
      {
        HTML+="\t\t<img src=\"/images/delete_icon.png\" title=\"Quitar\" onclick=\"EliminarNuevoRepresentanteLegal("+i+");\" onmouseover=\"this.style.cursor='hand'; this.style.cursor='pointer';\" />\n";
      }
      else
      {
        HTML+="&nbsp;";
      }
      HTML+="\t</td>\n";
      HTML+="<td class=\"celdaMantenedor derecha\" style=\"vertical-align: top; width: 15px; border-left: none;\">\n";
      HTML+="\t\t";
      HTML+=(parseInt(i)+1); 
      HTML+="<input type=\"hidden\" name=\"representantelegal[rut][]\" value=\""+aux["rut"]+"\">";
      HTML+="<input type=\"hidden\" name=\"representantelegal[rutdv][]\" value=\""+aux["rutdv"]+"\">";
      HTML+="<input type=\"hidden\" name=\"representantelegal[nombre_completo][]\" value=\""+aux["nombre_completo"]+"\">";
      HTML+="\n";
      HTML+="\t</td>\n";
      HTML+="<td class=\"celdaMantenedor derecha\" style=\"vertical-align: top; width: 120px;\">\n\t"+aux["rutdv"]+"\n</td>\n";
      HTML+="<td class=\"celdaMantenedorLastChild izquierda\" style=\"vertical-align: top;\">\n\t"+aux["nombre_completo"]+"\n</td>\n";
      HTML+="</tr>\n";
    }
    HTML+="</table>\n";
  }
  else
  {
    var HTML = "&nbsp;&nbsp;A\u00fan no ha definido representantes legales";
  }
  document.getElementById("representanteslegales_ya_definidos").innerHTML = HTML; 
  return true;
}

function LimpiarFormularioRepresentanteLegal()
{
  var rut = document.getElementById("representantelegal_rut");
  rut.value = "";
  rut.focus();
  return true;
}

function EliminarNuevoRepresentanteLegal(id)
{
  if (confirm("Realmente desea quitar el representante legal?\n\nNOTA: Puede ingresarlo nuevamente"))
  {
    var aux = new Array; 
    var i; 
    for (i in arrRepresentantesLegales)
    {
      if (i!=id)
      {
        aux[aux.length] = arrRepresentantesLegales[i]; 
      }
    }
    arrRepresentantesLegales = aux; 
    LimpiarFormularioRepresentanteLegal(); 
    ActualizaCapaRepresentanteLegal(); 
  }
  return true;
}

/* CARGOS */

function AgregarNuevoCargo()
{
  if (soloSucursal)
  {
    alert("No posee privilegios para agregar nuevos cargos");
  }
  else
  {  
    if (ValidaRUTPersona("cargo_rut", undefined))
    {
      var Rut = document.getElementById("cargo_rut"); 
      var Cargo = document.getElementById("cargo_id_cargo"); 
      var error = false; 
      if (Trim(Rut.value)=="" && !error)
      {
        alert("Debe ingresar el Rut de la persona"); 
        Rut.focus();
        error = true; 
      }
      if (Cargo.selectedIndex==0 && !error)
      {
        alert("Debe seleccionar al menos un cargo"); 
        Cargo.focus();
        error = true; 
      }
      
      var i; 
      var cargoDuplicado = false; 
      for (i in arrCargos)
      {
        if (arrCargos[i]["id_cargo"] == Cargo.value && arrCargos[i]["rutdv"] == Rut.value)
        {
          cargoDuplicado = true;
        }
      }  
      if (cargoDuplicado)
      {
        alert("Ya est&aacute; definido el cargo para la instituci&oacute;n"); 
        error = true;
      }
      if (!error)
      {
        var URL = "/inscribir/datospersona"; 
        var Vars = "rut="+Rut.value+"&id_cargo="+Cargo[Cargo.selectedIndex].value+"&cargo="+Cargo[Cargo.selectedIndex].text; 
        loadXMLDoc(URL, "POST", Vars, "ValidarCargo", true); 
      }
      return true;
    }
  }
}

function ValidarCargo(root)
{
  var Rut = document.getElementById("cargo_rut"); 
  var Cargo = document.getElementById("cargo_id_cargo");
  if (Mozilla)
  {
    var inicio = 1;
    var incremento = 2;
  }
  else
  {
    var inicio = 0; 
    var incremento = 1;
  }
  if (root.childNodes[inicio].getAttribute("found")=="true")
  {
    var root2 = root.childNodes[inicio];
    var aux = new Array; 
    if (Mozilla)
    {
      aux = {
        "editar" : true, 
        "rut" : root2.childNodes[1].getAttribute("value"), 
        "rutdv" : root2.childNodes[3].getAttribute("value"), 
        "nombre_completo" : htmlentities(root2.childNodes[5].getAttribute("value")), 
        "id_cargo" : root2.childNodes[7].getAttribute("value"), 
        "cargo" : htmlentities(root2.childNodes[9].getAttribute("value")) 
      }; 
    }
    else
    {
      aux = {
        "editar" : true, 
        "rut" : root2.childNodes[0].getAttribute("value"), 
        "rutdv" : root2.childNodes[1].getAttribute("value"), 
        "nombre_completo" : htmlentities(root2.childNodes[2].getAttribute("value")), 
        "id_cargo" : root2.childNodes[3].getAttribute("value"), 
        "cargo" : htmlentities(root2.childNodes[4].getAttribute("value"))
      };
    }
    arrCargos[arrCargos.length] = aux; 
    ActualizaCapaCargo();     
    LimpiarFormularioCargo(); 
  }
  else
  {
    if (confirm("La persona que quiere ingresar no existe\n\n\u00BFDesea registrar una nueva persona?"))
    {
      window.open('/registrese/nuevapersona?rut='+Rut.value, '_Blank', 'width=640,height=480,resizable,scrollbars'); 
    }
    else
    {
      Rut.focus();
    }
  }
}

function ActualizaCapaCargo(Error)
{
  if (arrCargos.length > 0)
  {
    var HTML = ""; 
    HTML+="<table class=\"tablaMantenedor\" style=\"margin-top: 0px;\" cellspacing=\"0\">\n";
    HTML+="<tr>\n";
    HTML+="<th class=\"tituloMantenedor ancho50\" colspan=\"2\">\n\t#\n</th>\n";
    HTML+="<th class=\"tituloMantenedor\">\n\tRUT\n</th>\n";
    HTML+="<th class=\"tituloMantenedor\">\n\tNombre\n</th>\n";
    HTML+="<th class=\"tituloMantenedorLastChild\">\n\tCargo\n</th>\n";
    HTML+="</tr>\n";  
    var i; 
    for (i in arrCargos)
    {
      aux = arrCargos[i]; 
      aux["nombre_completo"] = Trim(replaceSubstring(aux["nombre_completo"], "\"", "&quot;")); 
      aux["cargo"] = Trim(replaceSubstring(aux["cargo"], "\"", "&quot;"));
      HTML+="<tr>\n";
      HTML+="<td class=\"celdaMantenedor\" style=\"vertical-align: top; width: 20px;\" nowrap>\n";
      if (aux["editar"])
      {
        HTML+="\t\t<img src=\"/images/delete_icon.png\" title=\"Quitar\" onclick=\"EliminarNuevoCargo("+i+");\" onmouseover=\"this.style.cursor='hand'; this.style.cursor='pointer';\">\n";
      }
      HTML+="\t</td>\n";
      HTML+="<td class=\"celdaMantenedor derecha\" style=\"vertical-align: top; width: 10px; border-left: none;\">\n";
      HTML+="\t\t";
      HTML+=(parseInt(i)+1); 
      HTML+="<input type=\"hidden\" name=\"cargo[rut][]\" value=\""+aux["rut"]+"\">";
      HTML+="<input type=\"hidden\" name=\"cargo[rutdv][]\" value=\""+aux["rutdv"]+"\">";
      HTML+="<input type=\"hidden\" name=\"cargo[nombre_completo][]\" value=\""+aux["nombre_completo"]+"\">";
      HTML+="<input type=\"hidden\" name=\"cargo[id_cargo][]\" value=\""+aux["id_cargo"]+"\">";
      HTML+="<input type=\"hidden\" name=\"cargo[cargo][]\" value=\""+aux["cargo"]+"\">";
      HTML+="\n";
      HTML+="\t</td>\n";
      HTML+="<td class=\"celdaMantenedor derecha\" style=\"vertical-align: top; width: 80px;\">\n\t"+aux["rutdv"]+"\n</td>\n";
      HTML+="<td class=\"celdaMantenedor izquierda\" style=\"vertical-align: top;\">\n\t"+aux["nombre_completo"]+"\n</td>\n";
      HTML+="<td class=\"celdaMantenedorLastChild izquierda\" style=\"vertical-align: top;\">\n\t"+aux["cargo"]+"\n</td>\n";
      HTML+="</tr>\n";
    }
    HTML+="</table>\n";
    if (Error != undefined)
    {
      HTML += "<span style=\"color: red; font-weight: bold;\">" + Error + "</span>";
    }
  }
  else
  {
    var HTML = "&nbsp;&nbsp;A\u00fan no ha definido cargos"; 
    //var HTML = "Debe ingresar a lo menos un representante legal y otro cargo";
  }
  document.getElementById("cargos_ya_definidos").innerHTML = HTML; 
  return true;
}

function LimpiarFormularioCargo()
{
  var Rut = document.getElementById("cargo_rut");
  var Cargo = document.getElementById("cargo_id_cargo"); 
  Rut.value = "";
  Cargo.selectedIndex = 0;
  Rut.focus();
  return true;
}

function EliminarNuevoCargo(id)
{
  if (confirm("Realmente desea quitar el cargo?\n\nNOTA: Puede ingresarlo nuevamente"))
  {
    var aux = new Array; 
    var i; 
    for (i in arrCargos)
    {
      if (i!=id)
      {
        aux[aux.length] = arrCargos[i]; 
      }
    }
    arrCargos = aux; 
    LimpiarFormularioCargo(); 
    ActualizaCapaCargo(); 
  }
  return true;
}

function imprimirFicha(rut, clase)
{
  window.open("/fichas/verPopup?rut="+rut+"&clase="+clase+"&imprimir=true", "_Blank", "width=800,height=600,resizable,scrollbars"); 
  return true;
}

function imprimirCertificado(rut, clase)
{
  window.open("/certificado/verPopup?rut="+rut+"&clase="+clase+"&imprimir=true", "_Blank", "width=800,height=600,resizable,scrollbars"); 
  return true;
}

function InstitucionRegistrada(Rut)
{
  Rut = document.getElementById(Rut);
  var URL = "/inscribir/datosinstitucion";
  var Vars = "rut="+Rut.value;
  loadXMLDoc(URL, "POST", Vars, "esInstitucionRegistrada"); 
}

function esInstitucionRegistrada(root)
{
  if (Mozilla)
  {
    var inicio = 1;
  }
  else
  {
    var inicio = 0;
  }
  if (root.childNodes[inicio].getAttribute("found") == "true")
  {
    document.getElementById("ya_existe").style.display = "block";
  }
  else
  {
    document.getElementById("ya_existe").style.display = "none";
  }
}

