﻿function criaCookie (id, idx)
 {
  if (idx == 6) idx = 1; //Caso seja Principio Ativo, deixar todos abertos
  $.cookie('HIGHLIGHT-' + id, highlights.join(','));
  $.cookie('SHOW-' + id, idx);
 }


function buscaCookieLab(){
    if ($.cookie('bularium_lab') != null){
        var param = $.cookie('bularium_lab').split("&");
        for (i=0; i < param.length; i++){
            if (param[i].split("=")[0] == "highlight")
            {
                return param[i].split("=")[1];
            }   
        }
    }
    return null;
}

function makeBold (text)
  {
    var el;
    el = document.createElement("B");
    el.appendChild(document.createTextNode(text));
    if (buscaCookieLab() != null)
        el.style.backgroundColor = buscaCookieLab();
    else
        el.style.backgroundColor = "#d6d6e8";
    return el;
  }

function makeSuperscript (text)
   {
    var el;
    el = document.createElement("SUP");
    el.appendChild(document.createTextNode(text));
    return el;
   }

function changeText(el, sch, action) {
    var child;
    var b, i;
    var temp1, temp2;
    var newTextNode;
    var oldChildren = [];

    for (i = 0; i < el.childNodes.length; i++)
      oldChildren[i] = el.childNodes[i];

    if (el.nodeType == 3) {
        i = el.nodeValue.toUpperCase().indexOf(sch);
        if (i != -1) {
            temp1 = el.nodeValue.substr(i, sch.length);
            temp2 = el.nodeValue.substr(i + sch.length);
            el.nodeValue = el.nodeValue.substr(0, i);
            newTextNode = document.createTextNode(temp2);
            b = action(temp1);

            if (el.nextSibling) {
                el.parentNode.insertBefore(newTextNode, el.nextSibling);
                el.parentNode.insertBefore(b, el.nextSibling);
            }
            else {
                el.parentNode.appendChild(b);
                el.parentNode.appendChild(newTextNode);
            }
            
            changeText (newTextNode, sch, action);
        }

        return;
    }

    for (i = 0; i < oldChildren.length; i++)
    {
     changeText (oldChildren[i],sch, action);
    }
}

function highlightWords (div, words)
 {
  var arr, idx, word;
  div.normalize();

  for (idx = 0; idx < words.length; idx++) 
  {
   word = words[idx];
   word = word.replace(/^\s+|\s+$/g, '');
   if (word != '') 
     changeText (div, word, makeBold);
  }
 
  changeText (div, '\u00AE', makeSuperscript); // R
  changeText (div, '\u2122', makeSuperscript); // TM
 }

function createImgCol(rowEl, imgName, link) {
    var td, a, img;

    td = document.createElement('td');
    a = document.createElement('a');
    img = document.createElement('img');

    img.setAttribute('src', 'img/' + imgName);
    img.setAttribute('border', '0');
    if (link == '') {
        td.appendChild(img);
    }
    else {
        a.setAttribute("href", link);
        a.appendChild(img);
        td.appendChild(a);
    }

    rowEl.appendChild(td);
}

function createTextCol(dvEl, contents, link) {
    var sp, a, txt;

    sp = document.createElement('span');
    txt = document.createTextNode(contents);

    if ((link == '') || (contents == '')) {
        sp.appendChild(txt);
    }
    else {
        a = document.createElement('a');
        a.setAttribute("href", link);
        a.appendChild(txt);
        sp.appendChild(a);
    }

    dvEl.appendChild(sp);
}

