﻿//Função para Mostrar Div
function showDiv(id)
{
    document.getElementById(id).style.display = '';
}

//Função para Ocultar Div
function hideDiv(id)
{
    document.getElementById(id).style.display = 'none';
}

//Função para Mostrar Div a partir de um checkboxlist
function showDivCbl(id,estado)
{
    if(document.getElementById(estado).checked == true)
        document.getElementById(id).style.display = '';
    else
        document.getElementById(id).style.display = 'none';
}

// Função para abrir janela
function visualizar(url,w,h)
{
	var w1, h2, nav

	nav = navigator.appName;

	if(w==1 || h==1)
	{
		if(nav.search("Microsoft")==true)
		{
			w1 = screen.width;
			h2 = screen.height;
		}
		else
		{
			w1 = screen.width - 20;
			h2 = screen.height - 70;			
		}
		window.open(url,'xxx','width='+w1+'px,height='+h2+'px,scrollbars=yes,resizable=yes,menubar=no,statusbar=no,left=0,top=0');
	}
	else
	{
		w1 = (screen.width - w)/2;
		h2 = (screen.height - h)/2;
		window.open(url,'xxx','width='+w+'px,height='+h+'px,resizable=yes,scrollbars=yes,menubar=no,statusbar=no,left='+w1+'px,top='+h2+'px');
	}
}

// Máscara para RA (000.0.00.000)
function mascara_ra(e, ra)
{
	if(window.event) { 
		_TXT = e.keyCode; 
	}
	else 
		if(e.which) { 
			_TXT = e.which; 
		}
	if(_TXT > 47 && _TXT < 58) {
		var myra = '';
		myra = myra + ra.value;
		if (myra.length == 3) {
			myra = myra + '.';
			ra.value = myra;
		}
		if (myra.length == 5) {
			myra = myra + '.';
			ra.value = myra;
		}
		if (myra.length == 8) {
			myra = myra + '.';
			ra.value = myra;
		}

		return true;
	}
	else{
		if ((_TXT != 8) && (_TXT != 13) && (_TXT != 14) && (_TXT != undefined)){ 
			return false; 
		}
	 	else { 
			return true; 
		}
	}
}

//MÁSCARA PARA DATA
function mascara_data(e, data)
{
	if(window.event) { 
		_TXT = e.keyCode; 
	}
	else 
		if(e.which) { 
			_TXT = e.which; 
		}
	if(_TXT > 47 && _TXT < 58) {
		var mydata = '';
		mydata = mydata + data.value;
		if (mydata.length == 2) {
			mydata = mydata + '/';
			data.value = mydata;
		}
		if (mydata.length == 5) {
			mydata = mydata + '/';
			data.value = mydata;
		}
		return true;
	}
	else{
		if ((_TXT != 8) && (_TXT != 13) && (_TXT != 14) && (_TXT != undefined)){ 
			return false; 
		}
	 	else { 
			return true; 
		}
	}
}

//Função para validar CPF
function Valida_Cpf(cpf, id) 
{ 
    var i, s
    s = cpf; 
    var c = s.substr(0,9); 
    var dv = s.substr(9,2); 
    var d1 = 0; 
	if(s.length == 11)
	{
        for (i = 0; i < 9; i++) 
        { 
            d1 += c.charAt(i)*(10-i); 
        } 
            if (d1 == 0) 
            { 
                //alert("CPF Invalido") 
                return false; 
            } 
        d1 = 11 - (d1 % 11); 
        if (d1 > 9) d1 = 0; 
        if (dv.charAt(0) != d1) 
        { 
            //alert("CPF Invalido") 
            return false; 
        } 

        d1 *= 2; 
        for (i = 0; i < 9; i++) 
        { 
            d1 += c.charAt(i)*(11-i); 
        } 
        d1 = 11 - (d1 % 11); 
        if (d1 > 9) d1 = 0; 
        if (dv.charAt(1) != d1) 
        { 
            alert("CPF Inválido!");
            id.value = "";
            return false;
        } 
		return true;
	}
	alert("CPF Inválido!");
	id.value = "";
	return false;
}

