// global variables //
var TIMER = 5;
var SPEED = 10;
var WRAPPER = 'content';

// calculate the current window width //
function pageWidth() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// calculate the current window height //
function pageHeight() {
	if(navigator.userAgent.indexOf("Firefox")!=-1)
		return document.height;
	else if(navigator.userAgent.indexOf("MSIE")!=-1)
		return document.body.clientHeight;
// return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

// calculate the current window vertical offset //
function topPosition() {
  return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

// calculate the position starting at the left of the window //
function leftPosition() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

// build/show the dialog box, populate the data and call the fadeDialog function //
function showDialog(title,message,type,autohide) {
  if(!type) {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialogmask = document.getElementById('dialog-mask');
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = leftPosition();
  var top = topPosition();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = message;
  var content = document.getElementById(WRAPPER);
  dialogmask.style.height = height+'px'; //content.offsetHeight + 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMER);
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }
}

// hide the dialog box //
function hideDialog() {
  var dialog = document.getElementById('dialog');
  clearInterval(dialog.timer);
  dialog.timer = setInterval("fadeDialog(0)", TIMER);
}

// fade-in the dialog box //
function fadeDialog(flag) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = document.getElementById('dialog');
  var value;
  if(flag == 1) {
    value = dialog.alpha + SPEED;
  } else {
    value = dialog.alpha - SPEED;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(dialog.timer);
    dialog.timer = null;
  } else if(value <= 1) {
    dialog.style.visibility = "hidden";
    document.getElementById('dialog-mask').style.visibility = "hidden";
    clearInterval(dialog.timer);
  }
}

function validaArtigo(form) {
	var titulo = form.titulo.value;
	var nome = form.nome.value;
	var arquivo = form.arquivo.value;

	if(arquivo == "") {
		showDialog('Atenção!','<center>Selecione o <b>Arquivo</b> do Artigo para anexar!</center>','warning',2);
		return false;
	}
	else if(arquivo != "") {
		var extensao = arquivo.substring(arquivo.length-3, arquivo.length).toLowerCase();
		if(extensao != "pdf") {
			showDialog('Atenção!','<center>Este <b>tipo de arquivo</b> é inválido!<br>O artigo deve estar em <b>PDF</b>.</center>','warning',3);
			return false;
		}
	}
	if(titulo == "") {
		showDialog('Atenção!','<center>Digite o <b>Título</b> do Artigo!</center>','warning',2);
		return false;
	}
	if(nome == "") {
		showDialog('Atenção!','<center>Digite o <b>Nome</b> do Autor do Artigo!</center>','warning',2);
		return false;
	}
}

function validaCadastroMostra(form) {
	var cpf = form.cpf.value;
	//Atribue a "cpf" o valor digitado sem os pontos e traços...
	cpf = cpf.substring(0,3) + cpf.substring(4,7) + cpf.substring(8,11) + cpf.substring(12,cpf.length);
	
	var dtNasci = (form.dataNascimento ? form.dataNascimento.value : "//");
	//"Explode" a data de nascimento em dia, mês e ano para validação...
	var dia = dtNasci.substring(0,2); var mes = dtNasci.substring(3,5); var ano = dtNasci.substring(6,dtNasci.length);

	if(form.name == "loginMostra") {
		if(cpf == "") {
			showDialog('Atenção!','<center><p>Você não informou seu <b>CPF</b>!<br>Digite-o para realizar o acesso.','warning',3);
			return false;
		}
		else if(!checacpf(cpf)) {
			showDialog('Atenção!','<center><p>Este <b>CPF</b> é inválido!<br>Verifique-o e tente novamente.','warning',3);
			return false;
		}
		if(dtNasci == "") {
			showDialog('Atenção!','<center><p>Você não informou sua <b>Data de Nascimento</b>!<br>Digite-a para realizar o acesso.','warning',3);
			return false;
		}
		else if(dia<=0 || dia>31 || mes<=0 || mes>12 || ano<=0 || ano<1909 || ano>1994) {
			showDialog('Atenção',"<center><p>Esta <b>Data de Nascimento</b> não é válida para o cadastro<br>devido ao <b>dia, mês ou ano</b> informados.<br>Verifique e tente novamente.",'warning',4);
			return false;
		}
	}
	else if(form.name == "cadastroMostra") {
		if(cpf == "") {
			showDialog('Atenção!','<center><p>Você não informou seu <b>CPF</b>!<br>Digite-o para realizar seu cadastro.','warning',3);
			return false;
		}
		else if(!checacpf(cpf)) {
			showDialog('Atenção!','<center><p>Este <b>CPF</b> é inválido!<br>Verifique-o e tente novamente.','warning',3);
			return false;
		}
	}
	else if(form.name=="newCadastro" || form.name=="verCadastro") {
		var nome = form.nome.value;
		var endereco = form.endereco.value;
		var telefone = form.telefone.value;
		var celular = form.celular.value;
		var email = form.email.value;
		var interesse = form.interesse.value;
		if(nome == "") {
			showDialog('Atenção!','<center><p>Você não informou seu <b>Nome</b>!<br>Digite-o para realizar seu cadastro.','warning',3);
			return false;
		}
		if(cpf == "") {
			showDialog('Atenção!','<center><p>Você não informou seu <b>CPF</b>!<br>Digite-o para realizar seu cadastro.','warning',3);
			return false;
		}
		else if(!checacpf(cpf)) {
			showDialog('Atenção!','<center><p>Este <b>CPF</b> é inválido!<br>Verifique-o e tente novamente.','warning',3);
			return false;
		}
		if(dtNasci == "") {
			showDialog('Atenção!','<center><p>Você não informou sua <b>Data de Nascimento</b>!<br>Digite-a para realizar seu cadastro.','warning',3);
			return false;
		}														//Mais de 100 ou menos de 15 anos...
		else if(dia<=0 || dia>31 || mes<=0 || mes>12 || ano<=0 || ano<1909 || ano>1994) {
			showDialog('Atenção!',"<center><p>Esta <b>Data de Nascimento</b> não é válida para o cadastro<br>devido ao <b>dia, mês ou ano</b> informados.<br>Verifique-os e tente novamente.",'warning',4);
			return false;
		}
		if(endereco == "") {
			showDialog('Atenção!','<center><p>Você não informou seu <b>Endereço</b>!<br>Digite-o para realizar seu cadastro.','warning',3);
			return false;
		}
		if(telefone=="" && celular=="") {
			showDialog('Atenção!','<center><p>É necessário que você informe algum <b>telefone de contato</b>!<br>Digite-o para realizar seu cadastro.','warning',3);
			return false;
		}
		if(email == "") {
			showDialog('Atenção!','<center><p>É necessário que você informe um <b>E-mail</b><br>para que possamos enviar sua <b>Confirmação de Inscrição</b>!<br>Digite-o para realizar seu cadastro.','warning',4);
			return false;
		}
		if(interesse == "") {
			showDialog('Atenção!','<center><p>Para efeitos de pesquisa, queremos saber o seu <b>interesse</b> na Mostra.<br>Colabore com seu comentário!','warning',4);
			return false;
		}
	}
}

function disabCadastroMostra(boolean) {
	document.getElementById("nome").disabled = boolean;
	document.getElementById("cpf").disabled = boolean;
	document.getElementById("dataNascimento").disabled = boolean;
	document.getElementById("telefone").disabled = boolean;
	document.getElementById("celular").disabled = boolean;
	document.getElementById("endereco").disabled = boolean;
	document.getElementById("email").disabled = boolean;
	document.getElementById("escolaridade").disabled = boolean;
	document.getElementById("status").disabled = boolean;
	document.getElementById("alunoExcellence").disabled = boolean;
	document.getElementById("comoSoube").disabled = boolean;
	document.getElementById("interesse").disabled = boolean;
	document.verCadastro.acao.disabled = boolean;
	if(boolean) {
		document.verCadastro.acao.style.backgroundColor = "#666666";
		document.verCadastro.acao.style.color = "#FFFFFF";
	} else {
		document.verCadastro.acao.style.backgroundColor = "#FFFFFF";
		document.verCadastro.acao.style.color = "#666666";
	}
	document.getElementById("nome").focus();
}

function validaCadastroArquivos(form) {
	if(form.name == "loginArquivos") {
	with(document.loginArquivos) {
		if(email.value == "") {
			showDialog('Atenção!','<center><p>Você não informou seu <b>E-mail</b>!<br>Digite-o para realizar o acesso.','warning',3);
			return false;
		}if(cpf.value == "") {
			showDialog('Atenção!','<center><p>Você não informou seu <b>CPF</b>!<br>Digite-o para realizar o acesso.','warning',3);
			return false;
		}else if(!checacpf(cpf.value)) {
			showDialog('Atenção!','<center><p>Este <b>CPF</b> é inválido!<br>Verifique-o e tente novamente.','warning',3);
			return false;
		}
	}}
	else if(form.name == "newUserArquivos") {
	with(document.newUserArquivos) {
		if(cpf.value == "") {
			showDialog('Atenção!','<center><p>Você não informou seu <b>CPF</b>!<br>Digite-o para realizar seu cadastro.','warning',3);
			return false;
		}else if(!checacpf(cpf.value)) {
			showDialog('Atenção!','<center><p>Este <b>CPF</b> é inválido!<br>Verifique-o e tente novamente.','warning',3);
			return false;
		}
	}}
	else if(form.name=="usuarioArquivos") {
	with(document.usuarioArquivos) {
		if(nome.value == "") {
			showDialog('Atenção!','<center><p>Você não informou seu <b>Nome</b>!<br>Digite-o para realizar seu cadastro.','warning',3);
			return false;
		}if(cpf.value == "") {
			showDialog('Atenção!','<center><p>Você não informou seu <b>CPF</b>!<br>Digite-o para realizar seu cadastro.','warning',3);
			return false;
		}else if(!checacpf(cpf.value)) {
			showDialog('Atenção!','<center><p>Este <b>CPF</b> é inválido!<br>Verifique-o e tente novamente.','warning',3);
			return false;
		}if(dataNascimento.value == "") {
			showDialog('Atenção!','<center><p>Você não informou sua <b>Data de Nascimento</b>!<br>Digite-a para realizar seu cadastro.','warning',3);
			return false;
		}else if(!checkDate(dataNascimento.value)) {
			showDialog('Atenção!',"<center><p>Esta <b>Data de Nascimento</b> não é válida para o cadastro<br>devido ao <b>dia, mês ou ano</b> informados.<br>Verifique-os e tente novamente.",'warning',4);
			return false;
		}if(telefone.value=="" && celular.value=="") {
			showDialog('Atenção!','<center><p>Você não informou nenhum <b>telefone de contato</b>!<br>Digite algum para realizar seu cadastro.','warning',3);
			return false;
		}if(email.value == "") {
			showDialog('Atenção!','<center><p>Você não informou seu <b>E-mail</b>!<br>Digite-o para realizar seu cadastro.','warning',4);
			return false;
		}if(instituicao.value == "") {
			showDialog('Atenção!','<center><p>Você não informou sua <b>Instituição de Ensino</b>!<br>Digite-a para realizar seu cadastro.','warning',4);
			return false;
		}
	}}
}