// Funcoes globais em JavaScript

// JavaScript Document


// validar o email
function validaMail(mail) {
  if (mail != "") {
	 if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail))){
		// email invalido!
		//alert("Por favor informe um endereço de e-mail válido.")
		//mail.focus();
		return(1);
	 }
  }
  else
	 return(0);
}

// VALIDACAO DE CAMPOS DO FORMULARIO
/*

No campo colocar o id com um "-" na frente e colocar no title o nome do campo que aparecera na mensagem de erro
Exemplo: <input type="text" id="-nome" name="nome" title="Nome Completo" size="40" class="formularios" />
No form: <form method="post" name="formbolsa" id="formbolsa" action="bolsa_pos_inscricao.php" onsubmit="return validacampos('formbolsa');">
*/
function validacampos(formulario){
    var campo = document.forms[formulario];
    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    :  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    :  MascaraCEP(campo, teclaPress, objeto)           				 
// DESCRICAO :  Formata entrada de dados para CEP      				
// SINTÁXE   :  onKeyPress="MascaraCEP('cep',this,event);"
// PARAMETROS:  myfield  - o campo que sofrerá esta restricao(basta colocar this)         				 
//		        e - evento(basta colocar o event)   	
//              MascaraCEP('cep', window.event.keyCode, 'document.form1.cep');
// RETORNO   :  true caso seja numero e false caso contrario
/////////////////////////////////////////////////////////////////////////////////////////

	function MascaraCEP (formato, keypress, objeto)
	{
	campo = eval (objeto);
	if (formato=='cep')
		{
		caracteres = '01234567890';
		separacoes = 1;
		separacao1 = '-';
		conjuntos = 2;
		conjunto1 = 5;
		conjunto2 = 3;
		if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < 
		(conjunto1 + conjunto2 + 1))
			{
	if (campo.value.length == conjunto1) 
			   campo.value = campo.value + separacao1;
			}
		else 
			event.returnValue = false;
		}
	}






// scripts personalizados

function link(end){
	window.open(end,"nova","height=220,width=220,left=160,top=180,scroll=auto");
}



/***********************************************
* Fixed ToolTip script- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
		
var tipwidth='220px' //default tooltip width
var tipbgcolor='#F7E6CD'  //tooltip bgcolor
var disappeardelay=250  //tooltip disappear speed onMouseout (in miliseconds)
var vertical_offset="0px" //horizontal offset of tooltip from anchor link
var horizontal_offset="-3px" //horizontal offset of tooltip from anchor link

/////No further editting needed

var ie4=document.all
var ns6=document.getElementById&&!document.all

if (ie4||ns6)
document.write('<div id="fixedtipdiv" style="visibility:hidden;width:'+tipwidth+';background-color:'+tipbgcolor+'" ></div>')

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}


function showhide(obj, e, visible, hidden, tipwidth){
if (ie4||ns6)
dropmenuobj.style.left=dropmenuobj.style.top=-500
if (tipwidth!=""){
dropmenuobj.widthobj=dropmenuobj.style
dropmenuobj.widthobj.width=tipwidth
}
if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
obj.visibility=visible
else if (e.type=="click")
obj.visibility=hidden
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
if (whichedge=="rightedge"){
var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
}
else{
var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
}
return edgeoffset
}

function fixedtooltip(menucontents, obj, e, tipwidth){
if (window.event) event.cancelBubble=true
else if (e.stopPropagation) e.stopPropagation()
clearhidetip()
dropmenuobj=document.getElementById? document.getElementById("fixedtipdiv") : fixedtipdiv
dropmenuobj.innerHTML=menucontents

if (ie4||ns6){
showhide(dropmenuobj.style, e, "visible", "hidden", tipwidth)
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
}
}

function hidetip(e){
if (typeof dropmenuobj!="undefined"){
if (ie4||ns6)
dropmenuobj.style.visibility="hidden"
}
}

function delayhidetip(){
if (ie4||ns6)
delayhide=setTimeout("hidetip()",disappeardelay)
}

function clearhidetip(){
if (typeof delayhide!="undefined")
clearTimeout(delayhide)
}


/***********************************************
* End Fixed ToolTip script
***********************************************/









// 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    :  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    :  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;
}
