// JavaScript Document
function centro(p,n,l,a,v1,v2,v3,v4,v5,v6,v7,v8,v9){var l = l; var a = a; var t = (screen.height/2)-(a/2); var d = (screen.width/2)-(l/2);window.open(p,n,'width='+l+',height='+a+',top='+t+',left='+d+',scrollbars='+v1+',menubar='+v2+',directories='+v3+',location='+v4+',copyhistory='+v5+',status='+v6+',toolbar='+v7+',maximized='+v8+',resizable='+v9+'');}
function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

if(document.all) { // Internet Explorer
nTecla = evtKeyPress.keyCode; }
else if(document.layers) { // Nestcape
nTecla = evtKeyPress.which;
}

sValue = objForm[strField].value;
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( ":", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( " ", "" );
sValue = sValue.toString().replace( " ", "" );
fldLen = sValue.length;
mskLen = sMask.length;

i = 0;
nCount = 0;
sCod = "";
mskLen = fldLen;

while (i <= mskLen) {
bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == ":") || (sMask.charAt(i) == "/"))
bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

if (bolMask) {
sCod += sMask.charAt(i);
mskLen++; }
else {
sCod += sValue.charAt(nCount);
nCount++;
}

i++;
}

objForm[strField].value = sCod;

if (nTecla != 8) {
if (sMask.charAt(i-1) == "9") {
return ((nTecla > 47) && (nTecla < 58)); }
else {
return true;
} }
else {
return true;
}
}

//StrReplace 
function strreplace(str, char_busca, char_substr) {
  cnt = str.length;
  var str_novo = '';
  for (var vdigpos = 0; vdigpos < cnt; vdigpos++ ) {
    vdig = str.substr(vdigpos,1);
	if (vdig == char_busca) { 
	  str_novo += char_substr; }
	else { 
	  str_novo += vdig; }
  }
  return str_novo;
}

//valida cpf

function chkcpf(vcic){
	  var cic_old = vcic.value;
	  vcic.value = strreplace(vcic.value, '.', '');
	  vcic.value = strreplace(vcic.value, '-', '');
      expr  = new RegExp("0{11}|1{11}|2{11}|3{11}|4{11}|5{11}|6{11}|7{11}|8{11}|9{11}");
      if (vcic.value.match(expr)){
         vcic.value = cic_old;
         vcic.select();
         vcic.focus();
         alert('C.P.F. Inválido');
         return false;
      }
      if (isNaN(vcic.value) || vcic.value.length != 11){
         vcic.value = cic_old;
         vcic.select();
         vcic.focus();
         alert("Valor do C.P.F. Inválido");
         return false;
      }
      for (var vdigpos = 10; vdigpos < 12; vdigpos++ ){
         var vdig = 0;
         var vpos = 0;
         for (var vfator = vdigpos;vfator >= 2; vfator-- ){
            vdig = eval(vdig + vcic.value.substr(vpos,1) * vfator);
            vpos++;
         }
         vdig  = eval(11 -(vdig % 11)) < 10 ? eval(11 - vdig % 11) : 0;
         if (vdig != eval(vcic.value.substr(vdigpos-1,1))) {
         vcic.value = cic_old;
         vcic.select();
         vcic.focus();
         alert('Valor do C.P.F. Inválido');
         return false;
      }
      }
   return true;
}

//validação de cnpj
function chkcnpj(vcnpj){
	 var cnpj_old = vcnpj.value;
     vcnpj.value = strreplace(vcnpj.value, '.', '');
     vcnpj.value = strreplace(vcnpj.value, '/', '');
     vcnpj.value = strreplace(vcnpj.value, '-', '');
     if (isNaN(vcnpj.value) || vcnpj.value.length != 14){
		 vcnpj.value = cnpj_old;
		 vcnpj.select();
		 vcnpj.focus();
         alert("C.N.P.J. Inválido !");
         return false;
      }
      for (var vdigpos = 13; vdigpos < 15; vdigpos++ ){
         var vdig = 0;
         var vpos = 0;
         for (var vfator = vdigpos - 8 ;vfator >= 2; vfator-- ){
            vdig = eval(vdig + vcnpj.value.substr(vpos,1) * vfator);
            vpos++;
         }
         for (var vfator = 9 ;vfator >= 2; vfator-- ){
            vdig = eval(vdig + vcnpj.value.substr(vpos,1) * vfator);
            vpos++;
         }
         vdig  = eval(11 -(vdig % 11)) < 10 ? eval(11 - vdig % 11) : 0;
         if (vdig != eval(vcnpj.value.substr(vdigpos-1,1))) {
		 vcnpj.value = cnpj_old;
		 vcnpj.select();
		 vcnpj.focus();
         alert("C.N.P.J. Inválido !");
         return false;
      }
      }
   return true;
}
function FormataData(Campo, teclapres)
{
	var tecla = teclapres.keyCode;
	var vr = new String(Campo.value);
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");
	tam = vr.length + 1;
	
	if (tecla != 9 && tecla != 8) 
 		{
		if (tam > 2 && tam < 5)
			Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, tam);
		if (tam >= 5 && tam <=10)
			Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4);
		
		}
}
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
	
