var largura = screen.availWidth; // Retorna a largura em pixels da resolução do cliente
var altura = screen.availHeight; // Retorna a altura em pixels da resolução do cliente
var navegador = window.navigator.appName; // Retorna o nome do navegador do cliente
var navegadorCode = window.navigator.appCodeName; // Retorna o modelo do navegador do cliente
var navegadorVersion = window.navigator.appVersion; // Retorna a versão do navegador do cliente
var navegadorAgent = window.navigator.userAgent; // Retorna os dados do navegador userAgent do cliente
var Tloop = 0; // Variavel de timeout

function navegadorNome() {
	var ret = "";

	if (navegadorAgent.indexOf("Firefox") != -1) {
		ret = "firefox";

	} else if (navegadorAgent.indexOf("Chrome") != -1) {
		ret = "chrome";

	} else if (navegadorAgent.indexOf("Safari") != -1) {
		ret = "safari";

	} else if (navegadorAgent.indexOf("Opera") != -1) {
		ret = "opera";

	} else if (navegador == "Microsoft Internet Explorer") {
		ret = "ie";

	}

	return ret;

}

function windowSize() {
	var myWidth = 0, myHeight = 0;

	if (typeof window.innerWidth == "number") {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;

	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;

	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;

	}

	return Array(myWidth, myHeight);

}

function rand(){ // Retorna um numero randômico
	now = new Date();
	num = now.getSeconds() % 10;
	num = num + 1

	return num;

}

function queryString(valor){ // Retorna o valor de uma variável de uma queryString
	var variavel = Array();
	url = new String(this.location);
	queryIni = url.indexOf("?"); // Pega a posição do "?"
	string = url.substring(queryIni+1,url.length); // Pega tudo que tem depois do "?"
	stringCom = string.length // Conta o total de caracteres depois do "?"
	stringSem = string;
	while(stringSem.indexOf("&") != -1){
		stringSem = stringSem.replace("&",""); // Tira todos os "&"
	}
	stringVar = (stringCom-stringSem.length); // retorna o valor de variáveis existentes na string
	for(i=0;i<=stringVar;i++){
		variavelIni = string.indexOf("=");
		ecomercial = string.indexOf("&");
		if(ecomercial == -1){
			ecomercial = string.length;
		}
		variavelNome = string.substring(0,variavelIni); // retorna o nome da variável
		constante = string.substring(variavelIni+1,ecomercial); // retorna o valor da variável
		variavel[variavelNome] = constante; // Cria o vetor com o nome dessa variável
		string = string.substring(ecomercial+1,string.length); // Gera uma nova string sem a vaeriável e a constante acima
	}
	return variavel[valor];
}

function replace(texto, antes, depois){ // Substitui um valor por outro em uma String
	if (texto != "" && typeof texto != "undefined" && antes != "" && typeof antes != "undefined") {
		while(texto.indexOf(antes) != -1) {
			texto = texto.replace(antes, depois);

		}

	}
	return texto;
}

function focar(){ // Foca o primeiro campo text de uma página
	var i=0;
	var activeElement = typeof document.activeElement.name;
	if(activeElement == "undefined"){
		while(i < 50){
			try{
				tipo = document.forms[0].elements[i].type;
				visibilidade = document.forms[0].elements[i].style.visibility;
				if(tipo == "text"){
					try{
						document.forms[0].elements[i].focus();
						return false;
					}
					catch(e){
						i++;
						continue;
					}
				}
				i++;
			}
			catch(e){
				i = 50;
			}
		}
	}
}

function right(texto, quantidade){ // Retorna uma String com valor zero para as casas em branco de acordo com a quantidade de caracteres passada
	quantidadeAtual = texto.length;
	if(quantidade > quantidadeAtual){
		quantidadeNova = "";
		for(i=quantidadeAtual;i<quantidade;i++){
			quantidadeNova += "0";
		}
		quantidadeAtual = quantidadeNova+texto;
	}
	return quantidadeAtual;
}



