try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

function imprimir() {
	if (window.print){
		window.print();
	}else{
		alert("Para imprimir pressione Crtl+P.");
	}
}
function TrocaClasse(id, NomeDaClasse)
{
document.getElementById(id).className = NomeDaClasse;
}
function fecharID(cam) {
	var cam = document.getElementById(cam);
 	$j(cam).animate({ height: 'hide', opacity: 'hide' }, 'slow');
}
function abre_pagina(pagina, largura, altura, esquerda, linha, targetName ) {
var width = largura;
var height = altura;
var left = esquerda;
var top = linha;
var rolagem = '2';
if( targetName == null ){
	targetName = 'pagina';
}
window.open(pagina, targetName,'width='+width+' , height='+height+', top='+top+', left='+left+', scrollbars='+rolagem+',fullscreen=no');
}
function popup(cam, largura, altura){
  cam = document.getElementById(cam);
  e = e ? e : window.event;
  cam.style.left    = e.clientX+"px";
  cam.style.top     = e.clientY+"px";
  cam.style.height  = altura;
  cam.style.width   = largura;
  cam.style.display = 'block';
}
function atualiza_conteudo(){
    window.parent.conteudo.location="frm_conteudo.php";
}
//Aumentar e diminuir fonte
var tgs = new Array( 'div','td','tr', 'p', 'span');
var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
var startSz = 2;
function ts( trgt,inc ) {
  if (!document.getElementById) return
  var d = document,cEl = null,sz = startSz,i,j,cTags;
  sz += inc;
  if ( sz < 0 ) sz = 0;
  if ( sz > 6 ) sz = 6;
  startSz = sz;
  if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];
  cEl.style.fontSize = szs[ sz ];
  for ( i = 0 ; i < tgs.length ; i++ ) {
   cTags = cEl.getElementsByTagName( tgs[ i ] );
   for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
  }
}
function UserLog( CodL, Cod, Tabela, urlPrincipal ){
  var url = urlPrincipal + "userlog.php?NumLog=" + CodL + "&cod=" + Cod + "&tabela=" + Tabela;
  xmlhttp.open("GET", url, true);
  xmlhttp.onreadystatechange=function() {
	if (xmlhttp.readyState==4){
		result = xmlhttp.responseText;
		if( result != "" ){
			document.getElementById("erro").style.display = 'block';
			document.getElementById("erro").innerHTML = result;
		}
	}
  }
  xmlhttp.send(null);
}
function ExibeConteudo( urlQuery, gotoID, idBotao, acao ){

	var $j = jQuery.noConflict();
	var url = urlQuery;
	var cam = document.getElementById(gotoID);
	var cam_idBotao = document.getElementById(idBotao);
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange=function() {
	    if (xmlhttp.readyState==4){
			result = xmlhttp.responseText;
			//$j('#caixa_total').fadeTo(1000,0.4).css('display','block');
			if( acao==0){
				$j(cam_idBotao).attr("onclick","ExibeConteudo('"+urlQuery+"', '"+gotoID+"', '"+idBotao+"', 1 )");
				$j(cam).html(result).animate({ height: 'hide', opacity: 'hide' }, 'fast');
			}else{
				$j(cam_idBotao).attr("onclick","ExibeConteudo('"+urlQuery+"', '"+gotoID+"', '"+idBotao+"', 0 )");
				$j(cam).html(result).animate({ height: 'show', opacity: 'show' }, 'fast');
			}
	    }
	}
	xmlhttp.send(null)
}
function Trim(str){return str.replace(/^\s+|\s+$/g,"");}
function idShow( id, bg ){
	var $j = jQuery.noConflict();
	var opct = '0.5';
	if( bg != "undefined" ){ 
		var bg = "#fff";
		var opct = '0.0';
	}
	overlayBox( bg, opct, id);
	$j(id).css({'z-index' : '10000'}).animate({ height: 'show', opacity: 'show' }, 'slow'); 
}
function overlayBox(bg, opct, id){
	var $j = jQuery.noConflict();
	// Get page sizes
	var arrPageSizes = PageSize();
	var id = instanceId(id);
	
	if( $j("#overlayBox").length > 0 ){
		$j("#overlayBox").remove();
	}
	$j('embed, object, select').css({ 'visibility' : 'hidden' });
	$j('body').append('<div id="overlayBox"></div>');
	$j('#overlayBox').css({
		backgroundColor:	bg,
		opacity:			opct,
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	}).fadeIn();
	$j(id).css({'z-index' : '101'});
	$j("#overlayBox").click(function() { 
		finishBox(id);
	})

}
function PageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
function PageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};

