	
	function verifica_obrigatorio(conjunto_campo)
	{
		var erro = "";
		var prefixo_erro = "";
		var linhas_erro = "";
		for (var i = 0; i < conjunto_campo.cont_campo; i++)
		{
			prefixo_erro = "";
			linhas_erro = "";
			var erro_atual = "";
			var campo = conjunto_campo.vet_campo[i];
			if (document.getElementById(campo.num_iteracao))
			{
				var num_iteracao = document.getElementById(campo.num_iteracao).value;
			}
			else
			{
				var num_iteracao = 1;
			}

			for (var t = 0; t < num_iteracao; t++)
			{
				var id_campo = cria_id_campo(campo, t);

				erro_atual = verifica_campo_obrigatorio(campo, id_campo, num_iteracao, t)

				marca_erro_input_campo(campo, id_campo, num_iteracao, t, erro_atual != "");

				if (erro_atual != "")
				{
					prefixo_erro = erro_atual;
					linhas_erro += (t+1)+", ";
				}
			}
	
			if (prefixo_erro != "")
			{
				erro += prefixo_erro+" '"+campo.label_campo+"' ";
				if (num_iteracao > 1)
				{
					linhas_erro = linhas_erro.substr(0,(linhas_erro.length-2));
					if (campo.label_tabela != "")
					{
						erro += " de '"+campo.label_tabela+"'("+linhas_erro+")";
					}
					else
					{
						erro += " das linhas "+linhas_erro;
					}
				}
				if (campo.aba_campo != "")
				{
					erro += " na aba '"+campo.aba_campo+"'";
				}
				erro += "\n";
			}
		}	
	
		return erro;
	}
	
	function marca_erro_input_campo(campo, id_campo, num_iteracao, t, errado)
	{
		marca_erro_input(id_campo, errado, campo.tipo_campo);
	}

	function marca_erro_input(id_campo, errado, tipo_campo)
	{
		var campos_input = [];
		var input = document.getElementById(id_campo);

		switch(tipo_campo)
		{
			case "___hmi":
				var input = document.getElementById("hor_"+id_campo);
			break
		}

		if (input)
		{
			switch(tipo_campo)
			{
				case "___dta":
					campos_input[0] = document.getElementById("dia_"+id_campo);
					campos_input[1] = document.getElementById("mes_"+id_campo);
					campos_input[2] = document.getElementById("ano_"+id_campo);
				break;
				
				case "___hmi":
					campos_input[0] = document.getElementById("hor_"+id_campo);
					campos_input[1] = document.getElementById("min_"+id_campo);
				break;
				
				case "___nfo":
				case "___nfm":
					campos_input[0] = document.getElementById(id_campo+"_valor");
					if (campos_input[0].innerHTML == "")
					{
						campos_input[0].innerHTML += "&nbsp;"
					}
				break
				case "___vet":
				case "___int":
				case "___ckb":
				case "___rad":
					if (document.getElementById("area_"+id_campo))
					{
						campos_input[0] = document.getElementById("area_"+id_campo);
					}
					else
					{
						campos_input[0] = input;
					}
				break;
				default:
					campos_input[0] = input;
				break;
			}

			for (var i = 0; i < campos_input.length; i++)
			{
				if (campos_input[i])
				{
					marca_erro_input(campos_input[i], errado)
				}
			}
		}
	}


	function marca_erro_input(elemento, valor_errado)
	{
		var nome = "";
		if (elemento.type)
		{
			nome = elemento.id
		}
		else if (elemento != "")
		{
			nome = elemento;
		}
		
		var campo = document.getElementById(nome);
		if (campo)
		{
			if (valor_errado)
			{
				if (campo.className == "undefined" || !campo.className)
				{
					campo.className = "";
				}
				if ((!campo.class_antiga || campo.class_antiga == "undefined") && campo.className != "input-error")
				{
					campo.class_antiga = campo.className;
				}
				campo.className = "input-error";
			}
			else
			{
				if (campo.class_antiga != "undefined")
				{
					campo.className = campo.class_antiga;
				}
			}
		}
		else
		{
//		alert(nome);
		}
	}
	
	
	function cria_id_campo(campo, num_iteracao)
	{
		return campo.prefixo_campo+""+num_iteracao;
	}
	function verifica_campo_obrigatorio(campo, id_campo, numero_total, numero_atual)
	{
		var erro = "";
		var prefixo_erro = "";
		var input = document.getElementById(id_campo);
		
		var verifica = true;
		
		if (campo.campo_associado && campo.campo_associado != "")
		{
			id_campo_associado = cria_id_campo(campo.campo_associado, numero_atual);
			var erro_associado =  verifica_campo_obrigatorio(campo.campo_associado, id_campo_associado, numero_total, numero_atual);
			verifica = (erro_associado == "");
		}
		
		if (verifica)
		{
			if ((input || campo.tipo_campo == "___hmi") && campo.tipo_campo != "___rad")
			{
				switch (campo.tipo_campo)
				{
				//ordem dos campos interfere no resultado
					case "___hms"://hora e minuto segundo
						if (campo.tipo_campo == "___hms" && document.getElementById("seg_"+id_campo).value == "")
						{
							prefixo_erro = "É necessário inserir";
						}
					case "___hmi"://hora e minuto
						if (document.getElementById("hor_"+id_campo))
						{
							if (document.getElementById("hor_"+id_campo).value == "" || document.getElementById("min_"+id_campo).value == "")
							{
								prefixo_erro = "É necessário inserir";
							}
						}
					break;

					case "___dth"://data e hora
					case "___dta"://data
						var input1 = document.getElementById("dia_"+id_campo);
						var input2 = document.getElementById("mes_"+id_campo);
						var input3 = document.getElementById("ano_"+id_campo);
						if (input1 && input2 && input3)
						{
							if ((input1.value*1) == 0 || (input2.value*1) == 0 || (input3.value*1) == 0)
							{
								prefixo_erro = "É necessário selecionar";
							}
						}
					break;
		
					case "___edt"://editor
						var texto_editor = FCKeditorAPI.GetInstance(input.id);
						mensagem = texto_editor.GetXHTML();
						if (mensagem == "")
						{
							prefixo_erro = "É necessário inserir";
						}
					break;
					
					case "___rea"://real
					case "___int"://inteiro
						if (input.value == "" || input.value == "0" || input.value == "0,00")
						{
							prefixo_erro = "É necessário inserir";
						}
					break;
	
					case "___vet"://tabela interna
						if (input.value == "")
						{
							prefixo_erro = "É necessário selecionar";
						}
					break;
					case "___ckb"://checkbox
						if (input.value == "")
						{
							prefixo_erro = "É necessário selecionar";
						}
					break;

					case "___nfm":
					case "___nfo"://inteiro
						if (input.value == "" || input.value == 0)
						{
							prefixo_erro = "É necessário inserir";
						}
					break;
		
					case "___arq":
					case "___aqs":
						if (input.value == "" || input.value == 0)
						{
							if (document.getElementById("apaga_"+input.id))
							{
								if (document.getElementById("apaga_"+input.id).checked)
								{
									prefixo_erro = "É necessário inserir";
								}
							}
							else
							{
								prefixo_erro = "É necessário inserir";
							}
						}
					break;


					case "___cpf": //CPF
						if (input.value == "")
						{
							prefixo_erro = "É necessário inserir";
						}
						else if (!is_cpf_valido(input.value))
						{
							prefixo_erro = "CPF Inválido em";
						}
					break;

					case "___cnj": //CNPJ
						if (input.value == "")
						{
							prefixo_erro = "É necessário inserir";
						}
						else if (!is_cnpj_valido(input.value))
						{
							prefixo_erro = "CNPJ Inválido em";
						}
					break;

					case "___pwc": //senha com confirmacao
						var input2 = document.getElementById("confirm_"+id_campo);
						if (input.value == "")
						{
							prefixo_erro = "É necessário inserir";
						}
						else if (input2 && input2.value != input.value)
						{
							prefixo_erro = "Confirmação incorreta de";
						}
					break;
					
					case "___mai":
						if (input.value == "")
						{
							prefixo_erro = "É necessário inserir";
						}
						else if (!is_mail_valido(input.value))
						{
							prefixo_erro = "E-mail Inválido em";
						}
					break;


					case "___psw"://senha
					case "___cor"://cor
					case "___txt"://texto
					case "___str"://caracter
					default:
						if (input.value == "")
						{
							prefixo_erro = "É necessário inserir";
						}
					break;
				}
			}
			else
			{
				if (campo.tipo_campo == "___rad" && document.getElementById(id_campo+"_0")) //radio
				{
					var contador = 0;
					var checado = false;
					try
					{
						while( document.getElementById(id_campo+"_"+contador) )
						{
							checado = checado || document.getElementById(id_campo+"_"+contador).checked;
							contador++;
						}
					}
					catch(e)
					{
						checado = true;
					}
					
					if (!checado)
					{
						prefixo_erro = "É necessário selecionar";
					}
				}
			}
		}
		return prefixo_erro;
	}
	
	function get_valor_campo(elemento)
	{
		var id_campo = "";
		if (elemento.id)
		{
			id_campo = elemento.id;
		}
		else
		{
			id_campo = elemento;
		}
		var valor = "";
		var tipo_campo = get_tipo_campo(id_campo);

		switch (tipo_campo)
		{
			case "___c2c"://dois combos
				valor = document.getElementById(id_campo).value;
			break;
			case "___ckb"://checkbox
/*				if (document.getElementById(id_campo+"_0")) //checkbox
				{
					var contador = 0;
					var checado = false;
					try
					{
						while( document.getElementById(id_campo+"_"+contador) )
						{
							if (document.getElementById(id_campo+"_"+contador).checked)
							{
								valor += document.getElementById(id_campo+"_"+contador).value+",";
							}
							contador++;
						}
						valor = valor.substr(0, (strlen(valor)-1) );
					}
					catch(e) {}
				}*/
				valor = document.getElementById(id_campo).value;
			break;
			case "___rad":
				if (document.getElementById(id_campo+"_0")) //radio
				{
					var contador = 0;
					var checado = false;
					try
					{
						while( document.getElementById(id_campo+"_"+contador) )
						{
							if (document.getElementById(id_campo+"_"+contador).checked)
							{
								valor = document.getElementById(id_campo+"_"+contador).value;
							}
							contador++;
						}
					}
					catch(e) {}
				}
			break;
	
			case "___dta"://data
			case "___dth"://data e hora
			case "___hms"://hora e minuto segundo
			case "___hmi"://hora e minuto
				if (id_campo.substr(0,4) == "dia_" || id_campo.substr(0,4) == "mes_" || id_campo.substr(0,4) == "ano_" ||
					id_campo.substr(0,4) == "hor_" || id_campo.substr(0,4) == "min_" || id_campo.substr(0,4) == "seg_")
				{
					id_campo = id_campo.substr(4)
				}
				
				var input1 = document.getElementById("dia_"+id_campo);
				var input2 = document.getElementById("mes_"+id_campo);
				var input3 = document.getElementById("ano_"+id_campo);
				if (input3 && input2 && input1)
				{
					valor = input3.value+"-"+input2.value+"-"+input1.value
				}
				if (tipo_campo != "___dta")
				{
					valor += " "+document.getElementById("hor_"+id_campo).value;
					valor += ":"+document.getElementById("min_"+id_campo).value;
				}
				if (tipo_campo == "___hms")
				{
					valor += ":"+document.getElementById("seg_"+id_campo).value;
				}
			break;
	
			case "___edt"://editor
				var insere_texto_editor = FCKeditorAPI.GetInstance(id_campo);
			    valor = insere_texto_editor.GetXHTML();
			break;

			case "___psw"://senha
			case "___cor"://cor
			case "___rea"://real
			case "___txt"://texto
			case "___str"://caracter
			case "___int"://inteiro
			case "___vet"://tabela interna
			case "___ckb"://checkbox
			default:
				if (document.getElementById(id_campo))
				{
					valor = document.getElementById(id_campo).value;
				}
			break;
		}
		return valor;
	}
	
	
	function set_valor_campo(elemento, valor)
	{
		var id_campo = "";
		if (elemento.id)
		{
			id_campo = elemento.id;
		}
		else
		{
			id_campo = elemento;
		}

		var tipo_campo = get_tipo_campo(id_campo);
		switch (tipo_campo)
		{
			case "___rad":
			break;
	
			case "___dta"://data
			case "___dth"://data e hora
			case "___hms"://hora e minuto segundo
			case "___hmi"://hora e minuto

				var valor_temp = valor.split(" ");

				var input1 = document.getElementById("dia_"+id_campo);
				var input2 = document.getElementById("mes_"+id_campo);
				var input3 = document.getElementById("ano_"+id_campo);
	
				if (input3 && input2 && input1)
				{
					var valores_temp = valor_temp[0].split("-");
	
					input3.value = valores_temp[0];
					input2.value = valores_temp[1];
					input1.value = valores_temp[2];
				}

				var valor_hora = "";
				if (tipo_campo == "___hms" || tipo_campo == "___hmi")
				{
					valor_hora = valor;
				}
				else if (tipo_campo != "___dta")
				{
					valor_hora = valor_temp[1];
				}
				if (valor_hora != "")
				{
					var vet_hora = valor_hora.split(":");
					if (tipo_campo != "___dta")
					{
						document.getElementById("hor_"+id_campo).value = vet_hora[0];
						document.getElementById("min_"+id_campo).value = vet_hora[1];
					}
					if (tipo_campo == "___hms")
					{
						document.getElementById("seg_"+id_campo).value = vet_hora[2];
					}
				}
				
			break;
	
			case "___psw"://senha
			case "___cor"://cor
			case "___rea"://real
			case "___edt"://editor
			case "___txt"://texto
			case "___str"://caracter
			case "___int"://inteiro
			case "___vet"://tabela interna
			case "___ckb"://checkbox
			default:
				if (document.getElementById(id_campo))
				{
					document.getElementById(id_campo).value = valor;
				}
			break;
		}
		return valor;
	}
	
	
	function class_conjunto_campo()
	{
		this.vet_campo = [];
		this.cont_campo = 0;
		
		this.insere_campo = function (campo)
		{
			this.vet_campo[this.cont_campo] = campo;
			this.cont_campo = this.cont_campo+1;
		}
		this.get_campo= function(cod_campo)
		{
			var ret = "";
			for (var i =0; i < conjunto_campo.vet_campo.length; i++)
			{
				var campo = conjunto_campo.vet_campo[i];
	
				if (cod_campo == campo.cod_campo)
				{
					ret = campo;
				}
			}
			return ret;
		}
	}
	
	
	function class_campo(label_tabela, nom_var_num_iteracao, prefixo, tipo, label, nom_aba_cadastro, campo_associado, cod_campo, nome_campo, valor, valor_format)
	{
		this.cod_campo = cod_campo;
		this.nome_campo = nome_campo;
		this.prefixo_campo = prefixo;
		this.tipo_campo = tipo;
		this.label_campo = label;
		this.num_iteracao = nom_var_num_iteracao;
		this.aba_campo = nom_aba_cadastro;
		this.label_tabela = label_tabela;
	
		this.campo_associado = campo_associado;
	
		this.valor_campo = valor;
		this.valor_format_campo = valor_format;
	}
	
	
	function verifica_tabela_obrigatoria(conjunto_tabela)
	{
		var erro = "";
		
		
		for (var i = 0; i < conjunto_tabela.cont_tabela; i++)
		{
			var num_iteracao_real = conjunto_tabela.vet_tabela[i].get_num_elementos();
			var erro_atual = "";

			if (conjunto_tabela.vet_tabela[i].num_min_relacionada > 0)
			{
				if (num_iteracao_real < conjunto_tabela.vet_tabela[i].num_min_relacionada)
				{
					erro_atual += " no mínimo "+conjunto_tabela.vet_tabela[i].num_min_relacionada+"";
				}
			}
			if(conjunto_tabela.vet_tabela[i].num_max_relacionada > 0)
			{
				if (num_iteracao_real > conjunto_tabela.vet_tabela[i].num_max_relacionada)
				{
					if (erro_atual != "")
					{
						erro_atual += " e";
					}
					erro_atual += " no máximo "+conjunto_tabela.vet_tabela[i].num_max_relacionada+"";
				}
			}
			if (erro_atual != "")
			{
				erro += "Em '"+conjunto_tabela.vet_tabela[i].label_tabela+"'";
				if (conjunto_tabela.vet_tabela[i].nom_aba != "")
				{
					erro += " na aba '"+conjunto_tabela.vet_tabela[i].nom_aba+"'";
					}
				erro += " é necessário inserir"+erro_atual+" itens.\n";
			}
		}
		
		return erro;
	}
	
	function class_conjunto_tabela()
	{
		this.vet_tabela = [];
		this.cont_tabela = 0;
	
		this.insere_tabela = function (tabela)
		{
			this.vet_tabela[this.cont_tabela] = tabela;
			this.cont_tabela = this.cont_tabela+1;
		}
	}
	
	function class_tabela(label_tabela, nom_var_num_iteracao, nom_var_num_iteracao_real, nom_aba_cadastro, num_min, num_max, flg_tipo_relacionamento, flg_campo_mult_valor, nome_prefixo_campo_input_multi_valor)
	{
		this.num_iteracao = nom_var_num_iteracao;
		this.num_iteracao_real = nom_var_num_iteracao_real;
		this.nom_aba = nom_aba_cadastro;
		this.label_tabela = label_tabela;

		this.num_min_relacionada = num_min;
		this.num_max_relacionada = num_max;

		this.flg_tipo_relacionamento = flg_tipo_relacionamento;
		this.flg_campo_mult_valor = flg_campo_mult_valor;
		
		this.nome_prefixo_campo_input_multi_valor = nome_prefixo_campo_input_multi_valor;
		
		this.get_num_elementos = function()
		{
			switch(this.flg_tipo_relacionamento)
			{
				case "2":
					if (document.getElementById(this.num_iteracao_real))
					{
						ret = document.getElementById(this.num_iteracao_real).value;
					}
				break;
				case "3":
					if(this.flg_campo_mult_valor == "1")
					{
						if (document.getElementById(this.nome_prefixo_campo_input_multi_valor+"0_1"))
						{
							ret = document.getElementById(this.nome_prefixo_campo_input_multi_valor+"0_1").options.length;
						}
						else if (document.getElementById(this.nome_prefixo_campo_input_multi_valor+"0") && document.getElementById(this.nome_prefixo_campo_input_multi_valor+"0").options)
						{
							for (var i = 0; i < document.getElementById(this.nome_prefixo_campo_input_multi_valor+"0").options.length; i++)
							{
								if (document.getElementById(this.nome_prefixo_campo_input_multi_valor+"0").options[i].selected)
								{
									ret++;
								}
							}
						}
					}
					else
					{
						for (var i =0; i < document.getElementById(this.num_iteracao).value; i++)
						{
							if (document.getElementById(this.nome_prefixo_campo_input_multi_valor+i+"_0") && document.getElementById(this.nome_prefixo_campo_input_multi_valor+i+"_0").checked)
							{
								ret++;
							}
						}
					}
				break;
			}
			return ret;
		}
	}
	
	
	function mostra_esconde_filhos(id_linha, forca_aberto)
	{
		var	linha = document.getElementById(id_linha);

		if (linha)
		{
			var i = 0;
			var filho;
			var id_filho = id_linha+"_"+i;
//		alert(id_filho);
			var aberto = 1;
			while((filho = document.getElementById(id_filho)) && (i < 1000))
			{
				if (forca_aberto)
				{
					if (forca_aberto > 0)
					{
						filho.style.display = "";
						aberto = 1;
					}
					else
					{
						filho.style.display = "none";
						aberto = -1;
					}
				}
				else
				{
					if (filho.style.display == "none")
					{
						filho.style.display = "";
						aberto = 1;
					}
					else
					{
						filho.style.display = "none";
						aberto = -1;
					}
				}
	
				if (aberto < 0)
				{
					try {
						mostra_esconde_filhos(id_filho, aberto)
					}catch(e){}
				}
	
				i++;
				id_filho = id_linha+"_"+i;
			}
	
			try {
				var id_img = "img_"+id_linha;
				var imagem = document.getElementById(id_img);
				if (imagem)
				{
					if (aberto > 0)
					{
						imagem.src = "images/base_arvore_menos.gif";
					}
					else
					{
						imagem.src = "images/base_arvore_mais.gif";
					}
				}
			}
			catch(e){}
	
		}
	}
	function get_num_iteracao(elemento)
	{
		var ret = "";
		if (elemento)
		{
			var nome = "";
			if (elemento.name)
			{
				nome = elemento.name
			}
			else if (elemento != "")
			{
				nome = elemento;
			}
			if (nome != "")
			{
				var partes = nome.split("//");
				if (partes[1])
				{
					var iteracao = partes[1].split("_");
					ret = iteracao[0];

					var temp = ret.split("/");
					if (temp.length > 1)
					{
						ret = temp[1];
					}
				}
			}
		}
		return ret;
	}
	
	function get_tipo_campo(id_campo)
	{
		var ret = "";
		if (id_campo)
		{
			var nome = id_campo.split("___");
			if (nome[1])
			{
				var temp = nome[1].split("//");
				ret = "___"+temp[0];
			}
		}
		return ret;
	}
	
	function muda_super_campo(cod_tabela, pai, cod_pai, cod_filho, cod_referencia, nom_filho, sufixo_filho)
	{
		url = "index2.php?page=meta/muda_super_campo&num_iteracao="+get_num_iteracao(pai)+"&cod_tabela="+cod_tabela+"&cod_pai="+cod_pai+"&cod_filho="+cod_filho+"&cod_referencia="+cod_referencia+"&valor_super="+pai.value+"&nom_filho="+nom_filho+"&sufixo_filho="+sufixo_filho;
		frame_trabalho.location = url;
	}
	
	function verifica_unicidade(elemento, campo)
	{
		var ret = true;
	
		if (document.getElementById(campo.num_iteracao))
		{
			var num_iteracao = document.getElementById(campo.num_iteracao).value;
			var valor_atual = elemento.value;
	
			for (var i = 0; i < num_iteracao; i++)
			{
				var nom_elemento_verif = campo.prefixo_campo+""+i;
				if (elemento.id != nom_elemento_verif)
				{
					if (document.getElementById(nom_elemento_verif))
					{
						if (document.getElementById(nom_elemento_verif).value == elemento.value)
						{
							ret = false;
						}
					}
				}
			}
		}
		if (!ret)
		{
			alert("Este '"+campo.label_campo+"' já foi selecionado, por favor selecione outro '"+campo.label_campo+"'.");
			elemento.value = "";
		}
	}
	
	function marca_todos_check(check, campo)
	{
		var num_iteracao = document.getElementById(campo.num_iteracao).value;
	
		for (var t = 0; t < num_iteracao; t++)
		{
			var id_campo = cria_id_campo(campo, t)+"_0";
			var elemento = document.getElementById(id_campo);
			if (elemento)
			{
				if (elemento.checked != check.checked)
				{
					elemento.click();
				}
			}
		}
	}
	
	
	function seleciona_novo_form_campo(id_campo, chave, valor, quantidade)
	{
		document.getElementById(id_campo).value = chave;
		document.getElementById(id_campo+"_valor").innerHTML = valor+" ";
		if (document.getElementById(id_campo+"_num_itens"))
		{
			document.getElementById(id_campo+"_num_itens").value = quantidade;
		}
	}
	
	function verifica_minuto(elemento)
	{
		var valor = elemento.value;
	
		valor = valor+"00";
		valor = valor.substr(0,2);
	
		if (valor*1 > 59)
		{
			valor = 59;
		}
		
		
		elemento.value = valor;
	}
	
	function exibe_esconde_area(id_area)
	{
		var area = document.getElementById(id_area);
		if (document.getElementById(id_area).style.display == "none")
		{
			area.style.display = '';
		}
		else
		{
			area.style.display = 'none';
		}
	}
	
	function get_itens_selecao_listagem()
	{
		var ret = "";
		var i = 0;
		var elemento = document.getElementById("ctr_item_listagem_"+i);
		while (i < 1000 || (elemento != null && elemento != "undefined"))
		{
			if (elemento != null && elemento != "undefined")
			{
				if (elemento.checked)
				{
					ret += elemento.value+",";
				}
			}
			i++;
			elemento = document.getElementById("ctr_item_listagem_"+i);
		}
		ret = ret.substr(0, (ret.length-1) );
	
		return ret;
	}
	
	function check_itens_listagem(pai)
	{
		if (pai)
		{
			var i = 0;
			var elemento = document.getElementById(pai.id+"_"+i);
			while (i < 1000 || (elemento != null && elemento != "undefined"))
			{
				if (elemento != null && elemento != "undefined")
				{
					elemento.checked = pai.checked
				}
				i++;
				elemento = document.getElementById(pai.id+"_"+i);
			}
		}
	}
	


	function manda_itens_combo(inclusao, nome_campo_valor, nome_campo_origem, nome_campo_destino, envia_todos)
	{
		var obj_origem = document.getElementById(nome_campo_origem);
		var obj_destino = document.getElementById(nome_campo_destino);

		sendItens(obj_origem, obj_destino, envia_todos);

		var string_elementos = "";

		var obj_elementos = obj_destino;
		if (!inclusao)
		{
			obj_elementos = obj_origem;
		}
		
		for(i = 0; i < obj_elementos.options.length; i++)
		{
			string_elementos += obj_elementos.options[i].value+",";
		}
		string_elementos = string_elementos.substr(0,(string_elementos.length-1));

		document.getElementById(nome_campo_valor).value = string_elementos;
	}
	
	function sortNumber(a, b)
	{
		return a.text - b.text
	}
	
	function sendItens(obj_origem, obj_destino, envia_todos)
	{
		tempArr = new Array();
		
		for(i = (obj_origem.options.length-1); i >= 0; i--)
		{
			if(obj_origem.options[i].selected || envia_todos)
			{
				tempArr.push(obj_origem.options[i]);
				obj_origem.remove(i);
			}
		}
		if (obj_destino.options)
		{
			for(j = (obj_destino.options.length-1); j >= 0 ; j--)
			{
				tempArr.push(obj_destino.options[j]);
			}
		}

		//tempArr.sort(sortNumber);

		obj_destino.length = 0;
		obj_destino.length = tempArr.length;
		var cont = 0;
		for(k = (tempArr.length-1); k >= 0; k--)
		{
			if (!obj_destino.options)
			{
				cria_elemento(obj_destino, "option", "op_"+obj_destino.id+"_"+tempArr[k].value, "value = '"+tempArr[k].value+"'", tempArr[k].text);
			}
			else
			{
				obj_destino.options[cont].text = tempArr[k].text;
				obj_destino.options[cont].value = tempArr[k].value;
			}
			cont++;
		}
		obj_destino.selectedIndex = -1;
		
	}

	function is_cpf_valido(cpf)
    {
		var numeros, digitos, soma, i, resultado, digitos_iguais;
		digitos_iguais = 1;
		if (cpf.length < 11)
	    {
	    	return false;
		}
		
		cpf = cpf.replace(/[\.-]/gi, "");

		for (i = 0; i < cpf.length - 1; i++)
		{
			if (cpf.charAt(i) != cpf.charAt(i + 1))
			{
				digitos_iguais = 0;
				break;
			}
		}	   
		   
		if (!digitos_iguais)
		{
			numeros = cpf.substring(0,9);
			digitos = cpf.substring(9);
			soma = 0;
			for (i = 10; i > 1; i--)
			{
				soma += numeros.charAt(10 - i) * i;
			}

			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
	        
			if(resultado != digitos.charAt(0))
			{
				return false;
			}
			numeros = cpf.substring(0,10);
			soma = 0;
			for (i = 11; i > 1; i--)
			{
				soma += numeros.charAt(11 - i) * i;
			}

			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

			if(resultado != digitos.charAt(1))
			{
				return false;
			}
			return true;
		}
		else
		{
			return false;
		}
	}
	
	
	function is_cnpj_valido(CNPJ)
	{
		var erro = "";
		if (CNPJ.length < 18) 
		{
			erro += "Não foram digitados todos os digitos";
		}
		else if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-"))
		{
			erro += "É necessário preencher corretamente o número do CNPJ";
		}
		//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("/","");
		}

		if (CNPJ == "00000000000" || CNPJ == "11111111111" ||
			CNPJ == "22222222222" || CNPJ == "33333333333" || CNPJ == "44444444444" ||
			CNPJ == "55555555555" || CNPJ == "66666666666" || CNPJ == "77777777777" ||
			CNPJ == "88888888888" || CNPJ == "99999999999")
		{
			erro += "É necessario preencher corretamente o número do CNPJ";
		}

		var nonNumbers = /\D/;
		if (nonNumbers.test(CNPJ)) 
			erro += "A verificação de CNPJ suporta apenas números";	
		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 incorreto";
		}
		return erro;	
	}

	
	function is_mail_valido(email)
	{
		var ret = true;
		email = str_replace(";", ",", email);
		var itens = email.split(",");
		for (var i = 0; i < itens.length; i++)
		{
			ret = ret && is_mail_valido_item(itens[i]);
		}

		return ret;
	}
	function is_mail_valido_item(email)
	{
		return ((email.length != 0) && (email.indexOf("@") > 1) && (email.indexOf('.') > 0) );
	}


	function str_replace(r, w, s){
    	return s.split(r).join(w);
	}
		
	function get_id_linha_acima(elemento)
	{
		if (elemento && elemento.tagName.toUpperCase() == "TR")
		{
			ret = elemento.id;
		}
		else
		{
			ret = get_id_linha_acima(elemento.parentNode);
		}
		return ret;
	}
	
	
	function verifica_sinal_campo(elemento)
	{
		if ( (unformat(elemento.value)*1.0) < 0)
		{
			if (elemento.className != "input_numero_negativo")
			{
				elemento.class_antiga = elemento.className;
				elemento.className = "input_numero_negativo";
			}
		}
		else
		{
			if (elemento.className != "input_numero_negativo")
			{
				elemento.class_antiga = elemento.className;
				elemento.className = elemento.class_antiga;
			}
			else
			{
				elemento.className = "";
			}
		}
	}
	
	
	
	var edicao_dinamica_ativa = 0;
	function edicao_dinamica(nome_tabela, nome_campo, restricao_registro_tabela, id_area)
	{
//		alert(nome_tabela+", "+nome_campo+", "+restricao_registro_tabela+", "+id_area);
		if (edicao_dinamica_ativa == 0)
		{
			window.frame_trabalho.location = "index2.php?page=meta/ctr_edicao_dinamica&nome_tabela="+nome_tabela+"&nome_campo="+nome_campo+"&restricao_registro_tabela="+restricao_registro_tabela+"&id_area="+id_area+"&num_iteracao="+get_num_iteracao(id_area);
			edicao_dinamica_ativa = 1;
		}
	}
	
	function fecha_edicao_dinamica(id_area)
	{
		if (document.getElementById("div_"+id_area))
		{
			document.getElementById("div_"+id_area).parentNode.removeChild(document.getElementById("div_"+id_area));
		}
		edicao_dinamica_ativa = 0;
	}

	function salva_edicao_dinamica(nome_tabela, nome_campo, restricao_registro_tabela, id_area, nome_formulario_campo)
	{
		window.frame_trabalho.location = "index2.php?page=meta/ctr_edicao_dinamica&salva=1&nome_tabela="+nome_tabela+"&nome_campo="+nome_campo+"&restricao_registro_tabela="+restricao_registro_tabela+"&id_area="+id_area+"&num_iteracao="+get_num_iteracao(id_area)+"&valor_campo="+get_valor_campo(nome_formulario_campo).replace("\"", "\\\"");
	}
	
	function mostra_esconde_elemento(elemento)
	{	
		var id_elemento = "";
		if (elemento.id)
		{
			id_elemento = elemento.id
		}
		else if (elemento != "")
		{
			id_elemento = elemento;
		}
		if (id_elemento != "" && document.getElementById(id_elemento))
		{
			if (document.getElementById(id_elemento).style.display == "none")
			{
				document.getElementById(id_elemento).style.display = "";
			}
			else
			{
				document.getElementById(id_elemento).style.display = "none";
			}
		}
	}


	function abre_janela_cor(nome_campo, nome_area_cor)
	{
		window.open("cores.php?campo="+nome_campo+"&campo_cor="+nome_area_cor, 'remote', 'toolbar=no, location=no, directories=no, status=no, statusbar = no, menubar=no, scrollbars=no,resizable=no,width=170, height=170, left=400, top=300');
	}
	
	function digita_seleciona_habilita_div(id_campo, valor_digitado, nome_tabela, nome_campo_chave)
	{
		document.getElementById(id_campo+"_div").style.display = "";
		xajax_digita_seleciona_valores(id_campo, valor_digitado, nome_tabela, nome_campo_chave);
	}
	
	function digita_seleciona_clica_valor(id_campo, valor_digitado, nome_tabela, nome_campo_chave, valor_chave, valor_principal)
	{
		if(valor_chave != 0 && valor_principal != "Nenhum")
		{
			document.getElementById(id_campo).value = valor_chave;
			document.getElementById(id_campo+"_valor").value = valor_principal;	
		}
		else
		{
			document.getElementById(id_campo).value = "";
			document.getElementById(id_campo+"_valor").value = "";
		}
		document.getElementById(id_campo+"_div").style.display = "none";
		
	}