// JavaScript Document 
function submit_form(id)
{
   document.getElementById(id).submit();
}
function troca_fundo_popup(id,image)
{
  document.getElementById(id).src='../images/'+image;
}
function trata_data(id)
{
	alert('ok');	
}
function alerta(str)
{
	alert(str);	
}
function mascara_campo2(id,tipo)
{
	val = document.getElementById('razaosocial').value;	
	if(val == 'PF')
	{
		mascara_campo(id,'cpf');
	}
	else if(val == 'PJ')
	{
		mascara_campo(id,'cnpj');	
	}
	else
	{
		alert('Selecione primeiro a Razao Social');
		document.getElementById('razaosocial').focus();
		document.getElementById(id).value = '';		
	}
}
function mascara_campo(id,tipo)
{
    mydata = document.getElementById(id).value;
    if(tipo == "data")
    {         
		 if (mydata.length == 2)
		  {
			 mydata = mydata + '/';
			 document.getElementById(id).value=mydata;
		  }
		 if(mydata.length == 5)
		  {
			 mydata = mydata + '/';
				 document.getElementById(id).value=mydata;
		  }
    }
    else if(tipo == "cpf")
    {         
		 if (mydata.length == 3)
		  {
			 mydata = mydata + '.';
			 document.getElementById(id).value=mydata;
		  }
		 if(mydata.length == 7)
		  {
			 mydata = mydata + '.';
				 document.getElementById(id).value=mydata;
		  }
		 if(mydata.length == 11)
		  {
			 mydata = mydata + '-';
				 document.getElementById(id).value=mydata;
		  }
    }
    else if(tipo == "cnpj")
    {         
		 if (mydata.length == 2)
		  {
			 mydata = mydata + '.';
			 document.getElementById(id).value=mydata;
		  }
		 if(mydata.length == 6)
		  {
			 mydata = mydata + '.';
		     document.getElementById(id).value=mydata;
		  }
		 if(mydata.length == 10)
		  {
			 mydata = mydata + '/';
			 document.getElementById(id).value=mydata;
		  }
		 if(mydata.length == 15)
		  {
			 mydata = mydata + '-';
			 document.getElementById(id).value=mydata;
		  }
    }
    else if(tipo == "cep")
    {         
		 if (mydata.length == 5)
		  {
			 mydata = mydata + '-';
			 document.getElementById(id).value=mydata;
		  }
	}
}
function check_campos()
{
	tt = document.getElementById('tt').value;	
	check = document.getElementById('flag').value;	
	
	if(check == 0)
	{
		check = 1;
		flag = true;
	}
	else
	{
		check = 0;
		flag = false;
	}
	
	str = "";
	for(i=0;i<tt;i++)
	{
		document.getElementById('pes'+i).checked = flag;
		
		if(check == 1)
		{
		  nome = document.getElementById('hiden'+i).value+';';		
		  str = str+nome;
		}
	}
		
	document.getElementById('flag').value = check;
    document.getElementById('names').value = str;  
    val = document.getElementById('names').value;      
    //document.getElementById('names2').value = val.replace(/;{1}/gi,';\n');	
    document.getElementById('names2').value = val;	
}
function autoResize(acao)
{
	objTextArea = document.getElementById('names2');
	while (objTextArea.scrollHeight > objTextArea.offsetHeight)
	{
		if(acao == '+')
		{
			objTextArea.rows += 2;
		}
		else
		{
			objTextArea.rows -= 2;	
		}
	}
}
function joga_valor(id)
{ 
   nome = document.getElementById(id).value;
   val = document.getElementById('names').value;
   
   if(val != "")
   {
		 arrai = val.split(';');	  
	  
		 for(i=0;i<arrai.length-1;i++)
		 {
			val_flag = arrai[i];
			
			if(nome == val_flag)
			{
			   arrai.splice(i,1);
			   nome = "";
			}
		 }		 
		 
		 val = arrai.join(';');
   }

   if(nome != '')
   {
	   nome = nome+';';
   }
     
   document.getElementById('names').value = val+nome;   
   val2 = document.getElementById('names').value;      
   document.getElementById('names2').value = val2;
}
function check_campos2()
{
	tt = document.getElementById('tt').value;	
	check = document.getElementById('flag').value;	
	
	pesid = "";
	
	if(check == 0)
	{
		check = 1;
		flag = true;
	}
	else
	{
		check = 0;
		flag = false;
	}
	
	str = "";

	for(i=0;i<tt;i++)
	{
		document.getElementById('pes'+i).checked = flag;
		
		if(check == 1)
		{		   
		   cod = document.getElementById('pes'+i).value+';';
		   pesid = pesid+cod;		   
		   
		   nome = document.getElementById('hiden'+i).value+';';		
		   str = str+nome;
		}
	}
		
	document.getElementById('flag').value = check;
    document.getElementById('names').value = str;  
    val = document.getElementById('names').value;      
    //document.getElementById('names2').value = val.replace(/;{1}/gi,';\n');	
    document.getElementById('names2').value = val;	
	
	document.getElementById('pesid').value = pesid;
}
function joga_valor2(id,id2)
{ 
   nome = document.getElementById(id).value;
   cod = document.getElementById(id2).value;

   val = document.getElementById('names').value;
   val_pesid = document.getElementById('pesid').value;
   
   if(val != "")
   {
		 arrai = val.split(';');
		 arrai_pesid = val_pesid.split(';');
		 
	  
		 for(i=0;i<arrai.length-1;i++)
		 {
			val_flag = arrai[i];
			
			if(nome == val_flag)
			{
			   arrai.splice(i,1);
			   arrai_pesid.splice(i,1);
			   nome = "";
			   cod = "";
			}
		 }		 
		 
		 val = arrai.join(';');
		 val_pesid = arrai_pesid.join(';');
   }

   if(nome != '')
   {
	   nome = nome+';';
	   cod = cod+';';
   }
     
   document.getElementById('names').value = val+nome;
   val2 = document.getElementById('names').value;   
   document.getElementById('names2').value = val2;
      
   document.getElementById('pesid').value = val_pesid+cod;   
}
function check_grupo(id,idchec)
{
	check = document.getElementById(idchec).checked;
	
	if(check)
	{
		flag = true;
	}
	else
	{
		flag = false;
	}
		
	grupid = document.getElementById(id).value;		
	
	//alert(grupid);
	
	nome = document.getElementById('names').value;
	val_pesid = document.getElementById('pesid').value;				
	
	if(check)
	{
		arrai = grupid.split(';');
		arrai_pesid = val_pesid.split(';');
				
		for(i=0;i<arrai.length-1;i++)
		{
			nome = nome+document.getElementById('hiden'+i).value+';';
			val_pesid = val_pesid+document.getElementById('pes'+i).value+';';
			
			document.getElementById(arrai[i]).checked = flag;
		}//for
				
		document.getElementById('names').value = nome;
		document.getElementById('names2').value = nome;
		document.getElementById('pesid').value = val_pesid; 
	}
	else
	{
		arrainome = nome.split(';');
		
		arrai = grupid.split(';');
				
		for(i=0;i<arrai.length-1;i++)
		{
			val_flag = arrainome[i];
			nome = document.getElementById('hiden'+i);
			
			if(nome == val_flag)
			{
			   arrainome.splice(i,1);
			   arrai_pesid.splice(i,1);
			}
		}
	    
		nome = arrainome.join(';');
		val_pesid = arrai_pesid.join(';');
		
		document.getElementById('names').value = nome;
		document.getElementById('names2').value = nome;
		document.getElementById('pesid').value = val_pesid;
	}
}
function carrega_frame()
{
  document.getElementById('iframe').src='paginas/ramais_iframe.php?cod=1';
}
 function confirmar(msg,pagina)
 {
  
  var f = confirm(msg);  
  if(!f)
  {
   return false;
  }
  else
  {
    window.location.href=pagina;
  }
 }
 function confirma(txt)
 {
	return confirm(txt);
 }
 function confirma2(txt,cod)
 {
	if(confirm(txt))
	{
	   postar_valor(cod,'del_ark');
	}
	else
	{
		return false;
	}
			   
 } 
 function encaminhar(pagina)
 {
	window.location.href=pagina; 
 }
 function ver_campo(id)
 {
	val = document.getElementById(id).value;

    if(val.length<2)
	{
		alert('O campo deve conter pelo meno 2 caracter');
		return false;
	}
	else
	{
		return true;
	}
 }
