// Funcoes globais em JavaScript


// scripts personalizados

function link(end){
	window.open(end,"nova","height=220,width=220,left=160,top=180,scroll=auto");
}






// FUNCOES UTILIZADAS NAS MAIORIAS DOS CLIENTES

/////////////////////////////////////////////////////////////////////////////////////////
// FUNCAO    :  MM_openBrWindow(theURL,winName,features)               				 
// DESCRICAO :  Abrir janela padrao do DW               				
// SINTÁXE   :  onClick="MM_openBrWindow(theURL,winName,features)"
// PARAMETROS:  theURL - URL do arquivo que ira abrir na janela
//		        winName - nome da janela
// 				features - Caracteristicas da janela. Ex.: toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=400,height=350
/////////////////////////////////////////////////////////////////////////////////////////
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}




/////////////////////////////////////////////////////////////////////////////////////////
// FUNCAO    :  ShowImage(nome,detalhe)               				 
// DESCRICAO :  Passa o nome e descricao da fotos para ampliar na tela               				
// SINTÁXE   :  onClick="ShowImage(nome,detalhe)"
// PARAMETROS:  nome - Nome da fotos que sera exibida
//		        detalhe - descricao da fotos
/////////////////////////////////////////////////////////////////////////////////////////
function ShowImage(nome, detalhe) {
	document.images["NomeImagem"].src=nome;
	document.depoimento.depoimento.value=detalhe;
}


/////////////////////////////////////////////////////////////////////////////////////////
// FUNCAO    :  mudaFoco(caracteres,focoAtual,novoFoco)               				 
// DESCRICAO :  Passa o foco para outro componente apos digitar n caracteres                				
// SINTÁXE   :  onkeyup="javascript:mudaFoco(caracteres,focoAtual,novoFoco)"
// PARAMETROS:  caracteres - Numero - Quantidade de caracteres digitados ate perder o foco           				 
//		        focoAtual - Componente - Componente de um form onde será digitado os caracteres
// RETORNO   :  novoFoco - Componente - Componente de um form que ganhara o foco apos ser
//										digitado a qtd de caracteres no focoAtual
/////////////////////////////////////////////////////////////////////////////////////////
 function mudaFoco(caracteres,focoAtual,novoFoco)
 {
  if (focoAtual.value.length==caracteres) 
  {
    novoFoco.focus();		
  }
 }




 /////////////////////////////////////////////////////////////////////////////////////////
// FUNCAO    :  numbersOnly(myfield,e)               				 
// DESCRICAO :  Impede que o usuario digite valores nao numericos                				
// SINTÁXE   :  onKeyPress="return numbersOnly(this, event)"
// PARAMETROS:  myfield  - o campo que sofrerá esta restricao(basta colocar this)         				 
//		        e - evento(basta colocar o event)   		
// RETORNO   :  true caso seja numero e false caso contrario
/////////////////////////////////////////////////////////////////////////////////////////

function numbersOnly(myfield, e)
{
    if (myfield.length ==0)
	    myfield.value=0;  
	  var key;
	  var keychar;
    if (window.event)
     key = window.event.keyCode;
    else if (e)
     key = e.which;
    else
     return true;
	  keychar = String.fromCharCode(key);
    if ((key==null) || (key==0) || (key==8) || 
		      (key==9)|| (key==13)|| (key==27) )
     return true;
    else if ((("0123456789").indexOf(keychar) > -1))
	     return true;
	else
   return false;
}




 /////////////////////////////////////////////////////////////////////////////////////////
// FUNCAO    :  Mascara(tipo, campo, teclaPress)           				 
// DESCRICAO :  Formata entrada de dados para CPF, CNPJ, DATA, TEL      				
// SINTÁXE   :  onKeyPress="Mascara('TEL',this,event);"
// PARAMETROS:  myfield  - o campo que sofrerá esta restricao(basta colocar this)         				 
//		        e - evento(basta colocar o event)   		
// RETORNO   :  true caso seja numero e false caso contrario
/////////////////////////////////////////////////////////////////////////////////////////

function Mascara(tipo, campo, teclaPress) {
	if (window.event)
	{
		var tecla = teclaPress.keyCode;
	} else {
		tecla = teclaPress.which;
	}

	var s = new String(campo.value);
	// Remove todos os caracteres à seguir: ( ) / - . e espaço, para tratar a string denovo.
	s = s.replace(/(\.|\(|\)|\/|\-| )+/g,'');
 
	tam = s.length + 1;
 
	if ( tecla != 9 && tecla != 8 ) {
		switch (tipo)
		{
		case 'CPF' :
			if (tam > 3 && tam < 7)
				campo.value = s.substr(0,3) + '.' + s.substr(3, tam);
			if (tam >= 7 && tam < 10)
				campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,tam-6);
			if (tam >= 10 && tam < 12)
				campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,3) + '-' + s.substr(9,tam-9);
		break;
 
		case 'CNPJ' :
 
			if (tam > 2 && tam < 6)
				campo.value = s.substr(0,2) + '.' + s.substr(2, tam);
			if (tam >= 6 && tam < 9)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,tam-5);
			if (tam >= 9 && tam < 13)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,tam-8);
			if (tam >= 13 && tam < 15)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,4)+ '-' + s.substr(12,tam-12);
		break;
 
		case 'TEL' :
			if (tam > 2 && tam < 4)
				campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,tam);
			if (tam >= 7 && tam < 11)
				campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,4) + '-' + s.substr(6,tam-6);
		break;
 
		case 'DATA' :
			if (tam > 2 && tam < 4)
				campo.value = s.substr(0,2) + '/' + s.substr(2, tam);
			if (tam > 4 && tam < 11)
				campo.value = s.substr(0,2) + '/' + s.substr(2,2) + '/' + s.substr(4,tam-4);
		break;
		}
	}
}




 /////////////////////////////////////////////////////////////////////////////////////////
