var xmlHttp = new Object();
var idCont;

function _jsPage() {
	if (_jsPage.arguments.length == 1) idCont = "tdCont";
	else idCont = _jsPage.arguments[1];
	page = _jsPage.arguments[0];

	if (window.XMLHttpRequest) xmlHttp=new XMLHttpRequest()
	else if (window.ActiveXObject) xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")
	xmlHttp.onreadystatechange = _jsDone
	xmlHttp.open("GET", page, true)
	xmlHttp.send(null)
}

function _jsDone() { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		document.getElementById(idCont).innerHTML = xmlHttp.responseText;
	}
	else {
		document.getElementById(idCont).innerHTML = "<img src=\"../img/imgCarregando.gif\" style=\"margin:2px 2px 2px 2px;\">";
	}
}

function _jsImovel(ImovelID) {
	popup('detalheImovel.php?ImovelID='+ImovelID,'imovel','720','550','no');
}

function _jsGeraSelect(tabName, fieldName, searchKey, searchValue, paramJs, paramCss, fieldValue, resultText, resultValue, idCont) {
	_jsPage("../inc/geraSelect.php?tabName="+tabName+"&fieldName="+fieldName+"&searchKey="+searchKey+"&searchValue="+searchValue+"&paramJs="+paramJs+"&paramCss="+paramCss+"&fieldValue="+fieldValue+"&resultText="+resultText+"&resultValue="+resultValue,idCont)
}

function _jsGeraSelect2(tabName, fieldName, searchKey, searchValue, paramJs, paramCss, fieldValue, resultText, resultValue, idCont) {
	_jsPage("../inc/geraSelect.php?tabName="+tabName+"&fieldName="+fieldName+"&searchKey="+searchKey+"&searchValue="+searchValue+"&paramJs="+paramJs+"&paramCss="+paramCss+"&fieldValue="+fieldValue+"&resultText="+resultText+"&resultValue="+resultValue,idCont)
}

function _jsGeraSelect3(tabName, fieldName, searchKey, searchValue, paramJs, paramCss, fieldValue, resultText, resultValue, idCont) {
	_jsPage("../inc/geraSelect.php?tabName="+tabName+"&fieldName="+fieldName+"&searchKey="+searchKey+"&searchValue="+searchValue+"&paramJs="+paramJs+"&paramCss="+paramCss+"&fieldValue="+fieldValue+"&resultText="+resultText+"&resultValue="+resultValue,idCont)
}


function popup(x,n,w,h,s) {
    var l = ( (window.screen.width  - w) / 2 );
    var t = ( (window.screen.height - h) / 2 );
   janelinha = window.open(x,n,'leftmargin=-15,topmargin=-15,marginheight=0,marginwidth=0,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+s+',resizable=no,top='+t+',left='+l+',screenX='+l+',screenY='+t+',copyhistory=no,height='+h+',width='+w+'');
}

function _jsValidaCPF(CPF){ 
 	var i; 
	s = CPF  
	var c = s.substr(0,9); 
	var dv = s.substr(9,2); 
	var d1 = 0; 
	for (i = 0; i < 9; i++) { 
		d1 += c.charAt(i)*(10-i); 
	} 
	 
	if (d1 == 0) { 
		return false; 
	} 
	  
	d1 = 11 - (d1 % 11); 
	if (d1 > 9) d1 = 0; 
	if (dv.charAt(0) != d1) { 
 		return false;   
	} 
	  
	d1 *= 2; 	  
	for (i = 0; i < 9; i++)  {   
		d1 += c.charAt(i)*(11-i); 
	} 
	  
	d1 = 11 - (d1 % 11); 

	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1) { 
		return false; 
	} 
	  
	return true; 
}

function _jsSubmitAnuncie01() {
	if (!_jsValidaCPF(window.document.frmAnuncie.Anunciante_Cpf.value)) {
		alert('Você deve digitar o seu CPF corretamente, sem pontos, virgulas ou traços.\nEx: 12345678900');
		return false;
	}
}

function _jsGeraCidade(Estado_Id, Cidade_Id, idCont) {
	_jsPage("../admin/acoes.php?Acao=34&Estado_Id="+Estado_Id+"&Cidade_Id="+Cidade_Id,idCont)
}

function _jsGeraBairro(ImovelBairro, Estado_Id, Cidade_Id, idCont) {
	_jsPage("../admin/acoes.php?Acao=35&ImovelBairro="+ImovelBairro+"&Estado_Id="+Estado_Id+"&Cidade_Id="+Cidade_Id,idCont)
}

function _jsDetalheClass(Anuncio_Id) {
	popup("classificadosDetalhe.php?Anuncio_Id="+Anuncio_Id,"Classificados","684","462","no")
}

