//Variáveis globais
var intervalo = true;
var browser = navigator.appName;

var intervalo;
var tmpElem;
var tempoIntervalo;

//mudaCor(a, acao, bck)
function mudaCor(a, acao, bck){
	if(acao == 'hover'){
		a.style.background = '#082b7f';
		a.style.color = '#FFF';
	}else if(acao == 'out' && bck.length == 0){
		a.style.background = '#FFF';
		a.style.color = '#082b7f';
	}
}

//clickAbc(abc)
function clickAbc(abc)
{
	var medicamento;
	//Esse padrão de busca foi adicionado em substituição ao indexOf() que estava sendo utilizado
	var pattern = /\/versao\/(es|en|pt)\/produtos/;

	if(pattern.test(document.getElementById('pag').value))
	{
		medicamento = document.getElementById('medicamento').value;
		document.form.action = 'index.asp?medicamento='+medicamento;
	}else{
		document.form.action = 'lstProdutos.asp';
	}

	document.getElementById('hidAbc').value = abc;
	document.getElementById('pagAtual').value = 0;

	document.form.submit();
}

//goToLstProdutos(medicamento, nmLinha)
function goToLstProdutos(medicamento, nmLinha){
	document.form.medicamento.value = medicamento;
	document.form.nmLinha.value = nmLinha;
	document.form.submit();
}

//opnProduto(idProduto)
function opnProduto(medicamento, idProduto, restrito, idProfissional){
    try {

        var url = "";

		var divProduto = 'divProduto'+idProduto;

		if(document.getElementById(divProduto) != null){

			if(restrito == '1' && idProfissional == '' && medicamento==-1){
				document.getElementById('areaRestrita').style.display = 'block';
			}else{
				document.getElementById(divProduto).style.display = 'block';
				document.getElementById('divDadosProduto').style.display = 'block';
				document.getElementById('divLstProdutos').style.display = 'none';
				document.getElementById('bordaBottomConteudo').style.display = 'none';

				//Envia um click do produto para o relatório
				if (idProfissional != '') {
				    
				    url = 'fAjax.asp?acao=cliqueProduto&idProfissional=' + idProfissional + '&idProduto=' + idProduto + '&medicamento=' + medicamento + '&temp=' + Math.random();

				    sendXmlHttpRequest(url, false, true);
				}
			}

		}else{

			if((restrito == '1' && medicamento==-1) && idProfissional == ''){
				document.getElementById('areaRestrita').style.display = 'block';
			}else{

				document.getElementById('divLstProdutos').style.display = 'none';
				document.getElementById('bordaBottomConteudo').style.display = 'none';
				document.getElementById('divAguardeDadosProduto').style.display = 'block';

				url = '/versao/es/produtos/fAjax.asp?acao=getDadosProduto&idProduto=' + idProduto + '&idProfissional=' + idProfissional + '&medicamento=' + medicamento + '&temp=' + Math.random();
				sendXmlHttpRequest(url);

				intervalo = window.setInterval(
					function(){
						if(intervalProcessingAjax == 'ok'){

							clearInterval(intervalo);

							if(returnValue != 'erro' && returnValue != ''){
								document.getElementById('divAguardeDadosProduto').style.display = 'none';
								document.getElementById('divDadosProduto').innerHTML += returnValue;
								document.getElementById('divDadosProduto').style.display = 'block';

								//Envia um click do produto para o relatório
								if(idProfissional != ''){
								    sendXmlHttpRequest('fAjax.asp?acao=cliqueProduto&idProfissional=' + idProfissional + '&idProduto=' + idProduto + '&medicamento=' + medicamento + '&temp=' + Math.random(), false, true);
								}

								returnValue = '';
							}else{
								alert('Erro no processamento!');
								document.getElementById('divLstProdutos').style.display = 'block';
								document.getElementById('bordaBottomConteudo').style.display = 'block';
							}
						}
					}
				, 500);
			}
		}
	}catch(e){
		alert(e);	
	}
}

