﻿function textos() {
    var premio = 0;
    var str = "";
    Limpiar();
    var numero = normalizaNumero();
    if (numero != null) {
        premio = calcula(numero);
        document.getElementById('numeroPremiado2').value = numero;
        if (premio > 0) {
            str = "¡Enhorabuena! Su décimo ha sido premiado con <strong class='premiado'>";
            str = str + format(premio.toString()) + " €.</strong><br /><br />";
            str = str + "<label>Numero</label><br />";
            str = str + "<h2>" + numero + "</h2>";
            document.getElementById("Mensaje").innerHTML = str;
        }
        else {
            if (premio == -1) {
                document.getElementById("Mensaje").innerHTML = "Los premios aún no estan disponibles.";
            }
            else {
                document.getElementById("Mensaje").innerHTML = "Lo sentimos, su décimo no ha sido premiado.";
            }
        }
    }
}
                
function format(str) 
{
	var result = "";
	var pp = str;
	for (i = pp.length - 1; i >= 0; i--) {
		result = pp.charAt(i) + result;
		if (!((pp.length - i) % 3) && i > 0) {
		    result = "." + result;
		}
	}
	return result;
}
        
function normalizaNumero()
{
    var retValue = "";
    var errMensaje = "";
    var numero = document.getElementById('numeroPremiado2').value;
	
    if (numero == '') {
        numero = querySt('numero');
    }
    if (numero != null) {
        if (numero.length > 0) {
            if (!isNaN(numero)) {
                retValue = parseInt(numero);
            }
        }
    } 
	
	if (!isNaN(numero)) {
		if ((retValue) > 99999 || (retValue) < 0) {
			document.getElementById("resultado").style.display = "none";
			errMensaje = "El valor introducido debe estar comprendido entre 0 y 99999";
			document.getElementById("msgError").innerHTML = errMensaje;
			document.getElementById("divError").style.display = "block";
		}
		else {
			var numeroNormalizado = "000000" + numero;
			return numeroNormalizado.substring(numeroNormalizado.length - 5, numeroNormalizado.length);
		}
    }
    else{
        document.getElementById("resultado").style.display = "none";
        errMensaje = "Formato del valor no correcto.";
        document.getElementById("msgError").innerHTML = errMensaje;
        document.getElementById("divError").style.display = "block";
    }
}

function Limpiar(){
    document.getElementById("Mensaje").innerHTML = "";
    document.getElementById("aa").innerHTML = "";
    document.getElementById("msgError").innerHTML = "";
}

function validar(e) 
{
	var tecla = (document.all) ? e.keyCode : e.which;
	if (tecla==13) {
		return false;
	}
	if (tecla<48 || tecla >57) {
		var s = document.getElementById("numeroPremiado2").innerHTML;
		if (!isInteger(s)) {
			if (tecla == 8 || tecla == 9 || tecla == 27 || tecla == 32 || tecla == 127) return true;
			//alert("Debes entrar valores válidos.");
		}
		return false;
	}
	return true;	
} 