function finishBox(id) {
	$j('.aguarde').remove();
	$j(id).remove();
	$j('#overlayBox').fadeOut(function() { $j('#overlayBox').remove(); });
	$j('embed, object, select').css({ 'visibility' : 'visible' });
}
function idHide( id ){
	var $j = jQuery.noConflict();
	$j(id).animate({ height: 'hide', opacity: 'hide' }, 'slow');
	$j("#overlayBox").animate({ opacity: 'hide' }, 'fast');
}
function AjaxPHP( url ){
	var $j = jQuery.noConflict();
	var aguarde = "<div class='aguarde'><img alt='loading' src='http://www.transitobrasil.org/img/loading.gif' /><br />Atualizando XML, aguarde...</div>";
	if( $j(".aguarde").length > 0 ){
		$j(".aguarde").show();
	}else{
		$j(aguarde).appendTo("body").hide();
	}
	$j('.aguarde').ajaxStart(function(){
    	$j('.aguarde').show();
	});
	$j('.aguarde').ajaxStop(function(){
		$j('.aguarde').hide();   
	});
	$j.post( url );
}
function numdias(mes,ano) {
    if((mes<8 && mes%2==1) || (mes>7 && mes%2==0)) return 31;
    if(mes!=2) return 30;
    if(ano%4==0) return 29;
    return 28;
}
function somadias(data, dias) {
    data=data.split('/');
    diafuturo=parseInt(data[0])+dias;
    mes=parseInt(data[1]);
    ano=parseInt(data[2]);
    while(diafuturo>numdias(mes,ano)) {
        diafuturo-=numdias(mes,ano);
        mes++;
        if(mes>12) {
            mes=1;
            ano++;
        }
    }
    return diafuturo+"/"+mes+"/"+ano;
}
function LoadiFrame( url, id ){
	var $j = jQuery.noConflict();
	var versao = navigator.appVersion.indexOf("MSIE 6.0");
	if( versao != -1 && versao <= 18){
		abre_pagina(url,700,600,200,70);
	}else{
		overlayBox( '#000', 0.8, '#toDiviFrame');
		if( $j("#toDiviFrame").length > 0 ){
			$j("#toDiviFrame").remove();
		}
		$j('body').append("<div id='toDiviFrame' class='iFrameDiv'><iframe name='ifrm_anexo' scrolling='auto'  allowtransparency='true' src='" + url + "' frameborder='0' height='100%' width='100%' style='background-color: #fff;'></iframe></div>");
		$j('body').append("<div class='aguarde'><img alt='loading' src='"+PATH+"img/loading.gif' /><br />Aguarde Carregando...</div>");
	
		var arrPageSizes = PageSize();
		var arrPageScroll = PageScroll();

		$j('.aguarde').css( 'top',	arrPageScroll[1] + (arrPageSizes[3] / 10) + 200);
		$j('#toDiviFrame').css( 'top',	arrPageScroll[1] + (arrPageSizes[3] / 10));
		$j('.iFrameDiv').css('display','block');
		$j('iframe').load(function(){ $j(".aguarde").remove(); });
		$j('.iFrameDiv,.aguarde').click(function() { $j(".aguarde").remove(); });
	}
}

function doDestacaTexto(Texto, termoBusca){

	/*******************************************************************/
	// CASO QUEIRA MODIFICAR O ESTILO DA MARCAÇÃO ALTERE ESSAS VARIÁVEIS
	/*******************************************************************/
	inicioTag = "<span class='Hightlight'>";
	fimTag = "</span>";
	
	var novoTexto = "";
	var i = -1;
	var lcTermoBusca = termoBusca.toLowerCase();
	var lcTexto = Texto.toLowerCase();

	if( termoBusca.length > 3 ){
		while (Texto.length > 0){
			i = lcTexto.indexOf(lcTermoBusca, i+1);
			if (i < 0){
				novoTexto += Texto;
				Texto = "";
			}
			else{
				if (Texto.lastIndexOf(">", i) >= Texto.lastIndexOf("<", i)){
					if (lcTexto.lastIndexOf("/script>", i) >= lcTexto.lastIndexOf("<script", i)){
						novoTexto += Texto.substring(0, i) + inicioTag + Texto.substr(i, termoBusca.length) + fimTag;
						Texto = Texto.substr(i + termoBusca.length);
						lcTexto = Texto.toLowerCase();
						i = -1;
					}
				}
			}
		}
	}else{
		novoTexto = Texto;
	}
	return novoTexto;
}

function doDestacaTextoBusca(textoBusca, textoObj, ehFrase){
	
	$(textoObj).find('.Hightlight').removeClass('Hightlight');
	
	if (ehFrase){arrayBusca=[textoBusca];}
	else{arrayBusca = textoBusca.split(" ");}

	var Texto = textoObj.innerHTML;

	for (var i = 0; i < arrayBusca.length; i++){Texto = doDestacaTexto(Texto, arrayBusca[i]);}
	textoObj.innerHTML = Texto;
	return true;
}

