var cropping = false;
var key_f1 = 112;
var key_esc = 27;

function isset (obj) {
	return (obj != undefined && obj !== null);
}

function cropper_cancela_forcado () {
	/*
		forca o cancelamento do cropper, excluindo a imagem do servidor
		usar para caso a usuária resolva fechar a janela do navegador com o cropper aberto
		ficará no "onunload" da janela, porém verifica antes se o cropper está rodando
	*/
	cropping = (cropping != undefined && cropping !== null ? cropping : false);
	if (cropping) {
		cropper_bt_cancelar ();
		return true;
	}
	return false;
}

//document.onhelp = function() {return(false);}
//window.onhelp = function() {return(false);}

function key_press (event) {
	if (navigator.appName == "Microsoft Internet Explorer") {
		var key_pressed = event.keyCode;
		//key_f1 = DOM_VK_F1;
		//key_esc = DOM_VK_ESC;
		key_f1 = 112;
		key_esc = 27;
	}
	else {
		var key_pressed = event.keyCode;
		key_f1 = 112;
		key_esc = 27;
	}
	return key_pressed;
}

function key_press_esc (event) {
	cropping = (cropping != undefined && cropping !== null ? cropping : false);
	if (key_press (event) == key_esc) {
		if (!cropper_cancela_forcado()) {
			bodyAlpha_close();
		}
	}
}

function key_press_f1 (event) {
	if (key_press (event) == key_f1) {
		event.returnValue = false;
		event.cancelBubble = true;
		new_help();
		return false;
	}
}

function new_help () {
	alert("vamos te ajudar");
}

function bodyAlpha_close (fade) {
	/*
		fade == true | terá um FADE-IN na abertura no fundo;
		fade == null , undefined , false | não terá fade
	*/

	var combos = document.getElementsByTagName("select");
	for (var i in combos) {
		if(typeof combos[i] == "object") {
			combos[i].style.display = "block";
		}
	}

	fade = ((fade == null || fade == undefined) ? false : fade);
	if (!fade) {
		document.getElementById("body-alpha-janela").innerHTML = "";
		document.getElementById("body-alpha-janela").className = "zeropx";
		document.getElementById("body-alpha").style.display = "none";
	}
}

function bodyAlpha_open (classe , texto , fade) {
	/*
		fade == true | terá um FADE-IN na abertura no fundo;
		fade == null , undefined , false | não terá fade
	*/

	var combos = document.getElementsByTagName("select");
	for (var i in combos) {
		if(typeof combos[i] == "object") {
			combos[i].style.display = "none";
		}
	}

	fade = ((fade == null || fade == undefined) ? false : fade);
	classe = (classe == "" ? "zeropx" : classe);
	if (!fade) {
		document.getElementById("body-alpha").style.display = "block";
		document.getElementById("body-alpha-janela").className = classe;
		document.getElementById("body-alpha-janela").innerHTML = texto;
	} else {
	}
	document.getElementById("body-alpha").focus();
}

function str_replace (str_sai , str_entra , str_texto) {
	if (str_texto.indexOf(str_sai) >= 0) {
		str_flag = true;
	} else {
		str_flag = false;
	}
	while (str_flag) {
		str_texto = str_texto.replace(str_sai , str_entra);
		if (str_texto.indexOf(str_sai) == -1) {
			str_flag = false;
		}
	}
	return str_texto;
}

function trim (texto) {
	var tirar = new Array();
	tirar[0] = "T";
	tirar[9] = "T";
	tirar[10] = "T";
	tirar[11] = "T";
	tirar[13] = "T";
	tirar[32] = "T";
	if (texto != "") {
		flag = true;
		while (flag) {
			if (tirar[texto.charCodeAt(0)] == "T") {
				texto = texto.substr(1);
			} else {
				flag = false;
			}
		}
		flag = true;
		while (flag) {
			if (tirar[texto.charCodeAt(texto.length - 1)] == "T") {
				texto = texto.substr(0 , (texto.length - 1));
			} else {
				flag = false;
			}
		}
	}
	return texto;
}

