
	var Ajax = false, objTXT = "", divTxtAjax = 'divRodapeAviso', icoAguarde = '<img src="../imagens/ico_aguarde.gif" style="margin-left:2px; margin-top:1px;">', icoErro = '<img src="../imagens/ico_aguarde_erro.gif" style="margin-left:2px;">', divDicaSistema = 'dicaSistema';
	
	function paginaCarregada() {
		document.getElementById('divPreCarregamento').style.display="none";	
	}
	
	function AjaxRequest() {

		if(window.XMLHttpRequest) {
			Ajax = new XMLHttpRequest();
		}
		else if(window.ActiveXObject) {
			try {
				Ajax = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					Ajax = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {}
			}
		}
	}

	
	//FUNÇÃO QUE ABRE UMA PÁGINA AJAX POR GET
	function abreAjaxGet(url,id_div_result,modo,funcao) {
		
		if(typeof(modo)=="undefined") modo = 'TXT';
		if(typeof(id_div_result)=="undefined") id_div_result = 'divConteudoPrincipal';
		
		url = antiCacheRand(url);
		
		AjaxRequest();
		if(!Ajax) {
			document.getElementById(divTxtAjax).innerHTML = icoErro;
			return;
		}
		Ajax.onreadystatechange = function(){
			if(Ajax.readyState == 4) {
				if(Ajax.status == 200) {
					document.getElementById(divTxtAjax).innerHTML = '&nbsp;';	
					document.getElementById(divDicaSistema).style.display="none";	
					
					if(modo == 'TXT') {
						if(id_div_result != 'undefined') {
							document.getElementById(id_div_result).innerHTML = Ajax.responseText;
						}
						if(typeof(funcao) != 'undefined') {
							eval(funcao);
						}
					}
					else if(modo == 'XML') {
						objXML = Ajax.responseXML;
						//testeXML(Ajax.responseXML);
						if(typeof(funcao) != 'undefined') {
							eval(funcao);
						}
					}
				}
				else {
					document.getElementById(divTxtAjax).innerHTML = icoErro;
						
				}
			}
		}
		document.getElementById(divTxtAjax).innerHTML = icoAguarde;
		document.getElementById(divDicaSistema).style.display="block";	
		Ajax.open('GET', url, true);
		Ajax.setRequestHeader("Content-Type", "text/html; charset=ISO-8859-1");
		Ajax.send(null);
	}	


	//FUNÇÃO QUE ABRE UMA PÁGINA AJAX POR POST
	function abreAjaxPost(arquivo,parametros,funcao) {
					
		var url; 
		url = antiCacheRand(arquivo);
		
		AjaxRequest();
		if(!Ajax) {
			document.getElementById(divTxtAjax).innerHTML = icoErro;
			return;
		}
		Ajax.onreadystatechange = function(){
			if(Ajax.readyState == 4) {
				if(Ajax.status == 200) {
					document.getElementById(divTxtAjax).innerHTML = '&nbsp;';
					document.getElementById(divDicaSistema).style.display="none";	
					
					objTXT = ""+Ajax.responseText;
					if(typeof(funcao) != 'undefined') {
						eval(funcao);
					}					
				}
				else {
					document.getElementById(divTxtAjax).innerHTML = icoErro;
						
				}
			}
		}
		document.getElementById(divTxtAjax).innerHTML = icoAguarde;
		document.getElementById(divDicaSistema).style.display="block";		
		Ajax.open('POST', url, true);
		Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		Ajax.send(parametros);
	}
	
	//FUNÇÃO QUE ABRE UMA PAGINA AJAX
	function abrePagina(arquivo,GET,id_div_result,funcao){
		var url; 
		url = arquivo+'.php?'+GET;
		abreAjaxGet(url,id_div_result,'TXT',funcao);
	}
	
	function enviaForm(arquivo,controle,id_form,id_div_result,id_div_alerta,status_retorno) {
		
		if(typeof(status_retorno) == 'undefined') status_retorno = 'OK';
		
		var obj = document.getElementById(id_form);
		var url = arquivo+'.php?controle='+controle;
		var parametros = retornaParamsForm(obj);
		abreAjaxPost(url,parametros,'verificaRetornoAjax(\''+arquivo+'\',\''+status_retorno+'\',\''+id_div_result+'\',\''+id_div_alerta+'\')');
	
	}
	
	function verificaRetornoAjax(arquivo,status_retorno,id_div_result,id_div_alerta) {
		
		var retorno = objTXT.split('*');
		
		if(retorno[0] == status_retorno) {
			abrePagina(arquivo,'controle='+status_retorno+'&msg='+retorno[1],id_div_result);
			//document.getElementById(id_div_result).innerHTML = 'Operação concluída com sucesso!';
		}
		else {
			if(id_div_alerta != 'undefined') {
				document.getElementById(id_div_alerta).innerHTML = objTXT;
			}
		}
	
	}
	
	function retornaParamsForm(obj){ 

		var valores = new Array(); 
		var indices = new Array(); 
		var parametros = ""; 
		for(var i = 0; i < obj.length; i++){ 
			if (obj.elements[i].type == "radio"){
				if (obj.elements[i].checked == true) {
					indices[i] = obj.elements[i].id; 
					valores[i] = obj.elements[i].value; 
				}
			}
			else {
				indices[i] = obj.elements[i].id; 
				valores[i] = obj.elements[i].value; 
			}
			if(i > 0 && i < obj.length){ 
				parametros += "&"; 
			} 
			parametros += indices[i] + '=' + valores[i]; 
		} 
		return parametros; 
	} 
	
	function enviaChecksForm(arquivo,controle,id_form,id_div_result,id_div_alerta,status_retorno) {
		
		var obj = document.getElementById(id_form);
		var url = arquivo+'.php?controle='+controle;
		
		var valores = new Array(); 
		var indices = new Array(); 
		var parametros = ""; 
		var cont_checkboxs = 1;
		
		for(var i = 0; i < obj.length; i++){ 
			if (obj.elements[i].type == "checkbox"){
				if (obj.elements[i].checked == true) {
					indices[i] = 'checkMsg'+cont_checkboxs; 
					valores[i] = obj.elements[i].value;
					cont_checkboxs++;
				}
			}
			if(i > 0 && i < obj.length){ 
				parametros += "&"; 
			} 
			parametros += indices[i] + '=' + valores[i]; 
		} 
		
		parametros += '&cont_checkboxs='+cont_checkboxs;
		
		abreAjaxPost(url,parametros,'verificaRetornoAjax(\''+arquivo+'\',\''+status_retorno+'\',\''+id_div_result+'\',\''+id_div_alerta+'\')');
	}	
	
	function antiCacheRand(aurl) {
    	var dt = new Date();
    	if(aurl.indexOf("?")>=0){// já tem parametros
        	return aurl + "&" + encodeURI(Math.random() + "_" + dt.getTime());
    	}
		else { 
		return aurl + "?" + encodeURI(Math.random() + "_" + dt.getTime());}
	}
	
	function checaCamposIguaisLogin(id1,id2) {
		
		var obj1 = document.getElementById(id1);
		var obj2 = document.getElementById(id2);
		
		if(obj1.value == obj2.value && obj1.value!='') {
			document.getElementById('divChecaCamposIguais').innerHTML = '<img src="../imagens/ico_sucesso.gif"  style="margin: 3 3 3 3;">';
		}
		else {
			document.getElementById('divChecaCamposIguais').innerHTML = '';
		}
		
	}
	
	function alteraIconeDiv(div_icone,novo_icone) {
		document.getElementById(div_icone).innerHTML = '<img src="../imagens/'+novo_icone+'" border="0" style="margin-left:2px">';
	}
	

	function ocultaExibeObj(nome) {
		
		var obj = document.getElementById(nome);
		
		if(obj.style.display) {
			if (obj.style.display=="block") {
				obj.style.display="none";
			}
			else {
				if (obj.style.display=="none") {
					obj.style.display="block";
				}
			}
			
		}
		else if(obj.style.visibility) {
			if(obj.style.visibility=="visible") {
				obj.style.visibility="hidden";
			}
			else if(obj.style.visibility=="hidden") {
				obj.style.visibility="visible";
			}
		}
	}

	
	function exibeInformacao(id_div,msg) {
		document.getElementById(id_div).innerHTML = '<span style="color:#CC6600">'+msg+'</span>';	
	}
	
	
	/*function ativaAbaCadastro(sId) {
		
		var aAbas = new Array('abaLocalidades','abaCidadaos','abaVeiculos','abaDestinos','abaAcidentes','abaVitimas','abaVeiculos','abaCondutores','abaTestemunhas','abaUsuarios','abaLog');  
	
		for ( var iAba = 0; iAba < aAbas.length; iAba++ ){
			
			var aAba = aAbas[ iAba ];
			
			var obj = document.getElementById(aAba);
			
			if(aAba == sId && obj) {
				document.getElementById(aAba).className = 'itemAbaPrincipal';
			}
			else if(obj) document.getElementById(aAba).className = 'itemAbaPrincipalDown';
			
		}
	
	}
	
	function ativaAbaAcidente(sId) {
		
		var aAbas = new Array('abaCaracteristicas','abaCircunstancias');  
	
		for ( var iAba = 0; iAba < aAbas.length; iAba++ ){
			
			var aAba = aAbas[ iAba ];
			if(aAba == sId ) {
				document.getElementById(aAba).className = 'itemAbaAcidente';
			}
			else document.getElementById(aAba).className = 'itemAbaAcidenteDown';
			
		}
	
	}	*/
	
	function focoCampoForm(nome) {
		var obj = document.getElementById(nome);
		if(obj) obj.focus();
	}
	
	function limpaCampoForm(nome) {
		var obj = document.getElementById(nome);
		if(obj) obj.value = '';
	}	
	
	function blurLink(obj) {
		obj.blur();
	}	
	