function _jsProposta02() {
	if (document.getElementById("tabProposta").style.display == 'none') {
		document.getElementById("tabProposta").style.display = '';
		document.getElementById("tabDetalhes").style.display = 'none';
		document.getElementById("tabIndicar").style.display = 'none';
	} else {
		document.getElementById("tabProposta").style.display = 'none';
		document.getElementById("tabDetalhes").style.display = '';
		document.getElementById("tabIndicar").style.display = 'none';
	}

}

function _jsIndicar() {
	if (document.getElementById("tabIndicar").style.display == 'none') {
		document.getElementById("tabIndicar").style.display = '';
		document.getElementById("tabDetalhes").style.display = 'none';
		document.getElementById("tabProposta").style.display = 'none';
	} else {
		document.getElementById("tabIndicar").style.display = 'none';
		document.getElementById("tabDetalhes").style.display = '';
		document.getElementById("tabProposta").style.display = 'none';
	}
}


function _jsIndicar03() {
	if (document.getElementById("tabIndicar").style.display == 'none') {
		document.getElementById("tabIndicar").style.display = '';
		document.getElementById("tabDetalhes").style.display = 'none';
		document.getElementById("tabDetalhes2").style.display = 'none';
		document.getElementById("tabOutrosDetalhes").style.display = 'none';	
		document.getElementById("tabOutrosDetalhes2").style.display = 'none';			
		document.getElementById("tabProposta").style.display = 'none';
	} else {
		document.getElementById("tabIndicar").style.display = 'none';
		document.getElementById("tabDetalhes").style.display = '';
		document.getElementById("tabDetalhes2").style.display = '';
		document.getElementById("tabOutrosDetalhes").style.display = '';		
		document.getElementById("tabOutrosDetalhes2").style.display = '';		
		document.getElementById("tabProposta").style.display = 'none';
	}
}

function _jsProposta03() {
	if (document.getElementById("tabProposta").style.display == 'none') {
		document.getElementById("tabProposta").style.display = '';
		document.getElementById("tabDetalhes").style.display = 'none';
		document.getElementById("tabDetalhes2").style.display = 'none';
		document.getElementById("tabOutrosDetalhes").style.display = 'none';
		document.getElementById("tabOutrosDetalhes2").style.display = 'none';		
		document.getElementById("tabIndicar").style.display = 'none';
	} else {
		document.getElementById("tabProposta").style.display = 'none';
		document.getElementById("tabDetalhes").style.display = '';
		document.getElementById("tabDetalhes2").style.display = '';
		document.getElementById("tabOutrosDetalhes").style.display = '';
		document.getElementById("tabOutrosDetalhes2").style.display = '';
		document.getElementById("tabIndicar").style.display = 'none';
	}

}

function FormataValor(id,tammax,teclapres) {
   
    if(window.event) 
	{ // Internet Explorer
	 var tecla = teclapres.keyCode; 
	}
	else if(teclapres.which) 
	{ // Nestcape / firefox
	 var tecla = teclapres.which;
    }
	vr = document.getElementById(id).value;
	vr = vr.toString().replace( "/", "" );
	vr = vr.toString().replace( "/", "" );
	vr = vr.toString().replace( ",", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	tam = vr.length;
	
	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 <= 2 ){
		document.getElementById(id).value = vr; }
		if ( (tam > 2) && (tam <= 5) ){
		document.getElementById(id).value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }
		if ( (tam >= 6) && (tam <= 8) ){
		document.getElementById(id).value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
		if ( (tam >= 9) && (tam <= 11) ){
		document.getElementById(id).value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
		if ( (tam >= 12) && (tam <= 14) ){
		document.getElementById(id).value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
		if ( (tam >= 15) && (tam <= 17) ){
		document.getElementById(id).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 Mascara(tipo, campo, teclaPress) {
	if (window.event)
	{
		var tecla = teclaPress.keyCode;
	} else {
		tecla = teclaPress.which;
	}
 
	var s = new String(campo.value);
	// Remove todos os caracteres à seguir: ( ) / - . e espaço, para tratar a string denovo.
	s = s.replace(/(\.|\(|\)|\/|\-| )+/g,'');
 
	tam = s.length + 1;
 
	if ( tecla != 9 && tecla != 8 ) {
		switch (tipo)
		{
		case 'CPF' :
			if (tam > 3 && tam < 7)
				campo.value = s.substr(0,3) + '.' + s.substr(3, tam);
			if (tam >= 7 && tam < 10)
				campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,tam-6);
			if (tam >= 10 && tam < 12)
				campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,3) + '-' + s.substr(9,tam-9);
		break;
 
		case 'CNPJ' :
 
			if (tam > 2 && tam < 6)
				campo.value = s.substr(0,2) + '.' + s.substr(2, tam);
			if (tam >= 6 && tam < 9)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,tam-5);
			if (tam >= 9 && tam < 13)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,tam-8);
			if (tam >= 13 && tam < 15)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,4)+ '-' + s.substr(12,tam-12);
		break;
 
		case 'TELEFONE' :
			if (tam > 2 && tam < 4)
				campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,tam);
			if (tam >= 7 && tam < 11)
				campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,4) + '-' + s.substr(6,tam-6);
		break;
 
		case 'DATA' :
			if (tam > 2 && tam < 4)
				campo.value = s.substr(0,2) + '/' + s.substr(2, tam);
			if (tam > 4 && tam < 11)
				campo.value = s.substr(0,2) + '/' + s.substr(2,2) + '/' + s.substr(4,tam-4);
		break;
		
		case 'CEP' :
			if (tam > 5 && tam < 9)
				campo.value = s.substr(0,5) + '-' + s.substr(5,tam-4);
		break;
		}
	}
}

