/*
// ------------------------------------------------------------------------------------------ //
// Acrescenta algumas propriedades aos controles:
// .Indice			: indica o índice na tela para o controle
// .IndiceAnterior	: indica o índice do controle anterior
// .IndicePosterior	: indica o índice para o controle posterior
// .Tam				: tamanho máximo para digitação
// .AutoSkip		: indica se pula para o próximo campo após completar o tamanho do campo
// .Tipo			: indica o tipo de dado
//						'D' -> só dígitos de 0(zero) a 9(nove)
//						'N' -> dígitos de 0(zero) a 9(nove), "."(ponto) e ","(vírgula)
//						'C' -> caracteres de 'a' até 'z' e de 'A' até 'Z'
//						outro -> qualquer caracter entre ascii 32 e ascii 127
// .Saltar			: (reservado) indica o momento de saltar de campo
// ------------------------------------------------------------------------------------------ //
*/



/**
 * @desc gera um confirm e c for verdadeiro redireciona a pagina
 */
function confirma(URL,text)
{
	var truthBeTold = window.confirm(text);
	if (truthBeTold)
	{
		location.href=URL;
	}  
}

/**
 * @desc gera um confirm e c for verdadeiro executa algum comando
 */
function confirma_form(comand,text)
{
	var truthBeTold = window.confirm(text);
	if (truthBeTold)
	{
		eval(comand);
	}  
}

// Carrega índices para o próximo controle e controle anterior
function InicializarIndices()
{
	return;
	if (document.CargaInicial==null)
	{
		document.CargaInicial=false;		// Seta para só fazer uma vez por documento
		var ctrlAnterior=null;
		var IndAnt=0;
		for ( var i=0; i < document.forms[0].elements.length; i++)
		{
			var e=document.forms[0].elements[i];
			if ( e.type!="hidden" && e.type!="image" )		
			{
				if ( ctrlAnterior != null )
					ctrlAnterior.IndicePosterior=i;
				ctrlAnterior=e;
				e.Indice=i;
				e.IndiceAnterior=IndAnt;
			}
		}
		//if ( ctrlAnterior!=null )
		//{
		//	ctrlAnterior.IndicePosterior=i-1;
		//}
	}
}

// Colocar o foco em determinado campo
function SetarFoco ( ind )
	{
	InicializarIndices();
	if ( isNaN(ind) && document.forms[0].elements[ind].type!="hidden" )
		document.forms[0].elements[ind].focus();
	else
		for (;ind<document.forms[0].elements.length;ind++)
			if ( document.forms[0].elements[ind].type!="hidden" )
				break;
		if ( ind<=document.forms[0].elements.length )
			document.forms[0].elements[ind].focus();
	}

// Limpar o conteúdo do(s) campo(s)
function LimparCampo ( ind )
	// Para -1, limpa todos os elementos
	{
	if (isNaN(ind))			// Limpa pelo nome
		document.forms[0].elements[ind].value="";
	else if (ind != -1 )	// Limpa o elemento "ind" ( só considera "text" e "password" )
		for ( var i=ind; i < document.forms[0].elements.length;i++ )
			if ( document.forms[0].elements[i].type=="text" || document.forms[0].elements[i].type=="password")		// Só limpa campo "text"
				{
				document.forms[0].elements[i].value="";
				break;
				}
	else					// Limpa todos os elementos "text" e "password"
		for ( var i=0; i < document.forms[0].elements.length; i++ )
			if ( document.forms[0].elements[i].type=="text" || document.forms[0].elements[i].type=="password" )
				document.forms[0].elements[i].value="";
		
	}

// Verificar qual navegador
function QualNavegador() 
{
	var s = navigator.appName;
	if ( s == "Microsoft Internet Explorer" )
		return "IE";
	else if ( s == "Netscape" )
		return "NE";
	else
		return "";
}

// Verificar qual a versão do navegador
function QualVersao()
{
	var s = navigator.appVersion;
	if ( QualNavegador() == "IE" )
	{
		var i = s.search("MSIE");
		s=s.substring(i+5);
		i=s.search(".");
		return parseInt(s.substring(0,i+1));
	}
	else if ( QualNavegador() == "NE" )
		return parseInt(s.substring(0,1));
	else
		return 0;
}

