﻿function PaisTitulo(nro) {
    var combo = "lstTitPais" + nro;
    var valor = document.getElementById(combo);

    if (valor.value == "0") {
       document.getElementById("escInstOtro" + nro).value = "";
       document.getElementById("divInstOtro" + nro).innerHTML = "";
       document.getElementById("divTitOtro" + nro).innerHTML = "";
       document.getElementById("escTitOtro" + nro).value = "";
       document.getElementById("divInst" + nro).innerHTML = "";
       document.getElementById("divTit" + nro).innerHTML = "";
       document.getElementById("escTit" + nro).value = "";
       document.getElementById("escInst" + nro).value = "";
       document.getElementById("esc_ArcTit" + nro).value = "";       
       bt = document.getElementById('txtTitFecha' + nro);
       bt.value = "";
       bt.style.visibility = 'hidden';
       lab = document.getElementById("msgfecha" + nro);
       lab.style.visibility = 'hidden';
    }else{
    if (valor.value == "1") {
        document.getElementById("escInstOtro" + nro).value = "";
        document.getElementById("divInstOtro" + nro).innerHTML = "";    
        document.getElementById("escTit" + nro).value = "";
        document.getElementById("escInst" + nro).value = "";
        document.getElementById("divTitOtro" + nro).innerHTML = "";
        document.getElementById("escTitOtro" + nro).value = "";
        document.getElementById("esc_ArcTit" + nro).value = "";
        BuscarDatosTit(nro);
        } else {
        document.getElementById("divInst" + nro).innerHTML = "<input name='InstitucionTitulo" + nro + "' id='InstitucionTitulo" + nro + "' type='text' value='' onBlur='GuardarValorInst(" + nro + ")' class='Estilo43' size='50' maxlength='150'>"
        document.getElementById("divTit" + nro).innerHTML = "<input name='NombreTitulo" + nro + "' id='NombreTitulo" + nro + "' type='text' value='' onBlur='GuardarValorTit(" + nro + ")' class='Estilo43' size='50' maxlength='150'>"
        document.getElementById("divTitOtro" + nro).innerHTML = "";
        document.getElementById("escTitOtro" + nro).value = "";
        document.getElementById("escInstOtro" + nro).value = "";
        document.getElementById("divInstOtro" + nro).innerHTML = "";
        document.getElementById("esc_ArcTit" + nro).value = "";
    } 
document.getElementById("txtTitFecha" + nro).value = "";
bt = document.getElementById('txtTitFecha' + nro);
bt.style.visibility = 'visible';
lab = document.getElementById("msgfecha" + nro);
lab.style.visibility = 'visible';

}
validarCampos_Titulo(nro);
}

function BuscarDatosTit(nro) {
    var combo = "lstTitPais" + nro;
    var valor = document.getElementById(combo);
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange_BuscarDatosTit;
    xmlHttp.open("GET", "?combo=" + nro + "&valorcombo=" + valor.value , true);
    xmlHttp.send(null);
}

function handleStateChange_BuscarDatosTit() {
    if (xmlHttp.readyState == 4) {
        var s = xmlHttp.responseText.split("|");
        document.getElementById("divInst" + s[0]).innerHTML = s[1];
        document.getElementById("divTit" + s[0]).innerHTML = s[2];
    }
}

function GuardarValorTit(nro) {
    var campo = document.getElementById("NombreTitulo" + nro);
    document.getElementById("escTit" + nro).value = campo.value;
    if (campo.value == "422") {
        Mostrar_OtroTitulo(nro, "");
    } else {
    document.getElementById("escTitOtro" + nro).value = "";
    document.getElementById("divTitOtro" + nro).innerHTML = "";
    }
    validarCampos_Titulo(nro);
}

