// JavaScript Document
/*FUNÇÃO DE MÁSCARAS GERAIS */
/*Descrição.: formata um campo do formulário de
acordo com a máscara informada...
Parâmetros: - objForm (o Objeto Form)
- strField (string contendo o nome do textbox)

- sMask (mascara que define o formato que o dado será apresentado, usando o algarismo "9" para definir números e o símbolo "!" para qualquer caracter... 
- evtKeyPress (evento)
Uso.......: <input type="textbox" name="xxx" onkeypress="return txtBoxFormat(document.rcfDownload, 'str_cep', '99999-999', event);">
- Observação: As máscaras podem ser representadas como os exemplos abaixo:
CBO -> 9999-99
CEP -> 99.999-999
CPF -> 999.999.999-99
CNPJ -> 99.999.999/9999-99
Data -> 99/99/9999
Telefone -> (99) 9999-9999
Processo -> 99.999999999/999-99
C/C -> 999999-!
* E por aí vai... ***/

function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

if(document.all) { // Internet Explorer
    nTecla = evtKeyPress.keyCode;
} else if(document.layers) { // Nestcape
    nTecla = evtKeyPress.which;
} else {
    nTecla = evtKeyPress.which;
    if (nTecla == 8) {
        return true;
    }
}

sValue = objForm[strField].value;
// Limpa todos os caracteres de formatação que
// já estiverem no campo.
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( " ", "" );
sValue = sValue.toString().replace( " ", "" );
fldLen = sValue.length;
mskLen = sMask.length;

i = 0;
nCount = 0;
sCod = "";
mskLen = fldLen;

while (i <= mskLen) {
bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ":") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

if (bolMask) {
    sCod += sMask.charAt(i);
    mskLen++;
} else {
    sCod += sValue.charAt(nCount);
    nCount++;
}
i++;
}

objForm[strField].value = sCod;
if (nTecla != 8) { // backspace
    if (sMask.charAt(i-1) == "9") { // apenas números...
    return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
else { // qualquer caracter...
    return true;
}
} else {
    return true;
}
}

/*AUTO TAB - ao prencher o campo, automaticamente manda o foco para o próximo!*/
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
    var keyCode = (isNN) ? e.which : e.keyCode;
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    if(input.value.length >= len && !containsElement(filter,keyCode)) {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
    }

function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
        found = true;
    else
        index++;
        return found;
    }

function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
        if (input.form[i] == input)index = i;
        else i++;
        return index;
    }
    return true;
}

/*LIMITAR CARACTERES*/
function limitaText( p_objCampo, p_permitido ) {
   if (  p_objCampo.value.length > p_permitido ) {
      alert(  "A quantidade de caracteres digitados ultrapassou "+p_permitido+". Os caracteres excedidos serão retirados.");
      p_objCampo.value =  p_objCampo.value.substr( 0, p_permitido )

      if ( p_objCampo.value.length > p_permitido )  
         p_objCampo.value =  p_objCampo.value.substr( 0, p_permitido-1 )
   }
}
////USAR onKeyUp="limitaText( this, 100 )" onKeyDown="limitaText( this, 100 );"

/*ABRE POPUP CENTRALIZADA NA PÁGINA */
var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
}
//chamar assim: javascript:"NewWindow('arquivo.php','','500','510','no')"

function Limpar(valor, validos) {
// retira caracteres invalidos da string
var result = "";
var aux;
for (var i=0; i < valor.length; i++) {
aux = validos.indexOf(valor.substring(i, i+1));
if (aux>=0) {
result += aux;
}
}
return result;
}

//Formata número tipo moeda usando o evento onKeyDown
function Formata(campo,tammax,teclapres,decimal) {
var tecla = teclapres.keyCode;
vr = Limpar(campo.value,"0123456789");
tam = vr.length;
dec=decimal

if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

if (tecla == 8 )
{ tam = tam - 1 ; }

if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
{

if ( tam <= dec )
{ campo.value = vr ; }

if ( (tam > dec) && (tam <= 5) ){
campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 6) && (tam <= 8) ){
campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
}
if ( (tam >= 9) && (tam <= 11) ){
campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 12) && (tam <= 14) ){
campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 15) && (tam <= 17) ){
campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
} 
}