var dicionario_caracter = "";
// Faz a validação dos campos de acordo com os parametros passados
function ValidaCampo(ctrl, tam, tipo, autoSkip, caracteres){
	// Filtra navegadores conhecidos
//		alert("entrei")
//		alert(QualVersao())
//		alert(QualNavegador())

	dicionario_caracter = caracteres;

	var s = QualNavegador();
	if (s.length == 0)
		return;
	if ((s == "IE") && (QualVersao() > 6))
		return;
	if ((s == "NE") && (QualVersao() > 5))
		return;

	if (ctrl.onkeypress==null){
		if (autoSkip==null)
			autoSkip=true;
		if (tipo!=null)
			tipo.toUpperCase();
		ctrl.tam=tam;
		ctrl.tipo=tipo;
		ctrl.autoSkip=true;
		ctrl.Saltar=false;
		InicializarIndices();
		ctrl.onkeypress = ValidarTecla;
	}
}

// Corrige os valores dos campos de acordo com o tipo dos mesmos
function CorrigeCampo(campo, tipo, caracteres){
	var texto = campo.value;
	var texto_limpo = "";
	var pode = true;
	var c;

	for (var i=0; i < texto.length; i++){
		pode = true;
		// pega o char da string
		// texto.charAt(i)
		// pega o ASCII do char da string
		tk = texto.charCodeAt(i);

		switch (tipo){
			case "D": // Digitos
				if ((tk < 48) || (tk > 57))
					pode = false;
			break;
			case "V": // Digitos separados por virgula
			case "M": // Digitos separados por virgula
				if (((tk < 48) || (tk > 57)) && (tk != 44) && (tk != 45)) //48 = 0, 57 = 9, 44 = , e 45 = -
					pode = false;
				if ((tk == 44)){
					while(substr_count(texto, ',') > 1){
						var pos = texto.search(',');
						texto = texto.substring(0, pos)+texto.substring(pos+1, texto.length);
						if(pode)
							i--;
						pode = false;
					}
				}
			break;
			case "P": // Digitos separados por ponto
//				alert("quantos: "+this.value.search(","));
				if (((tk < 48) || (tk > 57)) && (tk != 46) && (tk != 45))
					pode = false;
//		if ((tk == 46) && ((this.value.search(".") > 0) || (this.value.length==0))){ naum funfo
				if ((tk == 46)){
					while(substr_count(texto, '.') > 1){
						var pos = retorna_pos(texto, '.');
						texto = texto.substring(0, pos)+texto.substring(pos+1, texto.length);
						if(pode)
							i--;
						pode = false;
					}
				}
			break;
			case "C": // Letras
				if ((tk > 96) && (tk < 123)) //97 = a && 122 = z
					tk = tk - 32;
				if(!((tk > 64) && (tk < 91)) && (tk != 32))
					pode = false;
			break;
			case "Dt": // Datas
				if (((tk < 48) || (tk > 57)) && (tk != 47))
					pode = false;
				else{
					if(tk == 47) {
						switch(texto_limpo.length){
							case 1:
								texto_limpo = "0"+texto_limpo.slice(0,1);
							break;
							case 4:
								texto_limpo = texto_limpo.slice(0,3)+"0"+texto_limpo.slice(3,4);
							break;
							case 2:
							break;
							case 5:
							break;
							default :
								pode = false;
						}
					}
					else{
						if((texto_limpo.length == 2) || (texto_limpo.length == 5))
							texto_limpo += "/";
					}
				}
				if(texto_limpo.length > 9)
					pode = false;
			break;
			case "AN": // Alpha Numerico
				if ((tk > 96) && (tk < 123)) //97 = a && 122 = z
					tk = tk - 32;
				if(!(((tk > 47) && (tk < 58)) || ((tk > 64) && (tk < 91))) && (tk != 32)) // Valido p/ numeros e letras
					pode = false;
			break;
			case "AX": // Alpha Numerico completo acentos ï¿?e outros
				if((tk < 42) || ( (tk > 90) && (tk < 97) ) || (tk > 122) && ((tk > 146) && (tk < 152)) )
				{
					if( (tk!=231) && (tk!=94) && (tk!=95) && (tk!=227) && (tk!=245) && (tk!=32))
					{
						pode = false;
					}
				}
			break;
			case "CNPJ": // para arquivos c/ numeros, ".", "/", "-"
				if ((tk < 45) || (tk > 57))
					pode = false;
			break;
			case "Dic": // os caracteres possiveis sao determinados pela variavel caracteres
				c = String.fromCharCode(tk);
				if(caracteres.search(c) < 0){
					pode = false;
				}
			break;
			case "NDic": // os caracteres descartados sao determinados pela variavel caracteres
				c = String.fromCharCode(tk);
				if(caracteres.search(c) > -1){
					pode = false;
				}
			break;
		}

		if(pode){
			texto_limpo += texto.charAt(i);
		}
	}
	if(tipo == "M")
		texto_limpo = format(unformat(texto_limpo));

	campo.value = texto_limpo;
}

