function salvar(opt, action){
	sendForm = 0;
	switch (opt) {
		case 1 :
		verifInputRadio(document.form);
		if(marcado==0)
		return false;
		if (document.form.UF.value == "") {
			alert("Selecione o estado");
			document.form.UF.focus();
			return false;
		}

		if (document.form.codcidade.value == "") {
			alert("Selecione a cidade");
			return false;
		}

		if (document.form.codsetor.value == "") {
			alert("Selecione o setor");
			return false;
		}

		if (document.form.endereco.value == "") {
			alert("Selecione o Endereço");
			document.form.endereco.focus();
			return false;
		}
				
		if(document.form.cep && document.form.cep_original.value=="") {
			//verificando se tem algum cep marcado
			flag = 0;
			for(i=0;i<document.form.elements.length;i++){
				if(document.form.elements[i].type=="radio" && document.form.elements[i].name=="cep"){
					if(document.form.elements[i].checked==true){
						flag++;
					}
				}
			}		
			if(flag==0){
				alert("Por favor faça a pesquisa e selecione o endereço do seu imóvel para aparecer no novo portal IMÓVEIS GOIÁS!");			
				return false;
			}
		}else if(!document.form.cep && document.form.cep_original.value==""){
			alert("Por favor faça a pesquisa e selecione o endereço do seu imóvel para aparecer no novo portal IMÓVEIS GOIÁS!");			
			return false;
		}

		if (document.form.codcategoria.value == "") {
			alert("Selecione a categoria do imóvel");
			document.form.codcategoria.focus();
			return false;
		}

		nomeCategoria = document.form.codcategoria.options[document.form.codcategoria.selectedIndex].text;
		document.form.nomeCategoria.value = nomeCategoria;

		break;

		case 2 :
		if (document.form.valor_imovel.value == "") {
			alert("Selecione o valor do imóvel");
			document.form.valor_imovel.focus();
			return false;
		}

		break;

		case 3 :
		if (document.form.imagem.value == "") {
			alert("Selecione a imagem");
			document.form.imagem.focus();
			return false;
		}
		else {
			document.form.action.value = action;
			document.form.okFoto.disabled = true;

			enviarCarregar(0);

			form.submit();
		}

		break;

		case 4 :

		break;
	}
	document.form.action.value = action;
	//document.bt.btsalvar.disabled = true;
	sendForm = 1;
	form.submit();
}

function verifInputRadio(nform) {
	//validacao de radio buttons sem saber quantos sao
	marcado = 0;
	if(nform.tipo.length){
		for (i=1; i<=nform.tipo.length; i++) {
			if (document.getElementById('tipo['+i+']').checked==true) {
				marcado = i
				break;
			}
		}


	}else{
		if(nform.tipo.value!=0 && nform.tipo.value!=0  && nform.tipo.checked==true){
			marcado=1
		}
	}
	if (marcado == 0) {
		alert("Selecione um Tipo de Imóvel");
		nform.tipo[0].focus();
	}
}


function rb(i) {
	document.getElementById('tipo['+i+']').checked = true;
}

function cadastro_cidade(uf) {
	window.open("select_cidade.php?uf="+uf,"cadastro_cidade","top=0,width=300,height=250, resizable=0,toolbar=0,status=0,menubar=0,scrollbars=0");
}

function cadastro_setor(uf, codcidade, cidade) {
	window.open("select_setor.php?uf="+uf+"&codcidade="+codcidade+"&cidade="+cidade,"cadastro_setor","top=0,width=300,height=250, resizable=0,toolbar=0,status=0,menubar=0,scrollbars=0");
}

function FtrataBackSpace(dado)
{
	NumDig = dado.value;
	TamDig = NumDig.length;
	TamDig--;
	Contador = 0;
	if ((TamDig >= 0) && (event.keyCode == 8))
	{ numer = "";
	for (i = TamDig; (i >= 0); i--){
		if ((parseInt(NumDig.substr(i,1))>=0) && (parseInt(NumDig.substr(i, 1))<=9))
		{
			Contador++;
			/*alert(NumDig.substr(i, 1) + " Contador " + Contador+ " numer "+ numer+ " TamDig " +TamDig + " i " + i );*/
			if ((Contador == 4) && ((TamDig -i) < 5))
			{numer = ","+numer;
			Contador = 0;
			}
			else if ((Contador == 3) && ((numer.length) > 4))
			{numer = "."+numer;
			Contador = 0;
			}

			numer = NumDig.substr(i, 1)+numer;

		}
	}
	if (numer == "001")
	numer="";
	if ((numer.length) == 3 )
	numer= "0," + numer;

	dado.value = numer;
	};
}

