// JavaScript Document
function valida1(){

	//Valores
	if ((document.formulario2.maximo.value != "") && (document.formulario2.maximo2.value == "")) {
		document.formulario2.maximo2.value = document.formulario2.maximo.value * 1.2;
		document.formulario2.maximo.value = document.formulario2.maximo.value * 0.8;
	}


	//Outros itens
	if ((document.formulario2.dormitorios.value != "") && (document.formulario2.dormitorios2.value == ""))
		document.formulario2.dormitorios2.value = document.formulario2.dormitorios.value;
	if ((document.formulario2.suites.value != "") && (document.formulario2.suites2.value == ""))
		document.formulario2.suites2.value = document.formulario2.suites.value;
	if ((document.formulario2.vagas.value != "") && (document.formulario2.vagas2.value == ""))
		document.formulario2.vagas2.value = document.formulario2.vagas.value;
	
	//Composicao
	var str_sql = '';
	for (i=0;i<document.formulario2.elements.length;i++) {
		var nome_campo = document.formulario2.elements[i].id;
		if (nome_campo.substr(0,4)=="cpo[") {
			//Composicao
			if ((document.formulario2.elements[i].type == "text") && (document.formulario2.elements[i].value != "")) {
				str_sql = str_sql+" and caracteristica(a.cd_imovel, a.cd_empresa, "+nome_campo.substr(4, nome_campo.length - 5)+") = "+document.formulario2.elements[i].value;
			}
			if ((document.formulario2.elements[i].type == "checkbox") && (document.formulario2.elements[i].checked == true)) {
				str_sql = str_sql+" and caracteristica(a.cd_imovel, a.cd_empresa, "+nome_campo.substr(4, nome_campo.length - 5)+") = 1";
			}				
		} else if (nome_campo.substr(0,4) == "ini[") {
			//Áreas e outros dados
			if ((document.formulario2.elements[i].value != "") && (document.formulario2.nome_campo.replace('ini','ate').value != "")) {
				var inicio = document.formulario2.nome_campo.value.replace('.','');
				inicio = inicio.replace(',','.');
				var fim = document.formulario2.nome_campo.replace('ini','ate').value.replace('.','');
				fim = fim.replace(',','.');
				str_sql = str_sql+" and a."+nome_campo.substr(4,nome_campo.length - 5)+" between "+inicio+" and "+fim;
			}
		} else if (nome_campo.substr(0,4) == "lik[") {
			//Dados textuais 'like'
			if (document.formulario2.elements[i].value != "") {
				str_sql = str_sql+" and a."+nome_campo.substr(4,nome_campo.length - 5)+" like '% "+document.formulario2.elements[i].value+"%'";
			}
		}
	}
	document.formulario2.sql_compo.value = str_sql;
	busca();
	document.formulario2.submit();
	
}
function valida2(){
	if(document.getElementById('codigo').value == ""){
		alert('Por favor, coloque o código do imóvel!');
		document.getElementById('codigo').focus();
		return false;
	} else
	document.formulario1.submit();
}
function valida3(){
	if(document.getElementById('finalidade').value == "0"){
		alert('Por favor, defina o seu interesse!');
		document.getElementById('finalidade').focus();
		return false;
	} else {
	document.getElementById('subtipos').style.display='none';
	document.getElementById('bairros').style.display='none';
  document.getElementById('loading').style.display=''
	document.formulario2.submit();
	}
}
function addItem(obj,strText,strValue,blSel,intPos,isMultiple){     
     var newOpt,i,ArTemp,selIndex;     
     selIndex = (blSel)?intPos:obj.selectedIndex; 
     newOpt = new Option(strText,strValue);     
     Len = obj.options.length+1     
     if (intPos > Len) return     
     obj.options.length = Len     
     if (intPos != Len) {     
          ArTemp = new Array();     
          for(i=intPos;i<obj.options.length-1;i++)     
               ArTemp[i] = Array(obj.options[i].text,obj.options[i].value);     
          for(i=intPos+1;i<Len;i++)     
               obj.options[i] = new Option(ArTemp[i-1][0],ArTemp[i-1][1]);     
     }     
     obj.options[intPos] = newOpt;     
     if (isMultiple==0) {
		 if (selIndex > intPos)     
			  obj.selectedIndex = selIndex+1;     
		 else if (selIndex == intPos)      
			  obj.selectedIndex = intPos;  
	} else {
		 if (selIndex > intPos)     
			  obj.options[selIndex+1].selected = true;
		 else if (selIndex == intPos)      
			  obj.options[intPos].selected = true;
	};
}
function pinta_campos() {
	for (i=0;i<document.getElementById('dados_imovel').elements.length;i++) {
		var nome_campo = document.getElementById('dados_imovel').elements[i].id;
		if (nome_campo!='') {
			if (nome_campo.substr(0,4)!='cpo[') {
				if (document.getElementById('dados_imovel').elements[i].value!='') {
					if (document.getElementById('dados_imovel').elements[i].value!='0.00') {
						if (document.getElementById('dados_imovel').elements[i].value!='0') {
							if (document.getElementById('dados_imovel').elements[i].value!='EM BRANCO') {
								document.getElementById('dados_imovel').elements[i].style.backgroundColor = "#FFFF00";
							}
						}
					}
				}
			}
		}
	}
}