function _jsValFale() {
	erro = false;
	if (document.frm1.Fale_Nome.value == '') {
		alert('O nome precisa ser preenchido');
		document.frm1.Fale_Nome.focus();
		erro = true;
		return false;
	}
	if (document.frm1.Fale_DDD.value == '') {
		alert('O DDD precisa ser preenchido');
		document.frm1.Fale_DDD.focus();
		erro = true;
		return false;
	}		
	if (document.frm1.Fale_Telefone.value == '') {
		alert('O Telefone precisa ser preenchido');
		document.frm1.Fale_Telefone.focus();
		erro = true;
		return false;
	}		
	if (document.frm1.Fale_Email.value == '') {
		alert('O e-mail precisa ser preenchido');
		document.frm1.Fale_Email.focus();
		erro = true;
		return false;
	}	
	if (document.frm1.Fale_Cidade.value == '') {
		alert('A cidade precisa ser preenchido');
		document.frm1.Fale_Cidade.focus();
		erro = true;
		return false;
	}	
	if (document.frm1.Fale_Estado.value == '') {
		alert('O estado precisa ser preenchido');
		document.frm1.Fale_Estado.focus();
		erro = true;
		return false;
	}	
	if (document.frm1.Fale_Mensagem.value == '') {
		alert('A mensagem precisa ser preenchida');
		document.frm1.Fale_Mensagem.focus();
		erro = true;
		return false;
	}		
	if (!erro) document.frm1.submit();
}

function _jsValVenda() {
	erro = false;
	if (document.frmVenda.Venda_Nome.value == '') {
		alert('O nome precisa ser preenchido');
		document.frmVenda.Venda_Nome.focus();
		erro = true;
		return false;
	}
	if (document.frmVenda.Venda_DDD.value == '') {
		alert('O DDD precisa ser preenchido');
		document.frmVenda.Venda_DDD.focus();
		erro = true;
		return false;
	}		
	if (document.frmVenda.Venda_Telefone.value == '') {
		alert('O Telefone precisa ser preenchido');
		document.frmVenda.Venda_Telefone.focus();
		erro = true;
		return false;
	}		
	if (document.frmVenda.Venda_Email.value == '') {
		alert('O e-mail precisa ser preenchido');
		document.frmVenda.Venda_Email.focus();
		erro = true;
		return false;
	}	
	if (document.frmVenda.Venda_Endereco.value == '') {
		alert('O endereço precisa ser preenchido');
		document.frmVenda.Venda_Endereco.focus();
		erro = true;
		return false;
	}	
	
	if (document.frmVenda.Venda_Bairro.value == '') {
		alert('O Bairro precisa ser preenchido');
		document.frmVenda.Venda_Bairro.focus();
		erro = true;
		return false;
	}
	if (document.frmVenda.Venda_CEP.value == '') {
		alert('O CEP precisa ser preenchido');
		document.frmVenda.Venda_CEP.focus();
		erro = true;
		return false;
	}		
	if (document.frmVenda.Venda_Cidade.value == '') {
		alert('A cidade precisa ser preenchido');
		document.frmVenda.Venda_Cidade.focus();
		erro = true;
		return false;
	}	
	if (document.frmVenda.Venda_Estado.value == '') {
		alert('O estado precisa ser preenchido');
		document.frmVenda.Venda_Estado.focus();
		erro = true;
		return false;
	}	
	if (document.frmVenda.Venda_Descricao.value == '') {
		alert('Os Detalhes do Imovel precisam ser preenchidos');
		document.frmVenda.Venda_Descricao.focus();
		erro = true;
		return false;
	}		
	if (!erro) document.frm1.submit();
}



function addFav(){
    var url      = "http://www.imobiliariamarechal.com.br";
    var title    = "IMOBILIÁRIA MARECHAL :: Um portal para a realização de GRANDES NEGÓCIOS!";
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}