function base64ToAscii(c) {
	var theChar = 0;
	
	if (0 <= c && c <= 25)
	{
		theChar = String.fromCharCode(c + 65);
	}
	else if (26 <= c && c <= 51)
	{
		theChar = String.fromCharCode(c - 26 + 97);
	}
	else if (52 <= c && c <= 61)
	{
		theChar = String.fromCharCode(c - 52 + 48);
	}
	else if (c == 62)
	{
		theChar = '+';
	}
	else if( c == 63 )
	{
		theChar = '/';
	}
	else
	{
		theChar = String.fromCharCode(0xFF);
	}

	return theChar;
}

function base64Decode(str) { // Decodifica Base64
	var result = "";
	var i = 0;
	var x;
	var shiftreg = 0;
	var count = -1;

	
	for (iii = 0; iii < str.length; iii++) {
		c = str.charAt(iii);

		if ('A' <= c && c <= 'Z')
			x = str.charCodeAt(iii) - 65;
		else if ('a' <= c && c <= 'z')
			x = str.charCodeAt(iii) - 97 + 26;
		else if ('0' <= c && c <= '9')
			x = str.charCodeAt(iii) - 48 + 52;
		else if (c == '+')
			x = 62;
		else if (c == '/')
			x = 63;
		else
			continue;

		count++;

		switch (count % 4) {
			case 0:
				shiftreg = x;
				continue;
			case 1:
				v = (shiftreg<<2) | (x >> 4);
				shiftreg = x & 0x0F;
				break;
			case 2:
				v = (shiftreg<<4) | (x >> 2);
				shiftreg = x & 0x03;
				break;
			case 3:
				v = (shiftreg<<6) | (x >> 0);
				shiftreg = x & 0x00;
				break;
		}

		is_binary = false; // Variavel que era passada para a funcao, que eu desabilitei e coloquei aqui como false

		if (!is_binary && (v < 32 || v > 126) && (v != 0x0d) && (v != 0x0a)) {
			/* Comentado pois não resolvia os acentos
			result = result + "<";
			result = result + "0123456789ABCDEF".charAt((v/16)&0x0F);
			result = result + "0123456789ABCDEF".charAt((v/1)&0x0F);
			result = result + ">";
			*/


			result2 = "<";
			result2 = result2 + "0123456789ABCDEF".charAt((v/16)&0x0F);
			result2 = result2 + "0123456789ABCDEF".charAt((v/1)&0x0F);
			result2 = result2 + ">";

			//document.all.titulo.value += result2;
			result2 = base64DecodeAdd01(result2);
			result = result + result2;


		} else {
			result = result + String.fromCharCode(v);

		}

	}

	return result.toString();
}

