function trim(palavra){
	palavra = palavra.replace(/^\s*/, "").replace(/\s*$/, "");
	return palavra;
}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){
                    return true; 
                }
    }else{
        return false;
        }
}

function replaceAll(string, token, newtoken) {
	while (string.indexOf(token) != -1) {
 		string = string.replace(token, newtoken);
	}
	return string;
}

$(function () {
	var tabContainers = $('div.tabs > div');
	tabContainers.hide().filter(':first').show();
	
	$('div.tabs ul.tabNavigation a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.tabs ul.tabNavigation a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
});
function printPage(direcao, id){
	w = 500; 
	h = 550;
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	janela=window.open("print.php?direcao="+direcao+"&id="+id,"Imprimir",'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width='+w+',height='+h+', top='+TopPosition+', left='+LeftPosition);
	text = "Se a janela não abrir\ntalvez seja porque você tenha um\nprograma bloqueador de pop-up!\nPara abrir a janela\né preciso desabilitar o bloqueador\nde popup's!";
	if(janela == null) { alert(text); return; }
}
function atendimento(idmsn){
	w = 500; 
	h = 330;
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	janela=window.open("atendimento.php?idmsn="+idmsn,"Atendimento"+idmsn,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+w+',height='+h+', top='+TopPosition+', left='+LeftPosition);
	text = "Se a janela não abrir\ntalvez seja porque você tenha um\nprograma bloqueador de pop-up!\nPara abrir a janela\né preciso desabilitar o bloqueador\nde popup's!";
	if(janela == null) { alert(text); return; }
}
function printDetail(id){
	w = 720; 
	h = 500;
	LeftPosition = 20;
	TopPosition = 20;
	janela=window.open("printDetail.php?id="+id,"Atendimento",'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width='+w+',height='+h+', top='+TopPosition+', left='+LeftPosition);
	text = "Se a janela não abrir\ntalvez seja porque você tenha um\nprograma bloqueador de pop-up!\nPara abrir a janela\né preciso desabilitar o bloqueador\nde popup's!";
	if(janela == null) { alert(text); return; }
}
function sendFriend(id){
	w = 450; 
	h = 500;
	LeftPosition = 10;
	TopPosition = 10;
	janela=window.open("sendFriend.php?id="+id,"Atendimento",'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+w+',height='+h+', top='+TopPosition+', left='+LeftPosition);
	text = "Se a janela não abrir\ntalvez seja porque você tenha um\nprograma bloqueador de pop-up!\nPara abrir a janela\né preciso desabilitar o bloqueador\nde popup's!";
	if(janela == null) { alert(text); return; }
}
function pesquisar(){
	var valor_de   = trim($('#valor_de').val());
	var valor_ate  = trim($('#valor_ate').val());
	
	if((valor_de != "") || (valor_ate != "")){
		
		valor_de  = replaceAll(valor_de, ".", "");
		valor_de  = replaceAll(valor_de, ",", "");
		
		valor_ate = replaceAll(valor_ate, ".", "");
		valor_ate = replaceAll(valor_ate, ",", "");
	
		if(valor_de > valor_ate){			
			alert("O primeiro valor deve ser menor que o segundo!");
			document.getElementById('valor_de').value = "";
			document.getElementById('valor_ate').value = "";			
		}

	}

document.pesquisa.submit();
}
function addEvent (o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{

            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
}; 

function formatCurrency(o, n, dig, dec){
    o.c = !isNaN(n) ? Math.abs(n) : 2;
    o.dec = typeof dec != "string" ? "," : dec, o.dig = typeof dig != "string" ? "." : dig;
    addEvent(o, "keypress", function(e){
        if(e.key > 47 && e.key < 58){
            var o, s, l = (s = ((o = this).value.replace(/^0+/g, "") + String.fromCharCode(e.key)).replace(/\D/g, "")).length, n;
            if(o.maxLength + 1 && l >= o.maxLength) return false;
            l <= (n = o.c) && (s = new Array(n - l + 2).join("0") + s);
            for(var i = (l = (s = s.split("")).length) - n; (i -= 3) > 0; s[i - 1] += o.dig);
            n && n < l && (s[l - ++n] += o.dec);
            o.value = s.join("");
        }
        e.key > 30 && e.preventDefault();
    });
}
function imovelDetail(id){
	window.location = "imovel_detail.php?id="+id;
}

			function naoLogado(corretor){			
				alert("O corretor(a) "+corretor+"\nnão está disponível no momento!");
			}
