/*
start_emp();
function start_emp()
{
	setInterval("load_emp()", 7000);
//	setTimeout("load_emp()", 7000);
}

function load_emp()
{
	//script ajax
	$.get('emp.php?rnd='+Math.random(), null, load_emp_ok, "xml");
}

function load_emp_ok (data, textStatus)
{
	// fadeout
	$("#Table1").fadeOut(1000, function(){
		$("#Table1").fadeIn(1000);
		
		var id = data.getElementsByTagName("id");
		if (id[0]) id = id[0].firstChild.nodeValue;
		var img = data.getElementsByTagName("img");
		if (img[0]) img = img[0].firstChild.nodeValue;
		var bairro = data.getElementsByTagName("bairro");
		if (bairro[0]) bairro = bairro[0].firstChild.nodeValue;
		var tipo = data.getElementsByTagName("tipo_de_imovel");
		if (tipo[0]) tipo = tipo[0].firstChild.nodeValue;
		var detalhes = data.getElementsByTagName("detalhes");
		if (detalhes[0]) detalhes = detalhes[0].firstChild.nodeValue;
		var valor = data.getElementsByTagName("valor");
		if (valor[0]) valor = valor[0].firstChild.nodeValue;
		
		// atualiza o ID
		var href = $("#dtlDestaques__ctl0_imgDestaque").attr("href");
		href = href.replace(/=.{4}/, "="+id);
		$("#dtlDestaques__ctl0_imgDestaque").attr("href", href);
		// atualiza a foto
		$("#dtlDestaques__ctl0_imgDestaque img:first").attr("src", img);
		// atualiza o bairro
		$("#dtlDestaques__ctl0_lblBairro").html(bairro);
		// atualiza o tipo de imóvel
		$("#dtlDestaques__ctl0_lblTipo").html(tipo);
		// atualiza os detalhes
		$("#dtlDestaques__ctl0_lblCaracteristicas").html(detalhes);
		// atualiza o valor
		$("#dtlDestaques__ctl0_lblValor").html(valor);
		
	});
}
*/

function allCheck(idform, idcheck)
{
	var el   = document.getElementById(idform);
	var main = document.getElementById(idcheck);
	var size = el.elements.length;
	
	if(main.checked)
	{
		for(var i=0; i < size; i++)
		{
			if(el.elements[i].type == 'submit')
			{
				if(el.elements[i].disabled == true)
				{
					el.elements[i].disabled = false;
				}
			}
			
			if(el.elements[i].type == 'checkbox')
			{
				el.elements[i].checked = true;
			}
		}
	}else{
		
		for(var i=0; i < size; i++)
		{
			if(el.elements[i].type == 'submit')
			{
				if(el.elements[i].disabled == false)
				{
					el.elements[i].disabled = true;
				}
			}
			
			if(el.elements[i].type == 'checkbox')
			{
				el.elements[i].checked = false;
			}
		}
	}
}

function _check(idform, idcheck)
{
	var el = document.getElementById(idform);
	//var ch = document.getElementById(idcheck);
	var size = el.elements.length;
	var j = 0;
	
		for(var i=0; i < size; i++)
			if(el.elements[i].type == 'checkbox') if(el.elements[i].checked) j++;			
			
	for(var i=0; i < size; i++)
	{
		if(el.elements[i].type == 'submit' )
		{
			if(j > 0)
			{
				el.elements[i].disabled = false;
			}else{
				el.elements[i].disabled = true;
			}
		}
	}
}


// onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;" 
// Fire FOx event.which
// Para evitar numeros em campos alfabéticos apenas mude o segundo Sinal para < 
// coloque OnKeyPress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false; para so numeros
// onKeypress="if (event.keyCode < 45 || event.keyCode < 57) event.returnValue = false;" 



/* Formatação para qualquer mascara */

function formatar(src, mask) 
{
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)
  
if (texto.substring(0,1) != saida) 
  {
	src.value += texto.substring(0,1);
  }
}

