// exemplo de uso no formulário:
// <input type="text" name="datanasc" onKeyDown="return formataCampo(this,event,'data')">
function formataCampo(campo, evento, tipo) {
	var tecla = evento.keyCode;	// pega tecla pressionada
	var ret = false;
	
	if (tecla < 47 && tecla != 32) // teclas de controle exceto barra de espaço
		ret = true;
	
	if ((tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105)) {
		ret = true;
		tmp = campo.value.length;
		switch(tipo) {
			case 'data':
				if (tmp == 2 || tmp == 5)
					campo.value += "/";
				break;
				
			case 'dataMesAno':
				if (tmp == 2)
					campo.value += "/";
				break;
				
			case 'hora':
				if (tmp == 2 || tmp == 5)
					campo.value += ":";
				break;
				
			case 'cep':
				if (tmp == 2)
					campo.value += ".";
				else if (tmp == 6)
					campo.value += "-";
				break;
				
			case 'fone':
				if (tmp == 0)
					campo.value += "(";
				else if (tmp == 3)
					campo.value += ") ";
				else if (tmp == 9)
					campo.value += "-";
				break;
				
			case 'cpf':
				if (tmp == 3 || tmp == 7)
					campo.value += ".";
				else if (tmp == 11)
					campo.value += "-";
				break;
				
			case 'cnpj':
				if (tmp == 2 || tmp == 6)
					campo.value += ".";
				else if (tmp == 10)
					campo.value += "/";
				else if (tmp == 15)
					campo.value += "-";
				break;
				
			case 'moeda':
				if (tecla > 95)
					tecla -= 48;
				tmp2 = campo.value.replace(/[\.,]/g,"") + String.fromCharCode(tecla);		// "limpa" numero
				tmp2 = tmp2.replace(/^0+/,"");				// remove zeros a esquerda
				var npts = parseInt((tmp2.length-3)/3);		// quantos pontos de milhar?
				while (tmp2.length < 3)
					tmp2 = "0"+tmp2;
				
				tmp2 = tmp2.substr(0,tmp2.length-2) + "," + tmp2.substr(tmp2.length-2);
				for (var p=0; p < npts; p++) {
					var pos = tmp2.length-3*(p+2)-p;
					tmp2 = tmp2.substr(0,pos) + "." + tmp2.substr(pos);
				}
				
				campo.value = tmp2;
				ret = false;
		}
	}
	
	return ret;
}

function geraCodigo(length) {
	caracteres = [
			'a','b','c','d','e','f','g','h','i','j','k','l','m',
			'n','o','p','q','r','s','t','u','v','w','x','y','z',
			'A','B','C','D','E','F','G','H','I','J','K','L','M',
			'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
			'0','1','2','3','4','5','6','7','8','9'];
	
	result = "";
	for (i_GC = 0; i_GC < length; i_GC++)
		result += caracteres[parseInt(Math.random() * caracteres.length)];
	
	return result;
}

function dataToStringFull(data) {
	meses = [
		"Janeiro", "Fevereiro", "Mar&ccedil;o", "Abril", "Maio", "Junho",
		"Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"];

	semanas = [
		"Domingo", "Segunda-feira", "Ter&ccedil;a-feira", 
		"Quarta-feira", "Quinta-feira", "Sexta-feira", "S&aacute;bado"];

	return semanas[data.getDay()] + ', ' + data.getDate() + ' de ' + meses[data.getMonth()] + ' de ' + data.getFullYear();
}

function dataToStringExtenso(data) {
	meses = [
		"Janeiro", "Fevereiro", "Mar&ccedil;o", "Abril", "Maio", "Junho",
		"Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"];

	return data.getDate() + ' de ' + meses[data.getMonth()] + ' de ' + data.getFullYear();
}

function dataToString(data) {
	return (
		(data.getDate() < 10 ? '0' : '') + data.getDate() + "/" +
		(data.getMonth() < 9 ? '0' : '') + (data.getMonth() + 1) + "/" +
		data.getFullYear()
	);
}