function makePagingElements(ct, pN, pS, sS) {
    var i;
    var initPage, finalPage;
    var textRowEl; // imgRowEl
    var baseLink, link;

    initPage = Math.max(1, pN - 10);
    finalPage = Math.min(pN + 10, Math.ceil(ct / pS));

    // imgRowEl = document.getElementById('trPagingImg');
    textRowEl = document.getElementById('dvPagingText');

    baseLink = 'Search.aspx?t=' + tS + '&pS=' + pS + '&q=' + sS + '&pN=';

    if (pN == 1) { // pagina atual == primeiro
        //createImgCol(imgRowEl, 'b.gif', '');
        createTextCol(textRowEl, '', '');
    }
    else {
        link = baseLink + (pN - 1);
        // createImgCol(imgRowEl, 'b.gif', link);
        createTextCol(textRowEl, 'Anterior ', link);
    }

    for (i = initPage; i <= finalPage; i++) {
        if (i == pN) {
            //createImgCol(imgRowEl, 'ured.gif', '');
            createTextCol(textRowEl, i, '');
        }
        else {
            link = baseLink + i;
            //createImgCol(imgRowEl, 'u.gif', link);
            createTextCol(textRowEl, i, link);
        }
    }

    if (pN == Math.ceil(ct / pS)) { // pagina atual == ultimo
        //createImgCol(imgRowEl, 'larium.gif', '');
        createTextCol(textRowEl, '', '');
    }
    else {
        link = baseLink + (pN + 1);
        //createImgCol(imgRowEl, 'larium.gif', link);
        createTextCol(textRowEl, ' Mais', link);
    }

    textRowEl.firstChild.className = 'clFirstCol';
    //imgRowEl.firstChild.className = 'clFirstCol';

    textRowEl.lastChild.className = 'clLastCol';
    //imgRowEl.lastChild.className = 'clLastCol';
}

function validaCPF(cpf) {
    erro = new String;
    if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n";
    var nonNumbers = /\D/;
    if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";
    if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999") {
        erro += "Numero de CPF invalido!"
    }
    var a = [];
    var b = new Number;
    var c = 11;
    for (i = 0; i < 11; i++) {
        a[i] = cpf.charAt(i);
        if (i < 9) b += (a[i] * --c);
    }
    if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11 - x }
    b = 0;
    c = 11;
    for (y = 0; y < 10; y++) b += (a[y] * c--);
    if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11 - x; }
    if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])) {
        erro += "Digito verificador com problema!";
    }
    if (erro.length > 0) {
        //alert(erro);
        return false;
    }
    return true;
}

function ehNum(val) {
    return !isNaN(val)
}

function validaEMail(mail) {
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if (er.test(mail)) { return true; }
    else {return false;}
}

function formataNome(nome) {
    if ($.trim(nome) != "") {
        var nomes = nome.split(" ")
        nome = "";
        for (i = 0; i < nomes.length; i++) {
            if (nomes[i].length > 3 || nomes[i].indexOf(".") != -1) {
                nomes[i] = nomes[i].toLowerCase()
                char1 = nomes[i].charAt(0)
                nomes[i] = nomes[i].replace(char1, char1.toUpperCase()) 
            }
            else { 
                nomes[i] = nomes[i].toLowerCase()
            }
            nome = nome + " " + nomes[i];
        }
    }
    nome = $.trim(nome);
    char1 = nome.charAt(0)
    nome = nome.replace(char1, char1.toUpperCase())
    
    return nome;
}

function sair() {
    sai = confirm("Gostaria de responder nossa pesquisa de satisfação antes de sair? \nCaso deseje responder clique em Cancelar. \n\nPara Sair clique OK.");
    if (sai) {
        return true;
    }
    else {
        window.location = "Default.aspx?resp=s";
        return false;
    }
}


 function enter(event, el) {
		if (event.keyCode == 13) {
			document.getElementById(el).click();
    		return false; 
    	}  
	    else
		return true;
 } 
 
 
 function expandAll()
    {
     if ($("#aExpand").html() == 'Expandir todos os itens da Bula'){
        
        $('.dvTexto').css('display','block'); 
        $('.clMore').addClass("clMinus");
        $("#aExpand").html('Comprimir todos os itens da Bula');
        
    }
    else{
        
        $('.dvTexto').css('display','none'); 
        $('.clMinus').removeClass("clMinus");
        $("#aExpand").html('Expandir todos os itens da Bula');
        
    }
}


function exibeVideo(id, titulo){
   tb_show(titulo,'Video.aspx?id=' + id + 'TB_iframe=true&height=350&amp;width=450','thickbox');           
   return false;

}

function tact(selector) { //trata acessibilidade campo texto
    $(selector).keydown(function() {
    if ($(selector).val() == "|") {
        $(selector).val("")
        $(selector).css("color", "#000");
        }
    });

    $(document).ready(function() {
    if ($(selector).val() == "|") {
       $(selector).css("color", "#FFF");
        }
    });


}





