var unidades = ["", "un", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince", "diecis&eacute;is", "diecisiete", "dieciocho", "diecinueve", "veinte", "veinti&uacute;n", "veintid&oacute;s", "veintitr&eacute;s", "veinticuatro", "veinticinco", "veintis&eacute;is", "veintisiete", "veintiocho", "veintinueve"];
var decenas = ["","","","treinta y ", "cuarenta y ", "cincuenta y ", "sesenta y ", "setenta y ", "ochenta y ", "noventa y "];
var centenas = ["", "ciento ", "doscient@s ", "trescient@s ", "cuatrocient@s ", "quinient@s ", "seiscient@s ", "setecient@s ", "ochocient@s ", "novecient@s "]
var ordenes = ["", " mil", " millones", " mil", " billones", " mil", " trillones", " mil", " cuatrillones", " mil"];
var gen = "num";
var cifras = "0";

window.onload = function(){
	if (document.getElementById("numero") == null) return true;
	with(document.getElementById("numero"))
	{
		value = "0";
		focus();
		select();
		onkeyup = function(){leerNumero()};
	}
	document.getElementById("gen_num").checked = true;
	document.getElementById("sumar_uno").onclick = function(){sumar(+1); return false;};	
	document.getElementById("restar_uno").onclick = function(){sumar(-1); return false;};
	document.getElementById("gen_num").onclick = function(){gen = "num"; leerNumero();};
	document.getElementById("gen_fem").onclick = function(){gen = "fem"; leerNumero();};
	document.getElementById("gen_mas").onclick = function(){gen = "mas"; leerNumero();};
}

function leerNumero()
{
  var numeral = "";

  var trozos = trocear(new String(document.getElementById("numero").value));

  for (i=0;i<trozos.length;i++) numeralizar(trozos, i);

  numeral += trozos.reverse().join(" ");

	if (numeral.length == 0) numeral = "cero";

	if (/((ll&oacute;n)|(llones))\s*$/.test(numeral) && gen != "num") numeral += " de";
	
	if (gen == "fem")
		numeral += (cifras=="1 "?" libra":" libras");
	else if (gen == "mas")
		numeral += (cifras=="1 "?" d&oacute;lar":" d&oacute;lares");

	if (gen != "num") cifras += (gen=="fem"?"&pound;":"$");

	document.getElementById("resultado").innerHTML = cifras + "<br><br>" + numeral;
}

function sumar(sumando)
{
	var numero;
	var cadena = new String(document.getElementById("numero").value);
	cadena = cadena.replace(/\D*/g,"").replace(/^0*/,"");
	if (cadena == "") cadena = "0";
	numero = parseInt(cadena,10);
	if ((sumando > 0 && numero >= 0 && numero < 1000000000000000) ||
	    (sumando < 0 && numero > 0 && numero <= 1000000000000000))
	{
		numero += sumando;
		document.getElementById("numero").value = numero;
		leerNumero();
	}
}

function numeralizar(trozos, pos)
{
	var centena, decena, unidad, numeral;

	centena = trozos[pos].charAt(0);
	decena = trozos[pos].charAt(1);
	unidad = trozos[pos].charAt(2); 

	if (parseInt(decena,10) < 3)
  	numeral = new String(centenas[centena] + unidades[parseInt(decena + unidad,10)]);
  else 
  	numeral = new String(centenas[centena] + decenas[decena] + unidades[unidad]);

	switch (centena+decena+unidad)
	{
		case "000":
			if (ordenes[pos] != " mil" && trozos[pos+1] != "000")
				numeral += ordenes[pos];
			break;
		case "001":
			numeral += ordenes[pos];
			numeral = numeral.replace(/^un\smil$/,"mil");
			if (pos == trozos.length - 1 || (pos < trozos.length - 1 && trozos[pos+1] == "000"))
				numeral = numeral.replace(/llones$/, "ll&oacute;n");
			if (pos == 0 && gen != "mas")
				numeral = (gen=="fem" ? "una" : "uno");
			break;
		case "100":
			numeral = "cien " + ordenes[pos];
			break;
		default:
			numeral = numeral.replace(/@/g,((gen=="fem" && pos<2) ? "a" : "o"));
			numeral = numeral.replace(/\sy\s$/,"");
			numeral += ordenes[pos];
	}

	if (pos == 0 && gen != "mas")
	{
  	numeral = numeral.replace(/veinti&uacute;n$/, (gen == "fem" ? "veintiuna" : "veintiuno"));
		numeral = numeral.replace(/un$/, (gen == "fem" ? "una" : "uno"));
  }
	else if (pos == 1 && gen == "fem")
	{
		numeral = numeral.replace(/veinti&uacute;n/, "veintiuna");
		numeral = numeral.replace(/un\smil/, "una mil");
	}
	trozos[pos] = numeral;
}
	
function trocear(cadena)
{
	var trozos = new Array();
	var numTrozos = 0;
	var numDigitos = 0;

	cadena = cadena.replace(/\D*/g,"").replace(/^0*/,"");
	if (cadena == "")
		cadena = "0";

	cifras = "";
	for (var i=cadena.length;i>0;i-=3)
	{
		numTrozos = trozos.push(cadena.substring(i-3,i));
		cifras = trozos[numTrozos-1] + " " + cifras;
	}

	numDigitos = trozos[numTrozos-1].length;
	if (numDigitos < 3)
		trozos[numTrozos-1] = (numDigitos==2?"0":"00") + trozos[numTrozos-1];

	return trozos;
}