function FormataValor(id,tammax,teclapres) {
    
        if(window.event) { // Internet Explorer
         var tecla = teclapres.keyCode; }
        else if(teclapres.which) { // Nestcape / firefox
         var tecla = teclapres.which;
        }

vr = document.getElementById(id).value;
vr = vr.toString().replace( "/", "" );
vr = vr.toString().replace( "/", "" );
vr = vr.toString().replace( ",", "" );
vr = vr.toString().replace( ".", "" );
vr = vr.toString().replace( ".", "" );
vr = vr.toString().replace( ".", "" );
vr = vr.toString().replace( ".", "" );
tam = vr.length;

if (tam < tammax && tecla != 8){ tam = vr.length + 1; }

if (tecla == 8 ){ tam = tam - 1; }

if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
if ( tam <= 2 ){
document.getElementById(id).value = vr; }
if ( (tam > 2) && (tam <= 5) ){
document.getElementById(id).value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }
if ( (tam >= 6) && (tam <= 8) ){
document.getElementById(id).value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
if ( (tam >= 9) && (tam <= 11) ){
document.getElementById(id).value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
if ( (tam >= 12) && (tam <= 14) ){
document.getElementById(id).value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
if ( (tam >= 15) && (tam <= 17) ){
document.getElementById(id).value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );}
}
}

/*Informativo */
function Enviar(Botao) {
if (Botao==1) {
document.getElementById("form_informativo").action="informativo.php";
} else {
document.getElementById("form_informativo").action="informativo_excluir.php";
}
document.getElementById("form_informativo").submit;}

function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) {
var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "meu_comentario";
var countBody = opt_countBody ? opt_countBody : "countBody";
var maxSize = opt_maxSize ? opt_maxSize : 1024;

var field = document.getElementById(countedTextBox);

if (field && field.value.length >= maxSize) {
field.value = field.value.substring(0, maxSize);
}
var txtField = document.getElementById(countBody);
if (txtField) {
txtField.innerHTML = field.value.length;
}
}

function Enquete(){
with (document.form_enquete) {
method = "POST";
action="enquete_votacao.php";
jan=window.open('enquete_votacao.php','enquete','width=400,height=310,scrollbar=yes');
target='enquete';
submit();
}
setTimeout('clear_form()', 200); 
return true;
}
function clear_form() {
document.form_enquete.reset();
}
function clearForm(oForm) {
   
  var elements = oForm.elements;
   
  oForm.reset();

  for(i=0; i<elements.length; i++) {
     
  field_type = elements[i].type.toLowerCase();
 
  switch(field_type) {
 
    case "text":
    case "password":
    case "textarea":
          case "hidden":  
     
      elements[i].value = "";
      break;
       
    case "radio":
    case "checkbox":
        if (elements[i].checked) {
          elements[i].checked = false;
      }
      break;

    case "select-one":
    case "select-multi":
                elements[i].selectedIndex = -1;
      break;

    default:
      break;
  }
    }
}
/* Banner */
function showPic(whichpic) {
if (document.getElementById) {
document.getElementById('banner_imagem').src = whichpic.href;
if (whichpic.title) {
document.getElementById('banner_legenda').childNodes[0].nodeValue = whichpic.title;
} else {
document.getElementById('banner_legenda').childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue;
}
return false;
} else {
return true;
}
} 

/*Convenios*/
function carregar() {
  var url;

  url = 'http://www.aspp.com.br/remote.php?g_servico='+document.Cadastro.g_servico[document.Cadastro.g_servico.selectedIndex].value;

  document.Cadastro.especialidade.options.length = 0;

  addItem(document.Cadastro.especialidade,"Aguarde...carregando","",false,document.Cadastro.especialidade.length);

  myIframe.location = url;

  document.Cadastro.especialidade.focus()
}

function inicia() {
  local1 = new Array();

  var maximo1, i, campo1;

  local1 = myIframe.loc;

  campo1 = document.Cadastro.especialidade;

  maximo1 = local1.length;

  document.Cadastro.especialidade.options.length = 0;

  addItem(campo1,'Selecione uma especialidade','',false,campo1.length)

  for (i=0;i<maximo1;i++) {
    addItem(campo1,local1[i][1],local1[i][0],false,campo1.length);
  }
}