// Setar o evento
function SetarEvento(ctrl, Tam, Tipo, AutoSkip )
{
	// Filtra navegadores conhecidos
	var s = QualNavegador();
	if (s.length == 0)
		return;
	if ((s == "IE") && (QualVersao() > 6))
		return;
	if ((s == "NE") && (QualVersao() > 5))
		return;

	if (ctrl.onkeypress==null)
	{
		if (AutoSkip==null)
			AutoSkip=true;
		if (Tipo!=null)
			Tipo.toUpperCase();
		ctrl.tam=Tam;
		ctrl.tipo=Tipo;
		ctrl.autoSkip=true;
		ctrl.Saltar=false;
		InicializarIndices();
		ctrl.onkeypress=ValidarTecla;
	}
}

function SaltarCampo(ctrl)
{
	if (ctrl==null)
		ctrl=this;
	if ( ctrl.AutoSkip && ctrl.Saltar)
		if (ctrl.Saltar)
		{
			ctrl.Saltar=false;
			if ( ctrl.IndicePosterior != null )
				SetarFoco(ctrl.IndicePosterior);
		}
}

function ValidarTecla (evnt){
	var tk;
	var c;

	tk = ( (QualNavegador()=="IE") ? event.keyCode : evnt.which);

	// Sï¿?aceita teclas alfanumï¿?icas. Nï¿? aceita teclas de controle
	if (tk < 32)
		return true;
/*
		if (tk > 127)
			return false;
*/
//alert(tk);

	switch (this.tipo){
		case "D": // Digitos
			if ((tk < 48) || (tk > 57))
				return false;
		break;
		case "V": // Digitos separados por virgula
		case "M": // Digitos separados por virgula
			if (((tk < 48) || (tk > 57)) && (tk != 44) && (tk != 45)) //48 = 0, 57 = 9, 44 = , e 45 = -
				return false;
			if ((tk == 44) && ((this.value.search(",") > -1) || (this.value.length == 0)))
				return false;
			if ((tk == 45) && (this.value.search("-") > -1)){
				return false;
			}
			else{
				if(tk == 45){
					this.value = "-"+this.value;
					return false;
				}
			}
		break;
		case "P": // Digitos separados por ponto
//			alert("quantos: "+this.value.search(","));
			if (((tk < 48) || (tk > 57)) && (tk != 46) && (tk != 45))
				return false;
//		if ((tk == 46) && ((this.value.search(".") > 0) || (this.value.length==0))){ naum funfo
			if ((tk == 46) && ((retorna_pos(this.value, '.') > -1) || this.value.length==0)){
				return false;
			}
			if ((tk == 45) && (this.value.search("-") > -1)){
				return false;
			}
			else{
				if(tk == 45){
					this.value = "-"+this.value;
					return false;
				}
			}
		break;
		case "C": // Letras
			if ((tk > 96) && (tk < 123)) //97 = a && 122 = z
				tk = tk - 32;
			if(!((tk > 64) && (tk < 91)) && (tk != 32))
				return false;
		break;
		case "Dt": // Datas
			if (((tk < 48) || (tk > 57)) && (tk != 47))
				return false;
			else
				if(tk == 47) {
					switch(this.value.length){
						case 1:
							this.value = "0"+this.value.slice(0,1);
						break;
						case 4:
							this.value = this.value.slice(0,3)+"0"+this.value.slice(3,4);
						break;
						case 2:
						break;
						case 5:
						break;
						default :
							return false;
					}
				}
				else
					if((this.value.length == 2) || (this.value.length == 5))
						this.value += "/";
		break;
		case "AN": // Alpha Numerico
			if ((tk > 96) && (tk < 123)) //97 = a && 122 = z
				tk = tk - 32;
			if(!(((tk > 47) && (tk < 58)) || ((tk > 64) && (tk < 91))) && (tk != 32)) // Valido p/ numeros e letras
				return false;
		break;
		case "AX": // Alpha Numerico completo acentos ï¿?e outros
			if((tk < 42) || ( (tk > 90) && (tk < 97) ) || (tk > 122) && ((tk > 146) && (tk < 152)) )
			{
				if( (tk!=231) && (tk!=94) && (tk!=95) && (tk!=227) && (tk!=245) && (tk!=32))
				{
					return false;
				}
			}
		break;
		case "CNPJ": // para arquivos c/ numeros, ".", "/", "-"
			if ((tk < 45) || (tk > 57))
				return false;
		break;
		case "Dic": // os caracteres possiveis sao determinados pela variavel dicionario_caracter
			c = String.fromCharCode(tk);
			if(dicionario_caracter.search(c) < 0){
				return false;
			}
		break;
		case "NDic": // os caracteres descartados sao determinados pela variavel dicionario_caracter
			c = String.fromCharCode(tk);
			if(dicionario_caracter.search(c) > -1){
				return false;
			}
		break;
	}

	if(this.Tam != 0)
		this.Saltar=(QualNavegador()=="IE")? (this.value.length>this.Tam-1) : (this.value.length>this.Tam-2);
		if(this.Saltar)
			SaltarCampo(this);

	return true;
}