function TelefoneFormat(Campo, e) { 
    var key = ''; 
    var len = 0; 
    var strCheck = '0123456789'; 
    var aux = ''; 
    var whichCode = (window.Event) ? e.which : e.keyCode; 
     
    if (whichCode == 13 || whichCode == 8 || whichCode == 0) 
    { 
        return true;  // Enter backspace ou FN qualquer um que não seja alfa numerico 
    } 
    key = String.fromCharCode(whichCode); 
    if (strCheck.indexOf(key) == -1){ 
        return false;  //NÃO E VALIDO 
    } 
     
    aux =  Telefone_Remove_Format(Campo.value); 
     
    len = aux.length; 
    if(len>=10) 
    { 
        return false;    //impede de digitar um telefone maior que 10 
    } 
    aux += key; 
     
    Campo.value = Telefone_Mont_Format(aux); 
    return false; 
} 

function  Telefone_Mont_Format(Telefone) 
{ 
    var aux = len = ''; 
     
    len = Telefone.length; 
    if(len<=9) 
    { 
        tmp = 5; 
    } 
    else 
    { 
        tmp = 6; 
    } 
     
    aux = ''; 
    for(i = 0; i < len; i++) 
    { 
        if(i==0) 
        { 
            aux = '('; 
        } 
        aux += Telefone.charAt(i); 
        if(i+1==2) 
        { 
            aux += ')'; 
        } 
         
        if(i+1==tmp) 
        { 
            aux += '-'; 
        } 
    } 
    return aux ; 
} 

function  Telefone_Remove_Format(Telefone) 
{ 
    var strCheck = '0123456789'; 
    var len = i = aux = ''; 
    len = Telefone.length; 
    for(i = 0; i < len; i++) 
    { 
        if (strCheck.indexOf(Telefone.charAt(i))!=-1) 
        { 
            aux += Telefone.charAt(i); 
        } 
    } 
    return aux; 
} 

/* 

Exemplos:
CEP
OnKeyPress="formatar(this, '#####-###')"
CPF
OnKeyPress="formatar(this, '###.###.###-##')"
DATA
OnKeyPress="formatar(this, '##/##/####')"
*/


function ImovelExibe(sUrl, sWinName, dWidth, dHeight, bShowScroll, winResize)
{   	   
   var winl = (screen.width - dWidth) / 2;
   var wint = (screen.height - dHeight) / 2;
   var winprops = 'height=' + dHeight + ',width=' + dWidth + ',top=' + wint + ',left=' + winl + ',scrollbars=' + bShowScroll + ', resizable=' + winResize;
   var win = window.open(sUrl, sWinName, winprops);   
   if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
} // Para usar a função no link é o seguinte: javascript:OpenWindow('file.php','',altura,largura,'1');  1 para scroll e 0 para não

function fechar(){
	window.close();
}

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

		
	if (event.keyCode > 57 || event.keyCode == 47 || event.keyCode == 45)
        event.returnValue = false;
	}
}

function Limpar(valor, validos) {
// retira caracteres invalidos da string
var result = "";
var aux;
for (var i=0; i < valor.length; i++) {
aux = validos.indexOf(valor.substring(i, i+1));
if (aux>=0) {
result += aux;
}
}
return result;
}

//Formata número tipo moeda usando o evento onKeyDown

function Formata(campo,tammax,teclapres,decimal) {
var tecla = teclapres.keyCode;
vr = Limpar(campo.value,"0123456789");
tam = vr.length;
dec=decimal

if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

if (tecla == 8 )
{ tam = tam - 1 ; }

if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
{

if ( tam <= dec )
{ campo.value = vr ; }

if ( (tam > dec) && (tam <= 5) ){
campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 6) && (tam <= 8) ){
campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
}
if ( (tam >= 9) && (tam <= 11) ){
campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 12) && (tam <= 14) ){
campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 15) && (tam <= 17) ){
campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
} 

}