function FormataHora(Campo, teclapres) 
{
	var tecla = teclapres.keyCode;
	
	var vr = new String(Campo.value);
	vr = vr.replace(":", "");
	vr = vr.replace(":", "");

	tam = vr.length + 1;
	
	if (tecla != 9 && tecla != 8) {
		if (tam > 2 && tam < 5)
			Campo.value = vr.substr(0, 2) + ':' + vr.substr(2, tam);
		if (tam >= 5 && tam < 9)
			Campo.value = vr.substr(0,2) + ':' + vr.substr(2,2) + ':' + vr.substr(4,2);
		}
	}
/////////////////////////////////////////////////////////////////
function TestaData(cData)
{
var data; 
data = LimpaCampo(cData.value,10);
var tam = data.length;
if	(tam != 8)
	{
	alert('O Campo data está incorreta');
	cData.value = "";
	cData.focus();
	return(false);
	}
var dia = data.substr(0,2)
var mes = data.substr (2,2)
var ano = data.substr (4,4)	
if ( dia == 00 || mes == 00 || ano == 0000 )
	{
	alert('O Campo data está incorreta');
	cData.value = "";
	cData.focus();
	return(false);
	}

if ( dia > 31 )
	{
	alert('O Campo data está incorreta');
	cData.value = "";
	cData.focus();
	return(false);
	}

switch (mes)
	{
	case '01':
		if  (dia <= 31) 
			return (true);
		break;
	case '02':
		if  (dia <= 29) 
			return (true);
		break;
	case '03':
		if  (dia <= 31) 
			return (true);
		break;
	case '04':
		if  (dia <= 30) 
			return (true);
		break;
	case '05':
		if  (dia <= 31) 
			return (true);
		break;
	case '06':
		if  (dia <= 30) 
			return (true);
		break;
	case '07':
		if  (dia <= 31) 
			return (true);
		break;
	case '08':
		if  (dia <= 31) 
			return (true);
		break;
	case '09':
		if  (dia <= 30) 
			return (true);
		break;
	case '10':
		if  (dia <= 31) 
			return (true);
		break;
	case '11':
		if  (dia <= 30) 
			return (true);
		break;
	case '12':
		if  (dia <= 31) 
			return (true);
		break;
	}	
	{
	alert('A data está incorreta');
	cData.value = "";
	cData.focus();
	return(false);
	}

return(true); 
}
///////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
	
function FormataCPF(Campo, teclapres)
{
	
    if( ValidarTecla(teclapres) == false ){
 	  return false;
	}
	
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1;
	

		if (tam > 3 && tam < 7)
			Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
		if (tam >= 7 && tam <10)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
		if (tam >= 10 && tam < 12)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);

		
}
/*Exemplo de formata CPF
<INPUT class=input  onkeypress="return FormataCPF(this,event)" id=cpf  tabIndex=1 maxLength=14 size=18 name=cpf>
declaração da função


*/
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////

function FormataCNPJ(Campo, teclapres)
{

	var tecla = teclapres.keyCode;

	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1 ;

	
	if (tecla != 9 && tecla != 8)
		{
		if (tam > 2 && tam < 6)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
		if (tam >= 6 && tam < 9)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
		if (tam >= 9 && tam < 13)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
		if (tam >= 13 && tam < 15)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
		}

}
/////////////////////////////////////////////////////////////////
/*Exemplo de formata CNPJ os tamanhos não interferem en nda 

<INPUT class=input  onkeypress="return FormataCNPJ(this,event)" id=cnpj  tabIndex=1 maxLength=14 size=18 name=cnpj>
declaração da função


*/



/////////////////////////////////////////////////////////////////

