// Devuelve una lista o -1=no encontrado, -2=demasiado corto.
function duracion(numero, lista)  {
    // Last element unused.
    for (var i=lista.length-2; i>=0; --i) {
	var pref = lista[i][0];
	if (pref.indexOf(numero)==0 && pref!=numero) {
	    return -2;
	}
	if (numero.indexOf(pref)==0) {
	    return lista[i];
	}
    }
    return -1;
}


function dur2str1( duracion ) {
	if (duracion >= 60 ) {
		return "m&aacute;ximo"; // Queda feo en arial/mayuscula inicial
	} else if (duracion > 0 ) {
	        return duracion + " min.";
	} else {
        	return "no permitido";
    	}
}

function dur2str( duracion ) {
	if (duracion == 60 ) {
		return "m&aacute;ximo"; // Queda feo en arial/mayuscula inicial
	} else if (duracion!='') {
		return duracion + " min.";
	} else {
		return "";
	}
}

naran_tbl_start = 
	'<table border="1" id="table1" cellspacing="4" cellpadding="4" '+
	'style=" border-color:#999999; border-style:solid; border-width:1px; border-collapse:collapse; margin-top: 0.15in;">'+
	'<col class="pais">'+
	'<colgroup class="precios">'+
	'<col class="precio">'+
	'<col class="precio">'+
	'</colgroup>'+
	'<colgroup class="precios">'+
	'<col class="precio">'+
	'<col class="precio">'+
	'</colgroup>'+
	'<thead>'+
	'<tr>'+
	'<th width="280" rowspan="2">Prefijo encontrado</th>'+
	'<th colspan="2">Desde Fijo</th>'+
	'<th colspan="2">Desde M&oacute;vil</th>'+
	'</tr>'+
	'<tr>'+
	'<th>Franja Normal</th>'+
	'<th>Franja Reducida</th>'+
	'<th>Franja Normal</th>'+
	'<th>Franja reducida</th>'+
	'</tr>'+
	'</thead>'+
	'<tbody>'+
	'';
naran_tbl_end = 
	'</tbody>'+
	'</script>'+
	'</table>'+
	'';

function haz_fila(fila) {
	document.write( '<tr><th align="left">', fila[0], '</th>');
	for (var i=1; i<5; ++i) {
	document.write( '<td>',dur2str(fila[i]),'</td>');
	}
	document.write( '</tr>');
}


function haz_tabla(tabla)  {
	for (var i=0; i<tabla.length-1; ++i) {
	haz_fila(tabla[i]);
	}
}
		

function calcular() {
    var n = document.getElementById("numero");
    var r = document.getElementById("resultado");
    var nv = n.value;
    var elnumero = nv.replace(/[^0123456789]+/g,'').replace(/^00/,'');
    if (elnumero == '') {
	r.innerHTML= '<h3 style="margin-top: 0.15in" >Introduzca el n&uacute;mero ( con indicativo de pa&iacute;s ) a comprobar.</h3>';
        
    } else {
	var item = duracion(elnumero,lista_4B_SD);
	if (item == -2) {
	    r.innerHTML= '<h3 style="margin-top: 0.15in" >"'+elnumero+'" es demasiado corto para determinar su duraci&oacute;n.</h3>';
	} else if (item == -1) {
	    r.innerHTML= '<h3 style="margin-top: 0.15in" >El n&uacute;mero '+elnumero+' no est&aacute; habilitado para llamar a casa.</h3>';
	} else { // Must be a list
	    r.innerHTML = 
		naran_tbl_start + 
		'<tr><th>' + item[0] + '</th>' +
		'<td>'+dur2str1(item[1]) + '</td>' +
		'<td>'+dur2str1(item[2]) + '</td>' +
		'<td>'+dur2str1(item[3]) + '</td>' +
		'<td>'+dur2str1(item[4]) + '</td>' +
		naran_tbl_end;
	}
    } 
    n.focus()
}
