﻿document.write("<script src='../js/jquery.js' type='text/javascript'></script>");
function validarAlfabetico(obj,id_div,mensaje) {
	var div = document.getElementById(id_div);
	var s = obj.value;

	var filter = /^[A-Za-z ]*$/;
	if (s.length == 0) return true;
	if (filter.test(s)) {
		ocultarMensaje(div);
		return true;
	} else {
		mostrarMensaje(div, mensaje);
		return false;
	}
}

function validarRutConDigitoAparte(Rut,dvr, Texto, obj) { // r=objeto a validar, texto=nombre de entidad(deudor, cliente,etc.), obj = objeto div

    obj.innerHTML = '';
    obj.style.display = 'none';

    if (validarVacio(Rut, obj, "Ingrese Rut " + Texto + ".") == false) {
        return false
    }
    if (validarVacio(dvr, obj, "Ingrese Digito Verificador Rut " + Texto + ".") == false) {
        return false
    }

//    if (Rut.value.indexOf('-') == -1) {
//        obj.innerHTML = '<img src =" ../img/exclamation.gif"> Ingrese Rut ' + Texto + '  con dígito verificador.';
//        obj.style.display = 'block';
//        return false
//    }

//    var i = 0;
//    var rutTemp = '';
//    while (Rut.value.substring(i, i + 1) != '-') {
//        rutTemp += Rut.value.substring(i, i + 1);
//        i = i + 1;
//    }
//    var DV = Rut.value.substring(i + 1, i + 2);
//    if (DV == 'k') {
//        DV = 'K';
//    }

    var count = 0;
    var count2 = 0;
    var factor = 2;
    var suma = 0;
    var sum = 0;
    var digito = 0;
    count2 = Rut.value.length - 1;
    while (count < Rut.value.length) {

        sum = factor * (parseInt(Rut.value.substr(count2, 1)));
        suma = suma + sum;
        sum = 0;

        count = count + 1;
        count2 = count2 - 1;
        factor = factor + 1;

        if (factor > 7) {
            factor = 2;
        }
    }
    digito = 11 - (suma % 11);

    if (digito == 11) {
        digito = 0;
    }
    if (digito == 10) {
        digito = "K";
    }

    if (digito != dvr.value) {
        obj.innerHTML = '<img src ="../img/exclamation.gif" >  Rut ' + Texto + ' no válido.';
        obj.style.display = 'block';
        return false
    }
    return true
}

function validarVacio(objeto, div, mensaje) {

    if (objeto.value == "") {
        mostrarMensaje(div , mensaje);
        return false
    }
	return true;
}

function validarCombo(objeto, div, mensaje) {

    if (objeto.value == 0) {
        mostrarMensaje(div, mensaje);
        return false
    }
    return true;
}


function validarDosRadio(objeto1,objeto2, div, mensaje) {

    if (objeto1.checked == false && objeto2.checked == false) {
        mostrarMensaje(div, mensaje);
        return false
    }
    return true;
}



function mostrarMensaje(nombreDiv, mensaje) {

    if (typeof nombreDiv == "string") {
        nombreDiv = document.getElementById(nombreDiv);
    }
        nombreDiv.innerHTML = '<img src ="../img/exclamation.gif">  ' + mensaje;
        nombreDiv.style.display = 'block';
        
    
}
function ocultarMensaje(nombreDiv) {
	try{
		nombreDiv.style.display = 'none';
	}catch(ex){
		alert("Error en ocultarMensaje. Detalle: " + ex.description);
	}
     
   // nombreDiv.style.visibility = 'hidden';
    
}

function ocultarMensajeSlideUp(nombreDiv) {
    try { nombreDiv.slideUp(500); } catch (ex) { }
}

function validaMail(objeto, div, mensaje) {

    var s = objeto.value;
    var filter = /^[A-Za-z.][A-Za-z0-9._-]*@[A-Za-z0-9._-]+\.[A-Za-z0-9_.-]+[A-za-z.-]$/;
    if (s.length == 0) return true;
    if (filter.test(s))
        return true;
    else
        mostrarMensaje(div, mensaje);
    return false;
}




function validaLargo(objeto, min, max, div, mensaje) {

    var valor = objeto.value;

    if ((valor.length >= min) && (valor.length <= max))
        return true;
    else
        mostrarMensaje(div, mensaje);
    return false;
}

function validaFono(objeto, div, mensaje) {
    var s = objeto.value;
    var filter = /^\(?[0-9]{0,3}\)?\-?\ ?[0-9]{6,7}$/;
    if (s.length == 0) return true;
    if (filter.test(s))
        return true;
    else
        mostrarMensaje(div, mensaje);
    return false;

}

function validaIguales(objeto, objetoComparar, div, mensaje) {
    if (objeto.value == objetoComparar.value) { return true }
    else { mostrarMensaje(div, mensaje); return false; }
}

function ValidarVacioDependiendedeotro(objeto, objeto2, div, mensaje) {
    if (objeto.checked == true && objeto2.value == "") {
        mostrarMensaje(div, mensaje)
        return false;
    }
  

}