function corrige_texto(campo, tipo){
	var texto = campo.value;
	var texto_limpo = "";
	var pode = true;

	for (var i=0; i < texto.length; i++){
		pode = true;
		// pega o char da string
		// texto.charAt(i)
		// pega o ASCII do char da string
		tk = texto.charCodeAt(i);

		switch (tipo){
			case "D": // Digitos
				if ((tk < 48) || (tk > 57))
					pode = false;
			break;
			case "V": // Digitos separados por virgula
			case "M": // Digitos separados por virgula
				if (((tk < 48) || (tk > 57)) && (tk != 44) && (tk != 45)) //48 = 0, 57 = 9, 44 = , e 45 = -
					pode = false;
				if ((tk == 44)){
					while(substr_count(texto, ',') > 1){
						var pos = texto.search(',');
						texto = texto.substring(0, pos)+texto.substring(pos+1, texto.length);
						if(pode)
							i--;
						pode = false;
					}
				}
			break;
			case "P": // Digitos separados por ponto
//				alert("quantos: "+this.value.search(","));
				if (((tk < 48) || (tk > 57)) && (tk != 46) && (tk != 45))
					pode = false;
//		if ((tk == 46) && ((this.value.search(".") > 0) || (this.value.length==0))){ naum funfo
				if ((tk == 46)){
					while(substr_count(texto, '.') > 1){
						var pos = retorna_pos(texto, '.');
						texto = texto.substring(0, pos)+texto.substring(pos+1, texto.length);
						if(pode)
							i--;
						pode = false;
					}
				}
			break;
			case "C": // Letras
				if ((tk > 96) && (tk < 123)) //97 = a && 122 = z
					tk = tk - 32;
				if(!((tk > 64) && (tk < 91)) && (tk != 32))
					pode = false;
			break;
			case "Dt": // Datas
				if (((tk < 48) || (tk > 57)) && (tk != 47))
					pode = false;
				else{
					if(tk == 47) {
						switch(texto_limpo.length){
							case 1:
								texto_limpo = "0"+texto_limpo.slice(0,1);
							break;
							case 4:
								texto_limpo = texto_limpo.slice(0,3)+"0"+texto_limpo.slice(3,4);
							break;
							case 2:
							break;
							case 5:
							break;
							default :
								pode = false;
						}
					}
					else{
						if((texto_limpo.length == 2) || (texto_limpo.length == 5))
							texto_limpo += "/";
					}
				}
				if(texto_limpo.length > 9)
					pode = false;
			break;
			case "AN": // Alpha Numerico
				if ((tk > 96) && (tk < 123)) //97 = a && 122 = z
					tk = tk - 32;
				if(!(((tk > 47) && (tk < 58)) || ((tk > 64) && (tk < 91))) && (tk != 32)) // Valido p/ numeros e letras
					pode = false;
			break;
			case "AX": // Alpha Numerico completo acentos ï¿?e outros
				if((tk < 42) || ( (tk > 90) && (tk < 97) ) || (tk > 122) && ((tk > 146) && (tk < 152)) )
				{
					if( (tk!=231) && (tk!=94) && (tk!=95) && (tk!=227) && (tk!=245) && (tk!=32))
					{
						pode = false;
					}
				}
			break;
			case "CNPJ": // para arquivos c/ numeros, ".", "/", "-"
				if ((tk < 45) || (tk > 57))
					pode = false;
			break;
		}

		if(pode){
			texto_limpo += texto.charAt(i);
		}
	}
	if(tipo == "M")
		texto_limpo = format(unformat(texto_limpo));

	campo.value = texto_limpo;
}