function GuardarValorInst(nro) {
    var campo = document.getElementById("InstitucionTitulo" + nro);
    document.getElementById("escInst" + nro).value = campo.value;
    if (campo.value == "233") {
        Mostrar_OtraInstitucion(nro, "");
    } else {
    document.getElementById("escInstOtro" + nro).value = "";
    document.getElementById("divInstOtro" + nro).innerHTML = "";
    }
    validarCampos_Titulo(nro);
}
function rellena_DatosTitulo(pais, id_institucion, id_titulo, fecha_titulo, institucion,titulo,lista_inst,lista_tit, certificado, nro) {
    // selecciona pais
    var p;
    if (pais != "") {
        if (nro != "1") {
            document.getElementById("op"+nro).style.display = '';
        }
        p = document.getElementById("lstTitPais" + nro);
        for (cont = 0; cont <= p.length - 1; cont++) {
            if (p.options[cont].value == pais) {
                p.options[cont].selected = true;
            }
        }
        // institución
        if (pais == "1") {
            document.getElementById("divInst" + nro).innerHTML = lista_inst;
            document.getElementById("escInst" + nro).value = id_institucion;

            // título
            document.getElementById("divTit" + nro).innerHTML = lista_tit;
            document.getElementById("escTit" + nro).value = id_titulo;

            // titulo de otro
            if (id_institucion == "233") {
                document.getElementById("escInstOtro" + nro).value = institucion;
                Mostrar_OtraInstitucion(nro, institucion);
            }

            if (id_titulo == "422") {
                document.getElementById("escTitOtro" + nro).value = titulo;
                Mostrar_OtroTitulo(nro, titulo);
            }
            
        } else {
        document.getElementById("divInst" + nro).innerHTML = "<input name='InstitucionTitulo" + nro + "' id='InstitucionTitulo" + nro + "' type='text' value='" + institucion + "' onBlur='GuardarValorInst(" + nro + ")' class='Estilo43' size='50' maxlength='150'>"
        document.getElementById("divTit" + nro).innerHTML = "<input name='NombreTitulo" + nro + "' id='NombreTitulo" + nro + "' type='text' value='" + titulo + "' onBlur='GuardarValorTit(" + nro + ")' class='Estilo43' size='50' maxlength='150'>"
            document.getElementById("escTit" + nro).value = titulo;
            document.getElementById("escInst" + nro).value = institucion;
        }
        //fecha titulo
        var f = document.getElementById("txtTitFecha" + nro);
        f.value = fecha_titulo;
        // certificado
        document.getElementById("labArcTit" + nro).innerHTML = certificado;
        document.getElementById("esc_ArcTit" + nro).value = certificado;
    } else {
    p = document.getElementById("lstTitPais" + nro);
    p.options[0].selected = true;
    }
}


// valida fecha

function esDigito(sChr) {
    var sCod = sChr.charCodeAt(0);
    return ((sCod > 47) && (sCod < 58));
}

function valSep(oTxt) {
    var bOk = false;
    bOk = bOk || ((oTxt.value.charAt(2) == "-") && (oTxt.value.charAt(5) == "-"));
    bOk = bOk || ((oTxt.value.charAt(2) == "/") && (oTxt.value.charAt(5) == "/"));
    return bOk;
}

function finMes(oTxt) {
    var nMes = parseInt(oTxt.value.substr(3, 2), 10);
    var nRes = 0;
    switch (nMes) {
        case 1: nRes = 31; break;
        case 2: nRes = 29; break;
        case 3: nRes = 31; break;
        case 4: nRes = 30; break;
        case 5: nRes = 31; break;
        case 6: nRes = 30; break;
        case 7: nRes = 31; break;
        case 8: nRes = 31; break;
        case 9: nRes = 30; break;
        case 10: nRes = 31; break;
        case 11: nRes = 30; break;
        case 12: nRes = 31; break;
    }
    return nRes;
}

function valDia(oTxt) {
    var bOk = false;
    var nDia = parseInt(oTxt.value.substr(0, 2), 10);
    bOk = bOk || ((nDia >= 1) && (nDia <= finMes(oTxt)));
    return bOk;
}

function valMes(oTxt) {
    var bOk = false;
    var nMes = parseInt(oTxt.value.substr(3, 2), 10);
    bOk = bOk || ((nMes >= 1) && (nMes <= 12));
    return bOk;
}

function valAno(oTxt) {
    var bOk = true;
    var nAno = oTxt.value.substr(6);
    bOk = bOk && ((nAno.length == 4));
    if (bOk) {
        for (var i = 0; i < nAno.length; i++) {
            bOk = bOk && esDigito(nAno.charAt(i));
        }
    }
    if (bOk) {
        if (nAno.value < 1900) {
            bOk = bOk && esDigito(nAno.charAt(i));
        }
    }
    return bOk;
}

function valFecha(oTxt,nro) {
    var bOk = true;
    validarCampos_Titulo(nro);
    if (oTxt.value != "" && oTxt.value != "__-__-____" && oTxt.value != "__/__/____") {
        if (oTxt.value.length == 10) {
            if (oTxt.value != "") {
                bOk = bOk && (valAno(oTxt));
                bOk = bOk && (valMes(oTxt));
                bOk = bOk && (valDia(oTxt));
                bOk = bOk && (valSep(oTxt));
                if (!bOk) {
                    alert("Fecha inválida");
                    oTxt.value = "";
                } else {
                    //   alert("validar que no sea la fecha mayor que la actual");
                    Verifica_Fecha_Titulo(oTxt);

                }
            }
        } else {
            alert("Fecha inválida");
        } 
    }
}