function max(txarea){ 
  
total = 400; 
  
tam = txarea.value.length; 
  
str=""; 
  
str=str+tam; 
  
Digitado.innerHTML = str; 
  
Restante.innerHTML = total - str; 
  
  
if (tam > total){ 
  
aux = txarea.value; 
  
txarea.value = aux.substring(0,total); 
  
Digitado.innerHTML = total 
  
Restante.innerHTML = 0 
	} 
} 

function contato(opcao){
	 d = document.contato;
	 parte1 = d.seu_email.value.indexOf("@");
     parte2 = d.seu_email.value.indexOf(".");
     parte3 = d.seu_email.value.length;
	if(opcao == 'C'){
		if(d.seu_nome.value == ''){
			alert("Digite seu nome");
			d.seu_nome.focus();
			
		}else if(d.seu_email.value == '' || d.seu_email.value == 'Nulo' || d.seu_email.value == 'nulo'){
			alert("Digite seu e-mail");
			d.seu_email.focus();
			
		}else if(!(parte1 >= 3 && parte2 >= 6 && parte3 >= 9)) {
	    	alert ("O campo seu e-mail deve ser conter um endereço eletrônico valido!");
    		d.seu_email.focus();
		
		}else if(d.assunto.value == ''){
			alert("Digite um assunto!");
			d.assunto.focus();
							
		}else if(d.msgs.value == ''){
			alert("Digite uma mensagem");
			d.msgs.focus();
			
		}else{
			d.submit();
		}
	}
}

// Script para a realização da Busca Instantanêa com Ajax
// Por Leandro Vieira Pinho, colunista iMasters (Dreamweaver)

// Função para iniciarmos o Ajax no browser do cliente.
function openAjax() {

var ajax;

try{
    ajax = new XMLHttpRequest(); // XMLHttpRequest para browsers decentes, como: Firefox, Safari, dentre outros.
}catch(ee){
    try{
        ajax = new ActiveXObject("Msxml2.XMLHTTP"); // Para o IE da MS
    }catch(e){
        try{
            ajax = new ActiveXObject("Microsoft.XMLHTTP"); // Para o IE da MS
        }catch(E){
            ajax = false;
        }
    }
}
return ajax;
}

// Função que realiza a busca instantânea
function buscaInstantanea() {
	if(document.getElementById) { // Para os browsers complacentes com o DOM W3C.
		var termo = document.getElementById('q').value; // Pega o termo digitado no campo de texto.
		var exibeResultado = document.getElementById('resultadoBusca'); // div que exibirá o resultado da busca.
		if(termo !== "" && termo !== null && termo.length >= 2 && termo.length < 11 ) { // Verifica se o campo não está vazio, ou se foi digitado no mínimo três caracteres.
			var ajax = openAjax(); // Inicia o Ajax.
			ajax.open("GET", "sub/busca.php?busca=" + termo, true); // Envia o termo da busca como uma querystring, nos possibilitando o filtro na busca.
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 1) { // Quando estiver carregando, exibe: carregando...
					exibeResultado.innerHTML = "<font size=2 color=red face=Arial><b>carregando...</b></font><img src=images/indicator.gif border=0 width=16 height=16>";
				}
				if(ajax.readyState == 4) { // Quando estiver tudo pronto.
					if(ajax.status == 200) {
						var resultado = ajax.responseText; // Coloca o resultado (da busca) retornado pelo Ajax nessa variável (var resultado).
						resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4)
						resultado = unescape(resultado); // Resolve o problema dos acentos
						exibeResultado.innerHTML = resultado;
						resultadoBusca.style.visibility = "visible";
					} else {
						exibeResultado.innerHTML = "Erro: ";
					}
				}
			}
			ajax.send(null); // submete
		}else{
			exibeResultado.innerHTML = '';
			resultadoBusca.style.visibility = "hidden";
		}
	}
}

function ClearInner(){
	var exibeResultado = document.getElementById('resultadoBusca');
	var termo = document.getElementById('q');
	termo.value = '';
	termo.focus();
	exibeResultado.innerHTML = '';
	resultadoBusca.style.visibility = "hidden";
}   
