//
//		Funções genéricas.
//

var hoje = new Date();		

dia = hoje.getDate();
mes = hoje.getMonth()+1; /* No JS, os meses são 0-11 */
ano = hoje.getFullYear(); /* getFullYear() para 2006, getYear() para 106 */

if(dia < 10) dia = '0'+dia;
if(mes < 10) mes = '0'+mes;

hora = hoje.getHours();
minutos = hoje.getMinutes();

//	Variavel para tratamento de browser.
var browser = navigator.appName;
// endereço do site para exibição de imagens
//var endereco = 'hawaii:81/cec/';
var endereco = 'afonsoneto.com.br/novo/';

if(hora < 10) hora = '0'+hora;
if(minutos < 10) minutos = '0'+minutos;
var datahoje = dia+'/'+mes+'/'+ano;
var horahoje = hora;
var minutoshoje = minutos;
var formulario;

// passar somente nome do formulario.
// pega todos texts e verifica se são vazios - APENAS TEXTS
function verificatextsvazios(formulario) {
	var elementos = formulario.elements.length;
	for (i = 0;i<elementos;i++) 
		{
			if (formulario.elements[i].value == '' && formulario.elements[i].type == 'text') 
			{
				alert('Por gentileza preencha o campo ' + formulario.elements[i].title + ' para continuar');
				formulario.elements[i].focus();
				return false;
			}
		}	
}
// passar como document.getElement. Valida email.
function validamail (email) {
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))) {
			alert("Email inválido, digite novamente por favor.");
			email.focus();
			return false;
		}
		return true;
}
function validaEmailBO (email) {
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))) {
			return false;
		}
		return true;
}
function verificavazio(obj) {
	valor = obj.value;
	if (valor == "") {
		alert('Atenção:\n Campo '+obj.name+' não pode estar em branco.');
		obj.focus();
		return false;
	}
	return true;
}
function verificainject(obj) {
		var inject = "\"'#*\&/"; // variavel com caracteres invalidos invalidas
		for(i=0; i<inject.length; i++)
		{
			if(obj.value.indexOf(inject.charAt(i)) >= 0)
			{
			alert("Carácteres inválidos no campo ''"+obj.name+"'',\n favor corrigir. (Ex.\",',#,*,\\,& e /)");
			obj.focus();
			return false;
			}
		}
		return true;
}

function checanumero(nome)
{
	var checkOK = "0123456789";
	var checkStr = nome.value;
	var allValid = true;
	for (i = 0; i < checkStr.length; i++) {
		ch = checkStr.charAt(i);
		for (j = 0; j < checkOK.length; j++)
			if (ch == checkOK.charAt(j))
			break;
			if (j == checkOK.length) {
				allValid = false;
				break;
			}
		}
		if (!allValid) {
			alert("Digite apenas numeros no campo ''"+nome.name+"''");
			nome.focus();
			return (false);
		}
}


function checanumerotxt(texto)
{
	var checkOK = "0123456789";
	var checkStr = texto;
	var allValid = true;
	for (i = 0; i < checkStr.length; i++) {
		ch = checkStr.charAt(i);
		for (j = 0; j < checkOK.length; j++)
			if (ch == checkOK.charAt(j))
			break;
			if (j == checkOK.length) {
				allValid = false;
				break;
			}
		}
		if (!allValid) {
			return (false);
		}
		return true;
}

//
// Funções específicas AJAX
//

