function ingresar(){
  var mensaje = "";
  var strDVs = "K";
  if (document.Inicio.sUsuario.value == ""){
     mensaje += "\n - Nombre de usuario";
     }
  if (document.Inicio.sDV.value == ""){
     mensaje += "\n - Dígito verificador";
     }
  if (document.Inicio.sPass.value == ""){
     mensaje += "\n - Password";
     }
  if (mensaje != ""){
     mensaje = "Falta completar los siguientes campos: \n " + mensaje;
     alert(mensaje);
     return (false);
  }
  else {
     var strDV = document.Inicio.sDV.value;
     if (strDV == "k" || strDV == "K") { strDV = strDVs; }
     if (fValidarRut(document.Inicio.sUsuario.value) != strDV){
	alert("Rut incorrecto, ingrese nuevamente");
	document.Inicio.sUsuario.focus();
	return (false);
     }
  }
}

function ValRut(sRut, sDV){
	var strRut = new String(sRut);
	var strDV = new String(sDV);
	var strDVs = "K";
	if (strRut != "" && strDV != ""){
	   if (strDV == "k" || strDV == "K"){ strDV = strDVs; }
	   if (fValidarRut(strRut) != strDV){
	      alert("Rut incorrecto, ingrese nuevamente");
	      document.Inicio.sUsuario.focus();
	   }
	}
	else{
	   alert("Debe ingresar el Rut y el Dígito Verficador");
	   if (strRut == ""){ document.Inicio.sUsuario.focus(); }
	   else{ document.Inicio.sDV.focus();}
	}
}