// Função para permitir somente a digitação de número
function permiteNumero(e, input)
{
	if(window.event) { 
		_TXT = e.keyCode; 
	}
	else 
		if(e.which) { 
			_TXT = e.which; 
		}
	if(_TXT > 47 && _TXT < 58) {
		var myinput = '';
		myinput = myinput + input.value;
		input.value = myinput;
		return true;
	}
	else{
		if ((_TXT != 8) && (_TXT != 13) && (_TXT != 14) && (_TXT != undefined)){ 
			return false; 
		}
	 	else { 
			return true; 
		}
	}
	return true;
}



// Função para permitir somente a digitação de número
function permiteNumeroVirgula(e, input)
{
	if(window.event) { 
		_TXT = e.keyCode; 
	}
	else 
		if(e.which) { 
			_TXT = e.which; 
		}
	if((_TXT > 47 && _TXT < 58) || _TXT == 44) {
		var myinput = '';
		myinput = myinput + input.value;
		input.value = myinput;
		return true;
	}
	else{
		if ((_TXT != 8) && (_TXT != 13) && (_TXT != 14) && (_TXT != undefined)){ 
			return false; 
		}
	 	else { 
			return true; 
		}
	}
	return true;
}


// Função para mostrar a quantidade de caracteres digitada em um textarea (o número é mostrado em uma div)
function progresso_tecla(obj,max,div)
{
  var max = max;
  var ancho = 300;
  var progresso = document.getElementById(div);  
  if (obj.value.length < max) 
  {
    progresso.style.backgroundColor = "#FFFFFF";    
    progresso.style.backgroundImage = "url('../../js/textarea.png')";    
    progresso.style.color = "#000000";
    var pos = ancho-parseInt((ancho*parseInt(obj.value.length))/max);
    progresso.style.backgroundPosition = "-"+pos+"px 0px";
  } 
  else 
  {
    progresso.style.backgroundColor = "#CC0000";    
    progresso.style.backgroundImage = "url()";    
    progresso.style.color = "#FFFFFF";
  } 
  progresso.innerHTML = "("+obj.value.length+" / "+max+")";
}

// Função para aumentar/diminuir a fonte do site
var intFontSize = 12;
var intLineHeight = 23;
function fncTrocaFontSize(strSinal) {
  if (strSinal == "+")
  { 
    intFontSize += 5;
    intLineHeight += 5;
  }
  if (strSinal == "-") 
  {
    intFontSize -= 5;
    intLineHeight -= 5;
  }
  if (strSinal == "*") 
  {
    intFontSize = 12;     
    intLineHeight = 23;
  }   
  objDiv = (document.getElementById("divConteudoInterna"));
  objDiv.style.fontSize = intFontSize + "px";
  objDiv.style.lineHeight = intLineHeight + "px";
}

// Função para negritar TEXTO
function selection(obj, set_text_default){
  /* @author Bernardo Rufino
   * @license Creative Commons Developing Nations: http://creativecommons.org/licenses/devnations/2.0/
   * @description Function to manipulate the actual selection
   * @returns It returns an object with an attribute *text* with the current selection text and
   *      a method *setText()* that receives one argument, the text to be set in the selection.
   * @parameters The first is the object where you want the selection (DOM or String with id), and
   *       the second (optional), a boolean, if is true then the *setText()* method will
   *       append the text if there's no way to replace the selection.
   *
   *     !Nao retire essas informacoes!
   *     !Do not drop this information!
  */
  if(obj.constructor == String){obj = document.getElementById(obj);}
  var set_text = (set_text_default) ? function(text){obj.value += text;} : function(){return false;};
  var selection = {text: null, setText: set_text};
  if(document.selection){
    var range = document.selection.createRange();
    selection.text = range.text;
    selection.setText = function(text){
      range.text = text.replace(/\\r?\\n/g, "\\r\\n");
    }
  } else if(typeof(obj.selectionStart) != "undefined"){
    selection.text = obj.value.substring(obj.selectionStart, obj.selectionEnd);
    selection.setText = function(text){
      obj.value = obj.value.substring(0, obj.selectionStart) + text + obj.value.substring(obj.selectionEnd);
    }
  } else if(window.getSelection){
    selection.text = window.getSelection().toString();
  }
  return selection;
}

function negrito(id)
{
  var texto, selected;
  texto = document.getElementById(id).value;
  alert(texto);
  selected = selection(id, true); 
  selected.setText("<b>" + selected.text + "</b>");
}