function gE(id){return document.getElementById(id)};

function init()
{
	mask = new DVDarkMask(document.body,"carregando arquivos...");
	$().ready(function(){$('#coda-slider-1').codaSlider({autoSlide: true,autoSlideInterval: 4000,autoSlideStopWhenClicked: true})});

	//mask = new DVDarkMask(document.body,"enviando mensagem...");
	
	var loc = document.location.pathname;
		loc = loc.substring(loc.lastIndexOf('/')+1,loc.lastIndexOf('.'));
	switch(loc)
	{
		case 'localizacao':
			break;
		case 'suite_noivos':
			$(function() {
				$('#cfotos_eventos a').lightBox();
			});		
			break;
		case 'lazer':
			$(function() {
				$('#cfotos_lazer a').lightBox();
			});		
			break;
		case 'convencoes_e_eventos':
			$(function() {
				$('#cfotos_eventos a').lightBox();
			});		
			break;
		case 'restaurante':
			$(function() {
				$('#cfotos_cafe a').lightBox();
			});		
			$(function() {
				$('#cfotos_bar a').lightBox();
			});		
			break;
		case 'apartamentos':
			$(function() {
				$('#cont_default a').lightBox();
			});					
			break;
		case 'contato':
			tel = new DVFormattedField(gE('c_fone'),'(##)####-####',function()
			{
				gE('c_email').focus();
			});
			break;
		default:
			loc = 'index';
			query = new DVQuery();
			toupdate = false;
			dti = new DVCalendar(gE('dtini'));
			dtf = new DVCalendar(gE('dtfim'));
			dtn = new DVFormattedField(gE('dtnasc'),"##/##/####",function(){gE('sexo').focus();},function(){});
			fone = new DVFormattedField(gE('fone'),"(##)####-####",function(){gE('celular').focus();},function(){});
			celular = new DVFormattedField(gE('celular'),"(##)####-####",function(){gE('email').focus();},function(){});

			var bcad = new DVButton(gE('comp_cad'),"Completar dados para pré-reserva",[9,3],function()
			{
				if(dti.dt == null || dtf.dt == null )
				{
					alert('Preencha os campos de entrada e saída corretamente!');
					return false;
				}
				
				if(dtf.dt.getTime() < dti.dt.getTime())
				{
					alert('A data de saída não pode ser menor que a data de entrada!');
					return false;
				}
				
				gE('datacad').style.display='block';						
				
			});
			
			cpf = new DVFormattedField(gE('cpf'),'###.###.###-##',checaCPF,function(){});
			bcad.alignTo('right');
			bcad.obj.style.margin='5px 5px 0 0';	
			
			var bconf = new DVButton(gE('conf'),"Solicitar pré-reserva",[9,3],function()
			{
				mask.setVisible(true);
				mask.setText('validando dados, aguarde...');

				if(checkForm())
				{
					var arr = [];
					var res = [];
					arr.push({field:"NOME",value:gE('nome').value});
					arr.push({field:"NACIONALIDADE",value:gE('nacionalidade').value});
					arr.push({field:"PROFISSAO",value:gE('ocupa').value});
					arr.push({field:"SEXO",value:gE('sexo').value});
					if(dtn.dt != null)
						arr.push({field:"DTNASC",value:dtn.dt.toDB()});
					arr.push({field:"TELEFONE",value:gE('fone').value});
					arr.push({field:"CELULAR",value:gE('celular').value});
					arr.push({field:"EMAIL",value:gE('email').value,uppercase:false});
					
					res.push({field:"APTO",value:gE('apto').value});
					res.push({field:"ENTRADA",value:dti.dt.toDB()});
					res.push({field:"SAIDA",value:dtf.dt.toDB()});
					res.push({field:"QUARTOS",value:gE('quartos').value});
					res.push({field:"ADULTOS",value:gE('adultos').value});
					res.push({field:"CRIANCAS",value:gE('criancas').value});
					res.push({field:"MOTIVO",value:gE('motivo').value});
					res.push({field:"IDH",value:gE('cpf').value.normalize()});
					
					
					if(!toupdate)
					{
						arr.push({field:"CPF",value:gE('cpf').value.normalize()});
						
						query.insert('hospede',arr,null,function(ret)
						{
							if(ret == 1)
							{
								//alert(res);
							}
							else
							{
								alert('ocorreram problemas, a solicitação não pode ser enviada!\nTente novamente mais tarde.');
								mask.setVisible(false);
							}

							gE('datacad').style.display='none';
							gE('form1').reset();
							toupdate = false;
							gE('cpf').disabled = false;
						});
					}
					else
					{
						mask.setText('enviando solicitação de reserva, aguarde...');
						query.update('hospede',arr,"WHERE CPF ='"+gE('cpf').value.normalize()+"'",function(ret)
						{
							if(ret >=0)
							{
								mask.setVisible(true);
								query.insert('reserva',res,null,function(ret)
								{
									
									var sm = new Ajax("sendmail.php",function(ret)
									{
										if(parseInt(ret))
										{
											alert("Solicitação de reserva enviada!\nO mais breve possível entraremos em contato para confirmação.");
											gE('form1').reset();
											toupdate = false;
											gE('datacad').style.display='none';
											gE('cpf').disabled = false;
										}
										else
											alert("Ocorreram problemas, a solicitação de reserva não pode ser enviada\n tente novamente mais tarde!");
									});
									sm.vars.push("ass=Nova Solicitação de Reserva");
									sm.vars.push("msg=compor texto com dados");
									sm.vars.push("mail="+gE('email').value);
									sm.vars.push("fone="+gE('fone').value+' / '+gE('celular').value);

									sm.vars.push("entrada="+dti.obj.value);
									sm.vars.push("saida="+dtf.obj.value);
									sm.vars.push("quartos="+gE('quartos').value);
									sm.vars.push("adultos="+gE('adultos').value);
									sm.vars.push("criancas="+gE('criancas').value);
									sm.vars.push("apto="+gE('apto').value);
									sm.vars.push("motivo=reserva");
									
									sm.requestText(null,mask);
								});
							}
							else
							{
								alert('ocorreram problemas, a solicitação não pode ser enviada!\nTente novamente mais tarde.');
								mask.setVisible(false);
							}
						});
					}
					
				}
				else
					mask.setVisible(false);
			});
			bconf.alignTo('right');
			bconf.obj.style.margin='0 5px 0 0';
			if(gE('reserva').getAttribute('sts')=='true')
			{
				gE('datacad').style.display='block';
				gE('dtini').focus();
				window.scrollBy(0,400);
			}
			
			break;
	}
	//gE('_'+loc).setAttribute('selected',true);
	//alteraStatusMsn();
}

