var segundoRefresh = 1;
var cronometro;

/*######################################################################################################*/
function bannerPaginaInicialFechar () {
	jQuery("#contFlutuante").fadeOut(500, function () {
		jQuery("#contFlutuante").remove();
		ajax = iniciaAjax();
		if (ajax) {
			ajax.open("POST", 'naoExibeBannerInicial.ajax.php', true);
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		    ajax.send(null);
		}
	});
}
/*######################################################################################################*/
function checaBannerForm () {
	if(document.bannerForm.titulo.value == '') {
		alert('Digite o título');
		document.bannerForm.titulo.focus();
		return false;
	}
}
/*######################################################################################################*/
function controlaRefreshPagina () {
	if (segundoRefresh == 60) {
		window.location.reload();
	}
	segundoRefresh ++;
	cronometro = setTimeout('controlaRefreshPagina()','1000');
}
/*######################################################################################################*/
function siscaAtualizaAutomaticamente (prCheck) {
	if (prCheck.checked) {
		controlaRefreshPagina ();
		var atualizaPagina = '1';
	} else {
		window.clearTimeout(cronometro);
		var atualizaPagina = '0';
	}
	ajax = iniciaAjax();
	if (ajax) {
		ajax.open("POST", 'autoAtualizaPagina.ajax.php', true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    ajax.send("atualiza="+atualizaPagina);
	}
}
/* AGENDAMENTOS */
/*######################################################################################################*/
function concluiAgendamento (prAgendamentoId) {
	limpaCampoAgendamento();
	document.agendamentoForm.acao.value = 'concluir';
	document.agendamentoForm.agendamentoId.value = prAgendamentoId;
	document.getElementById('ordemServico').style.display = 'block';
	document.getElementById('observacoes').style.display = 'block';
	document.agendamentoForm.login.focus();
}
/*######################################################################################################*/
function agendaAgendamento (prAgendamentoId) {
	limpaCampoAgendamento();
	document.agendamentoForm.acao.value = 'agendar';
	document.agendamentoForm.agendamentoId.value = prAgendamentoId;
	document.getElementById('dataAgendamento').style.display = 'block';
	document.agendamentoForm.login.focus();
}
/*######################################################################################################*/
function cancelaAgendamento (prAgendamentoId) {
	limpaCampoAgendamento();
	document.agendamentoForm.acao.value = 'cancelar';
	document.agendamentoForm.agendamentoId.value = prAgendamentoId;
	document.getElementById('motivoCancelamento').style.display = 'block';
	document.agendamentoForm.login.focus();
}
/*######################################################################################################*/
function assumeAgendamento (prAgendamentoId) {
	limpaCampoAgendamento();
	document.agendamentoForm.acao.value = 'assumir';
	document.agendamentoForm.agendamentoId.value = prAgendamentoId;
	document.agendamentoForm.login.focus();
}
/*######################################################################################################*/
function limpaCampoAgendamento () {
	document.getElementById('mensagemErro').style.display = 'none';
	document.getElementById('mensagemErro').innerHTML = '';
	document.getElementById('mensagemErro').style.display = 'none';
	document.agendamentoForm.login.value = '';
	document.agendamentoForm.motivoCancelamento.value = '';
	document.agendamentoForm.observacoes.value = '';
	document.getElementById('motivoCancelamento').style.display = 'none';
	document.getElementById('ordemServico').style.display = 'none';
	document.getElementById('dataAgendamento').style.display = 'none';
	document.getElementById('observacoes').style.display = 'none';
	/*Abre a caixa*/
	posY = document.body.scrollTop  + document.documentElement.scrollTop;
	$('#caixaAgendamento').fadeIn(500);
	document.getElementById("caixaAgendamento").style.top = posY + 50 +'px';
}

/*######################################################################################################*/
function checaAgendamento () {
	if (document.agendamentoForm.login.value == '') {
		retornaErro ('Digite o Código do Funcionário.','mensagemErro');
		document.agendamentoForm.login.focus();
		return false;
	}
	switch (document.agendamentoForm.acao.value) {
		case 'cancelar' : {
			if (document.agendamentoForm.motivoCancelamento.value == '') {
				retornaErro ('Digite o Motivo do Cancelamento.','mensagemErro');
				document.agendamentoForm.motivoCancelamento.focus();
				return false;
			}
			break;
		}
		case 'agendar' : {
			if (document.agendamentoForm.dataAgendamento.value == '') {
				retornaErro ('Digite a data do Agendamento.','mensagemErro');
				document.agendamentoForm.dataAgendamento.focus();
				return false;
			}
			if (document.agendamentoForm.horaAgendamento.value == '') {
				retornaErro ('Digite a hora do Agendamento.','mensagemErro');
				document.agendamentoForm.horaAgendamento.focus();
				return false;
			}
			break;
		}
		case 'concluir' : {
			if (document.agendamentoForm.ordemServico.value == '') {
				retornaErro ('Digite a Ordem de Serviço.','mensagemErro');
				document.agendamentoForm.ordemServico.focus();
				return false;
			}
			if (document.agendamentoForm.observacoes.value == '') {
				retornaErro ('Digite uma Observação.','mensagemErro');
				document.agendamentoForm.observacoes.focus();
				return false;
			}
			break;
		}
	}
}



										/* COTAÇÕES */
/*######################################################################################################*/
function checaCotacaoEnvioEmailForm () {
	if (document.cotacaoEmailForm.assunto.value == '') {
		retornaErro ('Digite o assunto do e-mail.','retornoMensagem');
		document.cotacaoEmailForm.assunto.focus();
		return false;
	}
	if (document.cotacaoEmailForm.texto.value == '') {
		retornaErro ('Digite o texto do e-mail.','retornoMensagem');
		document.cotacaoEmailForm.texto.focus();
		return false;
	}
	if (document.cotacaoEmailForm.destinatario.value == '') {
		retornaErro ('Digite o endereço do destinatário do e-mail.','retornoMensagem');
		document.cotacaoEmailForm.destinatario.focus();
		return false;
	}
	return true;
}
/*######################################################################################################*/
function concluiCotacao (prCotacaoId) {
	limpaCampoCotacao ();
	document.cotacaoForm.acao.value = 'concluir';
	document.cotacaoForm.cotacaoId.value = prCotacaoId;
	document.getElementById('observacoes').style.display = 'block';
	document.cotacaoForm.login.focus();
}
/*######################################################################################################*/
function cancelaCotacao (prCotacaoId) {
	limpaCampoCotacao();
	document.cotacaoForm.acao.value = 'cancelar';
	document.cotacaoForm.cotacaoId.value = prCotacaoId;
	document.getElementById('motivoCancelamento').style.display = 'block';
	document.cotacaoForm.login.focus();
}
/*######################################################################################################*/
function assumeCotacao (prCotacaoId) {
	limpaCampoCotacao();
	document.cotacaoForm.acao.value = 'assumir';
	document.cotacaoForm.cotacaoId.value = prCotacaoId;
	document.cotacaoForm.login.focus();
}
/*######################################################################################################*/
function limpaCampoCotacao () {
	document.getElementById('mensagemErroCotacao').style.display = 'none';
	document.getElementById('mensagemErroCotacao').innerHTML = '';
	document.getElementById('mensagemErroCotacao').style.display = 'none';
	document.cotacaoForm.login.value = '';
	document.cotacaoForm.motivoCancelamento.value = '';
	document.cotacaoForm.observacoes.value = '';
	document.getElementById('motivoCancelamento').style.display = 'none';
	document.getElementById('observacoes').style.display = 'none';
	/*Abre a caixa*/
	posY = document.body.scrollTop  + document.documentElement.scrollTop;
	$('#caixaCotacao').fadeIn(500);
	document.getElementById("caixaCotacao").style.top = posY + 50 +'px';
}
/*######################################################################################################*/
function controlaDivCotacao (prCotacaoId, prDivId, prLink) {
	if (document.getElementById(prDivId).style.display == 'none') {
		$("#"+prDivId).slideDown("slow");
		prLink.value = 'Fechar Dados';
	} else {
		$("#"+prDivId).slideUp("slow");
		prLink.value = 'Abrir Dados';
	}
	ajax = iniciaAjax();
	if (ajax) {
		ajax.open("POST", 'cotacaoDefineVisualizacao.ajax.php', true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    ajax.send("cotacaoId="+prCotacaoId);
	}
}
/*######################################################################################################*/
function checaCotacao () {
	if (document.cotacaoForm.login.value == '') {
		retornaErro ('Digite o Código do Funcionário.','mensagemErroCotacao');
		document.cotacaoForm.login.focus();
		return false;
	}
	switch (document.cotacaoForm.acao.value) {
		case 'cancelar' : {
			if (document.cotacaoForm.motivoCancelamento.value == '') {
				retornaErro ('Digite o Motivo do Cancelamento.','mensagemErroCotacao');
				document.cotacaoForm.motivoCancelamento.focus();
				return false;
			}
			break;
		}
	}
}
/*######################################################################################################*/
function controlaDivAgendamento (prAgendamentoId, prDivId, prLink) {
	if (document.getElementById(prDivId).style.display == 'none') {
		$("#"+prDivId).slideDown("slow");
		prLink.value = 'Fechar Dados';
	} else {
		$("#"+prDivId).slideUp("slow");
		prLink.value = 'Abrir Dados';
	}
	ajax = iniciaAjax();
	if (ajax) {
		ajax.open("POST", 'agendamentoDefineVisualizacao.ajax.php', true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    ajax.send("agendamentoId="+prAgendamentoId);
	}
}
/*######################################################################################################*/
function retornaErro (prErro, prDiv) {
	document.getElementById(prDiv).style.display = 'none';
	document.getElementById(prDiv).innerHTML = prErro;
	document.getElementById(prDiv).className = 'erro';
	$('#'+prDiv).fadeIn(500);
}
/*######################################################################################################*/
function controleTimerCotacao () {
	var allHTMLTags = document.getElementsByTagName("input");
    for (i = 0; i < allHTMLTags.length; i++) {
        if (allHTMLTags[i].className == 'contadorCotacao' && allHTMLTags[i].type === 'text') {
        	var segundo = '';
        	var minuto = '';
        	var hora = '';
        	var aTmp = '';
        	var conteudo = '';
        	aTmp = new Array();
        	aTmp = allHTMLTags[i].value.split(':');
        	
        	segundo = parseFloat(aTmp[2]);
        	minuto = parseFloat(aTmp[1]);
        	hora = parseFloat(aTmp[0]);
        	
    		/* Segundos */
    		if (segundo == 60) {
    			segundo = parseFloat(00);
    			minuto = parseFloat(minuto + 1);
    			if (minuto == 60) {
        			minuto = parseFloat(00);
        			if(hora == 24) {
        				hora = parseFloat(00);
        			} else {
        				hora = parseFloat(hora + 1);
        			}
        		}
    		} else {
    			segundo = parseFloat(segundo + 1);
    		}
    		
    		if (hora <= 9) { conteudo += '0'+hora+':'; } else { conteudo += hora+':'; }
    		if (minuto <= 9) { conteudo += '0'+minuto+':'; } else { conteudo += minuto+':'; }
    		if (segundo <= 9) { conteudo += '0'+segundo; } else { conteudo += segundo; }
        	allHTMLTags[i].value = conteudo;
        	continue;
        }
    }
    setTimeout("controleTimerCotacao()",1000);
}
/*######################################################################################################*/
function mascaraDataSemAno (prObjeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var data = prObjeto.value;
	
	if (data.length >= 10) {
		if (tecla != 9 && tecla != 13 && tecla != 46 && tecla != 8 && tecla != 16 & tecla != 36) { return false; } else { return true; }
	}
	
	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	((data.length == 2) && tecla != 8) {
			data += '/';
			prObjeto.value = data;
		}
		return true;
	} else {
		return false;
	}
}
/*######################################################################################################*/
function checaForm (prNomeForm, prDarSubmit){
	var elementoHTMLTextArea = document.getElementsByTagName('textarea');
	for	(var i = 0; i < elementoHTMLTextArea.length; i++) {
		if( (elementoHTMLTextArea[i].value == '') && (elementoHTMLTextArea[i].lang != '') && (elementoHTMLTextArea[i].form.name == prNomeForm) ){
			document.getElementById("retornoMensagem").style.display = 'none';
			document.getElementById("retornoMensagem").className = 'erro';
			document.getElementById("retornoMensagem").innerHTML = elementoHTMLTextArea[i].lang;
			$("#retornoMensagem").fadeIn(1000);
			elementoHTMLTextArea[i].focus();
			return false;
			break;
		}
	}
	var elementoHTMLText = document.getElementsByTagName('input');
	for	(var i = 0; i < elementoHTMLText.length; i++) {
		if (elementoHTMLText[i].type === 'text' || elementoHTMLText[i].type === 'password') {
			if( (elementoHTMLText[i].value == '') && (elementoHTMLText[i].lang != '') && (elementoHTMLText[i].form.name == prNomeForm) ){
				document.getElementById("retornoMensagem").style.display = 'none';
				document.getElementById("retornoMensagem").className = 'erro';
				document.getElementById("retornoMensagem").innerHTML = elementoHTMLText[i].lang;
				$("#retornoMensagem").fadeIn(1000);
				elementoHTMLText[i].focus();
				return false;
				break;
			}
		}
	}
	var elementoHTMLSelect = document.getElementsByTagName('select');
	for	(var i = 0; i < elementoHTMLSelect.length; i++) {
		if( (elementoHTMLSelect[i].value == '') && (elementoHTMLSelect[i].lang != '') && (elementoHTMLSelect[i].form.name == prNomeForm) ){
			document.getElementById("retornoMensagem").style.display = 'none';
			document.getElementById("retornoMensagem").className = 'erro';
			document.getElementById("retornoMensagem").innerHTML = elementoHTMLSelect[i].lang;
			$("#retornoMensagem").fadeIn(1000);
			elementoHTMLSelect[i].focus();
			return false;
			break;
		}
	}
	if (prDarSubmit) {
		document.forms[0].submit();
	}
	return true;
}
/*######################################################################################################*/
function contatoVerificaExibicaoDataAniversario (prObjeto) {
	if (prObjeto.checked) {
		document.forms[0].dataAniversario.focus();
		$("#dataAniversario").slideDown("slow");
	} else {
		document.forms[0].telefone.focus();
		$("#dataAniversario").slideUp("slow");
	}
}
/*######################################################################################################*/
function linkVoltar () {
	document.write('<a href="'+document.referrer+'" class="voltarLink" title="Voltar para a página anterior">Voltar</a>');
}
/*######################################################################################################*/
function fechaDiv (prDivId) {
	$("#"+prDivId).fadeOut("slow");
}
/*#######################################################################################*/
function iniciaAjax(){
	try {
 		ajax = new ActiveXObject("Microsoft.XMLHTTP");
  	} catch(e) {
		try {
    		ajax = new ActiveXObject("Msxml2.XMLHTTP");
 		} catch(ex) {
    		try {
      			ajax = new XMLHttpRequest();
	   		} catch(exc) {
				alert('Esse browser não tem recursos para uso do Ajax');
   				ajax = null;
    		}
 		}
	}
  	return ajax;
}
