/* FAVORITOS ===================================================== */
function adFavorito(imvid)
{
    //var post = $('#myForm').serialize();    

	$.post("/post/adfavorito.php", {'imvid':imvid}, function(data) { 
		if (data == 'ok')
			alert("OK, seu imóvel foi adicionado aos PREFERIDOS do site");
	});

}
function delFavorito()
{
    $.post("/post/delfavorito.php", {'imvid':0}, function(data) { 
		if (data == 'ok')
		{
		  var favlista = document.getElementById('favlista');
		  favlista.style.display = 'none';
		  alert("Lista apagada!");
		}
	});


}


/************************************************************************************************************
	TOOLTIP
	************************************************************************************************************/
	var dhtmlgoodies_tooltip = false;
	var dhtmlgoodies_tooltipShadow = false;
	var dhtmlgoodies_shadowSize = 4;
	var dhtmlgoodies_tooltipMaxWidth = 200;
	var dhtmlgoodies_tooltipMinWidth = 100;
	var dhtmlgoodies_iframe = false;
	var tooltip_is_msie = (navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('opera')==-1 && document.all)?true:false;
	function showTooltip(e,tooltipTxt)
	{

		var bodyWidth = Math.max(document.body.clientWidth,document.documentElement.clientWidth) - 20;

		if(!dhtmlgoodies_tooltip){
			dhtmlgoodies_tooltip = document.createElement('DIV');
			dhtmlgoodies_tooltip.id = 'dhtmlgoodies_tooltip';
			dhtmlgoodies_tooltipShadow = document.createElement('DIV');
			dhtmlgoodies_tooltipShadow.id = 'dhtmlgoodies_tooltipShadow';

			document.body.appendChild(dhtmlgoodies_tooltip);
			document.body.appendChild(dhtmlgoodies_tooltipShadow);

			if(tooltip_is_msie){
				dhtmlgoodies_iframe = document.createElement('IFRAME');
				dhtmlgoodies_iframe.frameborder='5';
				dhtmlgoodies_iframe.style.backgroundColor='#FFFFFF';
				dhtmlgoodies_iframe.src = '#';
				dhtmlgoodies_iframe.style.zIndex = 100;
				dhtmlgoodies_iframe.style.position = 'absolute';
				document.body.appendChild(dhtmlgoodies_iframe);
			}

		}

		dhtmlgoodies_tooltip.style.display='block';
		dhtmlgoodies_tooltipShadow.style.display='block';
		if(tooltip_is_msie)dhtmlgoodies_iframe.style.display='block';

		var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
		if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0;
		var leftPos = e.clientX + 10;

		dhtmlgoodies_tooltip.style.width = null;	// Reset style width if it's set
		dhtmlgoodies_tooltip.innerHTML = tooltipTxt;
		dhtmlgoodies_tooltip.style.left = leftPos + 'px';
		dhtmlgoodies_tooltip.style.top = e.clientY + 10 + st + 'px';


		dhtmlgoodies_tooltipShadow.style.left =  leftPos + dhtmlgoodies_shadowSize + 'px';
		dhtmlgoodies_tooltipShadow.style.top = e.clientY + 10 + st + dhtmlgoodies_shadowSize + 'px';

		if(dhtmlgoodies_tooltip.offsetWidth>dhtmlgoodies_tooltipMaxWidth){	/* Exceeding max width of tooltip ? */
			dhtmlgoodies_tooltip.style.width = dhtmlgoodies_tooltipMaxWidth + 'px';
		}

		var tooltipWidth = dhtmlgoodies_tooltip.offsetWidth;
		if(tooltipWidth<dhtmlgoodies_tooltipMinWidth)tooltipWidth = dhtmlgoodies_tooltipMinWidth;


		dhtmlgoodies_tooltip.style.width = tooltipWidth + 'px';
		dhtmlgoodies_tooltipShadow.style.width = dhtmlgoodies_tooltip.offsetWidth + 'px';
		dhtmlgoodies_tooltipShadow.style.height = dhtmlgoodies_tooltip.offsetHeight + 'px';

		if((leftPos + tooltipWidth)>bodyWidth){
			dhtmlgoodies_tooltip.style.left = (dhtmlgoodies_tooltipShadow.style.left.replace('px','') - ((leftPos + tooltipWidth)-bodyWidth)) + 'px';
			dhtmlgoodies_tooltipShadow.style.left = (dhtmlgoodies_tooltipShadow.style.left.replace('px','') - ((leftPos + tooltipWidth)-bodyWidth) + dhtmlgoodies_shadowSize) + 'px';
		}

		if(tooltip_is_msie){
			dhtmlgoodies_iframe.style.left = dhtmlgoodies_tooltip.style.left;
			dhtmlgoodies_iframe.style.top = dhtmlgoodies_tooltip.style.top;
			dhtmlgoodies_iframe.style.width = dhtmlgoodies_tooltip.offsetWidth + 'px';
			dhtmlgoodies_iframe.style.height = dhtmlgoodies_tooltip.offsetHeight + 'px';

		}

	}

	function hideTooltip()
	{
		dhtmlgoodies_tooltip.style.display='none';
		dhtmlgoodies_tooltipShadow.style.display='none';
		if(tooltip_is_msie)dhtmlgoodies_iframe.style.display='none';
	}

