    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; 
    } 
	function validaCNPJ() {
		CNPJ = document.formulario.CNPJ.value;
		erro = new String;
		if (CNPJ.length < 18) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
		if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
			if (erro.length == 0) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
		}
		//substituir os caracteres que nao sao numeros
		if(document.layers && parseInt(navigator.appVersion) == 4){
			x = CNPJ.substring(0,2);
			x += CNPJ.substring(3,6);
			x += CNPJ.substring(7,10);
			x += CNPJ.substring(11,15);
			x += CNPJ.substring(16,18);
			CNPJ = x;	
		} else {
			CNPJ = CNPJ.replace(".","");
			CNPJ = CNPJ.replace(".","");
			CNPJ = CNPJ.replace("-","");
			CNPJ = CNPJ.replace("/","");
		}
		var nonNumbers = /\D/;
		if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n";	
		var a = [];
		var b = new Number;
		var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
		for (i=0; i<12; i++){
			a[i] = CNPJ.charAt(i);
			b += a[i] * c[i+1];
		}
		if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
		b = 0;
		for (y=0; y<13; y++) {
			b += (a[y] * c[y]); 
		}
		if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
		if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
			erro +="Digito verificador com problema!";
		}
		if (erro.length > 0){
			alert(erro);
			return false;
		} else {
			alert("CNPJ valido!");
		}
		return true;
	}
	
function formatarCNPJ(objeto){
if (objeto.value.length == 2 || objeto.value.length == 6 ){
objeto.value = objeto.value+".";
}
if (objeto.value.length == 10 ){
objeto.value = objeto.value+"/";
}
if (objeto.value.length == 15 ){
objeto.value = objeto.value+"-";
}
if (objeto.value.length==18) {window.document.formulario.razao.focus();}
}


function formatarCEP(objeto){
if (objeto.value.length == 5){
objeto.value = objeto.value+"-";
}
if (objeto.value.length==9) {window.document.formulario.referencia.focus();}
}


function formatarCPF(objeto){
if (objeto.value.length == 3 || objeto.value.length == 7 ){
objeto.value = objeto.value+".";
}
if (objeto.value.length == 11 ){
objeto.value = objeto.value+"-";
}
if (objeto.value.length==14) {window.document.formulario.rg.focus();}
}