function abrePopLogin(pagina,largura,altura, rolando, nomePop) {
	//pega a resolução do visitante
	w = screen.width;
	h = screen.height;
	//divide a resolução por 2, obtendo o centro do monitor
	meio_w = w/2;
	meio_h = h/2;
	//diminui o valor da metade da resolução pelo tamanho da janela, fazendo com q ela fique centralizada
	altura2 = altura/2;
	largura2 = largura/2;
	meio1 = meio_h-altura2;
	meio2 = meio_w-largura2;
	//abre a nova janela, já com a sua devida posição
	window.open(pagina,nomePop,'height=' + altura + ', width=' + largura + ', top='+meio1+', left='+meio2+', scrollbars='+rolando+''); 
}

function lytelink(url, ftitle, revv) {
   var objLink = document.createElement('a');
   objLink.setAttribute('href', url);
   objLink.setAttribute('rel', 'lyteframe');
   objLink.setAttribute('rev', revv);
   objLink.setAttribute('title', ftitle);
   myLytebox.start(objLink, false, true);
}

function txt_max(txarea, total, id_tc, id_resto) { 
	tam = document.getElementById(txarea).value.length; 
	str=""; 
	str=str+tam; 
	if (id_tc && tam <= total) {
		document.getElementById(id_tc).innerHTML = str; 
	}
	if (id_resto && tam <= total) {
		document.getElementById(id_resto).innerHTML = total - str; 
	}
	if (tam > total){
		aux = document.getElementById(txarea).value;
		document.getElementById(txarea).value = aux.substring(0,total);
		str = document.getElementById(txarea).value.length;
		if (id_tc) {
			document.getElementById(id_tc).innerHTML = str; 
		}
		if (id_resto) {
			document.getElementById(id_resto).innerHTML = total - str; 
		}
	}
} 

function validaInteiro(x){
	s = new String(x);
	ok = true;
	for (i = 0; i < s.length; i++){
		r = (s.charAt(i) == "0"
		|| s.charAt(i) == "1" 
		|| s.charAt(i) == "2" 
		|| s.charAt(i) == "3" 
		|| s.charAt(i) == "4" 
		|| s.charAt(i) == "5" 
		|| s.charAt(i) == "6"  
		|| s.charAt(i) == "7" 
		|| s.charAt(i) == "8" 
		|| s.charAt(i) == "9");
		if(!r){
			ok = false;
			break;
		}
	}
	return ok;
}

function validaCampoVazio(s){
    return ((s== null) || (s.length == 0));
}


function validaEmail(email){
    var ExpReg = /^\w+([\.-]?\w)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    var result = ExpReg.test(email);
    if (!result){
       return false;
    } else {
       return true;
    }     
}
function valida_cnpj(cnpj)
      {
      var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
      digitos_iguais = 1;
      if (cnpj.length < 14 && cnpj.length < 15)
            return false;
      for (i = 0; i < cnpj.length - 1; i++)
            if (cnpj.charAt(i) != cnpj.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            tamanho = cnpj.length - 2
            numeros = cnpj.substring(0,tamanho);
            digitos = cnpj.substring(tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            tamanho = tamanho + 1;
            numeros = cnpj.substring(0,tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
            }
      else
            return false;
      } 
	  
function validaCPF(cpf) {
     erro = new String;
     if (cpf.length < 11) erro += "São necessários 11 digitos para verificação do CPF. \n\n";
     var nonNumbers = /\D/;
     if (nonNumbers.test(cpf)) erro += "A verificação de CPF suporta apenas números. \n\n";
     if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999" || cpf == "22233344405" || cpf == "12345678909"){
             erro += "Número de CPF inválido!"
   }
   var a = [];
   var b = new Number;
   var c = 11;
   for (i=0; i<11; i++){
           a[i] = cpf.charAt(i);
           if (i < 9) b += (a[i] * --c);
   }
   if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
   b = 0;
   c = 11;
   for (y=0; y<10; y++) b += (a[y] * c--);
   if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
   if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
           erro +="Por favor, conferir os números do CPF.";
   }
   if (erro.length > 0){
           alert(erro);
           return false;
   }
   return true;
}




function validaCPFpresente(cpf) {
     erro = new String;
     if (cpf.length < 11) erro += "Sao necessários 11 digitos para verificação do CPF \n\n";
     var nonNumbers = /\D/;
     if (nonNumbers.test(cpf)) erro += "A verificação de CPF suporta apenas números \n\n";
     if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
             erro += "Número de CPF inválido!"
   }
   var a = [];
   var b = new Number;
   var c = 11;
   for (i=0; i<11; i++){
           a[i] = cpf.charAt(i);
           if (i < 9) b += (a[i] * --c);
   }
   if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
   b = 0;
   c = 11;
   for (y=0; y<10; y++) b += (a[y] * c--);
   if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
   if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
           erro +="Digito verificador com problema!";
   }
   if (erro.length > 0){
           alert(erro);
           return false;
   }
   return true;
}


