/* DONANTES */

var arrDetallesTransferencia = new Array;
var arrDonacionesTransferencias = new Array; 

function MuestraOcultaCapaSucursalesDonantes(Valor)
{
  if (Valor==0)
  {
    document.getElementById("transferencia_institucion_donante").selectedIndex = 0; 
    document.getElementById("transferencia_institucion_donante").disabled = true; 
    document.getElementById("transferencia_suc_donante").length = 0; 
    document.getElementById("varias_sucursales").style.display = "none"; 
    document.getElementById("cambio_transferencia").style.display = "none"; 
    document.getElementById("cambio_fecha_aprobacion").style.display = "none"; 
    document.getElementById("cambio_donacion").style.display = "block"; 
    document.getElementById("cambio_fecha_autorizacion").style.display = "block";     
  }
  else
  {
    document.getElementById("transferencia_institucion_donante").disabled = false; 
    document.getElementById("cambio_transferencia").style.display = "block"; 
    document.getElementById("cambio_fecha_aprobacion").style.display = "block"; 
    document.getElementById("cambio_donacion").style.display = "none"; 
    document.getElementById("cambio_fecha_autorizacion").style.display = "none";
  }
  var URL = "/transferencias/clasificaciones"; 
  var Vars = "tipo="+Valor;
  loadXMLDoc(URL, "POST", Vars, "actualizarClasificacion", true); 
}

function actualizarClasificacion(root)
{
  var clasificacion = document.getElementById("transferencia_id_clas");
  var actual = clasificacion.value;
  clasificacion.length = 1;
  if (Mozilla)
  {
    var inicio = 1;
    var incremento = 2;
  }
  else
  {
    var inicio = 0;
    var incremento = 1;
  }
  for (var i = inicio; i<root.childNodes.length; i = i + incremento)
  {
    clasificacion.length = clasificacion.length + 1;
    clasificacion[clasificacion.length-1].value = root.childNodes[i].getAttribute("id");
    if (Trim(root.childNodes[i].getAttribute("ley")) == "")
    {
      clasificacion[clasificacion.length-1].text = root.childNodes[i].getAttribute("descripcion");
    }
    else
    {
      clasificacion[clasificacion.length-1].text = root.childNodes[i].getAttribute("descripcion") + " (" + root.childNodes[i].getAttribute("ley") + ")";
    }
  }
  if (actual>0)
  {
    var found = false;
    var i = 0;
    var j = inicio;
    while (i<clasificacion.length && !found)
    {
      if (clasificacion[i].value == actual)
      {
        clasificacion.selectedIndex = i;
        if (i < root.childNodes.length)
        {
          if (clasificacion.value == root.childNodes[j].getAttribute("id"))
          {
            document.getElementById("transferencia_marco_legal").value = root.childNodes[j].getAttribute("ley"); 
          }
        }
        found = true;
      }
      else
      {
        i++;
        j = j + incremento;
      }
    }
  }
}

function BuscarSucursalesDonantes()
{
  var Rut = document.getElementById("transferencia_institucion_donante"); 
  if (Rut.value>0)
  {
    var URL = "/transferencias/sucursalesdonantes"; 
    var Vars = "rut="+Rut.value;
    loadXMLDoc(URL, "POST", Vars, "ValidarSucursalesDonantes", true);   
  }
  else
  {
    document.getElementById("transferencia_suc_donante").length = 0; 
    document.getElementById("varias_sucursales").style.display = "none"; 
  }
}

function ValidarSucursalesDonantes(root)
{
  var Rut = document.getElementById("transferencia_institucion_donante"); 
  var Donante = document.getElementById("transferencia_suc_donante"); 
  if (Mozilla)
  {
    var inicio = 1;
    var incremento = 2; 
  }
  else
  {
    var inicio = 0; 
    var incremento = 1;
  }
  if (root.childNodes[inicio].getAttribute("found")=="true")
  {
    var i, aux;
    var root2 = root.childNodes[inicio];
    Donante.length = 1;   
    Donante[0].value = 0; 
    Donante[0].text = "Seleccione Sucursal"; 
    for (i=inicio; i<root2.childNodes.length; i=i+incremento)
    {
      aux = "";
      aux+= htmlspecialchars(root2.childNodes[i].getAttribute("calle"), false); 
      aux+= " ";
      aux+= root2.childNodes[i].getAttribute("numero"); 
      aux+= " "; 
      aux+= root2.childNodes[i].getAttribute("departamento"); 
      aux=Trim(aux); 
      aux+= ", ";
      aux+= root2.childNodes[i].getAttribute("comuna"); 
      Donante.length = Donante.length + 1; 
      Donante[Donante.length-1].value = root2.childNodes[i].getAttribute("id"); 
      Donante[Donante.length-1].text = aux; 
    }
    document.getElementById("varias_sucursales").style.display = "block"; 
  }
  else
  {
    alert("No existen sucursales ingresadas para el donante indicado");
    document.getElementById("varias_sucursales").style.display = "none"; 
    Rut.focus();
  }
}

