var msgCampoVazio = "Por favor preencha o campo ";

// passar somente nome do formulario.
// pega todos texts e verifica se são vazios - APENAS TEXTS
function verificaTextVazio(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 validaEmailBO (email) {
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))) {
			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;
}

// verifica se o arquivo tem a extensao solicitada
// passar vazio 1 se quiser que verifique se esta vazio tambem
function verificaExtensao(obj,exts,vazio) {
	var pos;
	var ext;
	if (!obj.value && vazio) {
		return false;
	}
	pos = obj.value.lastIndexOf('.'); 	
	ext = obj.value.substring(pos+1,obj.value.length);
	if (exts.indexOf(ext) == -1) {
		return false;
	}
	else {
		return true;
	}
}
// verifica se o campo Atual tem Chars caracteres, se tiver vai para PARA
function mudaPara(atual,para,chars) {
	var para = document.getElementById(para);
	if (atual.value.length >= chars) {
		para.focus();
	}
} 
// muda campos do formulário para inativos se for 0 e ativos se for 1
function mudaCampos(formulario,acao)
{
	var action
	action = acao == '0'?true: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 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 addList(texto,valor,destino) {
	var dest 	= 	document.getElementById(destino);
	dest.options[dest.length] = new Option(texto,valor);
}

// passar o no e o valor caso seja vazio.
function validaNoXml(no,valor)
{
	if (no.firstChild == null) 
		return valor; 
	else 
	{
		if (!document.all && 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;
	}
}


/* funcoes para controle das janelas */
function limpaJanelas()
{
	/*try 
	{
		confirma.acaoSim 		= null;
		confirma.acaoSimParam 	= null;
		confirma.mensagem 		= null;
	}
	catch(e) {}
	try
	{
		alerta.mensagem 		= null;
		alerta.tempoMensagem 	= null;
	}
	catch(e){}
	try
	{
		ok.mensagem 			= null;
		ok.tempoMensagem 		= null;
	}
	catch(e) {}*/
}

function validacao(campo,msg)
{
	limpaJanelas();
/*	alerta.mensagem 		= msg;
//	alerta.tempoMensagem 	= 3;
	alerta.acaoSim			= 'focoJan';
	alerta.acaoSimParam		= campo;
	alerta.popup();*/
	return false;
}
function validaData(data)
{
	var reDate = /^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(19|20)?\d{4}$/;
	if (reDate.test(data)) {
		return true;
	} 
	if (data != null && data != "") {
		return false;
	}
}
// data dd/mm 
function validaDataResumida(dados) {
	var er = /([012][0-9]|3[01])\/[01][0-9]/;
	if (er.test(dados)) {
		return true;
	} if (dados != null && dados != "") {
		return false;
	}
} 
function validaHora(hora)
{
	var reHora = /^([0-1]\d|2[0-3]):[0-5]\d$/;
	if (reHora.test(hora)) {
		return true;
	} 
	else if (hora != null && hora != "") {
		return false;
	}
}

// passar hora inicial, minuto inicial, segundos iniciais (nao obrigatorio), hora final, minutos finais, segundos finais
// retorna em SEGUNDOS, para minutos dividir por 60
function diferencaHora(h,m,s,hf,mf,sf)
{
	s 	= s == null?0:s;
	sf 	= sf == null?0:sf;
	
	var data1 = new Date(0,0,0,h,m,0);
	var data2 = new Date(0,0,0,hf,mf,0);
	return ((data2-data1)/1000);
}
// dia/mes/ano, dia/mes/ano finais
function diferencaData(dataIn,dataEn)
{
	dataIn 		= dataIn.split('/');
	dataEn		= dataEn.split('/');
	var data1 = new Date(dataIn[2],(dataIn[1]-1),dataIn[0],0,0,0);
	var data2 = new Date(dataEn[2],(dataEn[1]-1),dataEn[0],0,0,0);
	return ((data2-data1)/1000);
}
// data em formato d/m/a h m s
function diferencaDateTime(dataIn,h,m,s,dataEn,hf,mf,sf)
{
	dataIn 		= dataIn.split('/');
	dataEn		= dataEn.split('/');
	var data1 = new Date(dataIn[2],dataIn[1]-1,dataIn[0],h,m,s);
	var data2 = new Date(dataEn[2],dataEn[1]-1,dataEn[0],hf,mf,sf);
	return ((data2-data1)/1000);
}

function verificaRadio(obj)
{
	for(var i = 0; i < obj.length; i++)
		if (obj[i].checked)
			return obj[i].value;
	return false;
}

/* get element by id */
function gE(elemento) {
	return document.getElementById(elemento);
}

function verificaNumero(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;
}

function loading(param)
{
	var acao;
	if(param)
		acao = "";
	else
		acao = "none";
	document.getElementById('aguarde').style.display = acao;
}

function addCidades(elemento,xml,valor) {
	gE(elemento).options[0].text	= 'Selecione';
	limpaList(elemento,1);
	for (i = 0; i < xml.getElementsByTagName('cidade').length; i++) {
		gE(elemento).options[gE(elemento).options.length] = new Option(unescape(xml.getElementsByTagName('cidade')[i].getAttribute('nome')),xml.getElementsByTagName('cidade')[i].getAttribute('indice'));
	}
	gE(elemento).disabled	= false;
	if (valor) {
		gE(elemento).value	= valor;
	}
}

function validaCPF(cpf) {
	if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
		return false;
	
	soma = 0;
	for(i = 0; i < 9; i++)
		soma += parseInt(cpf.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
		resto = 0;
	if(resto != parseInt(cpf.charAt(9)))
		return false;
	soma = 0;
	for(i = 0; i < 10; i ++)
		soma += parseInt(cpf.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
		resto = 0;
	if(resto != parseInt(cpf.charAt(10)))
		return false;
	
	return true;
}