function tradutor_tags (texto) {
	texto = str_replace("\r" , "" , texto);
	texto = str_replace("\n" , "<br />" , texto);
	texto = str_replace("[quebrapagina]" , '</p><hr style="border-width: 0px; border-top: 1px DASHED #F00;" /><p>' , texto);

	flag = true;
	while (flag) {
		texto = texto.replace("[comentario]" , "<blockquote>");	texto = texto.replace("[/comentario]" , "</blockquote>");
		texto = texto.replace("[n]" , "<strong>");	texto = texto.replace("[/n]" , "</strong>");
		texto = texto.replace("[i]" , "<i>");			texto = texto.replace("[/i]" , "</i>");
		texto = texto.replace("[s]" , "<u>");			texto = texto.replace("[/s]" , "</u>");

		texto = texto.replace("[s1]" , "<h2>");		texto = texto.replace("[/s1]" , "</h2>");
		texto = texto.replace("[s2]" , "<h3>");		texto = texto.replace("[/s2]" , "</h3>");
		texto = texto.replace("[s3]" , "<h4>");		texto = texto.replace("[/s3]" , "</h4>");
		texto = texto.replace("[s4]" , "<h5>");		texto = texto.replace("[/s4]" , "</h5>");
		texto = texto.replace("[s5]" , "<h6>");		texto = texto.replace("[/s5]" , "</h6>");

		if (texto.indexOf("[n]") == -1 && texto.indexOf("[comentario]") == -1 && texto.indexOf("[i]") == -1 && texto.indexOf("[s]") == -1 && texto.indexOf("[s1]") == -1 && texto.indexOf("[s2]") == -1 && texto.indexOf("[s3]") == -1 && texto.indexOf("[s4]") == -1 && texto.indexOf("[s5]") == -1) {
			flag = false;
		}
	}
	flag = true;
	// converte links
	while (flag) {
		if (texto.indexOf("[link url") >= 0) {
			texto = texto.replace('externo="sim"' , 'target="_blank"');
			texto = texto.replace("externo='sim'" , 'target="_blank"');
			texto = texto.replace('externo="nao"' , '');
			texto = texto.replace("externo='nao'" , '');

			texto = texto.replace("[link url" , "<a href");
			temp = texto.indexOf("[link url");
			temp = texto.indexOf("]" , (temp + 1));
			texto = texto.substr(0 , temp) + ">" + texto.substr(temp + 1);
			texto = texto.replace("[/link]" , "</a>");
		} else if (texto.indexOf("[link url") == -1) {
			flag = false;
		}
	}
	flag = true;
	ini = 0;
	fim = texto.indexOf("<");
	if (fim == -1) {
		fim = texto.length;
	}
	while (flag) {
		temp = texto.substr(ini , (fim - ini));
		flag2 = true;
		while (flag2) {
			temp = temp.replace(String.fromCharCode(32) , "&nbsp;");
			if (temp.indexOf(String.fromCharCode(32)) == -1) {
				flag2 = false;
			}
		}
		texto = texto.substr(0 , ini) + temp + texto.substr(fim , (texto.length - fim));
		ini = texto.indexOf(">" , fim + 1) + 1;
		fim = texto.indexOf("<" , ini + 0);
		if (fim < 0 || fim == texto.length) {
			flag = false;
		}
	}
	texto = "<p>" + texto + "</p>";
	flag = true;
	while (flag) {
		texto = texto.replace("<br /><br />" , "</p>\n<p>");
		if (texto.indexOf("<br /><br />")) {
			flag = false;
		}
	}
	return texto;
}

function function_exists (funcao) {
	if (eval(funcao + " !== null && " + funcao + " != undefined")) {
		return eval('typeof ' + funcao + ' == "function"');
	}
	else {
		return false;
	}
}

