//sacar caracteres peligrosos

function sacarC(cad){
        var  aux;
        var exp = /<|>|'|"/;
        //var exp = /<|>|'/;
        aux = '';
        while (cad!=aux){
                aux = cad;
                cad = aux.replace(exp,'');
        }
        return cad;
}

//Validaciones

function salirEMail(input){
        var aux, valido;
        valido = true;
        aux = input.value;
        if (aux != "") {
                if (aux.match(/\w+@\w+.\w+/) == null) {
                        valido = false;
                        alert('Dirección de correo electrónico errónea. Por favor, reintroduzca el dato');
                        input.focus();
                }
        }
        return valido;
}

function salirFloat(input){
        var aux, valido;
        valido = true;
        aux = input.value;

        var partes = aux.split(',');
        if (partes.length > 2) {
                valido = false;
                alert('Valor no válido. Por favor, reintroduzca el dato');
                input.focus();
        }

        if ((aux != "") && (valido)){
                if (aux.match(/\d+,*\d*/) == null) {
                        valido = false;
                        alert('Valor no válido. Por favor, reintroduzca el dato');
                        input.focus();
                }
        }
        return valido;
}

function CalculaDC(Banco, Cuenta){
     // By Dragon Changer
     Pesos= new Array(6,3,7,9,10,5,8,4,2,1);
     var result ='';
     var iTemp =0;
     for (var n=0;n<=7;n++){
        iTemp  = iTemp + Banco.substr(7 - n, 1) * Pesos[n];
     }
     result = 11 - iTemp % 11;
     if (result > 9){
       result=1-result % 10;
     }
     iTemp=0;
     for (var n=0;n<=9;n++){
        iTemp  = iTemp + Cuenta.substr(9 - n, 1) * Pesos[n];
     }
     iTemp =11 - (iTemp % 11);
     if (iTemp > 9){
      iTemp =1-(iTemp % 10);
     }
     result=result*10+iTemp;
     return(result);
}

function formatearCuenta(cuenta){
        return cuenta.substr(0,4) + '-' + cuenta.substr(4,4) + '-' + cuenta.substr(8,2) + '-' + cuenta.substr(10,10);
}

function salirFecha(input){
        var aux = input.value;
        if (aux != "") {
                if (!compFecha(aux)) {
                        alert('Fecha incorrecta.\nPor favor, reintroduzca fecha.\n\nDebería tener este formato dd/mm/aaaa');
                        input.focus();
                        return false;
                }
        }
        return true;
}

function compFecha(cad){
    var valida = true;
    var encontro = 0;
    var aux = '';
    var array;
    var Expresion = /\d{1,2}\/\d{1,2}\/\d{2}$|\d{1,2}\/\d{1,2}\/\d{4}$/

    if (cad.match(Expresion) == null) {
                valida = false;
    }

    if (valida) {
        array = cad.split('/');
        var dia = array[0];
        var mes = array[1];
        var ano = array[2];
    }
    if (valida && (ano < 50)) {
                ano = 2000 + ano;
    }
    if (valida && (ano < 100)) {
                ano = 1900 + ano;
    }

    if (valida && ((mes < 1) || (mes > 12))){
                valida = false;
    }
    if (valida && ((dia < 1) || (dia > 31))){
                valida = false;
    }
    if (valida && ((mes == 1) || (mes == 3) || (mes == 5) || (mes == 7)
                         || (mes == 8) || (mes == 10) || (mes == 12))) {
                valida = true;
    }else {
                if (mes != 2) {
                        if (dia > 30) {
                                valida = false;
                        }
                } else {
                        var bisiesto = false;
                        if (ano % 4 == 0){
                                bisiesto = true;
                                if (ano % 100 == 0){
                                        bisiesto = false;
                                        if (ano % 400 == 0) {
                                                bisiesto = true;
                                        }
                                }
                        }
                        if (bisiesto) {
                                if (dia > 29) {
                                        valida = false;
                                }
                        } else {
                                if (dia > 28) {
                                        valida = false;
                                }

                        }
                }
    }

    return valida;
}

function SalirNumBol(input){
                if (input.value != "") {
                if (!compNumBol(input.value)) {
                        alert('Formato incorrecto. Por favor, reintroduzca dato (999/9999)');
                        input.select();
                        input.focus();
                        return false;
                }
        }
        return true;
}

function compNumBol(cad){
    var valido = true;
    var Expresion = /\d{1,3}\/\d{2}$|\d{1,3}\/\d{4}$/

    if (cad.match(Expresion) == null) {
                valido = false;
    }

    return valido;
}

//Eventos

/*
event.keyCode = caracter
a = 97
z = 122
A = 65
Z = 90
' = 39
" = 34
_ = 95
- = 45
& = 38
. = 46
/ = 47
< = 60
> = 62
ñ = 241
Ñ = 209
*/

function generalKeyPress(){
        if ((event.keyCode == 39) ||
                (event.keyCode == 34) ||
                (event.keyCode == 60) ||
                (event.keyCode == 62)) {
                event.keyCode = 0;
        }
}

function fechaKeyPress(ev) {
				mi_evento=teclaEvento(ev)
        if (mi_evento != null) {
                return ((mi_evento >= 47) && (mi_evento <= 57));
        }else{
                return false;
        }
}

function numKeyPress(){
        if ((event.keyCode < 48) || (event.keyCode > 57)) {
                return false;
        }
        return true;
}


function numYComaKeyPress(ev){
				mi_evento=teclaEvento(ev)
        if (((mi_evento < 48) || (mi_evento > 57)) && (mi_evento != 44)) {
                return false;
        }
        return true;
}