//dadosProduto(produto)
function dadosProduto(produto, idProfissional){

	var vetDadosProduto = produto.getElementsByTagName('td');
	var divUsuario = document.getElementById('divUsuario');
	var areaRestrita = document.getElementById('areaRestrita');
	var idProduto = vetDadosProduto[0].innerHTML;

	//Verifica se possui id do Profissional ou se o Produto não é restrito
	if(idProfissional.length > 0 || vetDadosProduto[11].innerHTML.length == 0){
		var divDadosProduto = document.getElementById('divDadosProduto');
		var divLstProdutos = document.getElementById('divLstProdutos');
		var bordaBottomConteudo = document.getElementById('bordaBottomConteudo');

		//Se o display do div 'divDadosProduto' for 'none', recebe 'block' e os dados do produto
		if(divDadosProduto.style.display == 'none'){

			//Envia um click do produto para o relatório
			if(idProfissional.length > 0){
			    sendXmlHttpRequest('fAjax.asp?acao=cliqueProduto&idProfissional=' + idProfissional + '&idProduto=' + idProduto + '&temp=' + Math.random(), false);
				//alert(returnValue.replace(/<br \/>/g, '\n')); //Se for true
			}

			//insere os dados do produto nos divs correspondentes
			//Medicamento
			if(vetDadosProduto[1].innerHTML.length > 0){
				document.getElementById('spnMedicamento').innerHTML = vetDadosProduto[1].innerHTML;
			}

			//highlight
			if(vetDadosProduto[2].innerHTML.length > 0){
				document.getElementById('divHighlight').style.display = 'block';
				document.getElementById('divHighlight').innerHTML = vetDadosProduto[2].innerHTML;
			}else{
				document.getElementById('divHighlight').style.display = 'none';
			}

			//Imagem
			if(vetDadosProduto[3].innerHTML.length > 0){
				document.getElementById('imgProduto').src = vetDadosProduto[3].innerHTML;
				document.getElementById('imgProduto').alt = 'Imagem';
			}

			//Bula Disponível
			if(vetDadosProduto[4].innerHTML.length > 0){
				var download = vetDadosProduto[4].innerHTML;
				var pasta = download.substring(0, download.lastIndexOf('/'));
				var arquivo = download.substring(download.lastIndexOf('/')+1, download.length);

				document.getElementById('linkBula').href = "javascript:linkDownload_Click('" + pasta + "', '" + arquivo + "', '" + idProfissional + "', '" + idProduto + "');";
				document.getElementById('divBula').style.display = 'block';
			}else{
				document.getElementById('divBula').style.display = 'none';
			}

			//Apresentação
			if(vetDadosProduto[5].innerHTML.length > 0){
				document.getElementById('divApresentacao').style.display = 'block';
				document.getElementById('lblApresentacao').innerHTML = vetDadosProduto[5].innerHTML;
			}else{
				document.getElementById('divApresentacao').style.display = 'none';
			}

			//Classe Terapêutica
			if(vetDadosProduto[6].innerHTML.length > 0){
				document.getElementById('divClasseTerapeutica').style.display = 'block';
				document.getElementById('lblClasseTerapeutica').innerHTML = vetDadosProduto[6].innerHTML;
			}else{
				document.getElementById('divClasseTerapeutica').style.display = 'none';
			}

			//Princípio Ativo
			if(vetDadosProduto[7].innerHTML.length > 0){
				document.getElementById('divPrincipioAtivo').style.display = 'block';
				document.getElementById('lblPrincipioAtivo').innerHTML = vetDadosProduto[7].innerHTML;
			}else{
				document.getElementById('divPrincipioAtivo').style.display = 'none';
			}

			//Indicação
			if(vetDadosProduto[8].innerHTML.length > 0){
				document.getElementById('divIndicacao').style.display = 'block';
				document.getElementById('lblIndicacao').innerHTML = vetDadosProduto[8].innerHTML;
			}else{
				document.getElementById('divIndicacao').style.display = 'none';
			}

			//Hot Site
			if(vetDadosProduto[9].innerHTML.length > 0){
				document.getElementById('divHotSite').style.display = 'block';
				document.getElementById('lblHotSite').innerHTML = vetDadosProduto[9].innerHTML;
			}else{
				document.getElementById('divHotSite').style.display = 'none';
			}

			//Registro
			if(vetDadosProduto[10].innerHTML.length > 0){
				document.getElementById('divRegistro').style.display = 'block';
				document.getElementById('lblRegistro').innerHTML = vetDadosProduto[10].innerHTML;
			}else{
				document.getElementById('divRegistro').style.display = 'none';
			}

			//Display texto genérico
			if(vetDadosProduto[12].innerHTML.length > 0){
				document.getElementById('divTxtGenerico').style.display = 'block';
			}else{
				document.getElementById('divTxtGenerico').style.display = 'none';
			}

			divDadosProduto.style.display = 'block';
			divLstProdutos.style.display = 'none';
			bordaBottomConteudo.style.display = 'none';
		}
	}else{
		if(areaRestrita.style.display == 'block'){
			pisca(areaRestrita);
		}else{

			areaRestrita.style.display = 'block';
		}
		document.getElementById('email').focus();
	}
}

//fechaDadosProduto()
function fechaDadosProduto(divDadosProduto){
	document.getElementById(divDadosProduto).style.display = 'none';
	document.getElementById('divDadosProduto').style.display = 'none';
	document.getElementById('divLstProdutos').style.display = 'block';
	document.getElementById('bordaBottomConteudo').style.display = 'block';
}

//pisca(elem)
function pisca(elem){
	tmpElem = elem;
	tempoIntervalo = 0;
	intervalo = window.setInterval(fPisca, 70)
}

//fPisca()
function fPisca(){
	if(browser == 'Netscape'){
		tmpElem.style.opacity = (tmpElem.style.opacity == '1') ? '0.95': '1';
	}else{
		tmpElem.style.filter = (tmpElem.style.filter == 'alpha(opacity=100)') ? 'alpha(opacity=95)': 'alpha(opacity=100)';
	}
	
	tempoIntervalo++;
	
	if(tempoIntervalo >= 8){
		clearInterval(intervalo);
		if(browser == 'Netscape'){
			tmpElem.style.opacity = '1';
		}else{
			tmpElem.style.filter = 'alpha(opacity=100)';
		}
	}
}