function FmascTempoReal(ConteudoCampo)
{
	if (((event.keyCode) > 47) && ((event.keyCode) < 58))
	{
		NumDig = ConteudoCampo.value;
		TamDig = NumDig.length;
		Contador = 0;
		if (TamDig > 1)
		{numer = "";
		for (i = TamDig; (i >= 0); i--){
			if ((parseInt(NumDig.substr(i,1))>=0) && (parseInt(NumDig.substr(i, 1))<=9))
			{
				Contador++;
				if ((Contador == 2) && ((TamDig -i) < 4))
				{numer = ","+numer;
				Contador = 0;
				}
				else if (Contador == 3)
				{numer = "."+numer;
				Contador = 0;
				}
				numer = NumDig.substr(i, 1)+numer;
			}
		}
		ConteudoCampo.value = numer;
		};
		return(true)}
		else return(false)
}

function validate(field)
{
	var valid = "0123456789.-,"
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		alert("Válido somente números");
		field.focus();
		field.select();
	}
}

function selecionarCidade(uf) {
	var cidade = document.form.selectCidade.value;

	if (document.form.selectCidade.value == "") {
		alert("Selecione uma cidade");
	}
	else {
		window.opener.top.cidade.location.href="cidade.php?action=add&uf="+uf+"&cidade="+cidade;
		window.close();
	}
}

function selecionarSetor(uf, codcidade, cidade) {
	var setor = document.form.selectSetor.value;

	if (document.form.selectSetor.value == "") {
		alert("Selecione um setor");
	}
	else {
		window.opener.top.setor.location.href="setor.php?action=add&cidade="+cidade+"&codcidade="+codcidade+"&uf="+uf+"&setor="+setor;
		window.close();
	}
}

function ir(m, codimovel) {
	if (codimovel > 0)
	document.form.action.value = "update";
	else if(codimovel=='undefined')
	codimovel='';
	window.location = "index.php?pg=imoveis.php&m="+m+"&codimovel="+codimovel;
}
function irLINK(m, codimovel) {
	if(codimovel=='undefined')
	codimovel='';
	window.location = "index.php?pg=imoveis.php&m="+m+"&codimovel="+codimovel;
}