function stringToData(dataStr) {
	var dataSplit = dataStr.split('/');
	
	var data = new Date();
	
	if (dataSplit.length != 3)
		return data;

	data.setFullYear(dataSplit[2]);
	data.setMonth(dataSplit[1]-1);
	data.setDate(dataSplit[0]);
	data.setHours(0);
	data.setMinutes(0);
	data.setSeconds(0);
	data.setMilliseconds(0);
	
	return data;
}

function showMenu(id) {
	document.getElementById("submenu_" + id).style.display = 'block';
}

function hideMenu(id) {
	document.getElementById("submenu_" + id).style.display = 'none';
}

function nada() {}





var functionOnClose;
var functionOnCancel;
var functionOnConfirm;
var documentOnKeyPressBKP_UTIL;

function showDialog(titulo, content, params) {
	functionOnClose = null;
	functionOnCancel = null;
	functionOnConfirm = null;

	hideDialog();
	
	if (params == null)
		params = new Array();
	
	if (params.width == null)
		params.width = 500;
		
	if (params.height == null)
		params.height = 300;
		
	if (params.onClose != null)
    	functionOnClose = params.onClose;
    	
    if (params.onCancel != null)
    	functionOnCancel = params.onCancel;
    	
    if (params.onConfirm != null)
    	functionOnConfirm = params.onConfirm;
    	
	messageContent = document.createElement('div');
	if (content != null) {
		if (content.tagName != 'DIV') {
			div = document.createElement('div');
			
			if (typeof(content) != 'object')
				div.innerHTML = content;
			else
				div.appendChild(content);
			
			content = div;
		}
	
		content.style['height'] = (params.height - 23 - 2*5) + 'px';
		content.style['textAlign'] = 'justify';
		content.style['overflowY'] = 'auto';
		
		if (params.type != null) {
			image = document.createElement('img');
			image.setAttribute('src', contextPath + '/images/' + params.type + '.png');
		
			tdImage = document.createElement('td');
			tdImage.setAttribute('valign', 'top');
			tdImage.setAttribute('width', '37px');
			tdImage.setAttribute('nowrap', 'nowrap');
			tdImage.appendChild(image);
			
			tdText = document.createElement('td');
			tdText.setAttribute('valign', 'top');
			//tdText.setAttribute('width', '100%');
			tdText.appendChild(content);
		
			tr = document.createElement('tr');
			tr.appendChild(tdImage);
			tr.appendChild(tdText);
		
			table = document.createElement('table');
			table.setAttribute('cellpadding', '0');
			table.setAttribute('cellspacing', '0');
			table.setAttribute('width', (params.width - 2*2 - 2*5) + 'px');
			table.setAttribute('height', (params.height - 23 - 2*2 - 2*5) + 'px');
			table.appendChild(tr);
			
			content = table;
		}
		
		messageContent.appendChild(content);
	}
	
	_dialogZoneTitle = document.getElementById('dialogZoneTitle');
	_dialogZoneTitle.innerHTML = titulo;
	
	_dialogZoneMessage = document.getElementById('dialogZoneMessage');
	_dialogZoneMessage.innerHTML = messageContent.innerHTML;
	
	_dialogZone = document.getElementById('dialogZone');
	_dialogZone.style['width'] = params.width +  'px';    
	_dialogZone.style['height'] = params.height + 'px';
	_dialogZone.style['marginLeft'] = (document.body.clientWidth - params.width)/2 + 'px';
    _dialogZone.style['marginTop'] = (document.body.clientHeight - params.height)/2 + 'px';
	
    document.getElementById('disabledBlackZone').style.display = 'block';
    document.getElementById('dialogZone').style.display = 'block';
    
    documentOnKeyPressBKP_UTIL = document.onkeypress;
	document.onkeypress = function(evt) {
		evt = evt || window.event;
		if (evt.keyCode == 27) // Esc
			hideDialog();
		return false;
	}
}