function substr_count(str_original, find)
{
	num_ocorrencia = 0;
	str = str_original;
	while ( retorna_pos(str, find) != -1 )
	{
		num_ocorrencia++;
		str = str.substring(retorna_pos(str, find) + find.length, str.length);
	}
	return num_ocorrencia;
}

function retorna_pos(str_original, find)
{
	for (var i=0; i<str_original.length; i++)
	{
		if (str_original.substring(i, i+1) == find)
		{
			return i;
		}
	}
	return -1
}

function format(num) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";	

	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);

	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents < 10)
	cents = "0" + cents;

	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+'.'+num.substring(num.length-(4*i+3));
	return (((sign)?'':'-')+num + ',' + cents);
}

function unformat(num) 
{
	num = num.replace(/\$|\./g,'');
	num = num.replace(/\$|\,/g,'.');
	return num;
}

function format_inteiro(num) 
{
	num = num.toString();
	var tam = num.length;
	if(isNaN(num))
		num = "0";	

	sign = (num == (num = Math.abs(num)));

	num = Math.floor(num).toString();
	
	var falta = tam - num.length;
	if (!sign)
	{
		falta--;
	}
	while (falta > 0)
	{
		num = "0"+num;
		falta--;
	}
	
	return (((sign)?'':'-')+num);
}

function unformat_inteiro(num) 
{
	return format_inteiro(num);
}

// Fazer o salto de campo
function __ValidarTecla (evnt)
{
	var tk;
    var c;
	// Recebe a tela pressionada
	tk = ( (QualNavegador()=="IE") ? event.keyCode : evnt.which);
    c=String.fromCharCode(tk);
	c=c.toUpperCase();
	
	// -- Este trecho faz com que o <Enter> tenha a função de <Tab>, mas acho inviável, pois não é possível
	//       colocar o foco em campos do Tipo "image", e, neste caso, nunca seria possível fazer a submissão
	//       do formulário
	// if ( tk == 13 )
	// {
	//	this.Saltar=true;
	//	SaltarCampo(this);
	//	return false;
	// }
	
	// Só aceita teclas alfanuméricas. Não aceita teclas de controle
    if ( tk < 32 )
		return true;

	switch ( this.Tipo )
	{
	case "M":
		if ( c<"0" || c>"9" )
			return false;
		break;
	case "N":
		if ( (c<"0" || c>"9") && (c!=",") && (c!=".") )
			return false;
		if ( (c==",") && ((this.value.search(",")>-1) || (this.value.length==0)) )
			return false;
		break;
	case "P":
		if ( (c<"0" || c>"9") && (c!=".") )
			return false;
		if ( (c==",") && ((this.value.search(",")>-1) || (this.value.length==0)) )
			return false;
		break;
	case "C":
		if ( c<"A" || c>"Z" )
			return false;
		break;
	default:
		break;
	}

	this.Saltar=(this.value.length==this.Tam-1);
	if ( ((QualNavegador()=="IE") && QualVersao()<5) || (QualNavegador()!="IE") )
		SaltarCampo(this);

	return true;
}

/*
funcao para pegar a primeira palavra de uma string
*/
function pega_primeira_palavra(frase){
	var i =0;
	var aspas = 0;
	var primeira = 0;
	var ultima = 0;
	var menus = 0;
	var retorno = [];

	while(i < frase.length){
		if(frase.charAt(i) != " "){
 			if(frase.charAt(i) == "'"){
				aspas = 1;
				i++;
			}
//alert(frase+" = "+frase.charAt(i)+" = "+aspas);
			primeira = i;
			i++;
			while(i < frase.length && (frase.charAt(i) != " " || aspas)){
 				if(frase.charAt(i) == "'") {aspas = 0};
//alert(frase+" = "+frase.charAt(i)+" = "+aspas);
				i++;
			}
			ultima = i;
			i = frase.length;
		}

		i++;
	}

	if((ultima != frase.length || frase.charAt(ultima-1) == "'") && primeira != 0){
		menus = (frase.charAt(ultima-1) == "'")? 3 : 1;
	}

	retorno[1] = frase.substr(primeira, ultima-menus);
	retorno[0] = frase.substr(ultima, frase.length);

	return retorno;
}