function Verifica_Fecha_Titulo(fechatit) {
    var fecha = new Date();
  //falta pasar los valores...
    var d;
    if (fechatit.value.charAt(0)=="0"){
    d= fechatit.value.charAt(1); //3;
    }else{
    d= fechatit.value.charAt(0) + fechatit.value.charAt(1);
    }
    var m;
    if (fechatit.value.charAt(3)=="0") {
    m=   fechatit.value.charAt(4);
    }else{
     m= fechatit.value.charAt(3) + fechatit.value.charAt(4);
    } 
    var a = fechatit.value.charAt(6)+fechatit.value.charAt(7)+fechatit.value.charAt(8)+fechatit.value.charAt(9);
  var dia = fecha.getDate();
  var mes = fecha.getMonth()+1;
  var ano = fecha.getFullYear();
  var msg = "La fecha no debe ser mayor que la actual";
   if (a > 1900) {
      if (a > ano) {
          alert(msg);
          fechatit.value = "";
          fechatit.focus();
      } else {
          if (a == ano) {
              if (m == mes) {
                  if (d > dia) {
                      alert(msg);
                      fechatit.value = "";
                      fechatit.focus();
                  }
              } else {
                    if (m > mes) {
                    alert(msg);
                      fechatit.value = "";
                      fechatit.focus();
                  }
              
              }
          }
      }
  } else {
  alert("Fecha no válida!!");
  fechatit.value = "";
  fechatit.focus();
  }
}
// para esconder campos del titulo
function cambia(numpreg)
{
 if (document.getElementById(numpreg).style.display == 'none')
	 document.getElementById(numpreg).style.display = '';
 else
	 document.getElementById(numpreg).style.display = 'none';
}

//validar campos fechas...

function validarCampos_Titulo(nro) {
   // alert(document.getElementById("lstTitPais" + nro).value);
    if (document.getElementById("escInst" + nro).value == "" || document.getElementById("escTit" + nro).value == "" || document.getElementById("txtTitFecha" + nro).value == "" || document.getElementById("lstTitPais" + nro).value == "0" || document.getElementById("txtTitFecha" + nro).value == "__-__-____" || document.getElementById("txtTitFecha" + nro).value == "__/__/____" || (document.getElementById("esc_ArcTit" + nro).value == "" && document.getElementById("FileTit" + nro).value == "")) {
      document.getElementById("MensajeTit" + nro).innerHTML = "Recuerde que para registrar los datos del título, los campos País, Institución, Título, Fecha y certificado son Obligatorios!!";
    } else {
    document.getElementById("MensajeTit" + nro).innerHTML = "";
}
//alert(document.getElementById("txtTitFecha" + nro).value);
if (document.getElementById("escInst" + nro).value != "" && document.getElementById("escTit" + nro).value != "" && document.getElementById("txtTitFecha" + nro).value != "" && document.getElementById("lstTitPais" + nro).value != "0" && document.getElementById("txtTitFecha" + nro).value != "__-__-____" && document.getElementById("txtTitFecha" + nro).value != "__/__/____" && document.getElementById("esc_ArcTit" + nro).value == "" && document.getElementById("FileTit" + nro).value == "") {
    alert("Recuerde que para registrar los datos del título, los campos País, Institución, Título, Fecha y certificado son Obligatorios!!");
    document.getElementById("lstTitPais" + nro).focus();
}
}

function Mostrar_OtroTitulo(nro,valor){
    document.getElementById("divTitOtro" + nro).innerHTML = "<span class='Estilo4'>Ingrese Título:</span> <input name='NombreTitulo" + nro + "' id='NombreTitulo" + nro + "' type='text'  onBlur='GuardarValorTitOtro(" + nro + ",this)' class='Estilo43' size='50' maxlength='150' value='" + valor + "'>";
}

function GuardarValorTitOtro(nro,valor) {
    document.getElementById("escTitOtro" + nro).value = valor.value;
  }

  function Mostrar_OtraInstitucion(nro, valor) {
    document.getElementById("divInstOtro" + nro).innerHTML = "<span class='Estilo4'>Ingrese Institución:</span> <input name='NombreInstitucion" + nro + "' id='NombreInstitucion" + nro + "' type='text'  onBlur='GuardarValorInstOtro(" + nro + ",this)' class='Estilo43' size='50' maxlength='150' value='" + valor + "'>";
}

function GuardarValorInstOtro(nro, valor) {
    document.getElementById("escInstOtro" + nro).value = valor.value;
}

// agregado el 21 de enero 2011

function Ver_ArchivoNivelEst() {
    var nivel = document.getElementById("lstNivelesEstudios");
    var ver = 0;
    for (var i=0; i<nivel.length;i++){
      if(nivel.value=="3" || nivel.value=="5" || nivel.value=="8" || nivel.value=="11" || nivel.value=="13"){
          ver = 1;
        i = nivel.length;
      }
  }

  if (ver == 0) {
      bt = document.getElementById('divArchTitulo');
      bt.style.visibility = 'hidden';
      document.getElementById('divArchTitulo').value = "";
  } else {
  bt = document.getElementById('divArchTitulo');
  bt.style.visibility = 'visible';
  }
   
}
