	var menu_form, menu_lista, teclas_atalho;

	function carregando3(){
		document.getElementById('l2_cl2_corpo').innerHTML = "<div class='Carregando'>carregando...</div>";		
	}

	function mostra_corpo(file){
		if(file == ''){	return false;	}
	
		var xmlhttp = false;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
	    	xmlhttp = new XMLHttpRequest();
	        if(xmlhttp.overrideMimeType){
	        	xmlhttp.overrideMimeType('text/xml');
			}
		}else if (window.ActiveXObject){ // IE
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	            } catch (e) {}
	        }
	    }
		carregando3();
		var url = "files/xml.php?file="+file;
		xmlhttp.open("GET", url ,true);
	
	    //Executada quando o navegador obtiver o código
	    xmlhttp.onreadystatechange=function() {
	
	        if(xmlhttp.readyState==4){
	            if(xmlhttp.status == 200){
					var nameNode, nameTextNode, i, Linha='';
		        	
					for(i=0; i<xmlhttp.responseXML.getElementsByTagName("Linha").length; i++){
						nameNode = xmlhttp.responseXML.getElementsByTagName("Linha")[i]; 
						nameTextNode = nameNode.childNodes[0];
						Linha += nameTextNode.nodeValue;					
					}
					document.getElementById('l2_cl2_corpo').innerHTML = Linha;
				}
			}
	    }
	    xmlhttp.send(null);