function ConfirmarEliminacion(texto) {
    if (confirm('Realmente desea  ' + texto) == true) {
        return true;
    }
    else {
        return false;
    }
}


function valida_numeros(objeto, div, mensaje) {
    var s = objeto.value;
    var filter = /^[0-9]*$/;
    if (s.length == 0) return true;
    if (filter.test(s)) {
        ocultarMensaje(div);
        return true;
    }
    else
    { 
        mostrarMensaje(div, mensaje);  
    return false;
    }
}
function valida_numeros2(evt) {

    var nav4 = window.Event ? true : false;

    var key = nav4 ? evt.which : evt.keyCode;

    return (key <= 13 || (key >= 48 && key <= 57) || key == 47);

}
function EsMayor(ObjetoMenor, ObjetoMayor, div, mensaje) {

    if (parseInt(ObjetoMenor.value) > parseInt(ObjetoMayor.value)) {
        mostrarMensaje(div, mensaje);
        return false;
    }

}
//-------------------------------------------------------------------

function fechaValCompromisoPago(F_Pago, div, mensaje) {
    //var diasVal = document.getElementById('HiddenField1').value;
    //var diasValSplit = diasVal.split('-');

    var FechaMax1 = new Date();
    FechaMax2 = FechaMax1.setDate(FechaMax1.getDate() + 5); //diasValSplit[1];
    var FechaMax3 = new Date(FechaMax2);
    dia = FechaMax3.getDate() 
    mes = FechaMax3.getMonth() + 1;
    anno = FechaMax3.getYear();
    if (dia < 10) dia = "0" + dia;
    if (mes < 10) mes = "0" + mes;
    fechaMax = anno + "-" + mes + "-" + dia;

    var FechaAlert1 = new Date();
    FechaAlert2 = FechaAlert1.setDate(FechaAlert1.getDate() + 3); //diasValSplit[0];
    var FechaAlert3 = new Date(FechaAlert2);
    dia2 = FechaAlert3.getDate()
    mes2 = FechaAlert3.getMonth() + 1;
    anno2 = FechaAlert3.getYear();
    if (dia2 < 10) dia2 = "0" + dia2;
    if (mes2 < 10) mes2 = "0" + mes2;
    fechaAlerta = anno2 + "-" + mes2 + "-" + dia2;

    if (F_Pago.value > fechaAlerta && F_Pago.value > fechaMax) {
        var mensaje = "Fecha compromiso No debe ser mayor a 5 días de la fecha de hoy.";
        mostrarMensaje(div, mensaje);
        return false;
    } else if (F_Pago.value > fechaAlerta) {
        //MUESTRA POP-UP
        if (confirm("La fecha de compromiso es mayor a 3 días de la fecha de hoy.\n¿Desea guardar la gestión de todas formas?."))
            return true;
        else
            return false;
    } 
    ocultarMensaje(div);
    return true;
}

//------------------------------------------------------------------- 

function fechaMayorOIgualQue(F_Pago, div,mensaje) {
    var FechActual = new Date();
    dia = FechActual.getDate();
    mes = FechActual.getMonth() + 1;
    anno = FechActual.getYear();
    if (dia < 10) dia = "0" + dia;
    if (mes < 10) mes = "0" + mes;
    //fechaHoy = dia + "/" + mes + "/" + anno;
    fechaHoy = anno + "-" + mes + "-" + dia;
    fechaHoy2 = dia + "-" + mes + "-" + anno;
    if (F_Pago.value < fechaHoy) {
        mostrarMensaje(div, mensaje + fechaHoy2 + '');
        return false;
    }
    ocultarMensaje(div);
    return true;

}  




function esFechaValida(objeto, div) {

    if (objeto != undefined && objeto.value != "") {
        if (!/^\d{2}\/\d{2}\/\d{4}$/.test(objeto.value)) {
            mostrarMensaje(div, "formato de fecha no válido (dd/mm/aaaa)");
            return false;
        }
        var dia = parseInt(objeto.value.substring(0, 2), 10);
        var mes = parseInt(objeto.value.substring(3, 5), 10);
        var anio = parseInt(objeto.value.substring(6), 10);

        switch (mes) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                numDias = 31;
                break;
            case 4: case 6: case 9: case 11:
                numDias = 30;
                break;
            case 2:
                if (comprobarSiBisisesto(anio)) { numDias = 29 } else { numDias = 28 };
                break;
            default:
                mostrarMensaje(div, "Fecha introducida errónea");
                return false;
        }

        if (dia > numDias || dia == 0) {
            mostrarMensaje(div, "Fecha introducida errónea");
            return false;
        }
        return true;
    }
}
//new Date()-->obtener fecha actual (para otra validacion de fecha...)

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//AVISA EN CASO QUE BLOQ MAYUSC ESTÉ ACTIVADO.
function capLock(e) {
    var Mayusc = document.getElementById("Mayusc");
    kc = e.keyCode ? e.keyCode : e.which;
    sk = e.shiftKey ? e.shiftKey : ((kc == 16) ? true : false);
    if (((kc >= 65 && kc <= 90) && !sk) || ((kc >= 97 && kc <= 122) && sk)) {
        Mayusc.innerHTML = "<img src ='img/exclamation.gif'> Bloq Mayús Activado!";
    } else {
        Mayusc.innerHTML = "";
    }
}




