function validaNumero(caja, idJuego) 
{
	valor = parseInt(caja.value);
	if (isNaN(valor) || valor == 0)
		caja.value = "";
	else
	{
	    if (document.getElementById("showPosibles"))
	    	if (document.getElementById("showPosibles").checked)
			{
				caja.maxLength = "4";
				if (valor > 9 && caja.className.lastIndexOf("Posibles") == -1)
					caja.className = caja.className + "Posibles";
			} 
//	    	else
//			{
//				caja.maxLength = "1";
//				if (caja.className.lastIndexOf("Posibles") != -1)
//					caja.className = caja.className.substring(0, caja.className.length - 8);
//			}
//	    	else
//			{
//				caja.maxLength = "1";
//				if (valor > 9)
//					caja.value = "";
//				if (caja.className.lastIndexOf("Posibles") != -1)
//					caja.className = substring(0, caja.className.length - 8);
//			}
	    i = new Number(caja.id.substring(4, 5));
	    j = new Number(caja.id.substring(6, 7));
	    pos = i * 9 + j;
	    if (valor <= 9)
	    {
			tiempo = temporizador;
	    	actualizaSol(pos, caja.value, idJuego, tiempo);
		} else
		{
			desActualizaSol(pos);
		}
	}
}
function actualizaSol(pos, valor, idJuego, tiempo)
{
	sol = document.getElementById("solucion").innerHTML;
	sol = sol.substring(0, pos) + valor + sol.substring(pos + 1, 81);
	if (sol.indexOf('.') == -1)
	{
		ajax=objetoAjax();
//		alert("consulta.php?act=val&idJuego=" + idJuego + "&sol=" + sol + "&tiempo=" + (tipoJuego == "com" ? tiempo : "-1") + "&opcJuego=" + opcJuego);
        ajax.open("GET", "consulta.php?act=val&idJuego=" + idJuego + "&sol=" + sol + "&tiempo=" + (tipoJuego == "com" ? tiempo : "-1") + "&opcJuego=" + opcJuego);
        ajax.onreadystatechange=function() 
        {
        	if (ajax.readyState==4)
        	{
        		//alert(ajax.responseText);
        		result = ajax.responseText;
        		if(result == "OK")
        		{
        			m = Math.floor(tiempo / 60);
        			s = tiempo % 60;

        			document.getElementById('tiempoGanado').innerHTML = m + '&nbsp;minutos y&nbsp;' + s + '&nbsp;segundos.';
        			document.getElementById("central").innerHTML = document.getElementById("juegoGanadoHidden").innerHTML;
        			document.getElementById('cronometro').innerHTML = "";
        			clearTimeout(tempor);
        		} //else
        			//alert(result);
        	}
        };
        ajax.send(null);
	}
	document.getElementById("solucion").innerHTML = sol;
}
function desActualizaSol(pos)
{
	sol = document.getElementById("solucion").innerHTML;
	document.getElementById("solucion").innerHTML = sol.substring(0, pos) + '.' + sol.substring(pos + 1, 81);
}
function muestraPista(caja, idJuego)
{
	if (document.getElementById("showHint") && document.getElementById("showHint").checked)
	{
		tiempo = temporizador;
        ajax=objetoAjax();
        i = new Number(caja.id.substring(4, 5));
        j = new Number(caja.id.substring(6, 7));
        pos = i * 9 + j;
        ajax.open("GET", "consulta.php?act=hint&idJuego=" + idJuego + "&pos=" + pos);
        ajax.onreadystatechange=function() 
        {
        	if (ajax.readyState==4)
        	{
        		caja.value = ajax.responseText;
        		actualizaSol(pos, caja.value, idJuego, tiempo);
        	}
        };
        ajax.send(null);
	}
}

function muestraError(idJuego)
{
	result = "";
    ajax=objetoAjax();
//        alert("consulta.php?act=error&idJuego=" + idJuego + "&sol=" + document.getElementById("solucion").innerHTML);
    ajax.open("GET", "consulta.php?act=error&idJuego=" + idJuego + "&sol=" + document.getElementById("solucion").innerHTML);
    ajax.onreadystatechange=function() 
    {
    	if (ajax.readyState==4)
    	{
    		result = ajax.responseText;
    		if (result != "")
    		{
    			errores = result.split("::");
    			for (k = 0; k < errores.length - 1; k++)
    			{
    				pos =  errores[k].split("-----");
    				for (i = 0; i < 9; i++)
    					for (j = 0; j < 9; j++)
    					{
    						if (i == pos[0] || j == pos[1])
							{
    							if (document.getElementById("caja" + i + "-" + j) != null)
    							{
    								caja = document.getElementById("caja" + i + "-" + j);
    								if (caja.className == "cellVar")
    									caja.className = "cellVarErrColRow";
    								else
    									caja.className = "cellVarErrColRowPosibles";
    							}
    							else
    								document.getElementById("cas" + i + "-" + j).className = "juegoCellFijaErrColRow";
							}
    					}
    			}
    			for (k = 0; k < errores.length - 1; k++)
    			{
    				pos =  errores[k].split("-----");
    				for (i = 0; i < 9; i++)
    					for (j = 0; j < 9; j++)
    						if (i == pos[0] && j == pos[1])
							{
    							if (document.getElementById("caja" + i + "-" + j) != null)
    							{
    								caja = document.getElementById("caja" + i + "-" + j);
//        								if (document.getElementById("showPosibles") && document.getElementById("showPosibles").checked)
//        									caja.className = "cellVarErrPosibles";
//        								else
    									caja.className = "cellVarErr";
    							}
							}
    			}
    		}
    	}
    };
    ajax.send(null);
//	if (!document.getElementById("showError").checked || result == "")
//		for (i = 0; i < 9; i++)
//			for (j = 0; j < 9; j++)
//				if (document.getElementById("caja" + i + "-" + j) != null)
//					document.getElementById("caja" + i + "-" + j).className = "cellVar";
		
}

function deletePosibles()
{
	if (document.getElementById("showPosibles") && document.getElementById("showPosibles").checked)
		return;

	for (i = 0; i < 9; i++)
		for (j = 0; j < 9; j++)
		{
			if (document.getElementById("caja" + i + "-" + j) != null)
			{
				caja = document.getElementById("caja" + i + "-" + j);
				if (caja.className.lastIndexOf("Posibles") != -1)
					caja.className = caja.className.substring(0, caja.className.length - 8);
				if (caja.value.length > 1)
					caja.value = "";
				caja.maxLength = "1";
			}
		}
}