/*============================ SELECT DINAMICO =========================
*** Multiple dynamic combo boxes
*** by Mirko Elviro, 9 Mar 2005
*/
// This script supports an unlimited number of linked combo boxed
// Their id must be "combo_0", "combo_1", "combo_2" etc.
// Here you have to put the data that will fill the combo boxes
// ie. data_2_1 will be the first option in the second combo box
// when the first combo box has the second option selected
// other parameters
displaywhenempty="-vazio-"
valuewhenempty=0

displaywhennotempty="-Selecione-"
valuewhennotempty=0

function change(currentbox)
{
    var numb = currentbox.id.split("_");
    var currentbox = numb[1];
    var i=parseInt(currentbox)+1;
    // I empty all combo boxes following the current one
    var _t=eval("typeof(document.getElementById('combo_"+i+"'))!='undefined'");
    while (_t && document.getElementById("combo_"+i)!=null)
    {
        var son = document.getElementById("combo_"+i);
        // I empty all options except the first (it isn't allowed)
        for (m=son.options.length-1;m>0;m--)
        son.options[m]=null;
        // I reset the first option
        son.options[0]=new Option(displaywhenempty,valuewhenempty);
        i=i+1;
    }

    // now I create the string with the "base" name ("stringa"), ie. "data_1_0"
    // to which I'll add _0,_1,_2,_3 etc to obtain the name of the combo box to fill
    var stringa='data';
    i=0;
    _t=eval("typeof(document.getElementById('combo_"+i+"'))!='undefined'");

    while (_t && document.getElementById("combo_"+i)!=null)
    {
        eval("stringa=stringa+'_'+document.getElementById(\"combo_"+i+"\").selectedIndex");
        if (i==currentbox)
        break;
        i=i+1;
    }

    // filling the "son" combo (if exists)
    var following=parseInt(currentbox)+1;
    _t=eval("typeof(document.getElementById('combo_"+following+"'))!='undefined'");
    if (_t && document.getElementById("combo_"+following)!=null)
    {
        son=document.getElementById("combo_"+following);
        stringa=stringa+"_";
        i=0;
        while ((eval("typeof("+stringa+i+")!='undefined'")) || (i==0))
        {
            // if there are no options, I empty the first option of the "son" combo
            // otherwise I put "-select-" in it
            if ((i==0) && eval("typeof("+stringa+"0)=='undefined'"))
            if (eval("typeof("+stringa+"1)=='undefined'"))
            eval("son.options[0]=new Option(displaywhenempty,valuewhenempty)");
            else
            eval("son.options[0]=new Option(displaywhennotempty,valuewhennotempty)");
            else
            eval("son.options["+i+"]=new Option("+stringa+i+".text,"+stringa+i+".value)")
            i++;
        }
        //son.focus();
        i=1;
        combostatus='';
        cstatus=stringa.split("_");
        while (cstatus[i]!=null)
        {
            combostatus=combostatus+cstatus[i];
            i=i+1;
        }
        return combostatus;
    }
}

/***********************************************
* ABRE JANELA
***********************************************/
function popUp2(u,w,h,o) {// example popUp('popup.html',400,300,'location,scrollbars');
          if (window.popWin && !popWin.closed) {
                 if (o != popWinOptions) {
                         popWin.close();
                         popUp(u,w,h,o);
                 }
                 if (popWin.resizeTo) {
                         popWin.resizeTo(w,h);
                 } else {
                         popWin.outerWidth = w;
                         popWin.outerHeight = h;
                 }
                 popWin.location = u;
                 popWin.focus();
                 return popWin;
         } else {
                 if (o) {
                         popWin = window.open(u,'pop','width='+w+',height='+h+','+o);
                         popWinOptions = o;
                 } else {
                         popWin = window.open(u,'pop','width='+w+',height='+h);
                         popWinOptions = '';
                 }
                 popWin.opener = window;
                 popWin.focus();
                 return popWin;
         }
}

