function preencheCampo (campo, msg){
	
	// Verifica campo
	if(campo.value == ""){
		
		campo.value = msg;
		
	}
	
}

function url_decode(str) { 
    var n, strCode, strDecode = ""; 

    for (n = 0; n < str.length; n++) { 
        if (str.charAt(n) == "%") { 
            strCode = str.charAt(n + 1) + str.charAt(n + 2); 
            strDecode += String.fromCharCode(parseInt(strCode, 16)); 
            n += 2; 
        } else { 
            strDecode += str.charAt(n); 
        } 
    } 
    return strDecode; 
} 

function AJAX(){
	try{
	    xmlhttp = new XMLHttpRequest();
	}catch(ee){
	    try{
	        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	    }catch(e){
	        try{
	            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	        }catch(E){
	            xmlhttp = false;
	        }
	    }
	}
	
	return xmlhttp;
}

var ttmnow=0;
var ths="0";
var thn=0;
var tms="0";
var tmn=0;
var tss="0";
var tsn=0;

function tstarttm()  
    {
  ths=thn;
  tms=tmn;
  tss=tsn;
 if (thn<10)
    ths="0" + ths;
  if (tmn<10)
    tms="0"+tms;
  if (tsn<10)
    tss="0"+tss;
  document.getElementById('tempo').innerHTML = "<b>Tempo Decorrido: </b>" + ths+":"+tms+":"+tss;
   if (ttmnow)
     {
     clearTimeout(ttmnow);
     ttmnow=0;
     }
    tsn+=1;
    if (tsn>59)
      {
      tmn+=1;
      tsn=0;
      if (tmn>59)
        {
        thn+=1;
        tmn=0;
       if (thn>23)
          thn=0;
        }
    }
   ttmnow=setTimeout("tstarttm()",1000);
  }

var tam = 12;

function mudaFonte(tipo,elemento){
	if (tipo=="mais") {
		if(tam<16) tam+=1;
	} else {
		if(tam>9) tam-=1;
	}
			
	document.getElementById(elemento).style.fontSize = tam+'px';
	// document.getElementById('mudaFoto').style.fontSize = tam+'px';
}
  
// Mask Nosso Número
function maskNN(nn, red){
	
	if(nn.value.length == 2){
		nn.value += "/";
	}
	if(nn.value.length == 14){
		nn.value += "-";
	}
	if(nn.value.length == 16){
		red.focus();
		red.select();
	}
	
}

// Mask CEP
function maskCEP(cep, red){
	
	if(cep.value.length == 5){
		cep.value += "-";
	}
	if(cep.value.length == 9){
		red.focus();
		red.select();
	}
	
}

// Mask CNPJ
function maskCNPJ(cnpj, red){
	
	if(cnpj.value.length == 2){
		cnpj.value += ".";
	}
	if(cnpj.value.length == 6){
		cnpj.value += ".";
	}
	if(cnpj.value.length == 10){
		cnpj.value += "/";
	}
	if(cnpj.value.length == 15){
		cnpj.value += "-";
	}
	if(cnpj.value.length == 18){
		red.focus();
		red.select();
	}
	
}

// Mask CPF
function maskCPF(cpf, red){
	
	if(cpf.value.length == 3){
		cpf.value += ".";
	}
	if(cpf.value.length == 7){
		cpf.value += ".";
	}
	if(cpf.value.length == 11){
		cpf.value += "-";
	}
	if(cpf.value.length == 14){
		red.focus();
		red.select();
	}
	
}

var comPontos;

function randomiza(n) 
{
    var ranNum = Math.round(Math.random()*n);
    return ranNum;
}

function mod(dividendo,divisor) 
{
	return Math.round(dividendo - (Math.floor(dividendo/divisor)*divisor));
}