/* RECEPTORES */ 

function BuscarReceptor()
{
  var Rut = document.getElementById("transferencia_rut_receptor"); 
  if (Trim(Rut.value) == "")
  {
    alert("Debe ingresar el Rut del receptor");
    Rut.focus();
  }
  else
  {
    var URL = "/transferencias/datosreceptor"; 
    var Vars = "rut="+Rut.value; 
    loadXMLDoc(URL, "POST", Vars, "ValidarReceptor", true); 
  }
}

function ValidarReceptor(root)
{
  var Rut = document.getElementById("transferencia_rut_receptor"); 
  var divReceptor = document.getElementById("receptor");
  var divReceptorNombre = document.getElementById("receptor_nombre"); 
  var divReceptorRazonSocial = document.getElementById("receptor_razon_social"); 
  var divSucursalReceptor = document.getElementById("sucursal_receptor"); 
  var SucursalReceptor = document.getElementById("transferencia_suc_receptor");
  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; 
    divReceptorNombre.innerHTML = root2.childNodes[inicio].getAttribute("nombre"); 
    divReceptorRazonSocial.innerHTML = root2.childNodes[inicio].getAttribute("razon_social"); 
    var root3 = root2.childNodes[inicio]; 
    var i, aux; 
    SucursalReceptor.length = 0; 
    for (i=inicio; i<root3.childNodes.length; i=i+incremento)
    {
      aux = "";
      aux+= htmlspecialchars(root3.childNodes[i].getAttribute("calle"), false); 
      aux+= " ";
      aux+= root3.childNodes[i].getAttribute("numero"); 
      aux+= " "; 
      aux+= root3.childNodes[i].getAttribute("departamento"); 
      aux=Trim(aux); 
      aux+= ", ";
      aux+= root3.childNodes[i].getAttribute("comuna"); 
      SucursalReceptor.length = SucursalReceptor.length + 1; 
      SucursalReceptor[SucursalReceptor.length-1].value = root3.childNodes[i].getAttribute("id"); 
      SucursalReceptor[SucursalReceptor.length-1].text = aux; 
      divReceptor.style.display = "block";     
    }
    if (SucursalReceptor.length == 0)
    {
      alert("¡El receptor no tiene sucursales definidas!");
    }
  }
  else
  {
    alert("No existen instituciones con el RUT indicado");
    Rut.focus();
  }
}

/* TRANSFERENCIAS */

function AgregarDetalleTransferencia()
{
  var Comuna = document.getElementById("detalletransferencia_id_comuna_aux"); 
  var Monto = document.getElementById("detalletransferencia_monto_aux"); 
  var error = false; 
  if (Comuna.selectedIndex == 0 && !error)
  {
    error = true; 
    alert("Debe seleccionar una comuna."); 
    Comuna.focus(); 
  }
  if (isNaN(parseInt(Monto.value)) && !error)
  {
    error = true; 
    alert("El monto ingresado no es correcto."); 
    Monto.focus(); 
  }
  if (parseInt(Monto.value)<=0 && !error)
  {
    error = true; 
    alert("El monto debe ser mayor a cero."); 
    Monto.focus(); 
  }
  if (!error)
  {
    var totalTransferencia = parseInt(Monto.value); 
    for (var i in arrDetallesTransferencia)
    {
      totalTransferencia += parseInt(arrDetallesTransferencia[i]["monto"]); 
    }
    if (totalTransferencia > 99999999999999)
    {
      error = true; 
      alert("El monto total de la transferencia es demasiado elevado."); 
      Monto.focus(); 
    }
  }
  if (!error)
  {
    var presente = false; 
    for (var i in arrDetallesTransferencia)
    {
      if (Comuna[Comuna.selectedIndex].value == arrDetallesTransferencia[i]["id_comuna"])
      {
        presente = true; 
      }
    }
    if (presente)
    {
      alert("La comuna de destino ya esta presente en la lista de donaciones"); 
      error = true; 
    }
  }
  if (!error)
  {
    var aux = new Array; 
    aux = {
      "id_comuna" : Comuna[Comuna.selectedIndex].value, 
      "comuna" : htmlentities(Comuna[Comuna.selectedIndex].text), 
      "monto" : Monto.value
    }; 
    arrDetallesTransferencia[arrDetallesTransferencia.length] = aux; 
    ActualizaCapaDetalleTransferencia();     
  }
}