function checaCPF()
{
	var check = false;
	var key =gE('cpf').value.normalize();

	if(Util.checkCpf(key))
	{
		mask.setVisible(true);
		mask.setText('verificando dados, aguarde...');
		var a = new Ajax("source/shared/sql.php",function(ret,_t)
		{
			if(parseFloat(ret))
			{
				var _this = _t;
				var a = new Ajax("reload.php",function(ret)
				{
					try
					{
						var ret = eval(ret);
						ret = ret[0]; //JSON

						gE('nome').value = ret.nome;
						gE('nacionalidade').value = ret.nacionalidade;
						gE('ocupa').value = ret.profissao;
						dtn.obj.value = ret.dtnasc;
						gE('sexo').value = ret.sexo;
						gE('fone').value = ret.fone;
						gE('celular').value = ret.celular;
						gE('email').value = ret.email;
						toupdate = true;
						gE('cpf').disabled = true;
						
					}
					catch(e)
					{
						alert('ocorreram problemas, o registro não pode ser carregado!');
					}
					finally
					{
						mask.setVisible(false);
					}
				});
				a.vars.push("ID="+key);
				//a.showmask = false;
				a.requestText();
			}
			else
				gE('nome').focus();
		});

		a.vars.push("SQL=".concat("select STS from hospede where CPF = '"+key+"' and STS = 'A'"));
		//a.showmask = false;
		a.requestText();
	}
	else
	{
		alert('Campo [CPF] inválido!');
		gE('cpf').focus();
		return false;
	}	
}



function checkForm()
{
	if(dti.dt == null || dtf.dt == null )
	{
		alert('Preencha os campos de entrada e saída corretamente!');
		return false;
	}
	
	if(dtf.dt.getTime() < dti.dt.getTime())
	{
		alert('A data de saída não pode ser menor que a data de entrada!');
		return false;
	}

	gE('datacad').style.display='block';

	if(!cpf.complete)
	{	
		if(!Util.checkCpf(cpf.obj.value.normalize()))
		{
			alert('Campo [CPF] inválido!');
			cpf.obj.focus();
			return false;
		}
	}

	if(gE('nome').value == '')
	{
		alert('Campo [Nome] inválido!');
		gE('nome').focus();
		return false;
	}

	if(gE('fone').value == '')
	{
		alert('Campo obrigatório [ Telefone ] em branco!');
		gE('fone').focus();
		return false;
	}
	
	return true;
}

function checaFormEmail()
{
	
	if(gE('c_nome').value == '')
	{
		alert('campo [nome] em branco!');
		gE('c_nome').focus();
		return false;
	}
	
	if(gE('c_assunto').value == '')
	{
		alert('campo [assunto] em branco!');
		gE('c_assunto').focus();
		return false;
	}

	if(gE('c_msg').value == '')
	{
		alert('campo [mensagem] em branco!');
		gE('c_msg').focus();
		return false;
	}
	return true;
}
	
function enviaEmail()
{
	if(checaFormEmail())
	{
		mask.setVisible(true);
		mask.setText('enviando mensagem...');
		var sm = new Ajax("sendmail.php",function(ret)
		{
			mask.setVisible(false);
			if(parseInt(ret))
			{
				alert("Mensagem enviada com sucesso!\n\nResponderemos o mais breve possível!");
				gE('c_form').reset();
			}
			else
				alert("Ocorreram problemas, a mensagem não pode ser enviada\n tente novamente mais tarde!");
		});
		mask.setVisible(true);
		sm.vars.push("ass="+gE('c_assunto').value);
		sm.vars.push("msg="+gE('c_msg').value);
		sm.vars.push("mail="+gE('c_email').value);
		sm.vars.push("fone="+gE('c_fone').value);
		sm.vars.push("nome="+gE('c_nome').value);
		sm.requestText(null,mask);
	}
}

function alteraStatusMsn()
{
	var img = gE('sts_msn');
}

	