//-->
//======================objetos para cadastro de chechbox=============
	function var_check ()
	{
		this.nome_var = "";
		this.tipo = "";  //"boolean"

		this.vet_id = [];
		this.cont_id = 0;
		
		this.atribui_nome = function (nome){
			this.nome_var = nome;
		}
		this.atribui_id = function (nom_id){
			this.vet_id[this.cont_id] = nom_id;
			this.cont_id ++;
		}
		
		this.atribui_tipo = function (tipo){
			this.tipo = tipo;
		}
	}
	
	function conjunto_check ()
	{
		this.cont_check = -1;
		this.vet_check = [];
		
		this.insere_check = function(nome)
		{
			this.cont_check++;
			this.vet_check[this.cont_check] = new var_check();
			this.vet_check[this.cont_check].atribui_nome(nome);
		}
		this.insere_id = function(nome)
		{
			this.vet_check[this.cont_check].atribui_id(nome);
		}
		
		this.insere_tipo = function (tipo)
		{
			this.vet_check[this.cont_check].atribui_tipo(tipo);
		}
		
		this.cria_id = function ()
		{
			return "check_temp_"+this.cont_check+"_"+this.vet_check[this.cont_check].cont_id;
		}
	}
	
		/*
		Atribui os valores dos checkbox multivalorados
		*/
		function atribui_check(conj_check)
		{
			for (var i  = 0; i <= conj_check.cont_check; i++)
			{
				if(conj_check.vet_check[i].tipo == "boolean")
				{
					document.getElementById(conj_check.vet_check[i].nome_var).value = 0;
					if (document.getElementById(conj_check.vet_check[i].vet_id[0]).checked)
					{
						document.getElementById(conj_check.vet_check[i].nome_var).value = 1;
					}
				}
				else
				{
					document.getElementById(conj_check.vet_check[i].nome_var).value = "";
					var str_check = "";
					for (j = 0; j < conj_check.vet_check[i].cont_id; j++)
					{
						if (document.getElementById(conj_check.vet_check[i].vet_id[j]).checked)
						{
							str_check += ","+document.getElementById(conj_check.vet_check[i].vet_id[j]).value
						}
					}
					if (str_check != "")
					{
						str_check += ",";
					}
					document.getElementById(conj_check.vet_check[i].nome_var).value = str_check;
				}
			}
		}


	/*************Deve ser substituido por isso***************/
	function check_campo_multi_valor(nome_base, num_elementos)
	{
		var str_check = "";

		for (var i = 0; i < num_elementos; i++)
		{
			var check = document.getElementById(nome_base+"_"+i);
			if (check)
			{
				if (check.checked)
				{
					str_check += ","+check.value;
				}
			}
		}
		if (str_check != "")
		{
			str_check += ",";
		}
		document.getElementById(nome_base).value = str_check;
	}
//======================Fim checkbox=======================================