function mayYNumKeyPressSinEsp(){ // letras cambia minusculas a mayusculas y admite números y _
        if (((event.keyCode <= 122) && (event.keyCode >= 97)) || (event.keyCode == 241)) {
                event.keyCode = event.keyCode - 32;
        }
        if (((event.keyCode >= 48) && (event.keyCode <= 57)) ||
                        ((event.keyCode >= 65) && (event.keyCode <= 90)) ||
                        (event.keyCode == 95) ||
                        (event.keyCode == 209))
                        {
                return true;
        }
        return false;
}

function aliasKeyPress(){ // letras cambia minusculas a mayusculas y admite números y _
        if (((event.keyCode <= 122) && (event.keyCode >= 97)) || (event.keyCode == 241)) {
                event.keyCode = event.keyCode - 32;
        }
        if (((event.keyCode >= 48) && (event.keyCode <= 57)) ||
                        ((event.keyCode >= 65) && (event.keyCode <= 90)) ||
                        (event.keyCode == 95) ||
                        (event.keyCode == 45) ||
                        (event.keyCode == 38) ||
                        (event.keyCode == 209) ||
                        (event.keyCode == 46))
                        {
                return true;
        }
        return false;
}

function mayYNumKeyPress(){ // letras cambia minusculas a mayusculas y admite números y _
        if (((event.keyCode <= 122) && (event.keyCode >= 97)) || (event.keyCode == 241)){
                event.keyCode =  event.keyCode - 32;
        }
        if (((event.keyCode >= 48) && (event.keyCode <= 57)) ||
                        ((event.keyCode >= 65) && (event.keyCode <= 90)) ||
                        (event.keyCode == 95) ||
                        (event.keyCode == 32) ||
                        (event.keyCode ==209))
                        {
                return true;
        }
        return false;
}

function letraYNumKeyPress(){ // admite letras, números y _
        if (((event.keyCode >= 48) && (event.keyCode <= 57)) ||
                        ((event.keyCode >= 65) && (event.keyCode <= 90)) ||
                        ((event.keyCode >= 97) && (event.keyCode <= 122)) ||
                        ((event.keyCode == 95) ||
                        (event.keyCode == 209) ||
                        (event.keyCode == 241))
                        ){
                return true;
        }
        return false;
}

function numBarraKeyPress(){ // Numeros y la barra de división
        if ((event.keyCode < 47) || (event.keyCode > 57)){
                return false;
        }
        return true;
}

//Cadenas

function eliminarEspacios(cad) {
    if (cad.length == 0) {
        return '';
    }
    var inicial = cad;
    var elfinal = '';
    var i = 0;
    // quitar espacios al principio
    while(inicial.charAt(0)==' '){
        inicial = inicial.substring(1,inicial.length)
    }
    // quitar espacios al final
    while(inicial.charAt(inicial.length-1)==' ') {
        inicial = inicial.substring(0,inicial.length-1);
    }
    // quitar más de un espacio en el medio del texto
    while (i < inicial.length){
        if ((inicial.substring(i,i + 2))==('  ')){
            i++;
        }else{
            elfinal = elfinal + inicial.substring(i,i+1);
            i++;
        }
    }
    return elfinal;
}

function eliminarComillas(cad) {
    var AuxCad = '';
    var i;
    for(i = 0;i<cad.length;i++){
        if ((cad.charAt(i)!='"') && (cad.charAt(i)!="'")){
            AuxCad += cad.charAt(i);
        }
    }
    return AuxCad;
}

//Impresión

function ImprimirPag(pag){
        var waux;
        var PosX,PosY;
        PosX = 0;
        PosY = 0;
        waux = window.open('/index/imprimir.asp?pag=' + pag,'prepImpresion',
                        'fullscreen=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,' +
                        'width=200,height=100,top=' + PosY + ',left=' + PosX,true);
/*        waux = window.open('/index/imprimir.asp?pag=' + pag,'prepImpresion',
                        'fullscreen=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,' +
                        'width=500,height=500,top=' + PosY + ',left=' + PosX,true);
*/
}

function imprime(){
        //window.parent.frames[0].document.body.innerHTML = '&nbsp;';
        window.parent.frames[0].document.body.all.centro.innerHTML = '<a href="javascript:window.parent.close();" class=otroslinks>CERRAR</a>';
        window.parent.frames[1].focus();
        window.parent.frames[1].print();
        //parent.window.close();
}



function teclaEvento(evento){ 
	if (evento.keyCode)
		return evento.keyCode;
	else
		return evento.which;
}

function continuarEvento(evento,continuar){ 
	if (evento.preventDefault && !continuar){
		evento.preventDefault();
		evento.stopPropagation();
	}
	return continuar;
}

function ValidarDecimal(event,txtCampo){
	var ac;
	//no utilizo el name ya tengo la referencia del text
	var texto = txtCampo.value;

	ac = teclaEvento(event);
	if ((ac >= 48) && (ac <= 57) || (ac == 44) || (ac == 46) || (ac == 13) || (ac == 8) || (ac == 9)) {
	
	}else {
		if (ac == 46) {
		//en fire fox no puedes cambiar el codigo de la tecla
		//o no lo he visto lo que puedes hacer es cancelar el evento
		//window.event.keyCode = ac - 2;
		continuarEvento(event,false)
		}else {
			if (ac == 13) {
				window.lnkIntro.click();
			}else {
				//window.event.keyCode = " ";
				//esta funcion te garantiza que canceles el evento tanto en ie como en ff
				return continuarEvento(event,false)
			}
		}
		//document.forms.item(0).item(txtCampo).value = texto;
		texto = "";
	}
}