function cpf()
{
	var n = 9;
	var n1 = randomiza(n);
 	var n2 = randomiza(n);
 	var n3 = randomiza(n);
 	var n4 = randomiza(n);
 	var n5 = randomiza(n);
 	var n6 = randomiza(n);
 	var n7 = randomiza(n);
 	var n8 = randomiza(n);
 	var n9 = randomiza(n);
 	var d1 = n9*2+n8*3+n7*4+n6*5+n5*6+n4*7+n3*8+n2*9+n1*10;
 	d1 = 11 - ( mod(d1,11) );
 	if (d1>=10) d1 = 0;
 	var d2 = d1*2+n9*3+n8*4+n7*5+n6*6+n5*7+n4*8+n3*9+n2*10+n1*11;
 	d2 = 11 - ( mod(d2,11) );
 	if (d2>=10) d2 = 0;
	retorno = '';
	if (comPontos) retorno = ''+n1+n2+n3+'.'+n4+n5+n6+'.'+n7+n8+n9+'-'+d1+d2;
	else retorno = ''+n1+n2+n3+n4+n5+n6+n7+n8+n9+d1+d2;
 	return retorno;
}

function faz()
{
	if (document.form1.tipo[0].checked)
		document.form1.numero.value = cpf();
	else
		document.form1.numero.value = cnpj();
}
// Botão de Submit
function buttonSubmit(obj, form){
	
	// Efetua o Submit
	form.submit(); 
	
	// Altera Valor
	obj.value = 'Aguarde...';
	
	// Altera Estado
	obj.disabled = true;
	
}

function checkedDisable(i){
	i.checked = false;
}

function checkedEnable(i){
	i.checked = true;
}

function disen(i){
	if(i.disabled == true){
		i.disabled = false;
	}else{
		i.disabled = true;
	}
}

function enable(i){
	 i.disabled = false;
}

function disable(i){
	 i.disabled = true;
}

function limpaValue(campo){
		campo.value = "";
}

function subForm1(form, url, target, msg){
	c = confirm(msg);
	if(c){
		form.action=url;
		if(target){
			form.target = target;
		}
		form.submit();
	}
}

function subFormP(form, url, msg, prt){
	var time=new Date();
	var date=time.getDate()
	var mes = (time.getMonth()+1);
	var year=time.getYear();
	var text = confirm("" + msg);
	if(text==true){
		data = prompt(prt, ((date < 10)?"0":"") + date + "/" + ((mes < 10)?"0":"") + mes + "/" + year);
		if ((data != null) && (data != "undefined" )){
			form.action=url+"&data="+data;
			form.submit();
		}
	}
}

function subForm(form, url, msg){
	c = confirm(msg);
	if(c){
		form.action=url;
		form.submit();
	}
}

function restaura(obj, dado){
	if(obj.value == "")
		obj.value = dado;
}

function hrefb(url){
	window.open(url,"print");
	return false;
}

function red(url, target){
	eval(target+".location='"+url+"'");
}

//Verifica se o campo está vazio
function campoBlanco(campo){
	if(campo.value = "")
		return true;
	else
		return false;
}

function newReg(obj, url,  popName, popWidth, popHeight, popScroll){
	vaObj = obj.value;
	popup(popName, "" + url + "?value=" + vaObj, popWidth, popHeight, popScroll);
}

function trocaOpcao(valor, objSel) {
    for (i=0; i < objSel.length; i++){
    qtd = valor.length;
        if (objSel.options[i].text.substring(0, qtd).toUpperCase() == valor.toUpperCase()) {
        objSel.selectedIndex = i;
            break;
        }
    }
}
function jump(targ,selObj){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
}

function jumpMenuEncaminhar(targ,selObj,restore, msg){ //v3.0
  confirma(msg,""+selObj.options[selObj.selectedIndex].value,'#');
  selObj.selectedIndex=0;
}

