// ver http://www.webreference.com/js/column5/rules.html

// Para input Boxes type="text"
// uso: <input type="text" name="pagina" value="" size="2" onKeyPress="return keyPressNumero()">
// uso: <input type="text" name="pagina" value="" size="2" onKeyPress="return keyPressNumero(decimais)">
function keyPressNumero(elemento,e,decimais)
{
	var state= false;
	if(decimais==null || decimais=="")
	{
		decimais= 0;
	}

    codigoTecla= document.all? event.keyCode : e.which;
    
    if(codigoTecla<32)
    {
		event.returnValue= true;
		return true;
    }

	// Validar conteudo
    //var elemento= objecto;
    var texto= "";
    if(elemento!=null) 
    {
    	texto= elemento.value;
    }
    texto= texto.replace(",", ".");
	
    // Ver se é numero
    if( codigoTecla >=48 && codigoTecla <=57 )				// de 0 a 9
    {
	    if( texto.indexOf(".")==-1 )
	    {
	    	// não encontrou virgulas, pode por todos digitos que quiser
			state= true;
	    }
	   	else
	   	{
	    	// encontrou virgulas, ver quantas casas decimais ja tem
	    	var array= texto.split(".");
	    	if(array[1].toString().length < decimais)
	    	{
				state= true;
			}
      	}
	}
	else					// não é numero
	{
		if(decimais>0)		// tem decimais?
		{
	        if( codigoTecla==46 || codigoTecla==44 )		// ponto 46(.), 44(,)
	        {
	        	if( texto.indexOf(".")==-1 && texto!="")
	        	{
	        		// entao não encontrou virgulas pode por uma
					//state= true;
	        		elemento.value= elemento.value + ".";
					state= false;
	        	}
			}
			
		}
		
	}
 
	//event.returnValue= state;
	return state;
}

// Para input Boxes type="text"
// uso: <input type="text" name="pagina" value="" size="2" onKeyPress="keyPressGenerica(/\d/);">
function keyPressGenerica(reg,e)
{
	var state= false;
	codigoTecla= document.all? event.keyCode : e.which;
    if(codigoTecla<32)
    {
		//event.returnValue= true;
		return true;
    }

	keyChar= String.fromCharCode(codigoTecla);

	state= reg.test(keyChar);
	//event.returnValue= state;
	return state;
}

// Para input Boxes type="text"
// uso: <input type="text" name="pagina" value="" size="2" onKeyPress="return keyPressDatas()">
function keyPressDatas(e)
{
	var reg=/[\d\-]/i
	return keyPressGenerica(reg,e);
}

// Para input Boxes type="text"
// uso: <input type="text" name="pagina" value="" size="2" onKeyPress="return keyPressEmail()">
function keyPressEmail(e)
{
	var reg=/[a-z\d\._@]/i
	return keyPressGenerica(reg,e);
}

// Para input Boxes type="text"
// uso: <input type="text" name="pagina" value="" size="2" onKeyPress="return keyPressFicheiro()">
function keyPressFicheiro()
{
	return keyPressGenerica(/[a-z\d\._\-\\]/i);
}
// Para Utilizacao em TextArea
// Simula o max length na text area
// uso: <input type="textarea" name="pagina" value="" onKeyPress="return keyPressTextAreaMaxLength()">
function keyPressTextAreaMaxLength(objecto,maximo)
{
	if (objecto.value.length>=maximo){
		objecto.value=objecto.value.substring(0,maximo);
		return false;
	}
	return true;
}