//selOpcaoProduto(opcao)
function selOpcaoProduto(opcao){
	if(opcao == 'medicamento'){
		document.getElementById('cmbClasseTerapeutica')[0].selected = true;
		document.getElementById('cmbPrincipioAtivo')[0].selected = true;
	}else if(opcao == 'classeTerapeutica'){
		document.getElementById('cmbMedicamento')[0].selected = true;
		document.getElementById('cmbPrincipioAtivo')[0].selected = true;
	}else if(opcao == 'principioAtivo'){
		document.getElementById('cmbMedicamento')[0].selected = true;
		document.getElementById('cmbClasseTerapeutica')[0].selected = true;
	}
	
	var medicamento = document.getElementById('medicamento').value;
	document.form.action = 'index.asp?medicamento='+medicamento;
	document.form.submit();
}

//paginacao()
function paginacao(acao)
{
	if(acao == 'proximo')
		document.getElementById('pagAtual').value++;
	else if(acao == 'anterior')
		document.getElementById('pagAtual').value--;
	else if(acao == 'pagina')
	{
		
		var irPag = document.getElementById('irPag').value;
		var numPag = document.getElementById('numPag').value;
		
		if(parseInt(irPag) <= parseInt(numPag) && !isNaN(irPag) && irPag > 0)

			document.getElementById('pagAtual').value = irPag-1;
	}

	var medicamento;
	//Esse padrão de busca foi adicionado em substituição ao indexOf() que estava sendo utilizado
	var pattern = /\/versao\/(es|en|pt)\/produtos/;
	

	if(pattern.test(document.getElementById('pag').value))
	{
		medicamento 		= document.getElementById('medicamento').value;
		document.form.action	= 'index.asp?medicamento='+medicamento;
	}
	else
	{
		document.form.action = 'lstProdutos.asp';
	}

	document.form.submit();	
}

//linkDownload_Click()
function linkDownload_Click(pasta, arquivo, idProfissional, idProduto){		
	document.form.acao.value = 'download';
	document.form.pasta.value = pasta;
	document.form.arquivo.value = arquivo;	
	document.form.action = '/funcao/asp/download.asp';
	
	//Envia um click do produto(download) para o relatório
	if(idProfissional.length > 0){
	    sendXmlHttpRequest('fAjax.asp?acao=cliqueDownload&idProfissional=' + idProfissional + '&idProduto=' + idProduto + '&temp=' + Math.random(), false);
		//alert(returnValue.replace(/<br \/>/g, '\n')); //Se for true
	}
	
	document.form.submit();
	
	//var url = pasta + arquivo;
	//window.open(url, 'Bula', 'width=790,height=590,left=5,top=5,resizable=yes');	
}

//spnEsqueciSenha_Click()
function spnEsqueciSenha_Click(){
	document.getElementById('divFormLogin').style.display = 'none';
	document.getElementById('recuperarSenha').style.display = 'block';
}

//btnCancelar_Click
function btnCancelar_Click(){
	document.getElementById('divFormLogin').style.display = 'block';
	document.getElementById('recuperarSenha').style.display = 'none';
}

//btnLogar_Click
function btnLogar_Click(){
	var erro = false;
	
	var email = document.getElementById('email').value;
	var senha = document.getElementById('senha').value;
	
	if(email.length == 0 || !validaEmail(email)){
		document.getElementById('email').style.border = '1px solid #F00';
        document.getElementById('email').style.background = '#FFF7F1';
		document.getElementById('email').focus();
		erro = true;		
	}else{
		document.getElementById('email').style.border = '1px solid #3D6694';
        document.getElementById('email').style.background = '#F3F6FD';
	}

	if(senha.length == 0 || senha.indexOf(" ") > -1){
		document.getElementById('senha').style.border = '1px solid #F00';
        document.getElementById('senha').style.background = '#FFF7F1';		
		if(!erro){
			erro = true;
			document.getElementById('senha').focus();
		}		
	}else{
		document.getElementById('senha').style.border = '1px solid #3D6694';
        document.getElementById('senha').style.background = '#F3F6FD';
	}

	if(erro){
		document.getElementById('divAlert').style.display = 'block';
		document.getElementById('spnCampoIncorreto').innerHTML = 'Rellene los campos correctamente!';
		return false;
		
	}else{
		document.getElementById('acao').value = 'logIn';
		document.getElementById('form').action = '/funcao/asp/logarSite.asp';
		document.getElementById('form').submit();
		return true;	
	}
}

//spnLogoff_Click
function spnLogoff_Click(){
	document.getElementById('acao').value = 'logOff';
	document.getElementById('form').action = '/funcao/asp/logarSite.asp';
	document.getElementById('form').submit();
}
