// JavaScript Document
cadastroJS = {
	idCadastro : 0,
	inicia : function(){
		cadastroJS.carregaCmbEstado();
		cadastroJS.idCadastro = document.getElementById('headerMenu1_hidIdCadastro').value;
		if(cadastroJS.idCadastro!=0){
			cadastroJS.carregaDadosForm();	
		}
	},

	carregaDadosForm : function(){
		cadastro.carregaDadosId(cadastroJS.idCadastro,cadastroJS.carregaDadosFormRetorno);
	},
	
	carregaDadosFormRetorno : function(retorno){
		var dt = retorno.value;
		if(dt!=null && typeof(dt) == "object"){
			var row = dt.Rows[0];	
			document.getElementById('txtCPF').value = row.cpf;
			document.getElementById('txtNome').value = row.nome;
			if(row.sexo=='M')
				document.getElementById('radioSexoM').checked=true;
			if(row.sexo=='F')
				document.getElementById('radioSexoF').checked=true;
			document.getElementById('txtNascimentoDia').value = row.dtNasc.getDate();
			document.getElementById('txtNascimentoMes').value = row.dtNasc.getMonth()+1;
			if(row.dtNasc.getYear()<100)
				document.getElementById('txtNascimentoAno').value = row.dtNasc.getYear()+1900;
			else
				document.getElementById('txtNascimentoAno').value = row.dtNasc.getYear();
			var cepQ = row.cep.toString().split('-');
			document.getElementById('txtCEP').value = cepQ[0];
			document.getElementById('txtCEP2').value = cepQ[1];
			document.getElementById('txtEndereco').value = row.endereco;
			document.getElementById('txtNumero').value = row.numero;
			document.getElementById('txtComplemento').value = row.complemento;
			document.getElementById('txtBairro').value = row.bairro;
			document.getElementById('cmbEstado').value = row.estado;
			cadastroJS.carregaCmbCidade(row.estado);
			
			var telQ = row.telefone.toString().split('-');
			document.getElementById('txtTelDDD').value = telQ[0];
			document.getElementById('txtTel').value = telQ[1];
			
			var celQ = row.celular.toString().split('-');
			document.getElementById('txtCelDDD').value = celQ[0];
			document.getElementById('txtCelular').value = celQ[1];
			
			document.getElementById('txtEmail').value = row.email;
			document.getElementById('txtSenha').value = row.senha;
			document.getElementById('txtConfSenha').value = row.senha;
			
			document.getElementById('chkEmail').checked = row.flgNewsEmail;
			document.getElementById('chkCelular').checked = row.flgNewsCelular;
			setTimeout("cadastroJS.selecionaCidade('" + row.cidade + "')",100);
		}
	},
	
	selecionaCidade : function(cidade){
		document.getElementById('cmbCidade').value = cidade;
	},
	
	carregaCmbEstado : function(){
		cadastro.carregaEstados(cadastroJS.carregaCmbEstadoRetorno);
	},
	
	carregaCmbEstadoRetorno : function(retorno){
		var dt = retorno.value;
		if(dt!=null && typeof(dt) == "object"){
			var drop = document.getElementById('cmbEstado');
			drop.length = 1;
			for(i=0;i<dt.Rows.length;i++){
				var row = dt.Rows[i];
				var novo = document.createElement("option");
			    novo.setAttribute("id", "opcoes");
			    novo.value = row.uf;
			    novo.text  = row.uf;
				drop.options.add(novo);
			}			
		}
	},
	
	carregaCmbCidade : function(uf){
		document.getElementById('cmbEstado').value = uf;
		cadastro.carregaCidades(uf,cadastroJS.carregaCmbCidadeRetorno);
	},
	
	carregaCmbCidadeRetorno : function(retorno){
		var dt = retorno.value;
		if(dt!=null && typeof(dt) == "object"){
			var drop = document.getElementById('cmbCidade');
			drop.length = 1;
			for(i=0;i<dt.Rows.length;i++){
				var row = dt.Rows[i];
				var novo = document.createElement("option");
			    novo.setAttribute("id", "opcoes");
			    novo.value = row.cidade;
			    novo.text  = row.cidade;
				drop.options.add(novo);
			}			
		}
	},
	
	camposArray : '',
	
	retornaAtributo : function(obj,atributo){
		var valor= '';
		for(i=0;i<obj.attributes.length;i++){
			//alert(obj.attributes[i].name + '-' + atributo);
				if(obj.attributes[i].name.toString()==atributo || obj.attributes[i].name.toString()==atributo.toString().toLowerCase())
					valor = obj.attributes[i].value;
		}
		return valor;
	},
	
	valida : function(){

		var obj;
		var flgValidacao = 1;
		var camposValidacao = '';
		var camposValidacaoMSG = '';
		cadastroJS.camposArray = '';


		if(document.getElementById('txtCPF')){
			obj = document.getElementById('txtCPF');
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + obj.value + '|';
			if(!FuncoesGerais.checaCPF(obj.value)){
				flgValidacao=0;
				camposValidacao+='txtCPF' + '___';
				camposValidacaoMSG+='CPF' + '___';
			}
		}
		
		if(document.getElementById('txtNome')){
			obj = document.getElementById('txtNome');
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + obj.value + '|';
			if(obj.value==''){
				flgValidacao=0;
				camposValidacao+='txtNome' + '___';
				camposValidacaoMSG+='Nome' + '___';
			}
		}
		
		if(document.getElementById('radioSexoM')){
			obj = document.getElementById('radioSexoM');
			var valor='';
			if(obj.checked)
				valor = obj.value;
			else if(document.getElementById('radioSexoF').checked)
				valor = document.getElementById('radioSexoF').value;
				
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + valor + '|';
			if(valor==''){
				flgValidacao=0;
				camposValidacao+='radioSexoM' + '___';
				camposValidacao+='radioSexoF' + '___';
				camposValidacaoMSG+='Sexo' + '___';
			}
		}
		
		if(document.getElementById('txtNascimentoDia')){
			obj = document.getElementById('txtNascimentoDia');
			var valor = '';
			valor =  document.getElementById('txtNascimentoAno').value + '-' +  document.getElementById('txtNascimentoMes').value + '-' +  document.getElementById('txtNascimentoDia').value;
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + valor + '|';
			if(obj.value==''){
				flgValidacao=0;
				camposValidacao+='txtNascimentoAno' + '___';
				camposValidacao+='txtNascimentoMes' + '___';
				camposValidacao+='txtNascimentoDia' + '___';
				camposValidacaoMSG+='Data de nascimento' + '___';
			}
		}
		
		if(document.getElementById('txtCEP')){
			obj = document.getElementById('txtCEP');
			var valor = '';
			valor =  document.getElementById('txtCEP').value + '-' +  document.getElementById('txtCEP2').value;
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + valor + '|';
			
		}
		
		if(document.getElementById('txtEndereco')){
			obj = document.getElementById('txtEndereco');
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + obj.value + '|';
			if(obj.value==''){
				flgValidacao=0;
				camposValidacaoMSG += 'Endereco' + '___';
				camposValidacao+= 'txtEndereco' + '___';
				
			}
		}
		
		if(document.getElementById('txtNumero')){
			obj = document.getElementById('txtNumero');
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + obj.value + '|';
			if(obj.value==''){
				flgValidacao=0;
				camposValidacao+='txtNumero' + '___';
				camposValidacaoMSG+='Número' + '___';
			}
		}
		
		if(document.getElementById('txtComplemento')){
			obj = document.getElementById('txtComplemento');
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + obj.value + '|';
		}
		
		if(document.getElementById('txtBairro')){
			obj = document.getElementById('txtBairro');
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + obj.value + '|';
			if(obj.value==''){
				flgValidacao=0;
				camposValidacao+='txtBairro' + '___';
				camposValidacaoMSG+='Bairro' + '___';
			}
		}
		
		if(document.getElementById('cmbEstado')){
			obj = document.getElementById('cmbEstado');
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + obj.value + '|';
			if(obj.value==''){
				flgValidacao=0;
				camposValidacao+='cmbEstado' + '___';
				camposValidacaoMSG+='Estado' + '___';
			}
		}
		
		if(document.getElementById('cmbCidade')){
			obj = document.getElementById('cmbCidade');
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + obj.value + '|';
			if(obj.value==''){
				flgValidacao=0;
				camposValidacao+='cmbCidade' + '___';
				camposValidacaoMSG+='Cidade' + '___';
			}
		}
		
		if(document.getElementById('txtTelDDD')){
			obj = document.getElementById('txtTelDDD');
			var valor = '';
			valor =  document.getElementById('txtTelDDD').value + '-' +  document.getElementById('txtTel').value;
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + valor + '|';
			if(obj.value==''){
				flgValidacao=0;
				camposValidacao+='txtTelDDD' + '___';
				camposValidacao+='txtTel' + '___';
				camposValidacaoMSG+='Telefone' + '___';
			}
		}
		
		if(document.getElementById('txtCelDDD')){
			obj = document.getElementById('txtCelDDD');
			var valor = '';
			valor =  document.getElementById('txtCelDDD').value + '-' +  document.getElementById('txtCelular').value;
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + valor + '|';
			
		}
		
		if(document.getElementById('txtEmail')){
			obj = document.getElementById('txtEmail');
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + obj.value + '|';
			if(!FuncoesGerais.ValidaEmail(obj.value)){
				flgValidacao=0;
				camposValidacao+='txtEmail' + '___';
				camposValidacaoMSG+='E-mail' + '___';
			}
		}
		
		if(document.getElementById('txtSenha')){
			obj = document.getElementById('txtSenha');
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + obj.value + '|';
			if(obj.value==''){
				flgValidacao=0;
				camposValidacao+='txtSenha' + '___';
				camposValidacaoMSG+='Senha' + '___';
			}
		}
		
		if(document.getElementById('txtSenha') && document.getElementById('txtConfSenha')){
			if(document.getElementById('txtSenha').value!=document.getElementById('txtConfSenha').value){
				flgValidacao=0;
				camposValidacao+='txtConfSenha' + '___';
				camposValidacaoMSG+='Confirmação de Senha' + '___';
			}
		}
		
		if(document.getElementById('chkEmail')){
			obj = document.getElementById('chkEmail');
			var valor = 0;
			if(obj.checked)
				valor=1;
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + valor + '|';
			
		}
		
		if(document.getElementById('chkCelular')){
			obj = document.getElementById('chkCelular');
			var valor = 0;
			if(obj.checked){
				valor=1;
			}
			cadastroJS.camposArray += cadastroJS.retornaAtributo(obj,'cDB') + '___' + cadastroJS.retornaAtributo(obj,'typeDB') + '___' + valor + '|';
			
		}
		//INSERINDO NO ARRAY FIM
		
		//REFERENTE A ESTE CADASTRO
		valor =  document.getElementById('txtCelDDD').value + '-' +  document.getElementById('txtCelular').value;
		if(document.getElementById('chkCelular').checked==true && valor.length<8){
			camposValidacao+='txtCelDDD' + '___';
			camposValidacao+='txtCelular' + '___';
			camposValidacaoMSG+='Celular' + '___';
			lightbox.abrir('divAvisoCelular', '000');
		}
		
		return flgValidacao + '-' + camposValidacao + '-' + camposValidacaoMSG;
	},
	
	
	
	envia : function(){
		var validado = cadastroJS.valida();
		validadoQuebrado = validado.split('-');

		if(validadoQuebrado[0]==1){
			if(cadastroJS.idCadastro==0){
				var idCadastro = cadastro.inserir(cadastroJS.camposArray).value;
				if(idCadastro.indexOf('KEY')==-1){
					_inc_header.geraCookie(idCadastro).value;
					window.location.href = 'cadastrarApostas.aspx';
				}else{
					alert('Esse cpf já foi cadastrado');	
				}
			}else{
				alert(cadastro.atualizar(cadastroJS.idCadastro,cadastroJS.camposArray).value);
			}
			document.getElementById('pValidacao').style.visibility = 'hidden';
		}else{
			//pValidacao
			document.getElementById('pValidacao').style.visibility = '';
			var inputs = document.getElementById('formCadastro').getElementsByTagName('input');
			var selects = document.getElementById('formCadastro').getElementsByTagName('select');
			for(i=0;i<inputs.length;i++){
				if(validadoQuebrado[1].indexOf(inputs[i].id)==-1)
					inputs[i].style.backgroundColor = 'white';
				else
					inputs[i].style.backgroundColor = '#ffe2e2';
			}
			for(i=0;i<selects.length;i++){
				if(validadoQuebrado[1].indexOf(selects[i].id)==-1)
					selects[i].style.backgroundColor = 'white';
				else
					selects[i].style.backgroundColor = '#ffe2e2';
			}
		}

	},
	
	logar : function(){
		var email = document.getElementById('txtEmail').value;
		var senha = document.getElementById('txtSenha').value;
		_inc_header.login(email,senha,cadastroJS.logarRetorno);
	},
	
	logarRetorno : function(retorno){
		var dt = retorno.value;
		if(dt!=null && typeof(dt) == "object"){
			if(dt.Rows.length==0){
				document.getElementById('pValidacao').style.visibility='';
			}else{
				var row = dt.Rows[0];
				_inc_header.geraCookie(row.idCadastro).value;
				window.location.href = 'cadastrarApostas.aspx';
			}
		}
	},
	
	abreEsqueceu : function(){
		lightbox.fechar();
		lightbox.abrir('divEsqueceuSenha', '000');
	},
	
	voltar : function(){
		lightbox.fechar();
		lightbox.abrir('divFacaLogin', '000');
	},
	
	esqueciEnviar : function(){
		var email = document.getElementById('txtEmailEsqueci').value;
		var retorno = _inc_header.enviaIndique(email).value;
		if(retorno=='ok'){
			lightbox.fechar();
			lightbox.abrir('divFacaLoginStatus', '000');
		}else{
			document.getElementById('pValidacaoEsqueci').style.visibility='';
		}
	}
	
}