function confirmar(){
	var mensaje ="";

	if (document.formRET.NumRET.value == "" || document.formRET.NumRET.value == 0){
	   mensaje = "- Ingresar un número de RET mayor a cero";
	}
     // if (document.formRET.Recolector.value == "" ){
        if (document.formRET.Recolector.value == "" && document.formRET.TipoArea.value==2){
     //    mensaje += "\n - Valores del Recolector";
           mensaje += "\n - Valores del Recolector (por tratarse de Banco Natural)";
        }
	if (document.formRET.CodArea.value == "" || document.formRET.CodArea.value == 0) {
	   mensaje += "\n - Ingresar código de Area";
	}
      //if (document.formRET.Recurso.selectedIndex == 0){                                                                             // JCN: 27-DIC-2010 (CODIGO COMENTADO)
        if (document.formRET.Recurso.selectedIndex == 0 && document.formRET.TipoArea.value==2){                                       // JCN: 27-DIC-2010 (CODIGO APORTADO)
	   mensaje += "\n - Seleccionar un Recurso";
	}
      //if (document.formRET.Cantidad.value == "" || document.formRET.Cantidad.value == 0){                                           // JCN: 27-DIC-2010 (CODIGO COMENTADO)
        if ((document.formRET.Cantidad.value == "" || document.formRET.Cantidad.value == 0) && document.formRET.TipoArea.value==2) {  // JCN: 27-DIC-2010 (CODIGO APORTADO)
	   mensaje += "\n - Ingresar la cantidad del recurso";
	}
      //if (document.formRET.UniMed.selectedIndex == 0){ // JCN: 27-DIC-2010 (CODIGO COMENTADO)
        if ((document.formRET.UniMed.selectedIndex == 0 && document.formRET.TipoArea.value==2) || (document.formRET.UniMed.selectedIndex == 0 && document.formRET.TipoArea.value!=2 && document.formRET.Cantidad.value>0)) { // JCN: 27-DIC-2010 (CODIGO APORTADO)
	   mensaje += "\n - Seleccionar la Unidad de Medida del recurso";
	}
        var yy1 = document.formRET.AnnoExt.options(document.formRET.AnnoExt.selectedIndex).text;
        var mm1 = document.formRET.MesExt.options(document.formRET.MesExt.selectedIndex).text;
        var dd1 = document.formRET.DiaExt.options(document.formRET.DiaExt.selectedIndex).text;
        var hh1 = document.formRET.Hora.options(document.formRET.Hora.selectedIndex).text;
        var mn1 = document.formRET.Min.options(document.formRET.Min.selectedIndex).text;

        var yy2 = document.formRET.AnnoExtFin.options(document.formRET.AnnoExtFin.selectedIndex).text;
        var mm2 = document.formRET.MesExtFin.options(document.formRET.MesExtFin.selectedIndex).text;
        var dd2 = document.formRET.DiaExtFin.options(document.formRET.DiaExtFin.selectedIndex).text;
        var hh2 = document.formRET.HoraFin.options(document.formRET.HoraFin.selectedIndex).text;
        var mn2 = document.formRET.MinFin.options(document.formRET.MinFin.selectedIndex).text;

        if ((yy1+mm1+dd1+hh1+mn1) > (yy2+mm2+dd2+hh2+mn2)) {
           mensaje += "\n - Fecha/Hora de Inicio Extracción debe ser Menor o Igual que Fecha/Hora de Término Extracción";
        }

	if (mensaje != ""){
	   mensaje = "Falta completar los siguientes campos: \n \n " + mensaje;
	   alert(mensaje);
	}
	else{
     //	   if(confirm('Desea ingresar un nuevo RET') != 0) { 
     //         document.formRET.NuevoRet.value = 1; 
     //	   	if(confirm('Mantener datos recolector RET anterior') != 0) { document.formRET.DatosRetAnt.value = 1; } 
     //		else { document.formRET.DatosRetAnt.value = 0; }
     //	   }
     //	   else { document.formRET.DatosRetAnt.value = 0;
     //		  document.formRET.NuevoRet.value = 0; }
     //	   if(document.formRET.Rut.value != '') { document.formRET.RutH.value = document.formRET.Rut.value; }
     //	   document.formRET.RecolectorH.value = document.formRET.Recolector.value;
     //	   window.document.formRET.submit();

	   if (document.formRET.NumRET.disabled == false) {
               if (confirm('Desea ingresar un nuevo RET') != 0) { 
                   document.formRET.NuevoRet.value = 1; 
	   	   if (confirm('Mantener datos recolector RET anterior') != 0) {
                       document.formRET.DatosRetAnt.value = 1;
                   } 
		   else {
                       document.formRET.DatosRetAnt.value = 0;
                   }
	        }
	        else {
                   document.formRET.DatosRetAnt.value = 0;
		   document.formRET.NuevoRet.value = 0;
                }
	        if (document.formRET.Rut.value != '') {
                    document.formRET.RutH.value = document.formRET.Rut.value;
                }
	        document.formRET.RecolectorH.value = document.formRET.Recolector.value;
	        window.document.formRET.submit();
           }
           else {
              document.formRET.DatosRetAnt.value = 0;
              document.formRET.NuevoRet.value = 0;
	      if (document.formRET.Rut.value != '') {
                  document.formRET.RutH.value = document.formRET.Rut.value;
              }
	      document.formRET.RecolectorH.value = document.formRET.Recolector.value;
	      window.document.formRET.submit();
          }
	}
}

function imprimirRET(){
	var mensaje ="";

	if (document.formRET.NumRET.value == "" || document.formRET.NumRET.value == 0){
	   mensaje = "\n - Ingresar un número de RET mayor a cero";
	}
	if (document.formRET.CodArea.value == "" || document.formRET.CodArea.value == 0){
	   mensaje += "\n - Ingresar código de Area";
	}
	if (document.formRET.Recurso.selectedIndex == 0){
	   mensaje += "\n - Seleccionar un Recurso";
	}
	if (document.formRET.Cantidad.value == "" || document.formRET.Cantidad.value == 0){
	   mensaje += "\n - Ingresar la cantidad del recurso";
	}
	if (document.formRET.UniMed.selectedIndex == 0){
	   mensaje += "\n - Seleccionar la Unidad de Medida del recurso";
	}
	if (mensaje != ""){
	   mensaje = "Falta completar los siguiente campos antes de imprimir el RET: \n \n " + mensaje;
	   alert(mensaje);
	}
	else{
	   flink ('modulos/printRET.asp?Form=formRET',400,400,100,100,'yes','yes');
	}
}

function flink  (URL, nAncho, nAlto, nLeft, nTop, sScroll, sResize){ 
	window.open(URL,'ventana1','width='+nAncho+', height='+nAlto+', left='+nLeft+', top='+nTop+', scrollbars='+sScroll+', menubar=no, location=no, resizable='+sResize+', status=no, toolbar=no, directories=no') 
} 