function TestaPIS(cPis)
{
var pis; 
pis = LimpaCampo(cPis.value,11);
var tam = pis.length;
if	(tam != 11)
	{
	alert('O Número do PIS informado está incorreto');
	cPis.value = "";
	cPis.focus();
	return(false);
	}
return(true); 
}
/////////////////////////////////////////////////////////////////

function VerAlfaNumerico(pInd)
{
 var pValor = document.forms[0].elements[pInd].value
 var AuxTam = pValor.length  
  for(var j=0;j<AuxTam;j++)
   if ((!IndAlfaNumerico(pValor.charAt(j))) || (pValor.charAt(j) == " ")){
     document.forms[0].elements[pInd].focus();  
     document.forms[0].elements[pInd].value = pValor = pValor.substring(0,j)           
   } 
  }
////////////////////////////////////////////////////////////////////

function IndAlfaNumerico(N)
{
 for(var i=0;i<10;i++)
	if(N == i)
		return true;
 return false;    
}

/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////

function TestaData(cData)
{
var data; 
data = LimpaCampo(cData.value,10);
var tam = data.length;
if	(tam != 8)
	{
	alert('O Campo data está incorreta');
	cData.value = "";
	cData.focus();
	return(false);
	}
var dia = data.substr(0,2)
var mes = data.substr (2,2)
var ano = data.substr (4,4)	
if ( dia == 00 || mes == 00 || ano == 0000 )
	{
	alert('O Campo data está incorreta');
	cData.value = "";
	cData.focus();
	return(false);
	}

if ( dia > 31 )
	{
	alert('O Campo data está incorreta');
	cData.value = "";
	cData.focus();
	return(false);
	}

switch (mes)
	{
	case '01':
		if  (dia <= 31) 
			return (true);
		break;
	case '02':
		if  (dia <= 29) 
			return (true);
		break;
	case '03':
		if  (dia <= 31) 
			return (true);
		break;
	case '04':
		if  (dia <= 30) 
			return (true);
		break;
	case '05':
		if  (dia <= 31) 
			return (true);
		break;
	case '06':
		if  (dia <= 30) 
			return (true);
		break;
	case '07':
		if  (dia <= 31) 
			return (true);
		break;
	case '08':
		if  (dia <= 31) 
			return (true);
		break;
	case '09':
		if  (dia <= 30) 
			return (true);
		break;
	case '10':
		if  (dia <= 31) 
			return (true);
		break;
	case '11':
		if  (dia <= 30) 
			return (true);
		break;
	case '12':
		if  (dia <= 31) 
			return (true);
		break;
	}	
	{
	alert('A data está incorreta');
	cData.value = "";
	cData.focus();
	return(false);
	}

return(true); 
}
///////////////////////////////////////////////////////////////////
function TestaHora(cHora)
{
var hora; 
hora = LimpaCampo(cHora.value,10);
var tam = hora.length;
if	(tam != 6)
	{
	alert('A hora está incorreta');
	cHora.value = "";
	cHora.focus();
	return(false);
	}
	
var hr = hora.substr(0,2)
var min = hora.substr (2,2)
var seg = hora.substr (4,2)	
if ((hr > 23) || (min > 59) || (seg > 59))
	{
	alert('A hora está incorreta');
	cHora.value = "";
	cHora.focus();
	return(false);
	}
return(true);
 
}
/////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////
/*teclapres colocar event*/
function FormataPIS(Campo, teclapres)
{
	var tecla = teclapres.keyCode;
	
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1;
	
	if (tecla != 9 && tecla != 8)
		{
		if (tam > 3 && tam < 9)
			Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
		if (tam >= 9 && tam <11)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,5) + '.' + vr.substr(8,tam-8);
		if (tam >= 11 && tam < 13)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,5) + '.' + vr.substr(8,2) + '-' + vr.substr(10,tam-10);
		}
		
}

/////////////////////////////////////////////////////////////////

function Bloqueia_Caracteres(evnt){   
//Função permite digitação de números   
    if (clientNavigator == "IE"){   
        if (evnt.keyCode < 48 || evnt.keyCode > 57){   
            return false   
        }   
    }else{   
        if ((evnt.charCode < 48 || evnt.charCode > 57) && evnt.keyCode == 0){   
            return false   
        }   
    }   
}   


function SubmitEnter(myfield,e)
{
    var keycode;
    if (window.event) 
		keycode = window.event.keyCode;
    
	else if (e) keycode = e.which;
    	else return true;
	
    if (keycode == 13)
    {
    	myfield.form.submit();
	    return false;
    }
    else
	    return true;
}