function exc(file, msg, idserv, d){
	c = confirm(msg);
	if(c){
		location.href= file + "?idserv=" + idserv + "&d=" + d;
	}
}
function select_all(form, e){
	for(x=0; x < document.forms[''+form].elements[e].length; x++){
		if(document.forms[''+form].elements[e][x].checked)
			document.forms[''+form].elements[e][x].checked = false;
		else
			document.forms[''+form].elements[e][x].checked = true;
	}
}

function preencheCampo(obj, valor){
	if(obj.value.length == ""){
		obj.value = valor;
	}
}
function limpaCampo(obj){
	if(obj.value.length = 10){
		obj.value = "";
	}
}

function LC(objId){
	document.getElementById(objId).value = "";
}

function confirma_opener(msg,link_sim,link_nao)
{
	var text=confirm("" + msg);
	if(text==true)
	{
		opener.location.href = "" + link_sim;
	}else if(text==false){
		opener.location.href = "" + link_nao;
	}
}

function confirma_opener2(msg,link_sim,link_nao)
{
	var text=confirm("" + msg);
	if(text==true)
	{
		opener.location.href = "" + link_sim;
		window.close();
	}else if(text==false){
		location.href = "" + link_nao;
	}
}


function confirmap(msg, link_sim, link_nao, prt, date){
	var text = confirm("" + msg);
	if(text==true){
		data = prompt(prt, date);
		if ((data != null) && (data != "undefined" ))
			location.href = "" + link_sim + "&data=" + data;
	}else if(text==false){
		location.href = "" + link_nao;
	}
}

function confirma(msg,link_sim,link_nao)
{
	var text=confirm("" + msg);
	if(text==true)
	{
		location.href = "" + link_sim;
	}else if(text==false){
		location.href = "" + link_nao;
	}
}

function popupTemp(name, url)
{
		window.open(url,name,'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no'); 
		return false;
}


function popup(name, url, width, height, scrol)
{
		window.open(url,name,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+scrol+',resizable=no,width='+width+',height='+height); 
		return false;
}

function redSel(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function verData(campoData){
	var data = new Date();
	var dia = data.getDate();
	var mes = data.getMonth()+1 ;
	if(campoData.value == ""){
		if(dia < 10){dia = "0" + data.getDate();}
		if(mes < 10){mes = "0" + (data.getMonth() + 1);}
		campoData.value = dia + "/" + mes + "/" + data.getFullYear();
	}
}

function moveFoco (length, focoAtual, proximoFoco) {
		if(focoAtual.value.length == length){
				proximoFoco.focus();
				proximoFoco.select();
		}
}

function maskData(campoData, campoRed){
	campoData.maxlength = 8;
	if(campoData.value.length == 2){
		campoData.value += "/";
	}
	if(campoData.value.length == 5){
		campoData.value += "/";
	}
	if(campoData.value.length == 10){
		campoRed.focus();
		campoRed.select();
	}
}

function move(id){
  if(id)
    id = numero;
	var tecla=window.event.keyCode;
	if (tecla==65) {
	  elemento = document.getElementById(id);
	  elemento.style.background='#eeeeee';
	  }
	}

function fncMoveFoco(qtd ,campo1, campo2)
{
	if (campo1.value.length == qtd){
	    campo2.focus();
	    campo2.select();
	}
}

function see(i)
{
  i = document.getElementById(i);
  if(!i)
  {
	return false;
  }else{
	if(i.style.display=='' || i.style.display=='none')
    {
		i.style.display = 'block';
 	}else{
		i.style.display = 'none';
  	}
  }
}

function sumir(i)
{
  i = document.getElementById(i);
  if(!i)
  {
	return false;
  }else{
	if(i.style.display == 'block')
    {
		i.style.display = 'none';
 	}
  }
}

function aparecer(i)
{
  i = document.getElementById(i);
  if(!i)
  {
	return false;
  }else{
	if(i.style.display == 'none')
    {
		i.style.display = 'block';
 	}
  }
}
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 ) ;}
} 

}