function ocultarTabla(sID) {
	var ver= document.frmBusAvanzada.checkBA.checked ;
	tabla = document.getElementById(sID);
	if(ver != true) { tabla.style.display = "none"; } //ocultar tabla
	else { tabla.style.display = ""; } 		  //mostrar tabla
}

function NomRec(sform) {
	if (sform.Recurso.options[sform.Recurso.selectedIndex].value != "00"){
		sform.nRecurso.value = sform.Recurso.options[sform.Recurso.selectedIndex].text;
	}
	else { sform.nRecurso.value = "" }
}

function OcultarInf(id1, id2, id3, id4, nInd, nIndC) {
	if (!document.getElementById) return false;
	fila1 = document.getElementById(id1);
	fila2 = document.getElementById(id2);
	fila3 = document.getElementById(id3);
	fila4 = document.getElementById(id4);
	if (nInd != nIndC) {
		fila1.style.display = "none"; //ocultar fila 
		fila2.style.display = "none"; //ocultar fila 
		fila3.style.display = "none"; //ocultar fila 
		fila4.style.display = "none"; //ocultar fila 
	} else {
		fila1.style.display = ""; //mostrar fila 
		fila2.style.display = ""; //mostrar fila 
		fila3.style.display = ""; //mostrar fila 
		fila4.style.display = ""; //mostrar fila 
	}
}

function activarSel(sform) {
	var sValor = new String(sform.CodArea.value);
	if (sValor == "") { 
		sform.Recurso.disabled = true; 
		sform.Cantidad.disabled = true;
	}
	else { 
		sform.Cantidad.disabled = false;
		sform.Recurso.disabled = false; 
		sform.Recurso.length = 1;
	}
}

function nuevoRET(){
	document.formRET.Recurso.disabled = true;
	document.formRET.Cantidad.disabled = true;
	document.formRET.UniMed.disabled = true;
	document.formRET.Rut.disabled = true;
	document.formRET.DV.disabled = true;
	document.formRET.NumRPA.disabled = true;
	OcultarInf('Cel1','Cel2','Cel3','Cel4',0,1);
	document.formRET.reset();
}

function fDatosSoli(sRut, sDV, sOP){
	var strRut = new String(sRut);
	var strDV = new String(sDV);
	var strDVs = "K";
	if (strRut != "" && strDV != ""){
	   if (strDV == "k" || strDV == "K"){ strDV = strDVs; }
	   if (fValidarRut(strRut) != strDV){
	      alert("Rut incorrecto, ingrese nuevamente");
	      document.formRET.Rut.focus();
	   }
	   else{ 
		document.formRET.RutH.value = sRut;
		sURL = 'modulos/recolector.asp?Op='+sOP+'&amp;Form=formRET&amp;Rut='+ strRut +'&amp;DV='+ strDV
		while (sURL.indexOf('amp;')!= -1) { sURL = sURL.replace("amp;", ""); }
		flink (sURL,450,150,150,150,'no','no');
	   }
	}
	else{
	   alert("Debe ingresar el Rut y el Dígito Verficador");
	   if (strRut == ""){ document.formRET.Rut.focus(); }
	   else{ document.formRET.DV.focus();}
	}
}

function fValidarRut(nRut){
	var strRut = new String(nRut);
	var rut = 0;
	var s = 0;
	var l_dv = "";

	rut = strRut;
	for (i=2; i< 8; i++){
	   s = s + ( rut % 10 ) * i;
	   rut = (rut - ( rut % 10 )) / 10;
	}
	s = s + ( rut % 10 ) * 2;
	rut = (rut - ( rut % 10 )) / 10;
	s = s + ( rut % 10 ) * 3;
	rut = (rut - ( rut % 10 )) / 10;
	s = 11 - ( s % 11 );
	if ( s == 10 )
	   l_dv = "K";
	else
	   if ( s == 11 ){
	      l_dv = "0";
	   }
	   else{
	      l_dv = s + "";
	   }
	return( l_dv );
}

// para cambiar la opción -> sform.TipoArea.options[1].selected = true;
// para agregar valores al select -> sform.Recurso.options[sform.Recurso.length] = new Option('(Seleccione Recurso)', '00');
