/*****************************************



	@Arquivo:

		js_appAgendamento.js



	@Data:

		21/08/2009



	@Objetivo:

		Funções javascript para a aplicação: AGENDAMENTO DE CONSULTAS



	@Modificações (Data - Descrição):

		- sem modificações -





*****************************************/



var appAgendamento = {

	validaFrm: function(frm){

		if(frm.nomePaciente.value.length == 0){

			alert("Informe o NOME DO PACIENTE para prosseguir!");

			frm.nomePaciente.focus();

			return false;

		}

		if(frm.idadePaciente.value.length == 0){

			alert("Informe a IDADE para prosseguir!");

			frm.idadePaciente.focus();

			return false;

		}

		if(!fieldMaskValidator.soNumeros(frm.idadePaciente))

			return false;

			

		if(frm.emailPaciente.value.length == 0){

			alert("Informe o E-MAIL para prosseguir!");

			frm.emailPaciente.focus();

			return false;

		}

		if(!fieldMaskValidator.email(frm.emailPaciente))

			return false;

		

		if(frm.telPaciente.value.length == 0){

			alert("Informe o TELEFONE para prosseguir!");

			frm.telPaciente.focus();

			return false;

		}

		if(!fieldMaskValidator.telefoneComDDD(frm.telPaciente))

			return false;

		

		if(!fieldMaskValidator.telefoneComDDD(frm.celPaciente))

			return false;

		

		if(frm.flagConvenio[0].checked){

			if(frm.convenioPaciente.selectedIndex == 0){

				alert("Selecione um CONVÊNIO para prosseguir!");

				frm.convenioPaciente.focus();

				return false;

			}

		}

		if(!frm.flagJaFoiAtendido[0].checked && !frm.flagJaFoiAtendido[1].checked){

			alert("Informe se JÁ FOI ATENDIDO NO HOSPITAL PACINI para prosseguir!");

			frm.flagJaFoiAtendido[0].focus();

			return false;

		}

		if(!frm.flagOQueDesejaAgendar[0].checked && !frm.flagOQueDesejaAgendar[1].checked){

			alert("Informe o que DESEJA AGENDAR para prosseguir!");

			frm.flagOQueDesejaAgendar[0].focus();

			return false;

		}

		

		//valida campos do agendamento tipo: consulta

		if(frm.flagOQueDesejaAgendar[0].checked){

			if(!frm.flagConsultaRotina[0].checked && !frm.flagConsultaRotina[1].checked){

				alert("Informe se é uma CONSULTA DE ROTINA para prosseguir!");

				frm.flagConsultaRotina[0].focus();

				return false;

			}

			

			var marcouDepartamento 		= false;

			var arListaDepartamento		= new Array();

			frm.listaDepartamento.value	= '';

			

			for(i=0;i<frm.departamento.length;i++){

				if(frm.departamento[i].checked){

					marcouDepartamento = true;

					

					strAux = (arListaDepartamento.length > 0) ? ' ' : '';

					arListaDepartamento[arListaDepartamento.length] = strAux + frm.departamento[i].value;

				}

			}

			

			if(!marcouDepartamento){

				alert("Selecione pelo menos um DEPARTAMENTO para prosseguir!");

				frm.departamento[0].focus();

				return false;

			}

			else

				frm.listaDepartamento.value	= arListaDepartamento.toString();

		}

		//valida campos do agendamento tipo: exame

		if(frm.flagOQueDesejaAgendar[1].checked){

			if(frm.exameNome.value.length == 0){

				alert("Informe o NOME DO EXAME para prosseguir!");

				frm.exameNome.focus();

				return false;

			}

			if(frm.exameSolicitadoPor.value.length == 0){

				alert("Informe por quem FOI SOLICITADO O EXAME para prosseguir!");

				frm.exameSolicitadoPor.focus();

				return false;

			}

			if(frm.flagUsaLente[1].checked){

				if(frm.exameTipoLente.selectedIndex == 0){

					alert("Selecione o TIPO DE LENTE para prosseguir!");

					frm.exameTipoLente.focus();

					return false;

				}

			}

		}

		

		if(frm.agendamentoDia.selectedIndex == 0){

			alert("Selecione o DIA que deseja agendar a consutla para prosseguir!");

			frm.agendamentoDia.focus();

			return false;

		}

		if(frm.agendamentoMes.selectedIndex == 0){

			alert("Selecione o MÊS que deseja agendar a consutla para prosseguir!");

			frm.agendamentoMes.focus();

			return false;

		}

		if(!this.validaDataAgendamento(frm.agendamentoDia,frm.agendamentoMes,frm.agendamentoAno))

			return false;

		

		if(frm.agendamentoHora.value.length == 0){

			alert("Informe a HORA que deseja agendar a consutla para prosseguir!");

			frm.agendamentoHora.focus();

			return false;

		}

		if(!fieldMaskValidator.hora(frm.agendamentoHora))

			return false;

		

		//INI: Valida os telefones para confirmação

			var qtdTelsConfirmInformados = 0;

			if(frm.agendamentoTelConfirma_1.value.length > 0) qtdTelsConfirmInformados++;

			if(frm.agendamentoTelConfirma_2.value.length > 0) qtdTelsConfirmInformados++;

			if(frm.agendamentoTelConfirma_3.value.length > 0) qtdTelsConfirmInformados++;

			

			if(qtdTelsConfirmInformados < 2){

				alert("Informe no mínimo 2 TELEFONES PARA CONFIRMAÇÃO DA CONSULTA para prosseguir!");

				if(frm.agendamentoTelConfirma_1.value.length == 0)

					frm.agendamentoTelConfirma_1.focus();

				else if(frm.agendamentoTelConfirma_2.value.length == 0)

					frm.agendamentoTelConfirma_2.focus();

				else if(frm.agendamentoTelConfirma_3.value.length == 0)

					frm.agendamentoTelConfirma_3.focus();

				

				return false;

			}

			

			if(!fieldMaskValidator.telefoneComDDD(frm.agendamentoTelConfirma_1)) return false;

			if(!fieldMaskValidator.telefoneComDDD(frm.agendamentoTelConfirma_2)) return false;

			if(!fieldMaskValidator.telefoneComDDD(frm.agendamentoTelConfirma_3)) return false;

		//FIM: Valida os telefones para confirmação

		

		if(frm.agendamentoHoraContato.value.length == 0){

			alert("Informe o MELHOR HORÁRIO PARA CONTATO para prosseguir!");

			frm.agendamentoHoraContato.focus();

			return false;

		}

		if(!fieldMaskValidator.hora(frm.agendamentoHoraContato))

			return false;

		

		frm.btSubmit.value = 'Enviando os dados. Aguarde...';

		frm.btSubmit.disabled = true;

		return true;

		

		alert('ok');

		return false;

	}

	

	, validaDataAgendamento: function(cpDia,cpMes,cpAno){

		_dia = parseInt(cpDia.value,10);

		_mes = parseInt(cpMes.value,10);

		_ano = parseInt(cpAno.value,10);

		

		if(_dia < 10) _dia = '0' + _dia;

		if(_mes < 10) _mes = '0' + _mes;

		

		_mes30 = '04,06,09,11';

		_mes31 = '01,03,05,07,08,10,12';

		

		//valida o dia em mêses de 30 dias

		if((_mes30.indexOf(_mes) != -1) && (_dia > 30)){

			alert('O dia deverá estar entre 01 e 30!');

			cpDia.focus();

			return false;

		}

		//valida o dia em mêses de 31 dias

		if((_mes31.indexOf(_mes) != -1) && (_dia > 31)){

			alert('O dia deverá estar entre 01 e 31!');

			cpDia.focus();

			return false;

		}

		//valida o dia para o mês de fevereiro

		if(_mes == '02'){

			//bisexto

			if(_ano%4 == 0){

				if(_dia > 29){

					alert('O dia deverá estar entre 01 e 29!');

					cpDia.focus();

					return false;

				}

			}

			else{

				//normal

				if(_dia > 28){

					alert('O dia deverá estar entre 01 e 28!');

					cpDia.focus();

					return false;

				}

			}

		}

		

		//valida se a data escolhida é menor do que a data de hoje

		var dtHj 	= new Date();

		_diaHj		= dtHj.getDate();

		_mesHj		= (dtHj.getMonth()+1);

		_anoHj		= dtHj.getYear();

		

		if(_ano == _anoHj){

			var nomeMes = cpMes.options[_mesHj].text.toUpperCase()+' de '+_anoHj;

			if(_mes < _mesHj){

				alert("Não é possível solicitar o agendamento em um mês anterior à "+nomeMes+"!");

				cpMes.focus();

				return false;

			}

			if((_mes == _mesHj) && (_dia < _diaHj)){

				alert("Não é possível solicitar o agendamento em um dia anterior à "+_diaHj+' de '+nomeMes+"!");

				cpDia.focus();

				return false;

			}

		}

		

		return true;

	}

	

	, habilitaComboConvenio: function(flag){

		if(flag == 'SIM'){

			$('convenioPaciente').disabled = false;

			$('convenioPaciente').focus();

		}

		else{

			$('convenioPaciente').options[0].selected = true;

			$('convenioPaciente').disabled = true;

		}

		return true;

	}

	

	, habilitaConsultaExame: function(flag){

		if(flag == 'CONSULTA'){

			$('campos_exame').hide();

			$('campos_consulta').show();

			//reseta os campos

			$('flagConsultaRotina_s').checked 		= false;

			$('flagConsultaRotina_n').checked 		= false;

			

			var objDepartamento						= $('frmAgendamento').departamento;

			for(i = 0; i < objDepartamento.length; i++){

				objDepartamento[i].checked = false;

			}

			

			$('consultaObs').value 					= '';

		}

		else{

			$('campos_consulta').hide();

			$('campos_exame').show();

			//reseta os campos

			$('exameNome').value 					= '';

			$('exameSolicitadoPor').value 			= '';

			$('flagUsaLente_n').checked 			= true;

			$('exameTipoLente').disabled			= true;

			$('exameTipoLente').options[0].selected	= true;

			$('exameObs').value 					= '';

		}

	}

	

	, habilitaComboTipoLente: function(flag){

		if(flag == 'SIM'){

			$('exameTipoLente').disabled = false;

			$('exameTipoLente').focus();

		}

		else{

			$('exameTipoLente').options[0].selected = true;

			$('exameTipoLente').disabled = true;

		}

		

		return true;

	}

}


