// JavaScript Document
function resizeMe(obj){
	docHeight = stats.document.body.scrollHeight
	obj.style.height = docHeight + 0 + 'px'
}
function FormataValor(objeto,teclapres,tammax,decimais) 
{

	var tecla			= teclapres.keyCode;
	var tamanhoObjeto	= objeto.value.length;

	if ((tecla == 8) && (tamanhoObjeto == tammax))
	{
		tamanhoObjeto = tamanhoObjeto - 1 ;
	}



if (( tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) && ((tamanhoObjeto+1) <= tammax))
	{

		vr	= objeto.value;
		vr	= vr.replace( "/", "" );
		vr	= vr.replace( "/", "" );
		vr	= vr.replace( ",", "" );
		vr	= vr.replace( ".", "" );
		vr	= vr.replace( ".", "" );
		vr	= vr.replace( ".", "" );
		vr	= vr.replace( ".", "" );
		tam	= vr.length;
		
		if (tam < tammax && tecla != 8)
		{
			tam = vr.length + 1 ;
		}

		if ((tecla == 8) && (tam > 1))
		{
			tam = tam - 1 ;
			vr = objeto.value;
			vr = vr.replace( "/", "" );
			vr = vr.replace( "/", "" );
			vr = vr.replace( ",", "" );
			vr = vr.replace( ".", "" );
			vr = vr.replace( ".", "" );
			vr = vr.replace( ".", "" );
			vr = vr.replace( ".", "" );
		}
	
		//Cálculo para casas decimais setadas por parametro
		if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
		{
			if (decimais > 0)
			{
				if ( (tam <= decimais) )
				{ 
					objeto.value = ("0," + vr) ;
				}
				if( (tam == (decimais + 1)) && (tecla == 8))
				{
					objeto.value = vr.substr( 0, (tam - decimais)) + ',' + vr.substr( tam - (decimais), tam ) ;	
				}
				if ( (tam > (decimais + 1)) && (tam <= (decimais + 3)) &&  ((vr.substr(0,1)) == "0"))
				{
					objeto.value = vr.substr( 1, (tam - (decimais+1))) + ',' + vr.substr( tam - (decimais), tam ) ;
				}
				if ( (tam > (decimais + 1)) && (tam <= (decimais + 3)) &&  ((vr.substr(0,1)) != "0"))
				{
				    objeto.value = vr.substr( 0, tam - decimais ) + ',' + vr.substr( tam - decimais, tam ) ; 
				}
				if ( (tam >= (decimais + 4)) && (tam <= (decimais + 6)) )
				{
			 		objeto.value = vr.substr( 0, tam - (decimais + 3) ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
				}
			 	if ( (tam >= (decimais + 7)) && (tam <= (decimais + 9)) )
				{
			 		objeto.value = vr.substr( 0, tam - (decimais + 6) ) + '.' + vr.substr( tam - (decimais + 6), 3 ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
				}
				if ( (tam >= (decimais + 10)) && (tam <= (decimais + 12)) )
				{
			 		objeto.value = vr.substr( 0, tam - (decimais + 9) ) + '.' + vr.substr( tam - (decimais + 9), 3 ) + '.' + vr.substr( tam - (decimais + 6), 3 ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
				}
				if ( (tam >= (decimais + 13)) && (tam <= (decimais + 15)) )
				{
			 		objeto.value = vr.substr( 0, tam - (decimais + 12) ) + '.' + vr.substr( tam - (decimais + 12), 3 ) + '.' + vr.substr( tam - (decimais + 9), 3 ) + '.' + vr.substr( tam - (decimais + 6), 3 ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
				}
			}
			else if(decimais == 0)
			{
				if ( tam <= 3 )
				{ 
			 		objeto.value = vr ;
				}
				if ( (tam >= 4) && (tam <= 6) )
				{
					if(tecla == 8)
					{
						objeto.value = vr.substr(0, tam);
						window.event.cancelBubble = true;
						window.event.returnValue = false;
					}
					objeto.value = vr.substr(0, tam - 3) + '.' + vr.substr( tam - 3, 3 ); 
				}
				if ( (tam >= 7) && (tam <= 9) )
				{
					if(tecla == 8)
					{
						objeto.value = vr.substr(0, tam);
						window.event.cancelBubble = true;
						window.event.returnValue = false;
					}
					objeto.value = vr.substr( 0, tam - 6 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, 3 ); 
				}
				if ( (tam >= 10) && (tam <= 12) )
				{
			 		if(tecla == 8)
					{
						objeto.value = vr.substr(0, tam);
						window.event.cancelBubble = true;
						window.event.returnValue = false;
					}
					objeto.value = vr.substr( 0, tam - 9 ) + '.' + vr.substr( tam - 9, 3 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, 3 ); 
				}











				if ( (tam >= 13) && (tam <= 15) )
				{
					if(tecla == 8)
					{
						objeto.value = vr.substr(0, tam);
						window.event.cancelBubble = true;
						window.event.returnValue = false;
					}
					objeto.value = vr.substr( 0, tam - 12 ) + '.' + vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, 3 ) ;
				}			
			}
		}
	}
	else if((window.event.keyCode != 8) && (window.event.keyCode != 9) && (window.event.keyCode != 13) && (window.event.keyCode != 35) && (window.event.keyCode != 36) && (window.event.keyCode != 46))
		{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
}

function resultado_cod() {
	if ((document.getElementById('codigo').value == "Digite o código") || (document.getElementById('codigo').value == "")) {
	 	alert("Favor digitar o código");
		document.getElementById('codigo').focus();
	} else {
	 	codigo = document.getElementById('codigo').value;
		open_page( "comprar_imoveis_detalhes.php?codint=" + codigo,'',10,10,790,590 );
	}
}
function resul_cod() {
	if ((document.getElementById('codigo').value == "Digite o código") || (document.getElementById('codigo').value == "")) {
	 	alert("Favor digitar o código");
		document.getElementById('codigo').focus();
	} else {
	 	codigo = document.getElementById('codigo').value;
		detalhes(codigo,'2');
	}
}

function open_page( page, name_window, x, y, w, h ){
	if ( x == undefined || x == '' ) { x = 0; }
	if ( y == undefined || y == '' ) { y = 0; }
	if ( w == undefined ) { w = 400; }
	if ( h == undefined ) { h = 300; }
	if ( name_window == undefined ) { name_window = ''; }
	window.open( page, name_window, 'statusbar,scrollbars,toolbar,resizable,top=' + x + ',left=' + y + ',width=' + w + ',height=' + h  );
}
function abre_site(width, height, nome,barra) {
   var top; var left; 
   if (barra == "") {
   		barra = 0;
   }
  top = 10;
  left = ( (screen.width/2) - (width/2) )
	window.open(nome, "","toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=0,scrollbars=1,width="+width+",height="+height+",left="+left+",top="+top);
  }
function _detalhe( cod ){
    open_page( "comprar_imoveis_detalhes.php?codimo=" + cod,'',10,10,790,590 );
}

function selecionar_linha_tabela( obj, nome ) {
	if ( obj.className == "Selecao" ) {
		obj.className = nome;
	} else {
		obj.className = "Selecao";
	}
}
function _foto(foto){
   open_page("foto.php?foto="+foto,'',10,10,530,410);
}
function detalhes(codigo,situacao){
	abre_site(517,450,"alugar_imoveis_detalhes.php?codigo="+codigo+"&situacao="+situacao);	
}
function fotos(codigo,situacao){
	abre_site(530,450,"fotos.php?codigo="+codigo+"&situacao="+situacao);	
}
function _foto2(codimo){
   open_page("galfotos.php?codimo="+codimo,'',10,10,500,550);
}

var req;
function loadXMLMenu(pagina){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeMenu;
        req.open("GET", pagina, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeMenu;
            req.open("GET", pagina, true);
            req.send();
        }
    }
}
function processReqChangeMenu(){
    if (req.readyState == 4) {
        if (req.status == 200) {
            document.getElementById('bairro').innerHTML = req.responseText;
        } else {
            alert("Houve um problema ao obter os dados:\n" + req.statusText);
			document.getElementById('bairro').innerHTML = "";
        }
    }
}

function fechar(){
	window.close()
}

function atualizabairro(){
	tipo = document.form1.codtipimo.value;
	sit = 0;
	loadXMLMenu("atualizabairro.php?codtipimo="+tipo+"&sit="+sit);
}
function atualizabairro2(){
	tipo = document.form1.tipo.value;
	cidade = document.form1.cidade.value;
	situacao = 2;
	loadXMLMenu("alugaratualizabairro.php?tipo="+tipo+"&cidade="+cidade+"&situacao="+situacao);
}
	function changeImageInfo(imageURL,title){
		document.big_image.src = imageURL;
	}
	function click() {
		if (event.button==2||event.button==3) {
			 oncontextmenu='return false';
		}
     }
	document.onmousedown=click
	document.oncontextmenu = new Function("return false;")
function _validaFormulario(frm){
		var campos = new Array();
		var mensagem = new Array();
		var valor_padrao = new Array();
		var temp1 = frm.campos_obrigatorios.value;
		var temp2 = frm.mensagem_preenchidos.value;
		var temp3 = frm.valor_padrao.value;
		campos = temp1.split(",");
		mensagem = temp2.split(",");
		valor_padrao = temp3.split(",");
		for(i = 0; i < campos.length; i++){		
			if(campo = document.getElementById(campos[i])){
				if(campo.type == "text" || campo.type == "password"){
					if((campo.value == valor_padrao[i]) || (campo.value == "")){
						alert(mensagem[i]);
						campo.focus();
						return false;
					}
				} else if(campo.type == "select-one"){
					if(campo.value == "0"){
						alert(mensagem[i]);
						campo.focus();
						return false;
					}
				} 
			}
		}
	}
function _detalhe(cod,imob){
    open_page( "detalhes.php?codigo=" + cod+"&imobiliaria="+imob,'',10,10,670,590 );
}
function buscarapida(){
	
	var cod = document.busca.codigo.value;
	if(cod != "" && cod != "Busca pelo código!"){
//		location = "busca_rapida.php?codigo=" + cod;	
		open_page( "busca_rapida.php?codigo=" + cod,'',10,10,670,590 );
	} else {
		document.busca.codigo.focus();
		alert("Favor informar o código");
	}
	
}
	function _validaPesquisa(frm){		
		if(frm.tipo.value == "0"){
			alert("Favor informar o Tipo do Imóvel!");
			return false;
		} else if(frm.cidade.value == "0"){
			alert("Favor informar a Cidade do Imóvel!");
			return false;
		}
	}
	function selecionar_linha_li( obj, nome ) {
	obj.className = nome;
}

function open_page( page, name_window, x, y, w, h ){
	if ( x == undefined || x == '' ) { x = 0; }
	if ( y == undefined || y == '' ) { y = 0; }
	if ( w == undefined ) { w = 400; }
	if ( h == undefined ) { h = 300; }
	if ( name_window == undefined ) { name_window = ''; }
	window.open( page, name_window, 'statusbar,scrollbars,toolbar,resizable,top=' + x + ',left=' + y + ',width=' + w + ',height=' + h  );
}
function addItem(obj,strText,strValue,blSel,intPos){ 
	 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 (selIndex > intPos) 
		  obj.selectedIndex = selIndex+1; 
	 else if (selIndex == intPos)  
		  obj.selectedIndex = intPos; 
}
