// JavaScript Document
function SelecionaRegiao(regiao) {
	if (regiao != "0") {//Esta clicnado no mapa, ent⭠zera os campos de pesquisa
		document.getElementById("cidade").value = "";
		document.getElementById("oficial").value = "";
		var cidade = "";
		var oficial = "";
		var ehform  = 0;
	}
	else {
		var cidade = document.getElementById("cidade").value;
		var oficial = document.getElementById("oficial").value;
		var ehform  = 1;
	}
	
	if ((regiao == "0") && (cidade == "") && (oficial == "")) {
		alert('Favor preencher o campo nome da cidade ou o campo nome do oficial!');
		
		if (cidade == "")
			$("#cidade").focus();
		else if (oficial == "")
			$("#oficial").focus();
		
		//return false;
	}
	
	if ((regiao != "0") || (cidade != "") || (oficial != "")) {
		try {
			xmlhttp = new XMLHttpRequest();
		}
		catch(ee) {
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch(e) {
				try {
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(E){
					xmlhttp = false;
				}
			}
		}
		
		Inicio();
		
		xmlhttp.open("POST","busca_oficios_ajax.php", true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4){
				var resultado = xmlhttp.responseText;
				document.getElementById("resultado").innerHTML = resultado;
				$(".tree_view").treeview({animated: "fast",collapsed: false});
				$("#aguarde").hide();
				Fim();
				if(ehform == 1)
					$("#cidade").focus();
				else
					$("#focar").focus();
			}
		}
		xmlhttp.send("regiao="+regiao+"&cidade="+cidade+"&oficial="+oficial+"&ehformulario="+ehform);
	}
	
	if (regiao == "0")//quando n⭠vier do mapa
		return false;//Para cancelar o submit do formul౩o
}


var w, h;
function tamanhos() {
	if (window.innerHeight || window.innerWidth) {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	else {
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}
}

function Inicio() {
	var getDoc = document;
	var load = getDoc.createElement('div');
	
	//tamanhos();
	var css = {
		position : 'fixed',
		font : 'normal 11px tahoma',
		background : "#DF0000",
		color : "#fff",
		padding : "3px",
		width : "80px",
		top : "330px",
		right: "0",
		left: "100px",
		'z-index':"9999999"
	}
	
	if ($.browser.msie) {
		css.position = 'absolute';
		css.top = (getDoc.documentElement.scrollTop || getDoc.body.scrollTop) + 'px';
	}
	
	$(load).attr('id','xLoading').css(css).html('Processando...');
	$('body',getDoc).prepend(load);
}

function Fim() {
	$("#xLoading").remove();
}

function findValue(li) {
	if (li == null) 
		return alert("N⭠foi encontrado!");
	
	if( !!li.extra ) 			//if coming from an AJAX call, let's use the CityId as the value
		var sValue = li.extra[0];
	else 						//otherwise, let's just display the value in the text box
		var sValue = li.selectValue;
	//alert("The value you selected was: " + sValue);
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return row[0];
}

$(document).ready(function(){
	$("#cidade").autocomplete("busca_oficios_autocomplete.php",{
		delay:10,
		minChars:2,
		matchSubset:1,
		matchContains:1,
		cacheLength:10,
		onItemSelect:selectItem,
		onFindValue:findValue,
		formatItem:formatItem,
		autoFill:false
	});
});