function ActualizaCapaDetalleTransferencia()
{
  if (arrDetallesTransferencia.length > 0)
  {
    var HTML = ""; 
    HTML+="<table class=\"tablaMantenedor\" style=\"margin-top: 0px;\" cellspacing=\"0\">\n";
    HTML+="<tr>\n";
    HTML+="<th class=\"tituloMantenedor ancho100\" colspan=\"2\">\n\t#\n</th>\n";
    HTML+="<th class=\"tituloMantenedor\">\n\Comuna\n</th>\n";
    HTML+="<th class=\"tituloMantenedorLastChild ancho150\">\n\tMonto\n</th>\n";
    HTML+="</tr>\n";  
    var i; 
    var total=0; 
    for (i in arrDetallesTransferencia)
    {
      aux = arrDetallesTransferencia[i];
      HTML+="<tr>\n";
      HTML+="<td class=\"celdaMantenedor arriba ancho50\">\n";
      HTML+="\t<img src=\"/images/delete_icon.png\" title=\"Quitar\" OnClick=\"EliminarDetalleTransferencia("+i+");\" OnMouseOver=\"this.style.cursor='hand'; this.style.cursor='pointer';\">\n";
      HTML+="</td>\n";
      HTML+="<td class=\"celdaMantenedor arriba derecha ancho50\" style=\"border-left: none;\" nowrap>\n";
      HTML+="\t";
      HTML+=(parseInt(i)+1); 
      HTML+="<input type=\"hidden\" name=\"detalletransferencia[id_comuna][]\" value=\""+arrDetallesTransferencia[i]["id_comuna"]+"\">";
      HTML+="<input type=\"hidden\" name=\"detalletransferencia[comuna][]\" value=\""+arrDetallesTransferencia[i]["comuna"]+"\">";
      HTML+="<input type=\"hidden\" name=\"detalletransferencia[monto][]\" value=\""+arrDetallesTransferencia[i]["monto"]+"\">";
      HTML+="\n";
      HTML+="</td>\n";
      HTML+="<td class=\"celdaMantenedor izquierda arriba\">\n\t"+arrDetallesTransferencia[i]["comuna"]+"\n</td>\n";
      HTML+="<td class=\"celdaMantenedorLastChild derecha arriba ancho150\">\n\t$&nbsp;"+addCommas(arrDetallesTransferencia[i]["monto"])+"\n</td>\n";
      HTML+="</tr>\n";
      total = total + parseFloat(arrDetallesTransferencia[i]["monto"]); 
    }
    HTML+="<tr>\n";
    HTML+="<td colspan=\"3\" class=\"celdaMantenedor derecha negrita\">\n";
    HTML+="\tTotal\n";
    HTML+="</td>";
    HTML+="<td class=\"celdaMantenedorLastChild derecha negrita\">\n";
    HTML+="\t$&nbsp;"+addCommas(total)+"\n";
    HTML+="</td>\n";
    HTML+="</tr>\n";
    HTML+="</table>\n";
  }
  else
  {
    var HTML = "no ha definido detalles para la transferencia";
  }
  document.getElementById("detalle_transferencia").innerHTML = HTML; 
  LimpiarFormularioDetalleTransferencia();
  return true;
}

function EliminarDetalleTransferencia(id)
{
  if (confirm("Realmente desea quitar el detalle de la transferencia?\n\nNOTA: Puede ingresarlo nuevamente"))
  {
    var aux = new Array; 
    var i; 
    for (i in arrDetallesTransferencia)
    {
      if (i!=id)
      {
        aux[aux.length] = arrDetallesTransferencia[i]; 
      }
    }
    arrDetallesTransferencia = aux; 
    ActualizaCapaDetalleTransferencia(); 
  }
  return true;
}

function LimpiarFormularioDetalleTransferencia()
{
  var Comuna = document.getElementById("detalletransferencia_id_comuna_aux"); 
  var Monto = document.getElementById("detalletransferencia_monto_aux"); 
  Comuna.selectedIndex = 0; 
  Monto.value = "";
  Comuna.focus();
}

function CambiaTitulosDonacionTransferencia()
{
  var Clasificacion = document.getElementById("transferencia_id_clas"); 
  var URL = "/transferencias/clasificaciones"; 
  var Vars = "id="+Clasificacion.value;
  loadXMLDoc(URL, "POST", Vars, "EjecutaCambiaTitulosDonacionTransferencia", true); 
}

function EjecutaCambiaTitulosDonacionTransferencia(root)
{
  if (Mozilla)
  {
    var elemento = 1;
  }
  else
  {
    var elemento = 0;
  }
  var ley = root.childNodes[elemento].getAttribute("ley");
  if (Trim(ley) == "")
  {
    var found = false;
  }
  else
  {
    var found = true;
  }
  if (found)
  {
    document.getElementById("cambio_transferencia").style.display = "none"; 
    document.getElementById("cambio_fecha_aprobacion").style.display = "none"; 
    document.getElementById("cambio_donacion").style.display = "block"; 
    document.getElementById("cambio_fecha_autorizacion").style.display = "block"; 
    document.getElementById("transferencia_marco_legal").value = ley;
    document.getElementById("transferencia_marco_legal").disabled = true;  
  }
  else
  {
    document.getElementById("cambio_transferencia").style.display = "block"; 
    document.getElementById("cambio_fecha_aprobacion").style.display = "block"; 
    document.getElementById("cambio_donacion").style.display = "none"; 
    document.getElementById("cambio_fecha_autorizacion").style.display = "none"; 
    document.getElementById("transferencia_marco_legal").value = "";
    document.getElementById("transferencia_marco_legal").disabled = false;  
  }
}


