// JavaScript Document
var pi = function(){
	return {
		
		////////////////////////////////CADASTRO/////////////////////
		cadastro : function(){
			return{
				
				validaCreci : function(){
					setTimeout('pi.cadastro.validaCreciInput()',100);
				},
				
				validaCreciInput : function(){
					var creci = document.getElementById('txtCreci').value;
					
					if(creci.length>3){
						portal_imobiliarias_MasterPage.carregaImobiliariasDT(creci,pi.cadastro.validaCreciRetorno);
					}
				},
				
				validaCreciRetorno : function(r){
					var dt = r.value;	
					if(dt!=null && typeof(dt) == "object"){
						if(dt.Rows.length==1){
							var row = dt.Rows[0];
							
							var nome = 	row.nome;
							var cnpj = 	row.cnpj;
							var email = row.email;
							
							alert(nome + '-' + cnpj + '-' + email);
						}
					}
				}
				
			};
		}(),
		////////////////////////////////CADASTRO/////////////////////
		
		////////////////////////////////LOGIN/////////////////////
		login : function(){
			return{
				loga : function(){
					var login   = document.getElementById('txtLoginPI').value;	
					var senha   = document.getElementById('txtSenhaPI').value;
					var lembrar = document.getElementById('txtSalvarSenha');
					
					if (lembrar.checked)
						lembrar = 1;	
					else
						lembrar = 0;
							
					if(login=='' && senha=='')
						pi.login.logaRetorno(false);
					else
						classPICorretores.login(login,senha,lembrar,pi.login.logaRetorno);
				},
				
				logaRetorno : function(r){
					var condicao=false;
					if(typeof(r) == "object")
						condicao = r.value;
					else
						condicao = r;	
					if(condicao)
						window.location = 'imobiliarias/';
					else{
						document.getElementById('divFormImobiliarias').style.display = 'none';
						document.getElementById('divValidaImobiliarias').style.display = '';
						document.getElementById('spanTextoValidaImobiliarias').innerHTML = 'E-mail ou senha incorretos';
						document.getElementById('spanTextoValidaImobiliarias').style.color = 'red';
					}
				},
				
				trocaInputs : function(){
					document.getElementById('txtSenhaPI').disabled = true;
					document.getElementById('aAcaoPI').href = 'javascript:pi.login.esqueci()';
				},
				
				lembrarUsuario : function(){
					strUsuario = FuncoesGerais.LerCookie('EvenPIL');
					
					if (strUsuario!=""){
						retorno = classPICorretores.lembrarUsuario().value;
						if (retorno!='error'){
							retorno = retorno.split('|')
							document.getElementById('txtLoginPI').value = retorno[0];
							document.getElementById('txtSenhaPI').value = retorno[1];
							document.getElementById('txtSalvarSenha').checked = true;
						}
					}
				},
				
				esqueci : function(){
					var email  = document.getElementById('txtLoginPI').value;
					if(email=='')
						pi.login.esqueciRetorno(false);
					else{
						classPICorretores.esqueciMinhaSenha(email,pi.login.esqueciRetorno);
						
					}
				},
				
				esqueciRetorno : function(r){
					var condicao=false;
					if(typeof(r) == "object")
						condicao = r.value;
					else
						condicao = r;	
					if(condicao){					
						document.getElementById('lblErro').style.display = 'none';
						document.getElementById('lblSucesso').style.display = '';
						document.getElementById('aAcaoPI').style.display = 'none';
						document.getElementById('btVoltarFormImobiliarias').style.display = '';
					}else{
						document.getElementById('lblErro').style.display = '';
						document.getElementById('lblSucesso').style.display = 'none';
					}
				},
				
				voltarForm : function(){
					document.getElementById('divFormImobiliarias').style.display = '';
					document.getElementById('divValidaImobiliarias').style.display = 'none';
				}
			};
		}(),
		
		
		////////////////////////////////LOGIN/////////////////////
		
		////////////////////////////////MENU/////////////////////
		
		menu : function(){
			return {
				////////////////////////////////BUSCA////////////////////
				busca : function(){
					var dtRegioes = null;
					var dtBairros = null;
					var dtEmpreendimentos = null;
					var idCidade = 0;
					var idRegiao = 0;
					var bairro = '';
					return{
						
						
						alteraRegiao : function(){
							var regiao = document.getElementById('ctl00_dropRegioesHeader').value;
							var idCorretor = document.getElementById('ctl00_hidIdCorretor').value;
							
							portal_imobiliarias_MasterPage.gravaRegiaoCookie(regiao,idCorretor).value
							window.location = "default.aspx"
						},
						
						alteraRegiaoLink : function(regiao){
							document.getElementById('ctl00_dropRegioesHeader').value = regiao;
							var regiao = regiao ;
							var idCorretor = document.getElementById('ctl00_hidIdCorretor').value;
							
							portal_imobiliarias_MasterPage.gravaRegiaoCookie(regiao,idCorretor).value
							window.location = "default.aspx"
						},
						
						
						alteraEspelho : function(){
							var regiao = document.getElementById('ctl00_dropRegioesHeader').value;
							
							
							switch(regiao) {
								case "SP":
								document.getElementById("userEspelho").value="imob_integracao"
								document.getElementById("passEspelho").value="fs39c77"
								break;
								
								case "RJ":
								document.getElementById("userEspelho").value="integracao_rj"
								document.getElementById("passEspelho").value="zax185"
								break;
								
								case "MG":
								document.getElementById("userEspelho").value=""
								document.getElementById("passEspelho").value=""
								break;
							}
							
						},
						
						carregaDropRegioesInicia : function(){
							idCidade = document.getElementById('ctl00_dropCidadeMenu').value;
							document.getElementById('ctl00_dropRegiaoMenu').value = 0;
							pi.menu.busca.carregaDropBairrosInicia();
							if(dtRegioes==null)
								portal_imobiliarias_MasterPage.carregaRegioesDT(pi.menu.busca.carregaDropRegioesIniciaRetorno);
							else
								pi.menu.busca.carregaDropRegioesFinaliza();
						},
						
						carregaDropRegioesIniciaRetorno : function(r){
							dtRegioes = r.value;			
							pi.menu.busca.carregaDropRegioesFinaliza();
							pi.menu.busca.carregaDropEmpreendimentosInicia();
						},
						
						carregaDropRegioesFinaliza : function(){
							var drop = document.getElementById('ctl00_dropRegiaoMenu');
							drop.length = 1;
							for(i=0;i<dtRegioes.Rows.length;i++){
								var row = dtRegioes.Rows[i];
								if(row.idCidade==idCidade || idCidade==0){
								  var novo = document.createElement("option");
								  novo.setAttribute("id", "opcoes");
								  novo.value = row.idRegiao;
								  novo.text  = row.regiao;
								  drop.options.add(novo);
								}
							}
							if(drop.length==1)
								document.getElementById('ctl00_dropRegiaoMenu').disabled = true;
							else
								document.getElementById('ctl00_dropRegiaoMenu').disabled = false;
						},
						
						carregaDropBairrosInicia : function(){
							idRegiao = document.getElementById('ctl00_dropRegiaoMenu').value;
							if(dtBairros==null)
								portal_imobiliarias_MasterPage.carregaBairrosDT(pi.menu.busca.carregaDropBairrosIniciaRetorno);
							else
								pi.menu.busca.carregaDropBairrosFinaliza();
						},
						
						carregaDropBairrosIniciaRetorno : function(r){
							dtBairros = r.value;			
							pi.menu.busca.carregaDropBairrosFinaliza();
							pi.menu.busca.carregaDropEmpreendimentosInicia();
						},
						
						carregaDropBairrosFinaliza : function(){
							var drop = document.getElementById('ctl00_dropBairroMenu');
							drop.length = 1;
							for(i=0;i<dtBairros.Rows.length;i++){
								var row = dtBairros.Rows[i];
								if((row.idRegiao==idRegiao || idRegiao==0) && (row.idCidade==idCidade || idCidade==0)){
								  var novo = document.createElement("option");
								  novo.setAttribute("id", "opcoes");
								  novo.value = row.bairro;
								  novo.text  = row.bairro;
								  drop.options.add(novo);
								}
							}
						},
						
						carregaDropEmpreendimentosInicia : function(){
							bairro = document.getElementById('ctl00_dropBairroMenu').value;
							if(dtEmpreendimentos==null)
								portal_imobiliarias_MasterPage.carregaEmpreendimentosDT(1,pi.menu.busca.carregaDropEmpreendimentosIniciaRetorno);
							else
								pi.menu.busca.carregaDropEmpreendimentosFinaliza();
						},
						
						carregaDropEmpreendimentosIniciaRetorno : function(r){
							dtEmpreendimentos = r.value;			
							pi.menu.busca.carregaDropEmpreendimentosFinaliza();
						},
						
						carregaDropEmpreendimentosFinaliza : function(){
							var drop = document.getElementById('ctl00_dropEmpreendimentoMenu');
							drop.length = 1;
							for(i=0;i<dtEmpreendimentos.Rows.length;i++){
								var row = dtEmpreendimentos.Rows[i];
								if((row.bairro==bairro || bairro=='') && (row.idRegiao==idRegiao || idRegiao==0) && (row.idCidade==idCidade || idCidade==0)){
								  var novo = document.createElement("option");
								  novo.setAttribute("id", "opcoes");
								  novo.value = row.idEmpreendimento;
								  novo.text  = row.nome;
								  drop.options.add(novo);
								}
							}
						},
						
						enviaBusca : function(){
							var idCidade = document.getElementById('ctl00_dropCidadeMenu').value;
							var idRegiao = document.getElementById('ctl00_dropRegiaoMenu').value;
							var bairro = document.getElementById('ctl00_dropBairroMenu').value;
							
							window.location = 'busca_resultado.aspx?c=' + idCidade +'&r=' + idRegiao +'&b=' + bairro;
						},
						
						redirecionaEmpreendimento : function(idEmpreendimento){
							if(idEmpreendimento!=0)
								window.location = 'empreendimentos.aspx?e=' + idEmpreendimento;
						}
						
					};
				}(),
				////////////////////////////////BUSCA////////////////////
				
				////////////////////////////////CALENDARIO DE EVENTOS////////////////////
				calendario : function(){
					var mesAtual = -1;
					var anoAtual = 0;
					return{
						alteraMes : function(acao){
							if(mesAtual==-1){
								mesAtual = parseInt(document.getElementById('ctl00_hidNumMes').value);	
								anoAtual = parseInt(document.getElementById('ctl00_hidNumAno').value);
							}
							mesAtual+=acao;
							if(mesAtual==0){
								mesAtual=12;
								anoAtual+=-1;
							}else if(mesAtual==13){
								mesAtual=1;
								anoAtual+=-1;
							}
							document.getElementById('ctl00_divCalendarioMenu').innerHTML = 'Carregando...';
							var data = anoAtual + '-' + mesAtual + '-1';
							document.getElementById('ctl00_liNomeMesMenu').innerHTML = funcoes.retornaMesString(mesAtual).value;
							
							var uf = document.getElementById('ctl00_dropRegioesHeader').value
							portal_imobiliarias_MasterPage.montaHTMLCalendario(data,uf,pi.menu.calendario.alteraMesRetorno);
						},
						
						alteraMesRetorno : function(r){
							document.getElementById('ctl00_divCalendarioMenu').innerHTML = r.value;	
						}
					};
				}()
				////////////////////////////////CALENDARIO DE EVENTOS////////////////////
				
			};
		}(),
		
		////////////////////////////////MENU/////////////////////
		
		////////////////////////////////ENVIO NEWS/////////////////////
		
		newsletter : function(){
			var assuntoEnvio = '';
			var emailsEnvio = '';
			var textoEnvio = '';
			return {
				
				valida : function(){
					assuntoEnvio = document.getElementById('txtAssuntoEnvio').value;
					emailsEnvio = document.getElementById('txtEmailsEnvio').value;
					textoEnvio = document.getElementById('txtTextoEnvio').value;
					var validacao = true
					
					if(assuntoEnvio==''){
						validacao = false;
						document.getElementById('txtAssuntoEnvio').style.backgroundColor = '#ffe2e2';
					}else
						document.getElementById('txtAssuntoEnvio').style.backgroundColor = 'white';
						
					if(emailsEnvio==''){
						validacao = false;
						document.getElementById('txtEmailsEnvio').style.backgroundColor = '#ffe2e2';
					}else
						document.getElementById('txtEmailsEnvio').style.backgroundColor = 'white';
						
					if(textoEnvio==''){
						validacao = false;
						document.getElementById('txtTextoEnvio').style.backgroundColor = '#ffe2e2';
					}else
						document.getElementById('txtTextoEnvio').style.backgroundColor = 'white';
						
					
					return validacao;
				},
				
				envia : function(){
					var validacao = pi.newsletter.valida();
					if(validacao){
						document.getElementById('divFormEnvio').style.display = 'none';
						portal_imobiliarias_envie_email.cadastraEnvio(assuntoEnvio,emailsEnvio,textoEnvio,document.getElementById('ctl00_contentMain_hidIdEmpreendimento').value,pi.newsletter.enviaRetorno);
					}else{
						document.getElementById('lblValidacaoEnvio').style.visibility = '';
					}
				},
				
				enviaRetorno : function(r){
					alert(r.value);
					document.getElementById('divSucessoEnvio').style.display = '';
				}
				
				
			};
		}(),
		
		////////////////////////////////ENVIO NEWS/////////////////////
		
		////////////////////////////////COMUNICADOS/////////////////////
		comunicados : function(){
			return{
				busca : function(){
					var valorBuscado = document.getElementById('txtBuscaComunicados').value;
					var idEmpreendimento=document.getElementById('ctl00_contentMain_dropEmpreendimentosComunicados').value;
					var pg=1;
					
					portal_imobiliarias_comunicados.carregaLista(valorBuscado,idEmpreendimento,pg,pi.comunicados.buscaRetorno);
					
				},
				
				buscaRetorno : function(r){
					document.getElementById('ctl00_contentMain_divConteudoComunicados').innerHTML = r.value;
				},
				
				abrirDetalhe : function(div){
					conteudo = document.getElementById(div).innerHTML;
					document.getElementById('contLightBox').innerHTML = "";
					
					document.getElementById('contLightBox').innerHTML = conteudo;
					lightbox.abrir('divRegulamentoBelaCintra');
				}
				
			};
		}(),
		////////////////////////////////COMUNICADOS/////////////////////
		
		////////////////////////////////ARQUIVOS/////////////////////
		arquivos : function(){
			return{
				busca : function(){
					var valorBuscado = document.getElementById('txtBuscaArquivos').value;
					var idEmpreendimento=document.getElementById('ctl00_contentMain_dropEmpreendimentosArquivos').value;
					var uf = document.getElementById('ctl00_dropRegioesHeader').value
					var tipo = document.getElementById('ctl00_contentMain_hidTipoArquivo').value;
					var pg=1;
					classPIArquivos.montaHTMLLista(tipo,pg,10,idEmpreendimento,uf,valorBuscado,pi.arquivos.buscaRetorno);
				},
				
				buscaRetorno : function(r){
					document.getElementById('ctl00_contentMain_divConteudoArquivos').innerHTML = r.value;
				},
				
				baixaArquivo : function(arquivo){
					window.location	= '_INC/download.aspx?pasta=_ARQUIVOS&arquivo=' + arquivo;
				}
			};
		}(),
		////////////////////////////////ARQUIVOS/////////////////////
		
		////////////////////////////////CONTATO/////////////////////
		contato : function(){
			var assuntoContato='';
			var mensagemContato='';
			return{
				
				exibeTelefone : function(){
					var estado = document.getElementById('ctl00_dropRegioesHeader').value;
					var telefone= ""
					
					switch (estado){
						case "SP":
							telefone = "(11) 3043-0123";
							break;
							
						case "RJ":
							telefone = "(11) 3043-0123";
							break;
						
						case "MG":
							telefone = "";
							break;
					}

					document.write(telefone);
				},
				
				valida : function(){
					assuntoContato = document.getElementById('txtAssuntoContato').value;
					mensagemContato=document.getElementById('txtMensagemContato').value;
					var validacao = true
					
					if(assuntoContato==''){
						validacao = false;
						document.getElementById('txtAssuntoContato').style.backgroundColor = '#ffe2e2';
					}else
						document.getElementById('txtAssuntoContato').style.backgroundColor = 'white';
						
					if(mensagemContato==''){
						validacao = false;
						document.getElementById('txtMensagemContato').style.backgroundColor = '#ffe2e2';
					}else
						document.getElementById('txtMensagemContato').style.backgroundColor = 'white';
						
					return validacao;
				},
				
				envia : function(r){
					var validacao = pi.contato.valida();

					if(validacao){
						var conteudo='';
						conteudo +='Corretor: ' + document.getElementById('ctl00_hidNomeCorretor').value + '<br>';
						conteudo +='E-mail: ' + document.getElementById('ctl00_hidEmailCorretor').value + '<br>';
						conteudo +='Mensagem: ' + mensagemContato + '<br>';
						
						funcoes.sendmail('bruno@playercom.net',document.getElementById('ctl00_hidEmailCorretor').value,assuntoContato,conteudo,'');
						funcoes.sendmail('parceria@evenvendas.com.br',document.getElementById('ctl00_hidEmailCorretor').value,assuntoContato,conteudo,'');
						funcoes.sendmail('campana@evenvendas.com.br',document.getElementById('ctl00_hidEmailCorretor').value,assuntoContato,conteudo,'');
						funcoes.sendmail('evenmails@gmail.com',document.getElementById('ctl00_hidEmailCorretor').value,assuntoContato,conteudo,'');
						document.getElementById('divFormContato').style.display = 'none';
						document.getElementById('divSucessoContato').style.display = '';
						/*
						Seguem as informações para portal Even Corretores.v
						Fale Conosco
						e-mail do Fale Conosco: parceria@evenvendas.com.br
						
						Telefones:
						(11) 3043-0123 - Renata (atendimento)
						*/
					}
				}
			};
		}()
		////////////////////////////////CONTATO/////////////////////
	};
}();