//================Funcoes para o gera_select_data===========================
		/*
		Abre a janela com o calendário para seleção da data
		*/
		function selecionaDia(nome_base, nao_definida)
		{
			try
			{
				var dth_referencia = document.getElementById("ano_"+nome_base).value+"-";
				dth_referencia += document.getElementById("mes_"+nome_base).value+"-";
				dth_referencia += document.getElementById("dia_"+nome_base).value;
			}
			catch(e)
			{
				var dth_referencia = '<? echo date("Y-m-d"); ?>';
			}
			
			popupWin = window.open('', 'remote', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=300, height=200, left=200, top=100');
			var url_calendario = "index2.php?page=principal/monta_calend";
			url_calendario += "&mostra=1";
			url_calendario += "&nome_base="+nome_base;
			url_calendario += "&dth_referencia="+dth_referencia;
			url_calendario += "&nao_definida="+nao_definida;
			popupWin.location = url_calendario;
		}



		function muda_gera_select_data(item, nome_base, nao_definida)
		{
			var dia = document.getElementById("dia_"+nome_base).value;
			var mes = document.getElementById("mes_"+nome_base).value;
			var ano = document.getElementById("ano_"+nome_base).value;

			verif_data_gera_select(item, nome_base, nao_definida);
			testa_data_valida(nome_base, ano, mes, dia);

			try
			{
				trata_data_retorno(nome_base, ano, mes, dia);
			}
			catch( ex )	{}
		}

		function verif_data_gera_select(item, nome_base, nao_definida)
		{
			var valor_temp = "";
			var nom_obj = "";

			if (item == 0)
			{
				nom_obj = "dia_";
			}
			else if (item == 1)
			{
				nom_obj = "mes_";
			}
			else
			{
				nom_obj = "ano_";
			}

			var obj_data = document.getElementById(nom_obj+""+nome_base);
			valor_temp = obj_data.value;
			if (item == 0 || item == 1)
			{
				if (item == 0)
				{
					var valor_atual = "01";
				}
				else
				{
					var valor_atual = "01";
				}

				if (valor_temp.length == 0)
				{
					obj_data.value = valor_atual.substr(0, (4-valor_temp.length) )+obj_data.value;
				}
				else if (valor_temp.length == 1)
				{
					obj_data.value = "0"+obj_data.value;
				}
			} 
			if (item == 2)
			{
				var valor_atual = "2008";
				if (valor_temp.length != 2)
				{
					obj_data.value = valor_atual.substr(0, (4-valor_temp.length) )+obj_data.value;
				}
				else
				{
					if ((obj_data.value*1) > 60 )
					{
						valor_atual = "19";
					}
					else
					{
						valor_atual = "20";
					}
					obj_data.value = valor_atual+""+obj_data.value;
				}
			}
			if (nao_definida == 0 && (obj_data.value*1) == 0)
			{
				obj_data.value = valor_atual;
			}
		}
		
		function testa_data_valida(nome_base, ano, mes, dia)
		{
			if(!document.getElementById("dia_"+nome_base)) 
			{
				return '0';
			}

			var fim_mes = [];
			fim_mes[1] = 31;
			fim_mes[2] = 28;
			fim_mes[3] = 31;
			fim_mes[4] = 30;
			fim_mes[5] = 31;
			fim_mes[6] = 30;
			fim_mes[7] = 31;
			fim_mes[8] = 31;
			fim_mes[9] = 30;
			fim_mes[10] = 31;
			fim_mes[11] = 30;
			fim_mes[12] = 31;
			
			if(ano%4 == 0)
				fim_mes[2] = 29;

			if ( (mes*1) > 12)
			{
				document.getElementById("mes_"+nome_base).value = 12;
			}
			if ( dia > fim_mes[(mes*1)] )
			{
				document.getElementById("dia_"+nome_base).value = fim_mes[(mes*1)];
			}
		} 		
//======================================================

function muda_tipo_cad_interface(tipo)
{
	var lb_Size = "Size";
	var lb_Maxlength = "Maxlength";

	if (tipo == 3 || tipo == 6 || tipo == 9 || tipo == 10 || tipo == 11 || tipo == 14)
	{
		lb_Size = " - ";
		lb_Maxlength = " - ";

		document.getElementById("size_campo___n").disabled = true;
		document.getElementById("maxlength_campo___n").disabled = true;
	}
	else
	{
		document.getElementById("size_campo___n").disabled = false;
		if (tipo != 16 && tipo != 17)
		{
			document.getElementById("maxlength_campo___n").disabled = false;
		}
		else
		{
			lb_Maxlength = " - ";
			document.getElementById("maxlength_campo___n").disabled = true;
		}
	}

	if (tipo == 5)
	{
		lb_Size = "Width";
		lb_Maxlength = "Heigth";
	}
	else if (tipo == 4)
	{
		lb_Size = "Colunas";
		lb_Maxlength = "Linhas";
	}
	else if (tipo == 16 || tipo == 17)
	{
		lb_Size = "No. Colunas";
	}

	document.getElementById("lb_Size").innerHTML = lb_Size+":";
	document.getElementById("lb_Maxlength").innerHTML = lb_Maxlength;

}

// Daqui pra frente, falta ver com a Diretoria.. Implementado por Lucas

//Funcao Semelhante ao document.getElementById();
function $()
{
	var elements = new Array();
	var x = arguments.length;
	for (var i=0; i < x; i++)
	{
		var element = arguments[i]; 
		if (typeof element.toLowerCase() == 'string')
		{
			element = document.getElementById(element);
		}
		if (arguments.length == 1)
		{
			return element;
		}
		elements.push(element);
	}
}