function irUF(pagina, targ, selObj, restore){
	eval(targ+".location='"+pagina+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

function irCidade(pagina, targ, selObj, restore){
	eval(targ+".location='"+pagina+selObj.options[selObj.selectedIndex].value+"&cidade="+selObj.options[selObj.selectedIndex].text+"'");
	if (restore) selObj.selectedIndex=0;

	top.form.codcidade.value = selObj.options[selObj.selectedIndex].value;
	Top.form.cidade.value = selObj.options[selObj.selectedIndex].text;
}

function irSetor(pagina, selObj, restore){
	//eval(targ+".location='"+pagina+selObj.options[selObj.selectedIndex].value+"&cidade="+selObj.options[selObj.selectedIndex].text+"'");
	if (restore) selObj.selectedIndex=0;

	top.form.codsetor.value = selObj.options[selObj.selectedIndex].value;
}

function selecionar(valor, campo)
{
	var cont;
	for (cont=0;cont < campo.length; cont++)
	{
		campo[cont].checked = valor;
	}
}

//============= carrregando
var largura = screen.availWidth;
var altura = screen.availHeight;

function abrir(){
	try{
		if(largura > 800){
			Tleft = (largura-780)/2;
			for(i=0;i<document.all.mov.length;i++){
				leftAtual = document.all.mov[i].style.left.replace("px","");
				if(!leftAtual){
					leftAtual = 0;
				}
				leftAtual = parseInt(leftAtual)+Tleft-10;
				document.all.mov[i].style.left = leftAtual;
			}
		}
		for(i=0;i<document.all.mov.length;i++){
			document.all.mov[i].style.display = "block";
		}

		document.all.site.style.display = "block";
		document.all.carregando.style.display = "none";
	}
	catch(e){
	}
}

function imgPreview(campo) {
	window.parent.document.getElementById("fotos_preview").style.height = 200;
	window.parent.document.getElementById("fotos_preview").style.width = 350;

	fotos_preview.location = "fotos_preview.php?img="+campo.value;
}


function enviarCarregar(acao){
	if(acao == 0){
		document.getElementById('enviarCarregar').style.display = "block";
	}
	if(acao < 450){
		document.getElementById('enviarCarregando').style.width = acao;
		acao = acao+1;
		Tloop = setTimeout('enviarCarregar('+acao+')',1);
	}
	else{
		enviarCarregar(1);
	}
}

function confirmDel(foto, codimovel, cod_foto, tipoFoto) {
	deleteItem = false;
	if (confirm("Deseja excluir esta imagem ?")==true) {
		deleteItem = true;
		window.location = "index.php?pg=imoveis.php&m=3&codimovel="+codimovel+"&cod_foto="+cod_foto+"&tipFoto="+tipoFoto+"&foto="+foto+"&action=del&opcao_fotos=1";
	}
}
function confirmDelTodasFotos(codimovel,tipoFoto) {
	deleteItem = false;
	if (confirm("Deseja excluir Todas as imagens ?")==true) {
		deleteItem = true;
		window.location = "index.php?pg=imoveis.php&m=3&codimovel="+codimovel+"&tipFoto="+tipoFoto+"&action=del&opcao_fotos=0";
	}
}

function confirmDelImovel(codimovel,tipo) {
	if (confirm("Deseja excluir esta imóvel ?")) {
		window.location = "index.php?pg=imoveis.php&m=6&codimovel="+codimovel+"&action=del&tipo="+tipo;
	}
}

function ucase(obj) {
	key = obj.value;
	obj.value = key.toUpperCase();
}

/*
function sn(obj) {
if (event.keyCode < 48 || event.keyCode > 57)
event.keyCode = 0;

if( isNaN( parseInt( obj.value ))) {
alert( "O campo deve conter somente números!" );
obj.focus();
obj.select();
return false;
}

}
*/


function sn(e) {
	if(window.event)
	key = e.keyCode;
	else if(e.which)
	key = e.which;

	if (key!=8 || key < 48 || key > 57) return (((key > 47) && (key < 58)) || (key==8));
	return true;
}

function formataData(docID){
	docId = document.getElementById(docID);
	text  = new String();
	text  = docId.value;
	if(text.length==2)
	text += "/"
	else if(text.length==5)
	text += "/"
	docId.value =  text;
}


function formata(objForm, strField, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode; }
		else if(document.layers) { // Nestcape
			nTecla = evtKeyPress.which;
		}

		sValue = objForm[strField].value;

		// Limpa todos os caracteres de formatação que
		// já estiverem no campo.
		sValue = sValue.toString().replace( "-", "" );
		sValue = sValue.toString().replace( "-", "" );
		sValue = sValue.toString().replace( ".", "" );
		sValue = sValue.toString().replace( ".", "" );
		sValue = sValue.toString().replace( "/", "" );
		sValue = sValue.toString().replace( "/", "" );
		sValue = sValue.toString().replace( "(", "" );
		sValue = sValue.toString().replace( "(", "" );
		sValue = sValue.toString().replace( ")", "" );
		sValue = sValue.toString().replace( ")", "" );
		sValue = sValue.toString().replace( " ", "" );
		sValue = sValue.toString().replace( " ", "" );
		fldLen = sValue.length;
		mskLen = sMask.length;

		i = 0;
		nCount = 0;
		sCod = "";
		mskLen = fldLen;

		while (i <= mskLen) {
			bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
			bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

			if (bolMask) {
				sCod += sMask.charAt(i);
				mskLen++; }
				else {
					sCod += sValue.charAt(nCount);
					nCount++;
				}

				i++;
		}

		objForm[strField].value = sCod;

		if (nTecla != 8) { // backspace
			if (sMask.charAt(i-1) == "9") { // apenas números...
				return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
				else { // qualquer caracter...
					return true;
				} }
				else {
					return true;
				}
}


function habilita(form) {
	if (form.disabled == true){
		disab=0;
		form.disabled = false;
	}
	else{
		disab=1;
		form.disabled = true;
	}

	if(disab==0)
	form.focus();
}

function descricaoFoto(texto, codimovel, foto) {
	var texto_return;
	texto_return = prompt("Altere a descrição desta imagem:", texto);

	if (texto_return != texto && texto_return != "" && texto_return != null)
	window.location = "index.php?pg=imoveis.php&m=3&codimovel="+codimovel+"&action=desc&texto=" + texto_return + "&foto=" + foto;
}


function ValidaForm1(){
	erro=false;
	myFrm = document.form1
	msgErro = "Seu Cadastro Não Pode Ser Efetuado. Preencha os Campos: \n"
	if(myFrm.cad_cidade_cid.value== ""){
		msgErro = msgErro+" Cidade;\n";
		erro=true;
	}
	if(myFrm.cad_cidade_uf.value== ""){
		msgErro = msgErro+" Estado;\n";
		erro=true;
	}
	if(erro){
		alert(msgErro);
		return false;
	}
	else{
		return true;
	}
}

function NewWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}


function confirmar(){
	var submit
	submit=confirm("Você Confirma a Ação Selecionada?");

	if (submit==false){
		return false;
	}

	else{
		return true;
	}
}


function onlyNumbers(){

	if (event.keyCode < 45 || event.keyCode > 57)
	event.returnValue = false;
	if(event.keyCode==32)
	event.returnValue = true;
}

function soNumeros(){
	if (event.keyCode < 45 || event.keyCode > 57)
	event.returnValue = false;
	if(event.keyCode==47)
	event.returnValue = false;
	if(event.keyCode==45)
	event.returnValue = false;
	if(event.keyCode==46)
	event.returnValue = false;
}


function makeget (frm,jw,jh,jt,jl) { // Recebe (formulario,largura,altura,top,left)
	var query = '?';
	for(i = 0; i < frm.elements.length; i++) {
		if(frm.elements[i].name != '') {
			var val = escape(frm.elements[i].value);
			var ctrl = false;
			if(frm.elements[i].type == 'radio' ||
			frm.elements[i].type == 'checkbox') {
				if(frm.elements[i].checked == true) {
					query += frm.elements[i].name +"="+ val;
					ctrl = true;
				}
			} else {
				query += frm.elements[i].name +"="+ val;
				ctrl = true;
			}
			if(i < frm.elements.length - 1 && ctrl) query += "&";
		}
	}
	window.open(frm.action+query,'_blank','resizable=yes,scrollbars=yes,top='+jt+',left='+jl+',height='+jh+',width='+jw);
}


function abreUrlHidden(url,width, height,scrools){
	win=window.open(url,"","width="+width+",height="+height+",top=200, left=200, scrollbars="+scrools+", resizable=NO");
	return win;
}

function abreUrlNew(url,width, height,scrools){
	win=window.open(url,"","width="+width+",height="+height+",top=200, left=200, scrollbars="+scrools+", resizable=NO");
	return win;
}


function RefreshUrl(url){
	window.opener.location=url
}

function ReloadURL(url){
	window.opener.reload();
}
function pisca(){
	window.focus();
	setTimeout("pisca()",2000);
}

function down() {
	if (event.button>1) {
		alert("Código bloqueado!");
	}
	event.button = false;
}
function up() {
	if (event.button>1) {
		alert("Código bloqueado!");
	}
	event.button = false;
}
function keypressed() {
	alert("Por motivos de segurança, os comandos com o teclado foram desativados.");
	document.onkeydown.disabled = true;
}

function dropDests(lay, disp) {
	myDropd = document.getElementById(lay);
	myDropd.style.display= disp;
}
function mudaCor(myTr,myCor){
	myTr.className=myCor;
}

//XMLTTPREQUEST

function fielsNoEmpty(ids, respostas){
	//var fiels = new Array()
	//var fieldsArr	= campos.split(";");
	var idsArr		= ids.split(";");
	var asksArr		= respostas.split(";");
	for(i=0; i<idsArr.length; i++){
		if(idsArr[i]!=""){
			if(document.getElementById(idsArr[i]).value==""){
				alert(asksArr[i]);
				document.getElementById(idsArr[i]).focus();
				return false;
			}
		}
	}
}

function mailInvalido(emailInvalido){
	emailInvalid = document.getElementById(emailInvalido);
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailInvalid.value)))
	{
		alert("E-mail Inválido");
		emailInvalid.focus();
		return false;
	}
}
var xmlhttp
function loadXMLDoc(url, idWrite, tRequest)
{
	if(tRequest!="GET" || tRequest!="POST")
	tRequest = "GET";
	element	=  document.getElementById(idWrite);
	element.innerHTML  = "<div style='color:#FF0000;' align='center'>Carregando...</div>";
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest()
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4)
			{
				// if OK
				if (xmlhttp.status==200)
				{
					element.innerHTML=xmlhttp.responseText
				}
				else
				{
					alert("Problem Ao Tentar Abrir Arquivo:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.open(tRequest,url,true)
		xmlhttp.send(null)
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		if (xmlhttp)
		{
			xmlhttp.onreadystatechange=function(){
				if (xmlhttp.readyState==4)
				{
					// if OK
					if (xmlhttp.status==200)
					{
						element.innerHTML=xmlhttp.responseText
					}
					else
					{
						alert("Problem Ao Tentar Abrir Arquivo:" + xmlhttp.statusText)
					}
				}
			}
			xmlhttp.open(tRequest,url,true)
			if(tRequest=="POST")
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlhttp.send()
		}
	}
}
function mudaImagem(imagem, div){
	immggg = document.getElementById(div);
	immggg.style.display = 'block'
	immggg.src = imagem;

}
function confirmar(){
	var submit
	submit=confirm("Você Confirma a Ação Selecionada?");

	if (submit==false){
		return false;
	}

	else{
		return true;
	}
}

function Abre(url, width, height, rola){
	if (!rola || rola==""){rola="yes"}
	window.open(url,"_blank","resizable=no,toolbar=no,status=no,top=" + (window.screen.height - height)/2 + ",left=" + (window.screen.width - width)/2 + ",menubar=no,scrollbars=" + rola + ",width=" + width + ",height=" + height);
}

function dispHidId(elemId, opt){
	elemId = document.getElementById(elemId);
	if(!opt){
		if(elemId.style.display=='block')
		elemId.style.display='none'
		else
		elemId.style.display='block'
	}else
	elemId.style.display=opt;
}