function base64DecodeAdd01(tij) { // Trabalha com a base64Decode
	var baseDecode = Array(7);
	baseDecode[0] = Array("<E3>", "<E2>", "<E1>", "<E0>", "<C3>", "<C2>", "<C1>", "<C0>");
	baseDecode[1] = Array("<EA>", "<E9>", "<E8>", "<CA>", "<C9>", "<C8>");
	baseDecode[2] = Array("<EE>", "<ED>", "<EC>", "<CE>", "<CD>", "<CC>");
	baseDecode[3] = Array("<F5>", "<F4>", "<F3>", "<F2>", "<D5>", "<D4>", "<D3>", "<D2>");
	baseDecode[4] = Array("<FB>", "<FA>", "<F9>", "<FC>", "<DB>", "<DA>", "<D9>", "<DC>");
	baseDecode[5] = Array("<E7>", "<C7>");
	baseDecode[6] = Array("<A0>", "<BA>", "<91>", "<92>", "<93>", "<94>");
	


	var baseOrigem = Array(7);
	baseOrigem[0] = Array("ã", "â", "á", "à", "Ã", "Â", "Á", "À");
	baseOrigem[1] = Array("ê", "é", "è", "Ê", "É", "È");
	baseOrigem[2] = Array("î", "í", "ì", "Î", "Í", "Ì");
	baseOrigem[3] = Array("õ", "ô", "ó", "ò", "Õ", "Ô", "Ó", "Ò");
	baseOrigem[4] = Array("û", "ú", "ù", "ü", "Û", "Ú", "Ù", "Ü");
	baseOrigem[5] = Array("ç", "Ç");
	baseOrigem[6] = Array("&nbsp;", "&ordm;", "&lsquo;", "&rsquo;", "&ldquo;", "&rdquo;"); // espaço, º, ‘, ’, “, ”


	ponto = "";
	for (ijj = 0; ijj < 7; ijj++) {
		for (jii = 0; jii < baseDecode[ijj].length; jii++) {
			if (baseDecode[ijj][jii] == tij) {
				ponto = "[" + ijj + "][" + jii + "]";

			}

		}

	}

	var ret = eval("baseOrigem" + ponto);

	return ret;
}

  var keyStr = "ABCDEFGHIJKLMNOP" + 
               "QRSTUVWXYZabcdef" + 
               "ghijklmnopqrstuv" + 
               "wxyz0123456789+/" + 
               "="; 

function base64Decode2(input) {
	var output = ""; 
	var chr1, chr2, chr3 = ""; 
	var enc1, enc2, enc3, enc4 = ""; 
	var i = 0; 

	// remove all characters that are not A-Z, a-z, 0-9, +, /, or = 
	var base64test = /[^A-Za-z0-9\+\/\=]/g; 
	if (base64test.exec(input)) { 
		alert("There were invalid base64 characters in the input text.\n" + 
		"Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" + 
		"Expect errors in decoding."); 
	} 
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); 

	do { 
		enc1 = keyStr.indexOf(input.charAt(i++)); 
		enc2 = keyStr.indexOf(input.charAt(i++)); 
		enc3 = keyStr.indexOf(input.charAt(i++)); 
		enc4 = keyStr.indexOf(input.charAt(i++)); 

		chr1 = (enc1 << 2) | (enc2 >> 4); 
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); 
		chr3 = ((enc3 & 3) << 6) | enc4; 

		output = output + String.fromCharCode(chr1); 

		if (enc3 != 64) { 
			output = output + String.fromCharCode(chr2); 
		} 

		if (enc4 != 64) { 
			output = output + String.fromCharCode(chr3); 
		} 

		chr1 = chr2 = chr3 = ""; 
		enc1 = enc2 = enc3 = enc4 = ""; 

	} while (i < input.length); 

	return unescape(output); 
} 

// -------------------------------------------------------------------------------------------------------- //

function trim(txt) {
	var trimAnt = new String(txt);
	var trimNovoPosicao = "";
	var trimNovo = "";
	var l = "";


	for (i = 0; i < trimAnt.length; i++) {
		l = trimAnt.substring(i, i + 1);

		if (l != " ") {
			trimNovoPosicao = trimAnt.substring(i, trimAnt.length);
			break;

		}

	}

	for (i = trimNovoPosicao.length; i >= 0; i--) {
		l = trimNovoPosicao.substring(i - 1, i);

		if (l != " ") {
			trimNovo = trimNovoPosicao.substring(0, i);
			break;

		}

	}

	return trimNovo;

}

// -------------------------------------------------------------------------------------------------------- //

function setImage(id, url){ // Seta a imagem no ID especificado
	var html = "<img src='"+url+"' border='0' height='75' id='imageID'>";
	document.getElementById(id).innerHTML = html;
}

// ------------------------------------------------------------------------------------- //
function abrirNomeSelecionado() {
	window.open("/uzzi/plataforma/pc/lib/componentes/nomeRelacionado", "popRelacionado", "width=400, height=300, top=10, left=10, scrollbars=no, status=yes");
}

