// JavaScript Document

function validate() {
	var pass = true;
	var error = '';
	
	if ($('#txtNome').val() == '') {
		pass = false;
		error = error + "\n- Nome é obrigatorio!";
	}
	
	if ($('#txtEndereco').val() == '') {
		pass = false;
		error = error + "\n- Endereço é obrigatorio!";
	}

	if ($('#txtEmail').val() == '') {
		pass = false;
		error = error + "\n- Email é obrigatorio!";
	}
	
	if ($('#txtTelefone').val() == '') {
		pass = false;
		error = error + "\n- Telefone é obrigatorio!";
	}
	
	if (!pass) {
		alert( 'Por favor verifique os erros abaixo: ' + error );
	}
	
	return pass;
	
}

function getPageSize() {
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	// all but Explorer Mac
	else if (document.body.scrollHeight > document.body.offsetHeight) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
	else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	// all except Explorer
	if (self.innerHeight) {	
		if (document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth; 
		}
		else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	}
	// Explorer 6 Strict Mode
	else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	// other Explorers
	else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if (yScroll < windowHeight) {
		pageHeight = windowHeight;
	}
	else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if (xScroll < windowWidth) {
		pageWidth = xScroll;		
	}
	else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

$(document).ready(function(){
	var pageSize = getPageSize();
	
	$('.dialogoEnviar').css('width', pageSize[0]);
	$('.dialogoEnviar').css('height', pageSize[1]);
	
	$("a[title='antecipeBtn']").click(function(){
		var id = $(this).attr('class');
		var title = $(this).attr('rel');
		
		$('#empreendimento_id').attr('value', id);
		$('#emp_t').text(title);
		$('.dialogoEnviar').css('visibility', 'visible');
		$('.dialogoInterna').css('visibility', 'visible');
	});
	
	$('#btnEnviar').click(function(){
		if (validate()) {
			$.ajax({
				url: 'ajax/cadastros.php',
				type: 'post',
				data: {
					acao: 'cadastrarAntecipe',
					emp: $('#empreendimento_id').val(),
					nome: $('#txtNome').val(),
					endereco: $('#txtEndereco').val(),
					email: $('#txtEmail').val(),
					telefone: $('#txtTelefone').val()
				},
				success: function (retorno) {
					if (retorno == 'true') {
						alert('Você foi cadastrado com sucesso!');
					}
					$('.dialogoEnviar').css('visibility', 'hidden');
					$('.dialogoInterna').css('visibility', 'hidden');
					$('#empreendimento_nome').removeAttr('value');
					$('#txtNome').removeAttr('value');
					$('#txtEmail').removeAttr('value');
					$('#txtTelefone').removeAttr('value');
				}
			});
		}
		return false;
	});
	
	$('#btnCancelar').click(function(){
		$('.dialogoEnviar').css('visibility', 'hidden');
		$('.dialogoInterna').css('visibility', 'hidden');
		$('#empreendimento_nome').removeAttr('value');
		$('#txtNome').removeAttr('value');
		$('#txtEmail').removeAttr('value');
		$('#txtTelefone').removeAttr('value');
		return false;
	});
	
	$('#btnFechar').click(function(){
		$('.dialogoEnviar').css('visibility', 'hidden');
		$('.dialogoInterna').css('visibility', 'hidden');
		$('#empreendimento_nome').removeAttr('value');
		$('#txtNome').removeAttr('value');
		$('#txtEmail').removeAttr('value');
		$('#txtTelefone').removeAttr('value');
		return false;
	});
});