function consultaCEP(cep){
	var cepL = cep.length;
	var cep = cep.replace(".","");
	for(i=0; i<=cepL; i++){
		cep = cep.replace("-","");
	}
	if( cep != "" ){
		var cepL = cep.length;
		if( cepL == 8 ){
			$j(".readOnly").attr('readOnly', 'true').css('background','#F8F3DE');
			$j("#cep").css('background', 'url('+PATH+'img/ajax_preloader.gif) no-repeat top right');
			emptyCEP();
			var url = PATH+"adm/cep.php";
			var param = "cep="+cep;
			$j.ajax({ 
					type: "POST", 
					url: url, 
					cache: true, 
					data: param,
					success: function(msg){
						result = msg.split( "|" );
						var logradouro = result[1];
						var bairro     = result[2];
						var ufStr      = result[3];
						var cidadeStr  = result[4];
						var uf         = result[5];
						var cidade     = result[6];
						var retorno    = result[7];
						$j('#logradouro').val(logradouro);
						$j('#bairro').val(bairro);
						$j('#ufStr').val(ufStr);
						$j('#cidadeStr').val(cidadeStr);
						$j('#uf').val(uf);
						$j('#cidade').val(cidade);
						if (retorno==0 || logradouro==" "){
							$j(".readOnly").removeAttr('readOnly').css('background','');
						}
						sayid(result[0],'cepMsg');
						$j('#cep').removeAttr('readOnly');
						$j("#cep").css('background','');
					}
			}).responseText;
		}else{
			sayid(cep+" CEP está incompleto, o correto são 8 dígitos.",'cepMsg');
		}
		$j('#cep').removeAttr('readOnly');
		$j("#cep").css('background','');
	}
}
function emptyCEP(){
	$j(".info").hide();
	//$j('#cep').empty();
	$j('#logradouro').empty();
	$j('#bairro').empty();
	$j('#ufStr').empty();
	$j('#cidadeStr').empty();
	$j('#uf').empty();
	$j('#cidade').empty();
}
function consultaCPF(cpf){
	$j("#cpf").css('background', 'url('+PATH+'img/ajax_preloader.gif) no-repeat top right');
	$j("#cpf").attr('disabled', 'true');
	var url = PATH+"adm/cpf.php";
	var param = "codCpf="+cpf;
	var cpfL = cpf.length;
	if( cpfL == 14 || cpfL == 11 ){
		$j.ajax({ 
				type: "POST", 
				url: url, 
				cache: true, 
				data: param,
				success: function(msg){
					sayid(msg, 'cpfMsg');
					$j('#cpf').removeAttr('disabled');
					$j("#cpf").css('background','#fff');
				}
		}).responseText;
	}else{
		sayid("CPF é um campo obrigatório, verifique se foi informado corretamente.", 'cpfMsg');
	}
	$j("#cpf").css('background','#fff');
	$j('#cpf').removeAttr('disabled');
}
function consultaLOGIN(login){
	$j("#Login").css('background', 'url('+PATH+'img/ajax_preloader.gif) no-repeat top right');
	$j("#Login").attr('disabled', 'true');
	var url = PATH+"adm/login.php";
	var login = RetiraAcentos(login);
	var param = "login="+login;
	var loginL = login.length;
	if( loginL >= 5 ){
		$j.ajax({ 
				type: "POST", 
				url: url, 
				cache: true, 
				data: param,
				success: function(msg){
					sayid(msg, 'loginMsg');
					$j("#Login").removeAttr('disabled');
					$j("#Login").css('background','');
				}
		}).responseText;
	}else{
		sayid("LOGIN com no minimo 05 caracteres.", 'loginMsg');
	}
	$j("#Login").removeAttr('disabled');
	$j("#Login").css('background','');
}
function RetiraAcentos(Campo) {
   var Acentos = "áàãââÁÀÃÂéêÉÊíÍóõôÓÔÕúüÚÜçÇabcdefghijklmnopqrstuvxwyz!@#$%¨&*()+=§/?°`´[]{}ªº^~;:,'"+"\"";
   var Traducao ="AAAAAAAAAEEEEIIOOOOOOUUUUCCABCDEFGHIJKLMNOPQRSTUVXWYZ";
   var Posic, Carac;
   var TempLog = "";
   for (var i=0; i < Campo.length; i++)
   {
   Carac = Campo.charAt (i);
   Posic  = Acentos.indexOf (Carac);
   if (Posic > -1)
	  TempLog += Traducao.charAt (Posic);
   else
      TempLog += Campo.charAt (i);
   }
      return (TempLog);
}
function sayid(texto, id){
	var cam = instanceId(id);
	var nomeClasse = $j(cam).attr("className");
	var nomeClasse = "."+nomeClasse;
	if( $j(cam).html() == "" ){
		$j(nomeClasse).fadeOut();
		$j(cam).html(texto).fadeIn();
	}else{
		$j(cam).html(texto);
	}
}
function instanceId(id){
	if( id.indexOf(".") == 0 || id.indexOf("#") == 0 ){
		var cam = id;
	}else{
		var cam = document.getElementById(id);
	}
	return cam;
}
function readOnly(campo){
	if( campo == "remove" ){
		$j("input[type='text']").removeAttr('readOnly').css('background', '');
		$j("input[type='password']").removeAttr('readOnly').css('background', '');
		$j("textarea").removeAttr('readOnly').css('background', '');
	}else{
		$j("input[type='text']").attr('readOnly','true').css('background', '#F8F3DE');
		$j("input[type='password']").attr('readOnly','true').css('background', '#F8F3DE');
		$j("textarea").attr('readOnly','true').css('background', '#F8F3DE');
	}
	if( campo != "" && campo != "remove" ){
		$j(campo).removeAttr('readOnly');
	}
}