function limpa_campo(id)
{
  document.getElementById(id).value='';
}
function carrega_frame2(cod,id_frame)
{
  document.getElementById(id_frame).src='paginas/ver_fotos_setor.php?cod='+cod;
}
function troca_classe(id,classe)
{
	document.getElementById(id).className = classe;
}
function troca_fundo(id,cor)
{
  document.getElementById(id).style.background=cor;
}
function troca_imagem(id,image)
{
  document.getElementById(id).src='images/'+image;
}
function troca_classe(id,classe)
{
  document.getElementById(id).className=classe;
}
function mostra_div(id)
{
   var element = document.getElementById(id);
   
   if (element != undefined) {
	   
	   var valor = element.style.display;
	   
	   if(valor=="none")
	   {
		   valor = "block";
	   }
	   else
	   {
		   valor = "none";
	   }   
	   
	   document.getElementById(id).style.display=valor;
	   
   }   
}

function mostra_div_area(id)
{
   valor = document.getElementById(id).style.display;

   if(valor=="none")
   {
      valor = "block";
   }
   else
   {
      valor = "none";
   }   
   
   document.getElementById(id).style.display=valor;
   
   if(valor=="block")
   {
	 document.getElementById('login').focus();   
   }
   
}
function ativa_evento(nome,coddono,titulo)
{	
	/*classe = document.getElementById('area').style.display;
	if(classe == "none")
	{
		nova = "block";
	}
	else
	{
		nova = "none";
	}*/
	
	document.getElementById('area').style.display = "block";
	document.getElementById('names').value = nome+';';
	document.getElementById('names2').value = nome+';';
	document.getElementById('pesid').value = coddono+';';
	document.getElementById('msgtitulo').value = 'RE: '+titulo;
	
	this.location = '#ancora';
}
function submeter_form(id)
{
	document.getElementById(id).submit();	
}
function postar_valor(val,id)
{
  document.getElementById(id).value = val;
}
function postar_valor_submit(id, val, id_form)
{
	document.getElementById(id).value = val;
	submeter_form(id_form);
}
function confirmar_posta(msg,val,id)
{
  if(!confirm(msg))
  {
     return false;
  }
  else
  {
	 postar_valor(val,id);
	 return true;
  }
}
function POPUPNAME(endereco,larg,alt)
{
 discoWin = window.open(endereco, 'WINNAME', 'status=YES,toolbar=NO,location=NO,scrollbars=YES,width='+larg+',height='+alt)
}
function Trim(str)
{
	while (str.charAt(0) == " ")
	str = str.substr(1,str.length -1);
	while (str.charAt(str.length-1) == " ")
	str = str.substr(0,str.length-1);
	return str;
}