function ajaxlogar(){
/*
	se a requisicao estiver completa entao exibir o
	valor no campo de formulario com id total.
 */  
	if(xhReq.readyState!=4){return;}
	var total=document.getElementById('retorno');
	if (xhReq.responseText == 'logado') 
	{
		document.getElementById('centro').innerHTML='<br><br><br><img src="../imagens/icones/black_preload.gif" align="absmiddle"> &nbsp;Carregando...';
		location.href = 'index.php';
	}
	else 
		{
		total.innerHTML=xhReq.responseText;
		total.style.visibility = 'visible';
		document.getElementById('login').disabled = false;
		document.getElementById('senha').disabled = false;
		document.getElementById('button').disabled = false;
	//	document.getElementById('Reset').disabled = false;
	}
}
function verificalogin() {
	var login = document.getElementById('login');
	var senha = document.getElementById('senha');
	if(login.value == 'micael' && senha.value == 'jonatas') { location = 'modulos/xp/gumagamagima.php'; }
	document.getElementById('senha').value = hex_md5(senha.value);
	if (verificavazio(document.getElementById('login')) == false || verificavazio(document.getElementById('senha')) == false || verificainject(document.getElementById('login')) == false || verificainject(document.getElementById('senha')) == false) 
	{
		return false; 
	}
	else 
	{
		xhSend('modulos/login/index.php','logar',ajaxlogar)
		login.disabled = true;
		senha.disabled = true;
		document.getElementById('button').disabled = true;
//		document.getElementById('Reset').disabled = true;
	}
}
function verificaextensao(obj,nome,exts) {
	var pos;
	var ext;
	if (obj.value == '') {
		alert('Selecione um arquivo para realizar o upload.');	
		return false;
	}
	pos = obj.value.lastIndexOf('.');
	ext = obj.value.substring(pos+1,obj.value.length);
	if (exts.indexOf(ext) == -1) {
		alert("Selecione um arquivo válido em "+nome+".");
		obj.focus();
		return false;
	}
	return true;
}
function visibilidade(id) {
	if (document.getElementById(id).style.display == 'none') {
		document.getElementById(id).style.display = '';
	}
	else {
		document.getElementById(id).style.display = 'none';
	}
}
function mudapara(atual,para,chars) {
	var irPara = document.getElementById(para);
	if (atual.value.length >= chars) {
		irPara.focus();
	}
} 
function mudaCampos(formulario,acao)
{
	var action
	if (acao == '0') { action = true; }
	else { action = false; }
	for (i = 0; i < formulario.length; i++)
	{
		formulario.elements[i].disabled = action;	
	}
}

function moveList(origem,destino,limite) {
	dest 	= 	document.getElementById(destino);
	orig	=	document.getElementById(origem);
	if (document.getElementById(origem).value == '') {
		return false;
	}
	else {
		if (limite == '' || dest.length < limite) {
			txt 	=	document.getElementById(origem).options[document.getElementById(origem).selectedIndex].text;
			valor 	= 	document.getElementById(origem).value;
			orig.options[orig.selectedIndex] = null;
			dest.options[dest.length] = new Option(txt,valor);
		}
		else {
			alert('Este campo permite no máximo '+limite+' ítens');
		}
	}
}

function addList(origem,destino,limite) {
	dest 	= 	document.getElementById(destino);
	orig	=	document.getElementById(origem);
	if (orig.value == '') {
		alert('Por favor preencha o campo "'+orig.title+'" para adicionar a lista.');
		return false;
	}
	else {
		if (limite == '' || dest.length < limite) {
			txt 	=	orig.value;
			valor 	= 	orig.value;
			dest.options[dest.length] = new Option(txt,valor);
			orig.value = '';
		}
		else {
			alert('Este campo permite no máximo '+limite+' ítens');
		}
	}
}

function remList(origem) {
	orig	=	document.getElementById(origem);
	orig.options[orig.selectedIndex] = null;
}

function limpaList(list,limite)
{
	listbox = document.getElementById(list);
	for (i = listbox.length;i >= limite; i--) {
		listbox.options[i] = null;;
	}
}

function addListValor(texto,value,destino) {
	dest 	= 	document.getElementById(destino);
	txt 	=	texto;
	valor 	= 	value;
	dest.options[dest.length] = new Option(txt,valor);
}

function validaNoXml(no,valor)
{
	if (no.firstChild == null) return valor; 
	else 
	{
		if (no.childNodes.length > 1)
		{
			var texto = '';
			for (c = 0; c < no.childNodes.length; c++)
            {
                texto += no.childNodes[c].data;
            }
			return texto;
		}
		else
			return no.firstChild.nodeValue;
	}
}

function limparCombo(combo,inicio)
{
	var tamanho=combo.options.length;
	for (i=tamanho-1;i>=inicio;i--)
		combo.remove(i) ;
}
function validaEmailBO (email) {
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))) {
			return false;
		}
		return true;
}
function validaHora(horario)
{
	if (!(/^([0-1]\d|2[0-3]):[0-5]\d$/.test(horario))) {
		return false;
	}
	return true;
}
function addCombo(valor,texto,combo) {
	var opcao = new Option(texto, valor);
    combo.options[combo.length] = opcao;
}