function form_validacao (campo , tipo , minimo , maximo) {
	/*
		campo
			- é o ID do campo que será verificado;
		tipo
			- é o tipo do campo que será verificado
			- tipos: (combo, texto , radio , check , multi)
			- tipo multi: combo onde pode selecionar vários usando a tecla ctrl
			- se tipo for radio, deverão ter todos os checks separados por vírgula no atributo campo
			- se tipo for multi, o campo deverá ser preenchido com os ids de todas as opções ("<option>") separadas por vírgula
			- se o tipo for file, você pode usar o campo mínimo para passar as extenções separadas por vírgula

		minimo = usar apenas quando o tipo for check ou multi. é o número mínimo de checkbox preenchidos
		maximo = usar apenas quando o tipo for check ou multi. é o número máximo de checkbox preenchidos
			- se minimo não for preenchido, será setado como dmin (mínimo default) automaticamente
			- se máximo não for preenchido, será setado como dmax (máximo default) automaticamente
			- o campo minimo pode ser usado com conteúdo de regular expression

		a função apresentará um alert() quando um tipo inválido for utilizado
		a função retornará true se validar o campo ou false se não validar.
	*/
	var cont = 0;
	var qtde = 0;
	var dmin = 0;
	var dmax = 9999;
	var retorno = true;
	ext_fl = (minimo != undefined && minimo !== null ? minimo : ""); // ext_fl = extenções de arquivo
	reg_ex = (minimo != undefined && minimo !== null ? minimo : ""); // reg_ex = regular expression
	minimo = (minimo != undefined && minimo !== null ? minimo : dmin);
	maximo = (maximo != undefined && maximo !== null ? maximo : dmax);
	minimo = (isNaN(parseInt(minimo)) ? dmin : parseInt(minimo));
	maximo = (isNaN(parseInt(maximo)) ? dmax : parseInt(maximo));

	switch (tipo) {
		case "texto" :
			oid = trim(campo);
			obj = document.getElementById(oid);
			if (trim(obj.value) == "") {
				retorno = false;
			}
			else if (trim(reg_ex) != "") {
				retorno = reg_ex.test(obj.value);
			}
		break;
		case "combo" :
			oid = trim(campo);
			obj = document.getElementById(oid);
			if (obj.value == 0) {
				retorno = false;
			}
		break;
		case "check" :
			var flag = true;
			while (flag) {
				oid = campo + "_" + cont;
				obj = document.getElementById(oid);
				if (obj != undefined && obj !== null) {
					if (obj.checked == true) {
						qtde++;
					}
				}
				else {
					flag = false;
				}
				cont++;
			}
			if (qtde < minimo || qtde > maximo) {
				retorno = false;
			}
		break;
		case "radio" :
			var campos = new Array ();
			if (campo.indexOf(",") >= 0) {
				campos = campo.split(",");
			}
			else {
				campos[0] = campo;
			}
			for (cont in campos) {
				oid = trim(campos[cont]);
				obj = document.getElementById(oid);
				if (obj.checked == true) {
					qtde++;
				}
			}
			if (qtde == 0) {
				retorno = false;
			}
		break;
		case "multi" :
			var campos = new Array ();
			if (campo.indexOf(",") >= 0) {
				campos = campo.split(",");
			}
			else {
				campos[0] = campo;
			}
			for (cont in campos) {
				oid = trim(campos[cont]);
				obj = document.getElementById(oid);
				if (obj != undefined && obj !== null) {
					if (obj.selected == true) {
						qtde++;
					}
				}
			}
			if (qtde < minimo || qtde > maximo) {
				retorno = false;
			}
		break;
		case "file" :
			if (trim(ext_fl) != "") {
				extencoes = ext_fl.split(",");
			}
			oid = trim(campo);
			obj = document.getElementById(oid);
			if (trim(obj.value) == "") {
				retorno = false;
			}
			if (retorno && extencoes !== null && extencoes != undefined) {
				retorno = false;
				for (xx in extencoes) {
					ext = trim(extencoes[xx]);
					arq = trim(obj.value);
					if (arq.substr(-3).toLowerCase() == ext.toLowerCase()) {
						retorno = true;
					}
				}
			}
		break;
		default:
			alert("tipo inválido");
			retorno = false;
		break;
	}
	return retorno;
}

function in_array (arr , val) {
	retorno = false;
	for (c in arr) {
		if (arr[c] == val) {
			retorno = true;
			break;
		}
	}
	return retorno;
}