function popUp(url,w,h,nome)
{
		var statusopcao = 'no';
		var scrollopcao = 'no';
		var alturaredu = '0';

	largura = screen.availWidth;
	altura = screen.availHeight;
	janela_larg = largura-6;
	janela_alt = altura;
	w = (w == 0 )? janela_larg:w;
	h = (h == 0 )? janela_alt:h;
	esquerda = (w == janela_larg)? 0:((largura - w)/2);
	topo = (h == janela_alt)? 0:((altura - h)/4);
	h = h - alturaredu;
	novaJanela=window.open(url,nome,'width='+w+',height='+h+',left='+esquerda+',top='+topo+' directory=no, status=no,scrollbars='+scrollopcao+', menubar=no');
    novaJanela.focus();
    return novaJanela;
}



/************************************************************************************************************
	POPUP INTERNO PARA MENU CAB BUSCA (RAPIDA|CODIGO)
************************************************************************************************************/


var delay = 10500;

function buscaOpcaoValida(tipo, form)
{

    if (tipo == 'codigo' && form.cod.value=='')
    {
        alert('O CODIGO ESTÁ EM BRANCO. \nVocê deve preenchê-lo antes de enviar a busca');
        return false;
    }
    else if (tipo == 'rapida' && form.idx_valmax.value.length > 0 && form.idx_busca_finalidade.selectedIndex == 0)
    {
        alert('Você deve selecionar a Finalidade antes: Venda ou Aluguel');
        return false;
    }
    return true;

}

function buscaOpcaoCodigo()
{
  popMostra('caixa_cod','on');
  popMostra('caixa_rap','off');
  document.getElementById('caixa_cod_link').className = 'codigodown';
  document.getElementById('caixa_rap_link').className = 'rapida';
}

function buscaOpcaoRapida()
{
  popMostra('caixa_cod','off');
  popMostra('caixa_rap','on');
  document.getElementById('caixa_cod_link').className = 'codigo';
  document.getElementById('caixa_rap_link').className = 'rapidadown';
}
function buscaOpcaoClear()
{
  document.getElementById('caixa_cod_link').className = 'codigo';
  document.getElementById('caixa_rap_link').className = 'rapida';
}


function popMostra(Div,Lock) {
  buscaOpcaoClear();
  if(navigator.appName == "Microsoft Internet Explorer") {

    if(Lock == 'on')
      document.all(Div).style.display = 'block';

   else if (Lock == 'off')
       document.all(Div).style.display = 'none';

  }

  else {
    if(Lock == 'on')
      document.getElementById(Div).style.display = 'block';
    else if (Lock == 'off')
      document.getElementById(Div).style.display = 'none';
  }

}
function popEsconde(div) {
	delayhide = setTimeout("popMostra('"+div+"', 'off')", delay);

}

function popClearDelay() {
	if (window.delayhide) {
		clearTimeout(delayhide);
	}
}
function validaFormTXTLimit(obj, intLim, carrest)
{
    intLng = obj.value.length;
	if (intLng>intLim){
		obj.value = obj.value.substring(0,intLim)
	}
	else{
		document.getElementById(carrest).innerHTML = (intLim - obj.value.length);
	}
}

/* COOKIES ====================================================================*/
function Get_Cookie(name) {
	   var start = document.cookie.indexOf(name+"=");
	   var len = start+name.length+1;
	   if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
	   if (start == -1) return null;
	   var end = document.cookie.indexOf(";",len);
	   if (end == -1) end = document.cookie.length;
	   return unescape(document.cookie.substring(len,end));
	}
function Set_Cookie(name,value,expires,path,domain,secure) {
		expires = expires * 60*60*24*1000;
		var today = new Date();
		var expires_date = new Date( today.getTime() + (expires) );
	    var cookieString = name + "=" +escape(value) +
	       ( (expires) ? ";expires=" + expires_date.toGMTString() : "") +
	       ( (path) ? ";path=" + path : "") +
	       ( (domain) ? ";domain=" + domain : "") +
	       ( (secure) ? ";secure" : "");
	    document.cookie = cookieString;

	}

function Delete_Cookie(name,path,domain) {
   if (Get_Cookie(name)) document.cookie = name + "=" +
      ( (path) ? ";path=" + path : "") +
      ( (domain) ? ";domain=" + domain : "") +
      ";expires=Thu, 01-Jan-70 00:00:01 GMT";
}

var today = new Date();
var zero_date = new Date(0,0,0);
today.setTime(today.getTime() - zero_date.getTime());
var cookie_expire_date = new Date(today.getTime() + (8 * 7 * 86400000));

function setVisitorID() {
   if (Get_Cookie('VisitorID')) {
       var VisitorID = Get_Cookie('VisitorID');
   }else{
       Set_Cookie('VisitorID',Math.random(),cookie_expire_date);
   }
}

function setSessionID() {
   if (!Get_Cookie('SessionID'))
       Set_Cookie('SessionID',Math.random());
}

var loaded_script = true;