/*
	function mascaraData(obj) { 

		var mdata = ''; 
        mdata = mdata + obj.value; 
        if (mdata.length == 2){ 
        	mdata = mdata + '/'; 
            obj.value = mdata; 
        } 
        if (mdata.length == 5){ 
        	mdata = mdata + '/'; 
            obj.value = mdata; 
        } 
        if (mdata.length == 10){ 
        	return true;
        } 
    } 
	
	function mascaraHora(obj) { 

		var mdata = ''; 
        mdata = mdata + obj.value; 
        if (mdata.length == 2){ 
        	mdata = mdata + ':'; 
            obj.value = mdata; 
        }
        if (mdata.length == 5){ 
        	mdata = mdata + ':'; 
            obj.value = mdata; 
        }		
        if (mdata.length == 8){ 
        	return true;
        } 
    } 	
	
	function validaPlaca(obj) {
		var expressao = /^([A-Z]|[0-9])/;

		if (obj.value.length == 3){ 
            obj.value = obj.value + '-';
        }	
		
		if(!expressao.test(obj.value)) {
			obj.value = obj.value.substring(0, obj.value.length -1);
		} 
 	}
	
	function validaEstado(obj) {
		var expressao = /^[A-Z]{2}/;

		if (obj.value.length == 2){ 
			if(!expressao.test(obj.value)) {
				obj.value = '';
			} 
		}
 	}
	*/
	function mascaraTel(obj) { 

		var mdata = ''; 
        mdata = mdata + obj.value; 
        if (mdata.length == 1){ 
			obj.value = '('+obj.value;
        }
        if (mdata.length == 3){ 
        	mdata = mdata + ')'; 
            obj.value = mdata; 
        }	
        if (mdata.length == 8){ 
        	mdata = mdata + '-'; 
            obj.value = mdata; 
        }			
        if (mdata.length == 13){ 
        	return true;
        } 
    
	} 		
	