// *** limita o numero de caracteres no campo ***
// Pmax = número máximo de caracteres desejados
// Pcampo = nome do campo a ser limitado
function countChars(Pmax,Pcampo) {
	campo = eval("document.form1." + Pcampo);
  if(campo.value.length > Pmax){
  	campo.value = campo.value.substring(0, Pmax);
  	alert("O texto não pode ultrapassar " + Pmax + " caracteres.");
		campo.focus();
  }
}

function validaHora(intHora,intMin,intSeg){
	bolOk = true;
	if(validaCampoVazio(intHora) || validaCampoVazio(intMin) || validaCampoVazio(intSeg)){
		bolOk = false;
	}
	else{
		if(!validaInteiro(intHora) || !validaInteiro(intMin) || !validaInteiro(intSeg)){
			bolOk = false;
		}
		else{
			if(intHora > 23 || intMin > 59 || intSeg > 59){
				bolOk = false;
			}
		}
	}
	return bolOk;
}

function validaData_(Data){
	var dma = -1;
	var data = Array(3);
	var ch = Data.charAt(0); 
	for(i=0; i < Data.length && (( ch >= '0' && ch <= '9' ) || ( ch == '/' && i != 0 ) ); ){
   		data[++dma] = '';
		if(ch!='/' && i != 0) return false;
		if(i != 0 ) ch = Data.charAt(++i);
		if(ch=='0') ch = Data.charAt(++i);
   		while( ch >= '0' && ch <= '9' ){
    		data[dma] += ch;
    		ch = Data.charAt(++i);
   		} 
  	}
	  if(ch!='') return false;
	  if(data[0] == '' || isNaN(data[0]) || parseInt(data[0]) < 1) return false;
	  if(data[1] == '' || isNaN(data[1]) || parseInt(data[1]) < 1 || parseInt(data[1]) > 12) return false;
	  if(data[2] == '' || isNaN(data[2]) || ((parseInt(data[2]) < 1900 || parseInt(data[2]) > 9999))) return false;
  	  switch(parseInt(data[1])){
   		case 2: { if(((parseInt(data[2])%4!=0 || (parseInt(data[2])%100==0 && parseInt(data[2])%400!=0)) && parseInt(data[0]) > 28) || parseInt(data[0]) > 29 ) return false; break; }
   	    case 4: case 6: case 9: case 11: { if(parseInt(data[0]) > 30) return false; break;}
   		default: { if(parseInt(data[0]) > 31) return false;}
  	  }
  return true; 
}

function autoTab(strCampo,intMax,strProxCampo){
	if(strCampo.value.length == intMax) eval(strProxCampo + ".focus();");
}




function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
//alert(n);
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



/* script de montagem de calendario*/