function hideDialog() {
	document.getElementById('disabledBlackZone').style.display = 'none';
    document.getElementById('dialogZone').style.display = 'none';
    
    document.onkeypress = documentOnKeyPressBKP_UTIL;
    documentOnKeyPressBKP_UTIL = null;
	
	if (functionOnClose != null)
		functionOnClose();
}

function showMessage(title, message, params) {
	if (params == null)
		params = new Array();
		
	if (params.width == null)
		params.width = 450;
		
	if (params.height == null)
		params.height = 100;
		
	showDialog(title, message, params);
}

function showInformation(title, message, params) {
	if (params == null)
		params = new Array();
		
	params.type = 'information';

	showMessage(title, message, params);
}

function showWarning(title, message, params) {
	if (params == null)
		params = new Array();
		
	params.type = 'warning';
	
	showMessage(title, message, params);
}

function showError(title, message, params) {
	if (params == null)
		params = new Array();
		
	params.type = 'error';
	
	showMessage(title, message, params);
}

function showConfirm(title, message, params) {
	if (params == null)
		params = new Array();
		
	if (params.width == null)
		params.width = 450;
		
	if (params.height == null)
		params.height = 125;
		
    if (params.onClose != null)
    	functionOnClose = params.onClose;
    	
    if (params.onConfirm != null)
    	functionOnConfirm = params.onConfirm;
		
	params.type = 'information';
	
	divMessage = document.createElement('div');
	divMessage.style['height'] = (params.height - 23 - 2*5 - 25) + 'px';
	divMessage.style['textAlign'] = 'justify';
	divMessage.style['overflowY'] = 'auto';
	divMessage.innerHTML = message;
	
	buttonConfirm = document.createElement('input');
	buttonConfirm.setAttribute('type', 'button');
	buttonConfirm.setAttribute('value', 'Confirmar');
	if (functionOnConfirm != null) {
		buttonConfirm.setAttribute('onclick', 'functionOnConfirm(); hideDialog();');
	} else {
		buttonConfirm.setAttribute('onclick', 'hideDialog();');
	}
	
	buttonCancel = document.createElement('input');
	buttonCancel.setAttribute('type', 'button');
	buttonCancel.setAttribute('value', 'Cancelar');
	if (functionOnCancel != null) {
		buttonCancel.setAttribute('onclick', 'functionOnCancel(); hideDialog();');
	} else {
		buttonCancel.setAttribute('onclick', 'hideDialog();');
	}
	
	divButtons = document.createElement('div');
	divButtons.setAttribute('align', 'right');
	divButtons.appendChild(buttonConfirm);
	divButtons.appendChild(buttonCancel);
	
	divContent = document.createElement('div');
	divContent.appendChild(divMessage);
	divContent.appendChild(divButtons);
	
	documentOnKeyPressBKP_UTIL = document.onkeypress;
	showMessage(title, divContent, params);
	document.onkeypress = function(evt) {
		evt = evt || window.event;
		if (evt.keyCode == 13) { // Enter
			if (functionOnConfirm != null)
				functionOnConfirm();
			hideDialog();
		} else if (evt.keyCode == 27) { // Esc
			hideDialog();
		}
		
		return false;
	}
}

function useLoadingImage() {
	DWREngine.setPreHook(function() {
		document.getElementById('disabledAjaxLoaderZone').style.display = 'block';
	});
	DWREngine.setPostHook(function() {
		document.getElementById('disabledAjaxLoaderZone').style.display = 'none';
	});
};

function isInteger(s) {
	return (s.toString().search(/^-?[0-9]+$/) == 0);
}

function getParameter(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	
	if (results == null)
		return null;
	else
		return results[1];
}

function setTitulo(titulo) {
	if (titulo != null && titulo.length > 0) {
		document.getElementById('titulo').innerHTML = titulo;
		document.title = 'CENAPAD-UFC - ' + titulo;
	} else {
		document.getElementById('titulo').style.display = 'none';
		document.title = 'CENAPAD-UFC';
	}
}