function addItem(obj,strText,strValue,blSel,intPos) {
  var newOpt,i,ArTemp,selIndex;

  selIndex = (blSel)?intPos:obj.selectedIndex;

  newOpt = new Option(strText,strValue);

  Len = obj.options.length+1

  if (intPos > Len) return

  obj.options.length = Len

  if (intPos != Len) {
   ArTemp = new Array();

   for(i=intPos;i<obj.options.length-1;i++)

    ArTemp[i] = Array(obj.options[i].text,obj.options[i].value);

   for(i=intPos+1;i<Len;i++)

    obj.options[i] = new Option(ArTemp[i-1][0],ArTemp[i-1][1]);

  }

  obj.options[intPos] = newOpt;

  if (selIndex > intPos)

   obj.selectedIndex = selIndex+1;

  else if (selIndex == intPos)

   obj.selectedIndex = intPos;
}

//cidade
function carregar_cid() {
  var url;

  url = 'http://www.aspp.com.br/remote1.php?especialidade='+document.Cadastro.especialidade[document.Cadastro.especialidade.selectedIndex].value;

  document.Cadastro.cidade.options.length = 0;

  addItem(document.Cadastro.cidade,"Aguarde...carregando","",false,document.Cadastro.cidade.length);

  myIframe2.location = url;

  document.Cadastro.cidade.focus()
}

function inicia_cid() {
  local1 = new Array();

  var maximo1, i, campo1;

  local1 = myIframe2.loc;

  campo1 = document.Cadastro.cidade;

  maximo1 = local1.length;

  document.Cadastro.cidade.options.length = 0;

  addItem(campo1,'Todas as cidades','TODAS',false,campo1.length)

  for (i=0;i<maximo1;i++){
    addItem(campo1,local1[i][1],local1[i][0],false,campo1.length);
  }
}

function leapto(Cadastro){
	document.Cadastro.submit();
}

function imagens(whichpic) {
if (document.getElementById) {
document.getElementById('imagem').src = whichpic.href;
if (whichpic.title) {
document.getElementById('legenda_galeria').childNodes[0].nodeValue = whichpic.title;
} else {
document.getElementById('legenda_galeria').childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue;
}
return false;
} else {
return true;
}
}

function habilita_desabilita(valor_campo) {
  var campos;
  campos = document.getElementsByTagName('modulo');
  if(valor_campo == "Administrador"){
	for(var i=0; i < campos.length; i++){
	  if(document.getElementsByTagName('input')[i].type == 'checkbox'){
		document.getElementsByTagName('input')[i].disabled= true;
	  }
	}
  } else {
	for(var i=0; i < campos.length; i++){
	  if(document.getElementsByTagName('input')[i].type == 'checkbox'){
		document.getElementsByTagName('input')[i].disabled= true;
	  }
	}
  }
}

function isAdministrador() {
  var $administrador = document.getElementById("modulo0");
  var $checkboxes = document.getElementsByTagName("input");

  if ($administrador.checked) {
	for (var i = 0; i < $checkboxes.length; i++) {
	  if ($checkboxes[i].type == "checkbox") {
		$checkboxes[i].checked = false;
	  }
	}
	$administrador.checked = true;
  }
}

function isPastaAdministrador() {
  var $administrador = document.getElementById("pasta0");
  var $checkboxes = document.getElementsByTagName("input");

  if ($administrador.checked) {
	for (var i = 0; i < $checkboxes.length; i++) {
	  if ($checkboxes[i].type == "checkbox") {
		$checkboxes[i].checked = false;
	  }
	}
	$administrador.checked = true;
  }
}

function verifica_senha(){
	var senha3 = document.getElementById('senha_atual').value;
	var senha1 = document.getElementById('senha_nova').value;
	var senha2 = document.getElementById('redigite_senha_nova').value;
	if (senha1 == "" || senha2 == ""){
	  alert("Campos Nova Senha e Redigite a Senha são obrigatórios! Preencha-os corretamente.");
	  document.getElementById("senha_nova").focus();
	  return false;
	} else {
	  if (senha1 == senha3){
		alert("A senha nova é igual a senha atual! Se deseja alterar digite outra combinação.");
		document.getElementById("senha_nova").focus();
	  } else if (senha1 != senha2){
	  alert("As senhas digitadas não conferem.");
	  }
	}
}
/*pesquisa opinião */
function troca(url) {
    tag = '<img src=\"imagens/'+url+'.gif">';
    document.getElementById("img1").innerHTML = tag;
}