// FUNCAO    :  validacampos(thisform)           				 
// DESCRICAO :  Valida os campos com "id" iniciado por "_"
// SINTÁXE   :  onsubmit="return validacampos(thisform);"
// PARAMETROS:  <input type="text" name="Nome" id="_Nome" maxlength="50" title="Nome completo"/>		
// RETORNO   :  true envia form e false mensagem de erro
/////////////////////////////////////////////////////////////////////////////////////////

function validacampos(thisform){
    var campo = thisform;
    var i=0;
    for (i=0; i<campo.elements.length; i++){
       var nomecampo = campo.elements[i].id;
      if (nomecampo.substring(1,0) == '_'){
            var valida = campo.elements[i].value;
          if (valida == ''){
              alert('O campo: ' +  campo.elements[i].title + ' deve ser preenchido!');
             return false;
         }
      }
   }
}


/////////////////////////////////////////////////////////////////////////////////////////
// FUNCAO    :  consultaCEP()           				 
// DESCRICAO :  Abre janela com consulta de CEP dos Correios     				
// SINTÁXE   :  javascript: consultaCEP();
// PARAMETROS:  myfield  - o campo que sofrerá esta restricao(basta colocar this)         				 
//		        e - evento(basta colocar o event)   		
// RETORNO   :  true caso seja numero e false caso contrario
/////////////////////////////////////////////////////////////////////////////////////////

function consultaCEP()
{
	window.open('http://www.correios.com.br/servicos/cep/cep_default.cfm','ConsultaCep','location=no,resize=no, width=730,height=400,left=18,top=18,maximized=0,scrollbars=1');
}



/////////////////////////////////////////////////////////////////////////////////////////
// FUNCAO    :  abrir_layer_select_valor(selObj,div,valor,restore)             				 
// DESCRICAO :  Abri e fecha layer com select            				
// SINTÁXE   :  onChange="abrir_layer_select_valor(this,'novo_div_para_abrir','valor_no_select',0)"
// PARAMETROS:  novo_div_para_abrir - Nome do div que devera abrir quando o valor no select for igual ao 'valor_no_select'          				 
//		        valor_no_select - valor no select que abre a div
// EXEMPLO   :  <select name="ensino_medio" id="ensino_medio" onChange="abrir_layer_select_valor(this,'como_conheceu_div','outros',0)">
//				<div id="como_conheceu_div" style="display: none;"><input type="text" size="15" id="como_conheceu" name="como_conheceu" />
/////////////////////////////////////////////////////////////////////////////////////////

function abrir_layer_select_valor(selObj,div,valor,restore) { //v3.0

	opcao = selObj.options[selObj.selectedIndex].value; // opcao

	// se valor da selecao = valor pre definido entao abre div
	if(opcao==valor) {
		document.getElementById(div).style.display="block";
	}  else {
		document.getElementById(div).style.display="none"; // inicia fechado	
	}
	
	if (restore) selObj.selectedIndex=0;
}







/////////////////////////////////////////////////////////////////////////////////////////
// FUNCAO    :  me(div)             				 
// DESCRICAO :  Abri div (para ocultar conteudo e mostrar apenas quanto usuario clica no link            				
// SINTÁXE   :  <span onClick="me('link1'); ma('link1_mais');" style="cursor: pointer;">
// PARAMETROS:  div - Nome do div que devera abrir quando usuario clicar
// EXEMPLO   :  <div id="link3_mais" style="border-bottom: 1px solid rgb(240, 240, 240); display: block; height: 25px;">
//              <span onClick="me('link3'); ma('link3_mais');" style="cursor: pointer;">
//				<img width="9" vspace="8" height="9" border="0" align="absmiddle" alt="" src="/2007/site/template/images/img_mais.gif" />
//				<strong>CONTEUDO</strong></span></div>
//				<div id="link3" style="display: none;"><span onClick="me('link3'); ma('link3_mais');" style="cursor: pointer;">[CONTEUDO AQUI]</div>
/////////////////////////////////////////////////////////////////////////////////////////

function me(div) {

	var divq = document.getElementById(div).style;

		if(divq.display == 'none') {
			divq.display = 'block';
		}
		else {
			divq.display = 'none';
		}
}


/////////////////////////////////////////////////////////////////////////////////////////
// FUNCAO    :  ma(div)             				 
// DESCRICAO :  Fecha div (para ocultar conteudo e mostrar apenas quanto usuario clica no link            				
// SINTÁXE   :  <span onClick="me('link1'); ma('link1_mais');" style="cursor: pointer;">
// PARAMETROS:  div - Nome do div que devera abrir quando usuario clicar
// EXEMPLO   :  <div id="link3_mais" style="border-bottom: 1px solid rgb(240, 240, 240); display: block; height: 25px;">
//              <span onClick="me('link3'); ma('link3_mais');" style="cursor: pointer;">
//				<img width="9" vspace="8" height="9" border="0" align="absmiddle" alt="" src="/2007/site/template/images/img_mais.gif" />
//				<strong>CONTEUDO</strong></span></div>
//				<div id="link3" style="display: none;"><span onClick="me('link3'); ma('link3_mais');" style="cursor: pointer;">[CONTEUDO AQUI]</div>
/////////////////////////////////////////////////////////////////////////////////////////
function ma(div) {

	var divq = document.getElementById(div).style;

		if (divq.display == 'block') {
			divq.display = 'none';
		}
		else {
			divq.display = 'block';
		}
}