/*	    var xmlhttp = false;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
	    	xmlhttp = new XMLHttpRequest();
	        if(xmlhttp.overrideMimeType){
	//        	xmlhttp.overrideMimeType('text/xml');
			}
		}else if (window.ActiveXObject){ // IE
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	            } catch (e) {}
	        }
	    }
		
		carregando();
	    xmlhttp.open("GET", "ctt/"+file,true);
	
	    //Executada quando o navegador obtiver o código
	    xmlhttp.onreadystatechange=function() {
	
	        if(xmlhttp.readyState==4){
	            if(xmlhttp.status == 200){
					var conteudo = xmlhttp.responseText;
					document.getElementById('l2_cl2_corpo').innerHTML = conteudo;
					
				}
			}
	    }
	    xmlhttp.send(null);
*/
	}

	function xmlhttp(){
		var xmlhttp = false;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
	    	xmlhttp = new XMLHttpRequest();
	        if(xmlhttp.overrideMimeType){
	        	xmlhttp.overrideMimeType('text/xml');
			}
		}else if (window.ActiveXObject){ // IE
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	            } catch (e) {}
	        }
	    }
		return xmlhttp;
	}
	function Arredonda( valor , casas ){
 		  var novo = Math.round( valor * Math.pow( 10 , casas ) ) / Math.pow( 10 , casas );
 		  return novo;
	}
	function dateFormat(date){
		if(date == ''){	return '';	}
		var year 	= date.substring(0,4);
		var month 	= date.substring(5,7);
		var day 	= date.substring(8,10);
		var end 	= date.substring(11,date.length);
		
		var date = day + "/" + month + "/" + year;
		
		if(end != ''){
			date = date + " " + end;
		}
		
		return date;
	}
	function formatDate(date){
		if(date == ''){	return '';	}
		var year 	= date.substring(6,10);
		var month 	= date.substring(3,5);
		var day 	= date.substring(0,2);
		var end 	= date.substring(11,date.length);
		
		var date = year + "-" + month + "-" + day;
		
		if(end != ''){
			date = date + " " + end;
		}
		
		return date;
	}
	function keyDown(){
//		if(event.keyCode == 13 && menu_form == true){
//			alert('Enter');
//		}
		if(teclas_atalho == false){
			return true;
		}
//		alert(event.keyCode);
		if(event.ctrlKey == true){
			switch (event.keyCode){		
				case 13: // Ctrl + Entrer -> Salvar
					if(menu_form == true){
						incluir();
						event.keyCode=0;
						event.returnValue=false;
					}
					break;
				case 72: // Ctrl + H -> Home
					url = 'index.php';
					window.location.replace(url);
					event.keyCode=0;
					event.returnValue=false;
					break;
				case 78: // Ctrl + N -> Novo
					if(menu_form == true){
						cancelar();					
						event.keyCode=0;
						event.returnValue=false;
					}
					break;
				case 80: // Ctrl + P -> Imprimir
					imprimir();
					event.keyCode=0;
					event.returnValue=false;
					break;
				case 113: // Ctrl + F2 -> Cadastro de Produtos
					url = 'cadastro_produto.php';
					window.location.replace(url);
					event.keyCode=0;
					event.returnValue=false;
					break;
			}
		}else{
			switch (event.keyCode){			
				case 13: // Entrer -> Próximo campo
					if(event.srcElement.type != 'textarea' && event.srcElement.type != 'submit' && event.srcElement.type != 'button'){
						event.keyCode = 9;
					}
					break;
//				case 113: // F2 -> Novo
//					if(menu_form == true){
//						cancelar();					
//						event.keyCode=0;
//						event.returnValue=false;
//					}
//					break;
//				case 114: // F3 -> Inserir
//					if(menu_form == true){
//						incluir();
//						event.keyCode=0;
//						event.returnValue=false;
//					}
//					break;
//				case 115: // F4 -> Alterar
//					if(menu_form == true){
//						incluir();
//						event.keyCode=0;
//						event.returnValue=false;
//					}
//					break;
//				case 116: // F5 -> Excluir
//					if(menu_form == true){
//						excluir();
//						event.keyCode=0;
//						event.returnValue=false;
//					}
//					break;
				case 118: // F7 -> Cancelar
					if(menu_form == true){
						cancelar();
						event.keyCode=0;
						event.returnValue=false;
					}
					break;
				case 119: // F8 -> Listar Todos
					if(menu_form == true){
						listar_todos();
						event.keyCode=0;
						event.returnValue=false;
					}
					break;
			}		
		}
	}
	
	document.onkeydown = keyDown

	function Foco(campo,acao,obrigatorio){
		if(acao == 'in'){
			switch (obrigatorio){
				case true:
					campo.style.backgroundColor = "#FFE784";
					break;
				case 'auto':
					campo.style.backgroundColor = "#E6EDFF";
					break;
				default:
					campo.style.backgroundColor = "#FFEAEA";
					break;
			}			
		}else{
			campo.style.backgroundColor = "#FFFFFF";
		}	
	}
	function mostra_seleciona_loja(){
		return false;
		if(document.formulario_seleciona_loja.IdLoja.length > 1){
			document.getElementById('loja_selecionada').style.display = 'none';
			document.formulario_seleciona_loja.style.display = 'block';
		}
		return false;
	}
	function seleciona_loja(campo){
		if(confirm('ATENÇÃO\n\nVocê está prestes a selecionar outra Loja.\nDeseja continuar?') == true){
			janelas('busca_loja_envia3',1,1,10000,10000,'?IdLoja='+campo);
		}else{
			document.getElementById('loja_selecionada').style.display = 'block';
			document.formulario_seleciona_loja.style.display = 'none';
		}
	}
	function mascara(campo,event,tipo,acesso){
		var nTecla;		
		if(document.all) { // Internet Explorer
		    nTecla = event.keyCode;
		} else if(document.layers) { // Nestcape
		    nTecla = event.which;
		} else {
		    nTecla = event.which;
		}
		 nTecla= parseInt(nTecla);
		if(tipo	==	"PlanoConta"){
			if(acesso == "N" || acesso == undefined){
				numMenor=0;
				numMaior=9;
				var string = campo.value;
				if(string.length-1 >= 0){
					if(string.substr(string.length-1,1) == "." && nTecla == 46){
						event.returnValue = false;				
					}
				}
				if ((nTecla < (48 + numMenor) && nTecla != 46) || nTecla > (48 + numMaior)){
					event.returnValue = false;
				}
				//48 a 57 ascII - 0 a 9
			}else{
				return false;
			}
		}
		if(tipo	==	"int"){
			numMenor=0;
			numMaior=9;
			if (nTecla < (48 + numMenor) || nTecla > (48 + numMaior)){
				event.returnValue = false;
			}
			//48 a 57 ascII - 0 a 9
		}
		if(tipo	==	"double"){
			var tamMax = campo.maxLength;
			var tam = campo.value.length;
			numMenor=0;
			numMaior=9;
			
			if(nTecla < (48 + numMenor) || nTecla > (48 + numMaior) || tamMax == tam){
				event.returnValue = false;
			}else{				
				 mascara_float(campo,'onkeypress');
			}
			//48 a 57 ascII - 0 a 9
			//44 ,
			//46 .
		}
		if(tipo	==	"float"){
			var tamMax = campo.maxLength;
			var tam = campo.value.length;
			numMenor=0;
			numMaior=9;
			
			if(nTecla < (48 + numMenor) || nTecla > (48 + numMaior) || tamMax == tam){
				event.returnValue = false;
			}else{				
				 mascara_double(campo,'onkeypress');
			}
			//48 a 57 ascII - 0 a 9
			//44 ,
			//46 .
		}
		if(tipo	==	"date"){			
			if(campo.value.length>=10){
				return false;
			}
			numMenor=0;
			numMaior=9;
			if (nTecla < (48 + numMenor) || nTecla > (48 + numMaior)){
				event.returnValue = false;
				//48 a 57 ascII - 0 a 9	
			}
			if(campo.value.length==2 || campo.value.length==5){
				campo.value = campo.value + "/";
			}
		}
		if(tipo	==	"cnpj"){
			if(campo.value.length>=18){
				return false;
			}
			numMenor=0;
			numMaior=9;
			if (nTecla < (48 + numMenor) || nTecla > (48 + numMaior)){
				event.returnValue = false;
				//48 a 57 ascII - 0 a 9
			}
			if(campo.value.length==2 || campo.value.length==6){
				campo.value = campo.value + ".";
			}
			if(campo.value.length==10){
				campo.value = campo.value + "/";
			}
			if(campo.value.length==15){
				campo.value = campo.value + "-";
			}
		}
		if(tipo== "cpf"){
			if(campo.value.length>=14){
				return false;
			}
			numMenor=0;
			numMaior=9;
			if (nTecla < (48 + numMenor) || nTecla > (48 + numMaior)){
				event.returnValue = false;
				//48 a 57 ascII - 0 a 9
			}
			var mycpf = '';
	        mycpf = mycpf + campo;
	        if (campo.value.length == 3){
	              campo.value = campo.value + '.';
	        }
	        if (campo.value.length == 7){
	              campo.value = campo.value + '.';
	        }
	        if (campo.value.length == 11){
	              campo.value = campo.value + '-';
	        }
		}
		if(tipo	==	"fone"){
			if(campo.value.length>=13){
				return false;
			}
			numMenor=0;
			numMaior=9;
			if (nTecla < (48 + numMenor) || nTecla > (48 + numMaior)){
				event.returnValue = false;
				//48 a 57 ascII - 0 a 9
			}
			if(campo.value.length==0){
				campo.value = campo.value + "(";
			}
			if(campo.value.length==3){
				campo.value = campo.value + ")";
			}
			if(campo.value.length==8){
				campo.value = campo.value + "-";
			}
		}
		if(tipo	==	"cep"){
			if(campo.value.length>=9){
				return false;
			}
			numMenor=0;
			numMaior=9;
			if (nTecla < (48 + numMenor) || nTecla > (48 + numMaior)){
				event.returnValue = false;
				//48 a 57 ascII - 0 a 9
			}
			if(campo.value.length==5){
				campo.value = campo.value + "-";
			}
		}
		if(tipo	==	"mes"){
			if(campo.value.length>7){
				return false;
			}
			numMenor=0;
			numMaior=9;
			if (nTecla < (48 + numMenor) || nTecla > (48 + numMaior)){
				event.returnValue = false;
				//48 a 57 ascII - 0 a 9
			}
			if(campo.value.length==2){
				campo.value = campo.value + "/";
			}
		}
	}

	function mascara_double(campo,evento){
		var tamMax = campo.maxLength;
		var tam = campo.value.length;
		var str = campo.value;
		var pos = 0;
		
		if(evento == ''){
			pos = 1;
		}	

		if(str!=''){
			str = str.replace(",","");	// Tira as vírgulas
			str = str.replace(".","");	// Tira os pontos
			str = str*1;	// Converte para inteiro
			campo.value = str;
		}
		
		tam = campo.value.length;
		
		switch (tam){
			case 0:
				campo.value = "0,0" + campo.value;
				break;	
			case 1:
				campo.value = "0," + campo.value;
				break;
			default:
				var decimal 	= campo.value.substr(tam-1-pos,1+pos);
				var inteiro 	= campo.value.substr(0,tam-1-pos);
				var inteiroTam 	= inteiro.length;
				var inteiros 	= new Array();
				var i=0;
				var ii;
					
				while((inteiroTam%3) != 0){
					inteiro		= "0"+inteiro;
					inteiroTam 	= inteiro.length;					
				}
				
				while(inteiro != ''){
					inteiros[i] = inteiro.substr(0,3);
					inteiro 	= inteiro.substr(3,inteiroTam);
					
					inteiroTam 	= inteiro.length;
					i++;
				}
				
				if(inteiros[0] != ''){
					inteiros[0]	= Number(inteiros[0]);
				}

				for(ii=0;ii<i;ii++){
					if(inteiros[ii]!='' && inteiros[ii]!=undefined){
						if(inteiro!=''){
							inteiro = inteiro + '.';
						}
						inteiro = inteiro + inteiros[ii];
					}
				}
				
				str = inteiro + ',' + decimal;
				campo.value = str;
				break;					
		}
	}
	function mascara_float(campo,evento){
		var tamMax = campo.maxLength;
		var tam = campo.value.length;
		var str = campo.value;
		var pos = 0;
		
		if(evento == ''){
			pos = 1;
		}	

		if(str!=''){
			str = str.replace(",","");	// Tira as vírgulas
			str = str.replace(".","");	// Tira os pontos
			str = str*1;	// Converte para inteiro
			campo.value = str;
		}
		
		tam = campo.value.length;
		
		switch (tam){
			case 0:
				campo.value = "0,00" + campo.value;
				break;	
			case 1:
				campo.value = "0,0" + campo.value;
				break;
			case 2:
				campo.value = "0," + campo.value;
				break;
			default:
				var decimal 	= campo.value.substr(tam-2-pos,2+pos);
				var inteiro 	= campo.value.substr(0,tam-2-pos);
				var inteiroTam 	= inteiro.length;
				var inteiros 	= new Array();
				var i=0;
				var ii;
					
				while((inteiroTam%3) != 0){
					inteiro		= "0"+inteiro;
					inteiroTam 	= inteiro.length;					
				}
				
				while(inteiro != ''){
					inteiros[i] = inteiro.substr(0,3);
					inteiro 	= inteiro.substr(3,inteiroTam);
					
					inteiroTam 	= inteiro.length;
					i++;
				}
				
				if(inteiros[0] != ''){
					inteiros[0]	= Number(inteiros[0]);
				}

				for(ii=0;ii<i;ii++){
					if(inteiros[ii]!='' && inteiros[ii]!=undefined){
						if(inteiro!=''){
							inteiro = inteiro + '.';
						}
						inteiro = inteiro + inteiros[ii];
					}
				}
				
				str = inteiro + ',' + decimal;
				campo.value = str;
				break;					
		}
	}
	function janela_busca_estado(IdPais,campo){
		if(IdPais.value!=''){
			if(campo != undefined){
				janelas('busca_estado_envia',1,1,10000,10000,'?IdPais='+IdPais.value+'&IdEstado='+campo.value);			
			}else{
				janelas('busca_estado',310,320,200,100,'');
			}			
		}
	}
	function janelas(nomeJanela,largura,altura,vtop,vleft,parametro,scro){
		if(scro == ''){
			scro = 'no';
		}
		dados 	=	"top="+vtop+",left="+vleft+",scrollbars="+scro+",status=no,toolbar=no,location=no,menu=no,width="+largura+",height="+altura;
		janela	=	window.open(nomeJanela+parametro,"_blank",dados);
		
		if(janela == null){
			alert('ERRO\nVerifique seu bloqueador de pop-up!');
		}
	}
	function pisca(){
//		window.focus();
//		setTimeout("pisca()",2000);
	}
	function addOption(objSelect,newName,newValor){
		var newOption; 
		var newDialog;
		newOption = new Option (newName,newValor,true,true);
		objSelect.options[objSelect.options.length] = newOption;
	}
	function checaEmail(nform){
		if (nform == "") {
			return false;
		}else {
			prim = nform.indexOf("@")
			if(prim < 2) {
				return false;
			}
			if(nform.indexOf("@",prim + 1) != -1) {
				return false;
			}
			if(nform.indexOf(".") < 1) {
				return false;
			}
			if(nform.indexOf(" ") != -1) {
				return false;
			}
			if(nform.indexOf(".@") > 0) {
				return false;
			}
			if(nform.indexOf("@.") > 0) {
				return false;
			}
			if(nform.indexOf("/") > 0) {
				return false;
			}
			if(nform.indexOf("[") > 0) {
				return false;
			}
			if(nform.indexOf("]") > 0) {
				return false;
			}
			if(nform.indexOf("(") > 0) {
				return false;
			}
			if(nform.indexOf(")") > 0) {
				return false;
			}
			if(nform.indexOf("..") > 0) {
				return false;
			}
			if(nform.indexOf("*") > 0) {
				return false;
			}
			if(nform.indexOf("+") > 0) {
				return false;
			}
		}
		return true;
	}
	function desabilidarMenu(bt,btn,acao){
		bt_tam	=	bt.length;
		if(bt.substr(bt_tam-6,2)=='_c'){
			if(acao == false){
				return bt.substr(0,bt_tam-6)+bt.substr(bt_tam-4,4);
			}else{
				return bt;
			}
		}else{
			if(acao == true){
				return bt.substr(0,bt_tam-4)+"_c"+bt.substr(bt_tam-4,bt_tam-1);
			}else{
				return bt.substr(0,bt_tam-4)+bt.substr(bt_tam-4,bt_tam-1);
			}		
		}
		return bt;
	}
	function imprimir(){
		window.print();
	}	
	function incluir(){
		if(validar()==true){
			document.formulario.submit();
		}
	}
	function processar(){
		if(document.bt_processar.disabled == false){
			document.formulario.Acao.value = 'processar';
			if(validar() == true){
				document.formulario.submit();
			}
		}
	}
	function JsMail(email){
		if(email!=''){
			parent.location.href='mailto:'+email;
		}else{
			return false;
		}
	}
	function JsHttp(urladress){
		var is_protocol_ok=urladress.indexOf('http://');
		var is_dot_ok=urladress.indexOf('.');
		if(urladress==''){
			return false;
		}
		if(is_protocol_ok == -1){
			is_protocol_ok=urladress.indexOf('HTTP://');
		}
		if ((is_protocol_ok==-1) || (is_dot_ok==-1)) { 
		  alert('A URL deve começar com o http://');
		}else
			window.open(''+urladress+'','New','type=fullWindow,toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=800,height=600,top=0,left=0,screenY=0,screenX=0');
	}
	function destacaRegistro(campo,acao){
		if(acao == true){
			campo.style.backgroundColor='#e2e7f3';
		}else{
			campo.style.backgroundColor='';
		}		
	}
	function excluir_registro(){
		return confirm("ATENCAO!\n\nVoce esta prestes a excluir um registro.\nDeseja continuar?","SIM","NAO");
	}
	function carregando(acao){
		if(acao == true){
			document.getElementById("carregando").style.display = 'block';
		}else{
			document.getElementById("carregando").style.display = 'none';
		}
		return true;
	}
	
	function tableMultColor(Id,Cor){
		for(var i=1; i < document.getElementById(Id).rows.length; i++){
			if(i%2 == 0){
				document.getElementById(Id).rows[i].bgColor = Cor;
			}else{
				document.getElementById(Id).rows[i].bgColor = '#FFFFFF';			
			}		
		}		
	}

	function busca_status(IdStatus){
		var nameNode, nameTextNode, url;
		
		var xmlhttp   = false;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
	    	xmlhttp = new XMLHttpRequest();
	        if(xmlhttp.overrideMimeType){
	        	xmlhttp.overrideMimeType('text/xml');
			}
		}else if (window.ActiveXObject){ // IE
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	            } catch (e) {}
	        }
	    }
	    
	   	url = "../default/xml/status.php?IdStatus="+IdStatus;
		xmlhttp.open("GET", url,true);
		
		xmlhttp.onreadystatechange = function(){ 

			// Carregando...
			carregando(true);

			if(xmlhttp.readyState == 4){ 
				if(xmlhttp.status == 200){
										
					if(xmlhttp.responseText == 'false'){	
						
						document.formulario.IdStatus.value			= '';
						document.formulario.Status.value			= '';
					
						// Fim de Carregando
						carregando(false);
					}else{
						nameNode = xmlhttp.responseXML.getElementsByTagName("IdStatus")[0]; 
						nameTextNode = nameNode.childNodes[0];
						var IdStatus = nameTextNode.nodeValue;
												
						nameNode = xmlhttp.responseXML.getElementsByTagName("Status")[0]; 
						nameTextNode = nameNode.childNodes[0];
						var Status = nameTextNode.nodeValue;
					
						document.formulario.IdStatus.value			= IdStatus;
						document.formulario.Status.value			= Status;
					}
					if(window.janela != undefined){
						window.janela.close();
					}
				}
			} 
			// Fim de Carregando
			carregando(false);
			return true;
		}
		xmlhttp.send(null);
	}
	
	function verifica_dado(dado){
		if(dado == ''){
			dado = "&nbsp;";
		}
		return dado;
	}
	
	function limpa_status(){
		document.formulario.IdStatus.value=''; 
		document.formulario.Status.focus();
	}
	
	/*function verifica_status(){
		for( var i in opc_status ){
			if(document.formulario.Status.value == opc_status[i]){
				document.formulario.IdStatus.value = val_status[i];
				return true;
			}
		}
		return false;
	}*/
	function janela_busca_pais(campo){
		janelas('../default/busca_pais.php?Cobranca='+campo,360,283,250,100,'');
	}
	function busca_pais(IdPais,Erro,Local){
		if(IdPais == ''){
			IdPais = 0;
		}
		if(Local == '' || Local == undefined){
			Local	=	document.formulario.Local.value;
		}
		var nameNode, nameTextNode, url;
		
		var xmlhttp   = false;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
	    	xmlhttp = new XMLHttpRequest();
	        if(xmlhttp.overrideMimeType){
	        	xmlhttp.overrideMimeType('text/xml');
			}
		}else if (window.ActiveXObject){ // IE
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	            } catch (e) {}
	        }
	    }
	    
	   	url = "../default/xml/pais.php?IdPais="+IdPais;
		xmlhttp.open("GET", url,true);
		
		xmlhttp.onreadystatechange = function(){ 

			// Carregando...
			carregando(true);

			if(xmlhttp.readyState == 4){ 
				if(xmlhttp.status == 200){

					if(Erro != false){
//						document.formulario.Erro.value = 0;
//						verificaErro();
					}
					if(xmlhttp.responseText == 'false'){
						
						document.formulario.IdPais.value 	= '';
						document.formulario.Pais.value		= '';
						
						if(Local == 'Pais'){
							document.formulario.Acao.value	= "inserir";
							document.formulario.Pais.focus();
							verificaAcao();							
						}else{
							document.formulario.IdPais.focus();
						}
											
						// Fim de Carregando
						carregando(false);
					}else{
						nameNode = xmlhttp.responseXML.getElementsByTagName("IdPais")[0]; 
						nameTextNode = nameNode.childNodes[0];
						IdPais = nameTextNode.nodeValue;
						
						nameNode = xmlhttp.responseXML.getElementsByTagName("NomePais")[0]; 
						nameTextNode = nameNode.childNodes[0];
						var NomePais = nameTextNode.nodeValue;					
						
						document.formulario.IdPais.value	= IdPais;
						document.formulario.Pais.value 		= NomePais;
						
						if(Local == 'Pais'){
							document.formulario.Acao.value	= "alterar";
							verificaAcao();				
						}
					}
					if(document.formulario.IdEstado != undefined){
						document.formulario.IdEstado.value 	= '';
						document.formulario.Estado.value	= '';
						
						if(Local == 'PessoaBusca'){
							document.formulario.IdEstado.focus();							
						}
					}
					if(document.formulario.IdCidade != undefined){
						document.formulario.IdCidade.value 	= '';
						document.formulario.Cidade.value	= '';							
					}
					
					if(window.janela != undefined){
						window.janela.close();
					}
				}
			} 
			// Fim de Carregando
			carregando(false);
			return true;
		}
		xmlhttp.send(null);
		return true;
	}
	
	function janela_busca_estado(campo){
		if(document.formulario.IdPais.value == ''){
			return false;
		}
		janelas('../default/busca_estado.php?Cobranca='+campo,360,283,250,100,'');
	}
	function busca_estado(IdPais,IdEstado,Erro,Local){
		
		if(IdEstado == ''){
			IdEstado = 0;
		}
		var nameNode, nameTextNode, url;
		
		var xmlhttp   = false;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
	    	xmlhttp = new XMLHttpRequest();
	        if(xmlhttp.overrideMimeType){
	        	xmlhttp.overrideMimeType('text/xml');
			}
		}else if (window.ActiveXObject){ // IE
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	            } catch (e) {}
	        }
	    }
	    
	   	url = "../default/xml/estado.php?IdPais="+IdPais+"&IdEstado="+IdEstado;
		xmlhttp.open("GET", url,true);
		
		xmlhttp.onreadystatechange = function(){ 

			// Carregando...
			carregando(true);

			if(xmlhttp.readyState == 4){ 
				if(xmlhttp.status == 200){

					if(Erro != false){
//						document.formulario.Erro.value = 0;
//						verificaErro();
					}
					if(xmlhttp.responseText == 'false'){
						document.formulario.IdEstado.value	 	= '';
						document.formulario.Estado.value		= '';
						
						if(IdPais == ''){
							document.formulario.IdPais.focus();
						}else{
							document.formulario.IdEstado.focus();
						}
						switch(Local){
							case 'Estado':
								document.formulario.SiglaEstado.value	=	'';
								document.formulario.Acao.value			=   'inserir';
								document.formulario.Estado.focus();
								verificaAcao();
								break;
						}
						// Fim de Carregando
						carregando(false);
					}else{
						nameNode = xmlhttp.responseXML.getElementsByTagName("IdEstado")[0]; 
						nameTextNode = nameNode.childNodes[0];
						IdEstado = nameTextNode.nodeValue;
						
						nameNode = xmlhttp.responseXML.getElementsByTagName("NomeEstado")[0]; 
						nameTextNode = nameNode.childNodes[0];
						var NomeEstado = nameTextNode.nodeValue;
											
						document.formulario.IdEstado.value	= IdEstado;
						document.formulario.Estado.value 	= NomeEstado;
						
						if(document.formulario.IdCidade != undefined){
							if(Local == 'PessoaBusca'){
								document.formulario.IdCidade.focus();
							}
						}
						
						switch(Local){
							case 'Estado':
								nameNode = xmlhttp.responseXML.getElementsByTagName("IdPais")[0]; 
								nameTextNode = nameNode.childNodes[0];
								IdPais = nameTextNode.nodeValue;
						
								nameNode = xmlhttp.responseXML.getElementsByTagName("NomePais")[0]; 
								nameTextNode = nameNode.childNodes[0];
								var NomePais = nameTextNode.nodeValue;
								
								nameNode = xmlhttp.responseXML.getElementsByTagName("SiglaEstado")[0]; 
								nameTextNode = nameNode.childNodes[0];
								var SiglaEstado = nameTextNode.nodeValue;					

								document.formulario.IdPais.value		= IdPais;
								document.formulario.Pais.value 			= NomePais;
								document.formulario.SiglaEstado.value	= SiglaEstado;	
								document.formulario.Acao.value			= 'alterar';												
								verificaAcao();
								break;
						}
					}
					if(document.formulario.IdCidade != undefined){
						document.formulario.IdCidade.value 	= '';
						document.formulario.Cidade.value	= '';							
					}
					if(window.janela != undefined){
						window.janela.close();
					}
				}
			} 
			// Fim de Carregando
			carregando(false);
			return true;
		}
		xmlhttp.send(null);
		return true;
	}
	
	function janela_busca_cidade(campo){
		if(document.formulario.IdPais.value == ''){
			return false;
		}
		if(document.formulario.IdEstado.value == ''){
			return false;
		}
		janelas('../default/busca_cidade.php?Cobranca='+campo,360,283,250,100,'');
	}	
	function busca_cidade(IdPais,IdEstado,IdCidade,Erro,Local){
		if(IdCidade == ''){
			IdCidade = 0;
		}
		var nameNode, nameTextNode, url;
		
		var xmlhttp   = false;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
	    	xmlhttp = new XMLHttpRequest();
	        if(xmlhttp.overrideMimeType){
	        	xmlhttp.overrideMimeType('text/xml');
			}
		}else if (window.ActiveXObject){ // IE
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	            } catch (e) {}
	        }
	    }
	    
	   	url = "../default/xml/cidade.php?IdPais="+IdPais+"&IdEstado="+IdEstado+"&IdCidade="+IdCidade;
		xmlhttp.open("GET", url,true);
		
		xmlhttp.onreadystatechange = function(){ 

			// Carregando...
			carregando(true);

			if(xmlhttp.readyState == 4){ 
				if(xmlhttp.status == 200){

					if(Erro != false){
//						document.formulario.Erro.value = 0;
//						verificaErro();
					}
					if(xmlhttp.responseText == 'false'){
						
						document.formulario.IdCidade.value 	= '';
						document.formulario.Cidade.value	= '';

						if(document.formulario.IdPais.value == ''){
							document.formulario.IdPais.focus();
						}else if(document.formulario.IdEstado.value == ''){
							document.formulario.IdEstado.focus();
						}else{
							if(Local != 'Cidade'){
								document.formulario.IdCidade.focus();
							}
						}
						if(Local == 'Cidade'){
							document.formulario.Acao.value 		= 'inserir';
						}					
						// Fim de Carregando
						carregando(false);
					}else{
						switch(Local){
							case 'Cidade':
								nameNode = xmlhttp.responseXML.getElementsByTagName("IdPais")[0]; 
								nameTextNode = nameNode.childNodes[0];
								var IdPais = nameTextNode.nodeValue;
								
								nameNode = xmlhttp.responseXML.getElementsByTagName("NomePais")[0]; 
								nameTextNode = nameNode.childNodes[0];
								var NomePais = nameTextNode.nodeValue;
								
								nameNode = xmlhttp.responseXML.getElementsByTagName("IdEstado")[0]; 
								nameTextNode = nameNode.childNodes[0];
								var IdEstado = nameTextNode.nodeValue;
								
								nameNode = xmlhttp.responseXML.getElementsByTagName("NomeEstado")[0]; 
								nameTextNode = nameNode.childNodes[0];
								var NomeEstado = nameTextNode.nodeValue;				
								
								document.formulario.IdPais.value 	= IdPais;
								document.formulario.Pais.value 		= NomePais;
								document.formulario.IdEstado.value 	= IdEstado;
								document.formulario.Estado.value 	= NomeEstado;
								
								document.formulario.Acao.value 		= 'alterar';
								break;
							case 'PessoaBusca':
								document.formulario.CEP.focus();
								break;
						}
					
						nameNode = xmlhttp.responseXML.getElementsByTagName("IdCidade")[0]; 
						nameTextNode = nameNode.childNodes[0];
						IdCidade = nameTextNode.nodeValue;
						
						nameNode = xmlhttp.responseXML.getElementsByTagName("NomeCidade")[0]; 
						nameTextNode = nameNode.childNodes[0];
						var NomeCidade = nameTextNode.nodeValue;					
						
						document.formulario.IdCidade.value	= IdCidade;
						document.formulario.Cidade.value 	= NomeCidade;
					}
					if(window.janela != undefined){
						window.janela.close();
					}
					verificaAcao();
				}
			} 
			// Fim de Carregando
			carregando(false);
			return true;
		}
		xmlhttp.send(null);
		return true;
	}
	
	
	function ativa_quadro(quadro,qtdQuadros,Local){
		for(var i=1; i<=qtdQuadros; i++){
			document.getElementById('quadro'+i).style.display 			= 'none';
			document.getElementById('abaMenuQuadros'+i).style.color 	= '#ACB6D7';
		}
		document.getElementById('quadro'+quadro).style.display 			= 'block';
		document.getElementById('abaMenuQuadros'+quadro).style.color 	= '#FFFFFF';
		if(Local == "Servico"){
			if(quadro == 3 || quadro == 4){
				document.getElementById('bt_inserir2').style.display = 'none';
				document.getElementById('bt_alterar2').style.display = 'none';
				
				document.bt_inserir.src 		= desabilidarMenu(document.bt_inserir.src,4,true);			// Bt Inserir 1
				document.bt_inserir.disabled 	= true;
				
				document.bt_alterar.src 		= desabilidarMenu(document.bt_alterar.src,4,true);			// Bt Alterar 1
				document.bt_alterar.disabled 	= true;
				
				document.bt_excluir.src 		= desabilidarMenu(document.bt_excluir.src,4,true);			// Bt Excluir
				document.bt_excluir.disabled 	= true;
			}else{
				document.getElementById('bt_inserir2').style.display = 'block';
				document.getElementById('bt_alterar2').style.display = 'block';
				
				verificaAcao();
			}
		}
		else if(Local == "Contrato"){
			if(quadro == 3){
				document.getElementById('bt_inserir2').style.display = 'none';
				document.getElementById('bt_alterar2').style.display = 'none';
				
				document.bt_inserir.src 		= desabilidarMenu(document.bt_inserir.src,4,true);			// Bt Inserir 1
				document.bt_inserir.disabled 	= true;
				
				document.bt_alterar.src 		= desabilidarMenu(document.bt_alterar.src,4,true);			// Bt Alterar 1
				document.bt_alterar.disabled 	= true;
				
				document.bt_excluir.src 		= desabilidarMenu(document.bt_excluir.src,4,true);			// Bt Excluir
				document.bt_excluir.disabled 	= true;
			}else{
				document.getElementById('bt_inserir2').style.display = 'block';
				document.getElementById('bt_alterar2').style.display = 'block';
				
				verificaAcao();
			}
		}
	}
	function selecionaSelect(campo,valor){
		for(var i=0; i<campo.length; i++){
			if(campo[i].value == valor){
				campo[i].selected = true;
			}else{
				campo[i].selected = false;
			}
		}
	}
	
	function data(){
		var hoje = new Date();

		var dia = hoje.getDate();
		var dias = hoje.getDay();
		var mes = hoje.getMonth();
		var ano = hoje.getYear();
		
		if(dia < 10) 	dia = "0" + dia;	
		if(mes < 10)	mes = "0" + mes;
		
		hoje = dia +"/"+mes+"/"+ano;
		
		return hoje;
	}	
	function filtro_ordenar(valor,typeDate){
		if(typeDate == undefined){
			typeDate = 'text';
		}
		
		if(document.filtro.filtro_tipoDado != undefined){
			document.filtro.filtro_tipoDado.value = typeDate;
		}
	
		if(document.filtro.filtro_ordem.value == valor){
			if(document.filtro.filtro_ordem_direcao.value == "ascending"){
				document.filtro.filtro_ordem_direcao.value = "descending";
			}else{
				document.filtro.filtro_ordem_direcao.value = "ascending";
			}
		}else{
			document.filtro.filtro_ordem.value = valor;
		}
		document.filtro.submit();
	}
	
	function salvar(){
		IdGrupoParametroSistema = 13;
		switch (document.formulario.Acao.value){
			case 'inserir':
				var IdParametroSistema = 40;
				break;
			case 'alterar':			
				var IdParametroSistema = 41;
				break;
			case 'login':			
				salva_formulario();
				break;
			default:
				return false;
				break;
		}
		if(document.formulario.Acao.value != 'login'){
			var msg = '';
			var xmlhttp   = false;
			if (window.XMLHttpRequest) { // Mozilla, Safari,...
	    		xmlhttp = new XMLHttpRequest();
		        if(xmlhttp.overrideMimeType){
		        	xmlhttp.overrideMimeType('text/xml');
				}
			}else if (window.ActiveXObject){ // IE
				try{
					xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}catch(e){
					try{
						xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		            } catch (e) {}
	    	    }
		    }
		    var Local = document.formulario.Local;
		    if(Local != undefined){
		    	if(Local.value == 'index'){
					url = "xml/codigo_interno_free.php";
				}else{	
		   			url = "../../xml/codigo_interno_free.php";
	   			}
	   		}
			url = url + "?IdGrupoParametroSistema=" + IdGrupoParametroSistema + "&IdParametroSistema=" + IdParametroSistema;
			xmlhttp.open("GET", url,true);
	   		xmlhttp.onreadystatechange = function(){ 
	   			if(xmlhttp.readyState == 4){ 
					if(xmlhttp.status == 200){
						nameNode = xmlhttp.responseXML.getElementsByTagName('ValorParametroSistema')[0]; 
						if(nameNode != null){
							nameTextNode = nameNode.childNodes[0];
							msg = nameTextNode.nodeValue;
						}else{
							msg = '';
						}
						if(confirm(msg) == true){
							salva_formulario();
						}
					}
				}
			}
			xmlhttp.send(null);
		}
	}
	
	function salva_formulario(){
		if(validar() == true){
			document.formulario.submit();
		}
	}
	
	function listar(){
		document.filtro.submit();
	}
	
	function ajuda(){
		if(document.getElementById('quadroAjuda') != null){
			if(document.getElementById('quadroAjuda').style.display == 'none' || document.getElementById('quadroAjuda').style.display == ''){
				document.getElementById('quadroAjuda').style.display = 'block';
			}else{				
				document.getElementById('quadroAjuda').style.display = 'none';
			}
		}
	}
	
	function val_Mes(valor){
		mes = (valor.substring(0,2)); 
		ano = (valor.substring(3,7)); 
		
		// verifica se o mes e valido 
		if (mes < 01 || mes > 12 ) { 
			return false;
		} 
		
		// verifica se ano é válido
		if (ano < 1000 ) { 
			return false;
		}
		
				
	}	
	function carregando2(acao){
		if(acao == true){
			document.getElementById("Carregando2").style.display = 'block';
		}else{
			document.getElementById("Carregando2").style.display = 'none';
		}
		return true;
	}
	
	
	function verifica_cidade(IdPais,IdEstado){
		var xmlhttp   = false;
			if (window.XMLHttpRequest) { // Mozilla, Safari,...
	    		xmlhttp = new XMLHttpRequest();
		        if(xmlhttp.overrideMimeType){
		        	xmlhttp.overrideMimeType('text/xml');
				}
			}else if (window.ActiveXObject){ // IE
				try{
					xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}catch(e){
					try{
						xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		            } catch (e) {}
	    	    }
		    }
			xmlhttp.open("GET", "xml/cidade.php?IdPais="+IdPais+"&IdEstado="+IdEstado); 
			
			// Carregando...
			carregando2(true);
			
			xmlhttp.onreadystatechange = function(){ 
				if(xmlhttp.readyState == 4){ 
					if(xmlhttp.status == 200){
						var nameNode, nameTextNode, IdCidade, NomeCidade, Cidade='';					
						
						while(document.formulario.IdCidade.options.length > 0){
							document.formulario.IdCidade.options[0] = null;
						}
							
						addOption(document.formulario.IdCidade,"Selecione","0");
	
						for(var i=0; i<xmlhttp.responseXML.getElementsByTagName("IdCidade").length; i++){
							nameNode = xmlhttp.responseXML.getElementsByTagName("IdCidade")[i]; 
							nameTextNode = nameNode.childNodes[0];
							IdCidade = nameTextNode.nodeValue;
							
							nameNode = xmlhttp.responseXML.getElementsByTagName("NomeCidade")[i]; 
							nameTextNode = nameNode.childNodes[0];
							var NomeCidade = nameTextNode.nodeValue;
							
							addOption(document.formulario.IdCidade,NomeCidade,IdCidade);
							document.formulario.IdCidade.options[0].selected = true;
						}
				
						// Fim de Carregando
						carregando2(false);
					}
				}
			}
			xmlhttp.send(null);	
		
	}	
	
	function envia_form(){
		mostra_corpo('ctt/conveniados.php?IdSelectConvenio='+document.formulario.IdSelectConvenio.value);
	
	}
	
	function validar(){	
		if(document.formulario.NomeFantasia.value == ''){			
			mensagens('2');
			return false;
		}
		if(document.formulario.RazaoSocial.value == ''){
			mensagens('2');
			return false;
		}
		if(document.formulario.CNPJ.value == ''){
			mensagens('2');
			return false;
		}else{
			if(isCNPJ(document.formulario.CNPJ.value) == false){
				mensagens('16');
				return false;
			}
		}
		if(document.formulario.Endereco.value == ''){
			mensagens('2');
			return false;
		}
		if(document.formulario.Bairro.value == ''){
			mensagens('2');
			return false;
		}
		if(document.formulario.Fone.value == ''){
			mensagens('2');
			return false;
		}
	
		if(document.formulario.ProprietarioDataNascimento.value != ''){
			if(verifica_data(document.formulario.ProprietarioDataNascimento.value) == false){
				mensagens('15');
				return false;
			}			
		}
		if(document.formulario.ProprietarioNome.value == ''){
			mensagens('2');
			return false;
		}
	    document.formulario.Acao.value= 'inserir';
	   return true;
	}
	function verificaErro(){
		var nerro = document.formulario.Erro.value;
		if(nerro == '1'){
			nerro='';
			url = 'index.php?ctt=associados_enviado';
			window.location.replace(url);
		}
		if(nerro!=''){
			mensagens(nerro);
		}
	}
	function inicia(){
		document.formulario.NomeFantasia.focus();
	}
	function limpa_form(){
		verifica_cidade();	
	}
	
	function verifica_data (data) { 
		dia = (data.substring(0,2)); 
		mes = (data.substring(3,5)); 
		ano = (data.substring(6,10)); 

        // verifica o dia valido para cada mes 
        if ((dia < 01)||(dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31){
			return false; 
		} 

		// verifica se o mes e valido 
		if (mes < 01 || mes > 12 ) { 
			return false;
		} 

		// verifica se e ano bissexto 
		if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))){
			return false;
		}
	}
	
	function help(msg,prioridade){
		document.getElementById('helpText').firstChild.nodeValue = msg;
		document.getElementById('helpText').style.display = "block";
		switch (prioridade){
			case 'atencao':
				document.getElementById('helpText').style.color = "#C10000";
				return true;
		}
	}
	
	function validar_CNPJ(valor){
		if(valor == ''){
			
			return false;
		}

		if(isCNPJ(valor) == false){
			mensagens('16');
			return false;
		}
	}
	
	function enviaFormBusca(){
		mostra_corpo('ctt/busca.php?termo='+document.busca.termo.value);	
	}
	
	//var opc_status = [ "ATIVO", 	"DESATIVADO" ];
	var val_status = [ 1,			2 ];
	
	window.status = 'ConAdmin - A Solucao para sua Empresa (63) 3312-5858';
