// JavaScript Document
/*
* Autor   : Thiago Medeiros Barros
* Data    : 11/02/2006
* Contato : thiago.mdeiros@gmail.com
* 
**************************************

/*
* summary: adicionar fun��es a comportamentos para um determinado objeto
* parametros:  - objeto
* 			   - tipo do evento
*              - fun��o que ser� chamada
*
* exemplo: 
* bot=document.getElementById("b");
Senha%20e/ou%20Usu%E1rio%20incorreto%21%20Digite%20novamente%21* addEvente(bot, "click", alertando()); 
*/
function addEvent(obj, evType, fn){
        if (obj.addEventListener)
                obj.addEventListener(evType, fn, true)
        if (obj.attachEvent)
                obj.attachEvent("on"+evType, fn)
}

/*
* summary: inicia ajax
*/
function initAjax() {
	
	var req = null;
	
	try {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(ex) {
			try {
				req = new XMLHttpRequest();
			}
			catch(exe) {
				alert(unescape("O navegador n%E3o possui suporte ao ajax!"));
				alert("Atualize seu navegador.");
				return null;
			}
		}
	}
	
	return req;
}

function Arredonda( valor , casas ){

   var novo = Math.round( valor * Math.pow( 10 , casas ) ) / Math.pow( 10 , casas );
   
   return( novo );

}

function formatar(src, mask) 
{
	var i = src.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i)
	if (texto.substring(0,1) != saida) 
	{
		src.value += texto.substring(0,1);
	}
}	
	
function soNums(e,args)
{        
// Fun��o que permite apenas teclas num�ricas e 
// todos os caracteres que estiverem na lista
// de argumentos.
// Deve ser chamada no evento onKeyPress desta forma
//  onKeyPress ="return (soNums(event,'0'));"
// caso queira apenas permitir caracters como por exemplo um campo que s� aceite valores em Hexadecimal (de 0 a F) usamos
//  onKeyPress ="return (soNums(event,'AaBbCcDdEeFf'));"

/* Esta parte comentada � a que testei exaustivamente e garanto que funciona em praticamente todos os browsers
    var evt='';// devido a um warning gerado pelo Console de Javascript que "enxergava" uma redeclara��o de "evt" decidi declar�-la uma vez e alterar ser valor posteriormente 

    if (document.all){evt=event.keyCode;} // caso seja IE
    else{evt = e.charCode;}    // do contr�rio deve ser Mozilla
O c�digo a seguir teste apenas em FireFox e Internet Explorer 6 e funcionou perfeitamente. Caso vc tenha algum problema com esta fun��o por favor entre em contato
*/
    var evt= (e.keyCode?e.keyCode:e.charCode);
    var chr= String.fromCharCode(evt);    // pegando a tecla digitada
    // Se o c�digo for menor que 20 � porque deve ser caracteres de controle
    // ex.: <ENTER>, <TAB>, <BACKSPACE> portanto devemos permitir
    // as teclas num�ricas v�o de 48 a 57
    return (evt <20 || (evt >47 && evt<58) || (args.indexOf(chr)>-1 ) );
} 

//http://phpbrasil.com/articles/article.php/pagerRow/0/id/1182
// url_encode version 1.0 
function url_encode(str) { 
    var hex_chars = "0123456789ABCDEF"; 
    var noEncode = /^([a-zA-Z0-9\_\-\.])$/; 
    var n, strCode, hex1, hex2, strEncode = ""; 

    for(n = 0; n < str.length; n++) { 
        if (noEncode.test(str.charAt(n))) { 
            strEncode += str.charAt(n); 
        } else { 
            strCode = str.charCodeAt(n); 
            hex1 = hex_chars.charAt(Math.floor(strCode / 16)); 
            hex2 = hex_chars.charAt(strCode % 16); 
            strEncode += "%" + (hex1 + hex2); 
        } 
    } 
    return strEncode; 
} 

// url_decode version 1.0 
function url_decode(str) { 
    var n, strCode, strDecode = ""; 

    for (n = 0; n < str.length; n++) { 
        if (str.charAt(n) == "%") { 
            strCode = str.charAt(n + 1) + str.charAt(n + 2); 
            strDecode += String.fromCharCode(parseInt(strCode, 16)); 
            n += 2; 
        } else { 
            strDecode += str.charAt(n); 
        } 
    } 

    return strDecode; 
}  