/**
 * Script genérico para abertura de janelas pop-up
 */
function popup(url, w, h, x, y, name, options) {
	if (!options) options = "resizeable=no,location=no,menu=no,scrollbars=yes,status=no";
	options = "width=" + w + ",height=" + h + "," + options;
	if (x) options = "left=" + x + "," + options;
	if (y) options = "top=" + y + "," + options;
	window.open(url, name, options);
}

/**
 * Seta o endereço do navegador para o fornecido, opcionalmente mediante confirmação.
 */
function setLocation(url, msg) {
	if (msg) {
		if (!confirm(msg)) return;
	}
	window.location = url;
}

/**
 * Seta o endereço da janela pai da atual para o especificado, exibindo a mensagem
 * de confirmação se ela for fornecida
 */
function setParentLocation(url, msg) {
	if (msg) {
		if (!confirm(msg)) return;
	}
	window.parent.location = url;
}

/**
 * Alterna o estado do display de uma layer entre "none" e "block"
 */
function toggleDisplay(id) {
	var l = document.getElementById(id);
	if ((l.style.display == "none") || (l.style.display == "")) {
		l.style.display = "block";
	} else {
		l.style.display = "none";
	}
}

/**
 * Preenche uma string com zeros à esquerda no tamanho especificado
 */
function fillWithZeros(s, size) {
	var zeros = "";
	for (var i = 0; i < size; i++) {
		zeros = zeros + "0";
	}
	s = zeros + s;
	return s.substr(s.length - size);
}

/**
 * Retorna uma string com a data formatada a partir da data informada
 */
function formatDate(d) {
	var day = fillWithZeros(d.getDate(), 2);
	var month = fillWithZeros(d.getMonth() + 1, 2);
	var year = d.getFullYear();
	return day + "/" + month + "/" + year;
}
 
/**
 * Retorna uma string com a hora formatada a partir da data informada
 */
function formatTime(d, includeSeconds) {
	var timeStr =
		fillWithZeros(d.getHours(), 2) + ":" +
		fillWithZeros(d.getMinutes(), 2);
	if (includeSeconds) {
		timeStr = timeStr + ":" + fillWithZeros(d.getSeconds(), 2);
	}
	return timeStr;
}