/*Em seguida, adicione em cada campo que desejar o conteúdo em negrito do exemplo abaixo:

    <FORM ACTION=”/login”>
    Nome:     <INPUT NAME=”nome”><BR>
    Senha: <INPUT NAME=”senha” TYPE=”PASSWORD”
    onKeyPress=”return submitenter(this,event,Funcao)”><BR>
    <INPUT TYPE=”SUBMIT” VALUE=”Logar”>
    </FORM>*/
	
/* mascara_global pode ser utilizada para formatar moeda
Ex:
onkeyup="this.value = mascara_global('[###.]###,##', this.value)"
Entrada 1000
Saida 10,00

*/
function mascara_global(mascara, valor){
	
	
	if(mascara == '###.###.###-##|##.###.###/####-##'){
		if(valor.length>14){
			return mascara_global('##.###.###/####-##', valor);
		}else{
			return mascara_global('###.###.###-##', valor);
		}
	}
	
	tvalor = "";
	ret = "";
	caracter = "#";
	separador = "|";
	mascara_utilizar = "";
	valor = removeEspacos(valor);
	if (valor == "")return valor;
	temp = mascara.split(separador);
	dif = 1000;
	
	valorm = valor;
	//tirando mascara do valor já existente
	for (i=0;i<valor.length;i++){
		if (!isNaN(valor.substr(i,1))){
			tvalor = tvalor + valor.substr(i,1);
		}
	}
	valor = tvalor;
	
	//formatar mascara dinamica
	for (i = 0; i<temp.length;i++){
		mult = "";
		validar = 0;
		for (j=0;j<temp[i].length;j++){
			if (temp[i].substr(j,1) == "]"){
				temp[i] = temp[i].substr(j+1);
				break;
			}
			if (validar == 1)mult = mult + temp[i].substr(j,1);
			if (temp[i].substr(j,1) == "[")validar = 1;
		}
		for (j=0;j<valor.length;j++){
			temp[i] = mult + temp[i];
		}
	}
	
	//verificar qual mascara utilizar
	if (temp.length == 1){
		mascara_utilizar = temp[0];
		mascara_limpa = "";
		for (j=0;j<mascara_utilizar.length;j++){
			if (mascara_utilizar.substr(j,1) == caracter){
				mascara_limpa = mascara_limpa + caracter;
			}
		}
		tam = mascara_limpa.length;
	}else{
		//limpar caracteres diferente do caracter da máscara
		for (i=0;i<temp.length;i++){
			mascara_limpa = "";
			for (j=0;j<temp[i].length;j++){
				if (temp[i].substr(j,1) == caracter){
					mascara_limpa = mascara_limpa + caracter;
				}
			}
			if (valor.length > mascara_limpa.length){
				if (dif > (valor.length - mascara_limpa.length)){
					dif = valor.length - mascara_limpa.length;
					mascara_utilizar = temp[i];
					tam = mascara_limpa.length;
				}
			}else if (valor.length < mascara_limpa.length){
				if (dif > (mascara_limpa.length - valor.length)){
					dif = mascara_limpa.length - valor.length;
					mascara_utilizar = temp[i];
					tam = mascara_limpa.length;
				}
			}else{
				mascara_utilizar = temp[i];
				tam = mascara_limpa.length;
				break;
			}
		}
	}
	
	//validar tamanho da mascara de acordo com o tamanho do valor
	if (valor.length > tam){
		valor = valor.substr(0,tam);
	}else if (valor.length < tam){
		masct = "";
		j = valor.length;
		for (i = mascara_utilizar.length-1;i>=0;i--){
			if (j == 0) break;
			if (mascara_utilizar.substr(i,1) == caracter){
				j--;
			}
			masct = mascara_utilizar.substr(i,1) + masct;
		}
		mascara_utilizar = masct;
	}
	
	//mascarar
	j = mascara_utilizar.length -1;
	for (i = valor.length - 1;i>=0;i--){
		if (mascara_utilizar.substr(j,1) != caracter){
			ret = mascara_utilizar.substr(j,1) + ret;
			j--;
		}
		ret = valor.substr(i,1) + ret;
		j--;
	}
	return ret;
}


function removeEspacos(valor){
var valorSemEspacos="";

var tamanho = valor.length;
	for (i = 0; i<30;i++){
		if(valor.substr(i,1)==" "){
		}else{
		valorSemEspacos = valorSemEspacos + valor.substr(i,1);
		}
	}
return valorSemEspacos;
}