function marcarNomeRelacionado(valor, texto) {
	var len = document.form.nomRel_nome.length;
	var nomRelOption;
	var existe = 0;
	var msg = "";

	for (i = 0; i < len; i++) {
		if (document.form.nomRel_nome[i].value == valor) {
			existe = 1;
		}
	}

	if (existe == 0) {
		try{
			nomRelOption = document.createElement("OPTION");
			nomRelOption.text = texto;
			nomRelOption.value = valor;
			
			document.form.nomRel_id.value += "<" + valor + ">";
			document.form.nomRel_nome.add(nomRelOption);
		} catch(e) {
			alert("Ocorreu um erro ao marcar o nome selecionado.\r\nPor favor, informe o administrador do sistema!");
		}
	} else {
		msg = "Este nome já está selecionado!!";
	}

	return msg;
}

function removerNomeSelecionado() {
	var selecionado = document.form.nomRel_nome.selectedIndex;
	if (selecionado != -1) {
		nomRel_id = document.form.nomRel_nome.value;
		document.form.nomRel_nome.remove(selecionado);
		document.form.nomRel_id.value = replace(document.form.nomRel_id.value, "<" + nomRel_id + ">", "");
	}
	else {
		alert("Você deve selecionar um nome para exclui-lo!");
	}
}


// ---------------------------------------------------------------------------------------- //
function fecharMenu(fechar) {	
	if (fechar > 0) {
		w = 100 - fechar;

		if (w > 0) {
			document.getElementById("tableMenu").style.width = w;

		}

		if (fechar < 100) {
			fechar = fechar + 10;
			Tloop = setTimeout("fecharMenu(" + fechar + ")", 10);

		} else {
			clearTimeout(Tloop);
			document.getElementById("tableMenu").style.display = "none";
			document.getElementById("palhetaMenuFechado").style.display = "block";

		}

	} else {
		fecharItensMenu(0);

	}

}

function fecharItensMenu(item) {
	var exec = 0;

	try {
		document.getElementById("tableMenuItem" + item).style.visibility = "hidden";
		exec = 1;

	} catch (e) {}

	if (exec == 1) {
		item++;
		Tloop = setTimeout("fecharItensMenu(" + item + ")", 100);

	} else {
		clearTimeout(Tloop);
		fecharMenu(1);

	}

}

function abrirMenu(abrir) {	
	if (abrir <= 100) {
		if (abrir == 10) {
			document.getElementById("tableMenu").style.display = "block";
			document.getElementById("palhetaMenuFechado").style.display = "none";
		}

		w = abrir;
		document.getElementById("tableMenu").style.width = w;

		if (abrir <= 100) {
			abrir = abrir + 10;

		} else {
			clearTimeout(Tloop);

		}
		Tloop = setTimeout("abrirMenu(" + abrir + ")", 10);

	} else {
		abrirItensMenu(0);

	}

}

function abrirItensMenu(item) {
	var exec = 0;

	try {
		document.getElementById("tableMenuItem" + item).style.visibility = "visible";
		exec = 1;

	} catch (e) {}

	if (exec == 1) {
		item++;
		Tloop = setTimeout("abrirItensMenu(" + item + ")", 100);

	} else {
		clearTimeout(Tloop);

	}

}

// ---------------------------------------------------------------------------------------- //

function abrirSubmenu(item) {
	document.getElementById("subItem" + item).style.display = "block";
}

function fecharSubmenu(item) {
	document.getElementById("subItem" + item).style.display = "none";
}
// ---------------------------------------------------------------------------------------- //

function explorer(diretorio, campo) {
	window.open("/dmk/plataforma/pc/lib/componentes/explorer/frame.php?diretorio=" + diretorio + "&campo=" + campo, "popDmkExplorer", "width=600, height=400, top=50, left=50, scrollbars=no, resizeble=no, status=no");

}
// ---------------------------------------------------------------------------------------- //