////////////////////////////////////////////////////////////////////////////////////////////////////////////
//CAMBIARÁ DE COLOR DEGRADANDO DESDE AMARILLO HASTA BLANCO DESPUES DE PRESIONAR UN BOTÓN
hexadecimal = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F")
function convierteHexadecimal(num) {
    var hexaDec = Math.floor(num / 16)
    var hexaUni = num - (hexaDec * 16)
    return hexadecimal[hexaDec] + hexadecimal[hexaUni]
}
//desde FFFF00 (AMARILLO)
//hasta FFFFFF (BLANCO)

function degradado(cld) {

    //NOMBRE DE LA CELDA
    // celda = document.getElementById(cld)
    //while (color3 != 255) {

    color3++
    color_hexadecimal = convierteHexadecimal(color3)

    if (color3 < 255 && color3 > 0) { //AL INTERCAMBIAR RÁPIDAMENTE LA FILA SELECCIONADA OCURRE ERROR.. ESTA CONDICIÓN EVITA EL ERROR
        cld.style.backgroundColor = "FE" + color_hexadecimal + color_hexadecimal
    }

    //RETARDO
    if (color3 < 255)
        setTimeout("degradado(" + cld.id + ")", 1)
    //    }
}


function Login() {
    if (document.getElementById("LBL_Mensajes") != undefined) {
        document.getElementById("LBL_Mensajes").style.visibility = "hidden";
    }

    if (document.getElementById("TXT_Usuario").value == '') {
        SPN_Mensajes.innerHTML = '<img src ="img/exclamation.gif">  ' + ' Ingrese su usuario';
        document.getElementById("SPN_Mensajes").style.visibility = "visible";

        color3 = 50
        //         degradado(document.getElementById("CELL_Usuario"))
        return false

    } else if (document.getElementById("TXT_Pass").value == '') {
    SPN_Mensajes.innerHTML = '<img src ="img/exclamation.gif">  ' + ' Ingrese su contraseña';
        document.getElementById("SPN_Mensajes").style.visibility = "visible";
        color3 = 50
        //         degradado(document.getElementById("CELL_Pass"))
        return false
    }
   }



   function validarRut(Rut, Texto, id_obj) { // Rut = objeto a validar, texto=nombre de entidad(deudor, cliente,etc.), obj = id objeto div que tendrá mensaje

   	var obj = document.getElementById(id_obj);
   	obj.innerHTML = '';
   	obj.style.display = 'none';

   	if (validarVacio(Rut, obj, "Ingrese Rut " + Texto + ".") == false) {
   		return false
   	}

   	if (Rut.value.indexOf('-') == -1) {
   		obj.innerHTML = '<img src =" ../img/exclamation.gif"> Ingrese Rut ' + Texto + '  con dígito verificador.';
   		obj.style.display = 'block';
   		return false
   	}

   	var i = 0;
   	var rutTemp = '';
   	while (Rut.value.substring(i, i + 1) != '-') {
   		rutTemp += Rut.value.substring(i, i + 1);
   		i = i + 1;
   	}
   	var DV = Rut.value.substring(i + 1, i + 2);
   	if (DV == 'k') {
   		DV = 'K';
   	}

   	var count = 0;
   	var count2 = 0;
   	var factor = 2;
   	var suma = 0;
   	var sum = 0;
   	var digito = 0;
   	count2 = rutTemp.length - 1;
   	while (count < rutTemp.length) {

   		sum = factor * (parseInt(rutTemp.substr(count2, 1)));
   		suma = suma + sum;
   		sum = 0;

   		count = count + 1;
   		count2 = count2 - 1;
   		factor = factor + 1;

   		if (factor > 7) {
   			factor = 2;
   		}
   	}
   	digito = 11 - (suma % 11);

   	if (digito == 11) {
   		digito = 0;
   	}
   	if (digito == 10) {
   		digito = "K";
   	}

   	if (digito != DV) {
   		obj.innerHTML = '<img src =" ../img/exclamation.gif">  Rut ' + Texto + ' no válido.';
   		obj.style.display = 'block';

   		return false
   	}
   	return true
   }

   function bloquear(obj, valor) {

   	if (typeof obj == "string")
   		obj = document.getElementById(obj);
    obj.disabled = valor;
}


//carga una ventana aprte hay que pasarle la url de parametro
function Abrir_ventana(pagina,titulo,ancho,alto) 
{
    var opciones = "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes,top=100px, left=20px";
    var anchoalto = opciones + ", width=" + ancho + "px, height=" + alto + "px";
    window.open(pagina, titulo, anchoalto);
}

function centrarVentana(ancho,alto) {
    x = ((screen.width)- ancho) / 2;
    y = ((screen.height)- alto) / 2;
    moveTo(x, y);
}

////////////////////

function format(input)
{
var num = input.value.replace(/\./g,'');
if(!isNaN(num)){
num = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1.');
num = num.split('').reverse().join('').replace(/^[\.]/,'');
input.value = num;
}
}