function popdate(obj,div,tam,ddd)
{
    if (ddd) 
    {
        day = ""
        mmonth = ""
        ano = ""
        c = 1
        char = ""
        for (s=0;s<parseInt(ddd.length);s++)
        {
            char = ddd.substr(s,1)
            if (char == "/") 
            {
                c++; 
                s++; 
                char = ddd.substr(s,1);
            }
            if (c==1) day    += char
            if (c==2) mmonth += char
            if (c==3) ano    += char
        }
        ddd = mmonth + "/" + day + "/" + ano
    }
  
    if(!ddd) {today = new Date()} else {today = new Date(ddd)}
    date_Form = eval (obj)
    if (date_Form.value == "") { date_Form = new Date()} else {date_Form = new Date(date_Form.value)}
  
    ano = today.getFullYear();
    mmonth = today.getMonth ();
    day = today.toString ().substr (8,2)
  
    umonth = new Array ("Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro")
    days_Feb = (!(ano % 4) ? 29 : 28)
    days = new Array (31, days_Feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

    if ((mmonth < 0) || (mmonth > 11))  alert(mmonth)
    if ((mmonth - 1) == -1) {month_prior = 11; year_prior = ano - 1} else {month_prior = mmonth - 1; year_prior = ano}
    if ((mmonth + 1) == 12) {month_next  = 0;  year_next  = ano + 1} else {month_next  = mmonth + 1; year_next  = ano}
    txt  = "<table bgcolor='#efefff' style='border:solid #330099; border-width:2' cellspacing='0' cellpadding='3' border='0' width='"+tam+"' height='"+tam*1.1 +"'>"
    txt += "<tr bgcolor='#FFFFFF'><td colspan='7' align='center'><table border='0' cellpadding='0' width='100%' bgcolor='#FFFFFF'><tr>"
    txt += "<td width=20% align=center><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+((mmonth+1).toString() +"/01/"+(ano-1).toString())+"') class='Cabecalho_Calendario' title='Ano Anterior'><<</a></td>"
    txt += "<td width=20% align=center><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+( "01/" + (month_prior+1).toString() + "/" + year_prior.toString())+"') class='Cabecalho_Calendario' title='Mês Anterior'><</a></td>"
    txt += "<td width=20% align=center><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+( "01/" + (month_next+1).toString()  + "/" + year_next.toString())+"') class='Cabecalho_Calendario' title='Próximo Mês'>></a></td>"
    txt += "<td width=20% align=center><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+((mmonth+1).toString() +"/01/"+(ano+1).toString())+"') class='Cabecalho_Calendario' title='Próximo Ano'>>></a></td>"
    txt += "<td width=20% align=right><a href=javascript:force_close('"+div+"') class='Cabecalho_Calendario' title='Fechar Calendário'><b>X</b></a></td></tr></table></td></tr>"
    txt += "<tr><td colspan='7' align='right' bgcolor='#ccccff' class='mes'> <a href=javascript:pop_month('"+obj+"','"+div+"','"+tam+"','" + ano + "') class='mes'>" + umonth[mmonth] + "</a>"
    txt += " <a href=javascript:pop_year('"+obj+"','"+div+"','"+tam+"','" + (mmonth+1) + "') class='mes'>" + ano.toString() + "</a> <div id='popd' style='position:absolute'></div></td></tr>"
    txt += "<tr bgcolor='#B5A7B8'><td width='14%' class='dia' align=center><b>Dom</b></td><td width='14%' class='dia' align=center><b>Seg</b></td><td width='14%' class='dia' align=center><b>Ter</b></td><td width='14%' class='dia' align=center><b>Qua</b></td><td width='14%' class='dia' align=center><b>Qui</b></td><td width='14%' class='dia' align=center><b>Sex<b></td><td width='14%' class='dia' align=center><b>Sab</b></td></tr>"
    today1 = new Date((mmonth+1).toString() +"/01/"+ano.toString());
    diainicio = today1.getDay () + 1;
    week = d = 1
    start = false;

    for (n=1;n<= 42;n++) 
    {
        if (week == 1)  txt += "<tr bgcolor='#efefff' align=center>"
        if (week==diainicio) {start = true}
        if (d > days[mmonth]) {start=false}
        if (start) 
        {
            dat = new Date((mmonth+1).toString() + "/" + d + "/" + ano.toString())
            day_dat   = dat.toString().substr(0,10)
            day_today  = date_Form.toString().substr(0,10)
            year_dat  = dat.getFullYear ()
            year_today = date_Form.getFullYear ()
            colorcell = ((day_dat == day_today) && (year_dat == year_today) ? " bgcolor='#FEEFDE' " : "" )
            txt += "<td"+colorcell+" align=center><a href=javascript:block('"+  d + "/" + (mmonth+1).toString() + "/" + ano.toString() +"','"+ obj +"','" + div +"') class='data'>"+ d.toString() + "</a></td>"
            d ++ 
        } 
        else 
        { 
            txt += "<td class='data' align=center> </td>"
        }
        week ++
        if (week == 8) 
        { 
            week = 1; txt += "</tr>"} 
        }
        txt += "</table>"
        div2 = eval (div)
        div2.innerHTML = txt 
}
  
// função para exibir a janela com os meses
function pop_month(obj, div, tam, ano)
{
  txt  = "<table bgcolor='#CCCCFF' border='0' width=80>"
  for (n = 0; n < 12; n++) { txt += "<tr><td align=center><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+("01/" + (n+1).toString() + "/" + ano.toString())+"')>" + umonth[n] +"</a></td></tr>" }
  txt += "</table>"
  popd.innerHTML = txt
}

// função para exibir a janela com os anos
function pop_year(obj, div, tam, umonth)
{
  txt  = "<table bgcolor='#CCCCFF' border='0' width=160>"
  l = 1
  for (n=2007; n<2020; n++)
  {  if (l == 1) txt += "<tr>"
     txt += "<td align=center><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+(umonth.toString () +"/01/" + n) +"')>" + n + "</a></td>"
     l++
     if (l == 4) 
        {txt += "</tr>"; l = 1 } 
  }
  txt += "</tr></table>"
  popd.innerHTML = txt 
}

// função para fechar o calendário
function force_close(div) 
    { div2 = eval (div); div2.innerHTML = ''}
    
// função para fechar o calendário e setar a data no campo de data associado
function block(data, obj, div)
{ 
	
    force_close (div)
    obj2 = eval(obj)
    obj2.value = data 
	document.getElementById('data_erro_vazio').style.display = 'none';
}

/* fim do script de calendario*/

