function CheckAll(frm) { 
	cont = 0;
   for (var i=0;i<frm.elements.length;i++) {
		var x = frm.elements[i];
		if (x.checked == true) { 
			x.checked = false;
		}
		else{	
			x.checked = true;
		}
	}
}


function CheckAll_2(frm,nom) { 
	cont = 0;
	for (var i=0;i<frm.elements.length;i++) {
		var x = frm.elements[i];
		//seleciona somente os que tiverem o nome igual a variavel nom, passada por parâmetro na função
		if(x.name.indexOf(nom) == 0){
			if (x.checked == true) { 
				x.checked = false;
			}
			else{	
				x.checked = true;
			}
		}
	}
}


function formataDigito(obj,valor,pos){
	valor	= valor.replace( "-", "");
	tam	= valor.length ;
	obj.value = valor.substr( 0, tam - pos ) + '-' + valor.substr( tam - pos, tam);
}
function campo_obri(frm,arr_campos,arr_linhas){
	for (c=0; c<arr_campos.length; c++){
		campo	= arr_campos[c];
		linha		= arr_linhas[c];
		if ((!eval('frm.'+campo+'.value'))||(eval('frm.'+campo+'.value')==" ")){
			document.getElementById(linha).bgColor="#cccccc";
			eval('frm.'+campo+'.focus()');
			return false;
		}
		else {
			document.getElementById(linha).bgColor="";
		}
	}
}

function veremail(obj, email){
	if (email){
		reemail=/^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
		if (reemail.test(email)) {
			return true;
		}
		else {
			alert(email + " NÃO é um endereço de e-mail válido.");
			obj.focus();
			return false;
		}
	}
}
function mudacor(obj, cor){
	obj.bgColor=cor;
}
function selcampo(obj, cor){
	obj.style.background=cor;
}

function masc_data(objeto,data) 
{ 
    var mydata = ''; 
    mydata = mydata + data; 
    if (mydata.length == 2) { 
        mydata = mydata + '/'; 
		objeto.value = mydata; 
    } 
	if (mydata.length == 5) { 
        mydata = mydata + '/'; 
		objeto.value = mydata; 
    } 
}
function masc_mes(objeto,data) 
{ 
    var mydata = ''; 
    mydata = mydata + data; 
    if (mydata.length == 2) { 
        mydata = mydata + '/'; 
		objeto.value = mydata; 
    } 
	
}
function masc_hora(objeto,hora) 
{ 
    var myhora = ''; 
    myhora = myhora + hora; 
    if (myhora.length == 2) { 
        myhora = myhora + ':'; 
		objeto.value = myhora; 
    } 
}

function masc_mat(objeto,dados) 
{ 
    var mydados = ''; 
    mydados = mydados + dados; 
    if (mydados.length == 6) { 
        mydados = mydados + '-'; 
		objeto.value = mydados; 
    } 
	if (mydados.length == 9) { 
        mydados = mydados + '-'; 
		objeto.value = mydados; 
    } 
}

function masc_cep(objeto,cep) 
{ 
    var mycep = ''; 
    mycep = mycep + cep; 
    if (mycep.length == 5) { 
        mycep = mycep + '-'; 
		objeto.value = mycep; 
    } 
}

function MascaraFone(campo){
	var v = '';
	v = v + campo.value;
	if (v.length == 0){
		v = '(' + v;
		campo.value = v;
	}
	if (v.length == 3){
		v = v + ') ';
		campo.value = v;
	}
	if (v.length == 9){
		v = v + '-';
		campo.value = v;
	}
}

function masc_cpf(objeto,cpf) 
{ 
    var mycpf = ''; 
    mycpf = mycpf + cpf; 
    if (mycpf.length == 3) { 
        mycpf = mycpf + '.'; 
		objeto.value = mycpf; 
    } 
	if (mycpf.length == 7) { 
        mycpf = mycpf + '.'; 
		objeto.value = mycpf; 
    }
	if (mycpf.length == 11) { 
        mycpf = mycpf + '-'; 
		objeto.value = mycpf; 
    }
}

function masc_cnpj(objeto,cnpj) 
{ //22.222.222/0001-11
    var mycnpj = ''; 
    mycnpj = mycnpj + cnpj; 
    if (mycnpj.length == 2) { 
        mycnpj = mycnpj + '.'; 
		objeto.value = mycnpj; 
    } 
	if (mycnpj.length == 6) { 
        mycnpj = mycnpj + '.'; 
		objeto.value = mycnpj; 
    }
	if (mycnpj.length == 10) { 
        mycnpj = mycnpj + '/'; 
		objeto.value = mycnpj; 
    }
	if (mycnpj.length == 15) { 
        mycnpj = mycnpj + '-'; 
		objeto.value = mycnpj; 
    }
}

function masc_rgie(objeto,rgie) 
{ //22.222.222/0001-11
    var myrgie = ''; 
    myrgie = myrgie + rgie; 
    if (myrgie.length == 3) { 
        myrgie = myrgie + '.'; 
		objeto.value = myrgie; 
    } 
	if (myrgie.length == 7) { 
        myrgie = myrgie + '.'; 
		objeto.value = myrgie; 
    }
}

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
  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];}
}
function valida_CPF(obj){
	if (obj.value!=''){
		s = obj.value;
		if (isNaN(s)) {
			alert('preencha somente números!');
			obj.focus();
			return false;
		}
		var i;
		var c = s.substr(0,9);
		var dv = s.substr(9,2);
		var d1 = 0;
		for (i = 0; i < 9; i++) {
		d1 += c.charAt(i)*(10-i);
	}
	if (d1 == 0){
		alert('CPF inválido!');
		obj.focus();
		return false;
	}         
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;         
	if (dv.charAt(0) != d1) {
		alert('CPF inválido!');
		obj.focus();
		return false;         
	}
	d1 *= 2;
	for (i = 0; i < 9; i++) {
		d1 += c.charAt(i)*(11-i);
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1) {
		alert('CPF inválido!');
		obj.focus();
		return false;
    }
    return true;
	}
	else {
		return true;
	}
}
function confsenha(frm){
	if (frm.senha.value){
		if (frm.senha.value!=frm.csenha.value){
			alert('Confirmação de senha inválida, tente novamente');
			frm.senha.value='';
			frm.csenha.value='';
			frm.senha.focus();
			return false;
		}
	}
}
function elimina_virgula(obj, valor){	
	obj.value = valor.replace(',','.');
}
function valida_CNPJ(obj){
	if (obj.value!=''){
		s = obj.value;
		if (isNaN(s)) {
			alert('Preencha somente números!');
			obj.focus();
			return false;
		}
		var i;
		var c = s.substr(0,12);
		var dv = s.substr(12,2);
		var d1 = 0;
		for (i = 0; i <12; i++){
			d1 += c.charAt(11-i)*(2+(i % 8));
		}
		if (d1 == 0) {
			alert('CNPJ inválido!');
			obj.focus();
			return false;
		}
		d1 = 11 - (d1 % 11);
		if (d1 > 9) d1 = 0;
		if (dv.charAt(0) != d1){
			alert('CNPJ inválido!');
			obj.focus();
			return false;
		}
		d1 *= 2;
		for (i = 0; i < 12; i++){
			d1 += c.charAt(11-i)*(2+((i+1) % 8));
		}
		d1 = 11 - (d1 % 11);
		if (d1 > 9) 
			d1 = 0;
			if (dv.charAt(1) != d1){
				alert('CNPJ inválido!');
				obj.focus();
				return false;
			}
			return true;
	}
	else {
		return true;
	}
}
function formatnumber(num) {
	if (isNaN(num)) 	{ 
		num = "0"; 
	} 
	sign = (num == (num = Math.abs(num))); 
	num = Math.floor(num*100+0.50000000001); 
	cents = num%100; 
	num = Math.floor(num/100).toString(); 
	if(cents<10) {
		cents = "0" + cents; 
	} 
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
		num = num.substring(0,num.length-(4*i+3))+''+ num.substring(num.length-(4*i+3)); 
	} 
	return (((sign)?'':'-') + num + '.' + cents); 
} 

function max(txarea,num,c){ 
	total = num; 
	tam = txarea.value.length; 
	str=""; 
	str=str+tam; 
	document.getElementById("digitado"+c+"").innerHTML = str; 
	document.getElementById("restante"+c+"").innerHTML = total - str; 
	if (tam > total){ 
		aux = txarea.value; 
		txarea.value = aux.substring(0,total); 
		document.getElementById("digitado"+c+"").innerHTML = total; 
		document.getElementById("restante"+c+"").innerHTML = 0; 
	} 
}

function opcunico(checked,cod){
	var elements = document.getElementsByTagName('input');
	for(i = 0; i<elements.length; i++){
		if(elements[i].type == 'checkbox' && elements[i].name.indexOf('codigos['+cod+']') != -1)	{
			if(!checked)
				elements[i].checked = '';
			else
				elements[i].checked = 'checked';		
		}
	}
}

function opc(checked){
	var elements = document.getElementsByTagName('input');
	for(i = 0; i<elements.length; i++){
		if(elements[i].type == 'checkbox' && elements[i].name.indexOf('codigos') != -1)	{
			if(!checked)
				elements[i].checked = '';
			else
				elements[i].checked = 'checked';		
		}
	}
}

/**
  * Formata data (Mascara)
  * @param $campo int id
  * @param $event Evento
  * @return String telefone formatado
  **/
function formataData(campo, e) {	
	var key = typeof window.event != "undefined" ? window.event.keyCode : e.which;
	if((key == 0) || (key == 8)) {
		return true;
	}
	if(key != 45 && key != 46 && key != 8 && key != 32 && key != 13 && key != 0) {
		if(isNaN(String.fromCharCode(key)) || (key == 32)) {
			return false;
		}
	}
	
	if ( ((campo.value.length == 2) || (campo.value.length == 5))) {
		campo.value += '/';
		if(key != 47) {
			return true;
		}
		return false;
	}
}

/**
  * Formata a digitação do CPF
  *
  * @param campo String campo que será atualizado
  * @param e event evento
  * @return void
  **/
function formataCpf(campo, e) {
	t = typeof window.event != "undefined" ? window.event.keyCode : e.which;
	if((isNaN(String.fromCharCode(t)) || (t == 32)) && (t != 8 && t != 0)) {
		return false;
	}
	
	if( typeof window.event != "undefined" ) {
		if (window.event.keyCode != 45 && window.event.keyCode != 46 && window.event.keyCode != 8 && window.event.keyCode != 0 ) {
			var str = campo.value;
			str = str.replace('.','');
			str = str.replace('.','');
			str = str.replace('/','');
			str = str.replace('-','');
			temp = str.substr(0,3) ;
			if(temp.length == 3) {
				temp += '.';
			}
			temp += str.substr(3,3);
			if(temp.length == 7) {
				temp += '.';
			}
			temp += str.substr(6,3);
			if(temp.length == 11) {
				temp += '-';
			}
			temp += str.substr(9,2);
			campo.value= temp;
		}
	} else {
		if (e.which != 45 && e.which != 46 && e.which != 8 && e.which != 32 && e.which != 13 &&  e.which != 0) {
			var str = campo.value;
			str = str.replace('.','');
			str = str.replace('.','');
			str = str.replace('/','');
			str = str.replace('-','');
			temp = str.substr(0,3) ;
			if(temp.length == 3) {
				temp += '.';
			}
			temp += str.substr(3,3);
			if(temp.length == 7) {
				temp += '.';
			}
			temp += str.substr(6,3);
			if(temp.length == 11) {
				temp += '-';
			}
			temp += str.substr(9,2);
			campo.value= temp;
		}
	}
	
}

/**
  * Formata a digitação do CNPJ
  *
  * @param campo String campo que será atualizado
  * @param e event evento
  * @return void
  **/
function formataCnpj(campo, e) {
	t = typeof window.event != "undefined" ? window.event.keyCode : e.which;
	if((isNaN(String.fromCharCode(t)) || (t == 32)) && (t != 8 && t != 0)) {
		return false;
	}
	if( typeof window.event != "undefined" ) {
		if (window.event.keyCode != 45 && window.event.keyCode != 46 && window.event.keyCode != 8 && window.event.keyCode != 0 ) {
			var str = campo.value;
			str = str.replace('.','');
			str = str.replace('.','');
			str = str.replace('-','');
			str = str.replace('/','');
			temp = str.substr(0,2);
			if(temp.length == 2) {
				temp += '.';
			}
			temp += str.substr(2,3);
			if(temp.length == 6) {
				temp += '.';
			}
			temp += str.substr(5,3);
			if(temp.length == 10) {
				temp += '/';
			}
			temp += str.substr(8,4);
			if(temp.length == 15) {
				temp += '-';
			}
			temp += str.substr(12,2);
			campo.value= temp;
		}
	} else {
		if (e.which != 45 && e.which != 46 && e.which != 8 && e.which != 32 && e.which != 13 && e.which != 0) {
			var str = campo.value;
			str = str.replace('.','');
			str = str.replace('.','');
			str = str.replace('-','');
			str = str.replace('/','');
			temp = str.substr(0,2);
			if(temp.length == 2) {
				temp += '.';
			}
			temp += str.substr(2,3);
			if(temp.length == 6) {
				temp += '.';
			}
			temp += str.substr(5,3);
			if(temp.length == 10) {
				temp += '/';
			}
			temp += str.substr(8,4);
			if(temp.length == 15) {
				temp += '-';
			}
			temp += str.substr(12,2);
			campo.value= temp;
		}
	}
}

/**
  * Formata a digitação do CNPJ
  *
  * @param campo String campo que será atualizado
  * @param e event evento
  * @return void
  **/
function formataIdFederal(campo, e) {
	t = typeof window.event != "undefined" ? window.event.keyCode : e.which;
	if((isNaN(String.fromCharCode(t)) || (t == 32)) && (t != 8 && t != 0)) {
		return false;
	}
	if(campo.value.length > 14) {
		if( typeof window.event != "undefined" ) {
			if (window.event.keyCode != 45 && window.event.keyCode != 46 && window.event.keyCode != 8 && window.event.keyCode != 0 ) {
				var str = campo.value;
				str = str.replace('.','');
				str = str.replace('.','');
				str = str.replace('-','');
				str = str.replace('/','');
				temp = str.substr(0,2);
				if(temp.length == 2) {
					temp += '.';
				}
				temp += str.substr(2,3);
				if(temp.length == 6) {
					temp += '.';
				}
				temp += str.substr(5,3);
				if(temp.length == 10) {
					temp += '/';
				}
				temp += str.substr(8,4);
				if(temp.length == 15) {
					temp += '-';
				}
				temp += str.substr(12,2);
				campo.value= temp;
			}
		} else {
			if (e.which != 45 && e.which != 46 && e.which != 8 && e.which != 32 && e.which != 13 && e.which != 0) {
				var str = campo.value;
				str = str.replace('.','');
				str = str.replace('.','');
				str = str.replace('-','');
				str = str.replace('/','');
				temp = str.substr(0,2);
				if(temp.length == 2) {
					temp += '.';
				}
				temp += str.substr(2,3);
				if(temp.length == 6) {
					temp += '.';
				}
				temp += str.substr(5,3);
				if(temp.length == 10) {
					temp += '/';
				}
				temp += str.substr(8,4);
				if(temp.length == 15) {
					temp += '-';
				}
				temp += str.substr(12,2);
				campo.value= temp;
			}
		}
	} else {
		if( typeof window.event != "undefined" ) {
			if (window.event.keyCode != 45 && window.event.keyCode != 46 && window.event.keyCode != 8 && window.event.keyCode != 0 ) {
				var str = campo.value;
				str = str.replace('.','');
				str = str.replace('.','');
				str = str.replace('/','');
				str = str.replace('-','');
				temp = str.substr(0,3) ;
				if(temp.length == 3) {
					temp += '.';
				}
				temp += str.substr(3,3);
				if(temp.length == 7) {
					temp += '.';
				}
				temp += str.substr(6,3);
				if(temp.length == 11) {
					temp += '-';
				}
				temp += str.substr(9,2);
				campo.value= temp;
			}
		} else {
			if (e.which != 45 && e.which != 46 && e.which != 8 && e.which != 32 && e.which != 13 &&  e.which != 0) {
				var str = campo.value;
				str = str.replace('.','');
				str = str.replace('.','');
				str = str.replace('/','');
				str = str.replace('-','');
				temp = str.substr(0,3) ;
				if(temp.length == 3) {
					temp += '.';
				}
				temp += str.substr(3,3);
				if(temp.length == 7) {
					temp += '.';
				}
				temp += str.substr(6,3);
				if(temp.length == 11) {
					temp += '-';
				}
				temp += str.substr(9,2);
				campo.value= temp;
			}
		}
	}
}

/**
  * Formata telefone (Mascara)
  * @param $campo Int id
  * @param $event Evento
  * @return String telefone formatado
  **/
function formataTelefone(campo, e) {
	t = typeof window.event != "undefined" ? window.event.keyCode : e.which;
	if((isNaN(String.fromCharCode(t)) || (t == 32)) && (t != 8 && t != 0)){
		return false;
	}
	campo = typeof(campo) == "object" ? campo : document.getElementById(campo);
	if(campo.value.charAt(0) != "(" && !isNaN(String.fromCharCode(t)))
		campo.value = "("+campo.value;
	if( typeof window.event != "undefined" ) {
		if(window.event.keyCode != 40) {
			if(campo.value.length == 0) {
				campo.value += '(';
			}
		}
		if(window.event.keyCode != 41) {
			if(campo.value.length == 3) {
				campo.value += ')';
			}
		}
		if(window.event.keyCode != 45) {
			if(campo.value.length == 8) {
				campo.value += '-';
			}
		}
	} else {
		if (e.which != 8) {
			if(e.which != 0) {
				if(e.which != 40) {
					if(campo.value.length == 0) {
						campo.value += '(';
					}
				}
			}
			if(e.which != 41) {
				if(campo.value.length == 3) {
					campo.value += ')';
				}
			}
			if(e.which != 45) {
				if(campo.value.length == 8) {
					campo.value += '-';
				}
			}
		}
	}
}

/**
  * Formata a digitação do CEP
  * @param campo String campo que será atualizado
  * @param e event evento
  * @return void
  **/
function formataCep(campo, e) {
	t = typeof window.event != "undefined" ? window.event.keyCode : e.which;
	if((isNaN(String.fromCharCode(t)) || (t == 32)) && (t != 8 && t != 0)) {
		return false;
	}
	if( typeof window.event != "undefined" ) {
		if (window.event.keyCode != 45) {
			if (campo.value.length == 5) {
				campo.value += '-';
			}
		}
	} else {
		if (e.which != 8) {
			if (e.which != 45) {
				if (campo.value.length == 5) {
					campo.value += '-';
				}
			}
		}
	}
}

/**
  * Formata a número decimal máscara
  * @param obj String campo que será atualizado  
  * @return void
  **/
function mascara_num(obj) {
	valida_num(obj)
	valor = obj.value.replace("-","");
	valor = valor.replace(",","");
	if (valor.length >= 3) {
		valor = poe_ponto_num(valor.substring(0,valor.length-2))+","+valor.substring(valor.length-2, valor.length);
	}
	obj.value = valor;
}

/**
  * Formata a número decimal colocando os pontos
  * @param valor String campo que será atualizado  
  * @return void
  **/
function poe_ponto_num(valor) {
	valor = valor.replace(/\./g,"");
	if (valor.length > 3) {
		valores = "";
		while (valor.length > 3) {
			valores = "."+valor.substring(valor.length-3,valor.length)+""+valores;
			valor = valor.substring(0,valor.length-3);
		}
		return valor+""+valores;
	} else {
		return valor;
	}
}

/**
  * Formata a número decimal valida número
  * @param obj String campo que será atualizado  
  * @return void
  **/
function valida_num(obj) {
	numeros = new RegExp("[0-9]");
	while (!obj.value.charAt(obj.value.length-1).match(numeros)) {
		if(obj.value.length == 1 && obj.value == "-") {
			return true;
		}
		if(obj.value.length >= 1) {
			obj.value = obj.value.substring(0,obj.value.length-1);
		} else {
			return false;
		}
	}
}

/**
  * Formata número
  * @param $campo int id
  * @param $event Evento
  * @return String 
  **/
function formataNumero(campo, e) {
	t = typeof window.event != "undefined" ? window.event.keyCode : e.which;
	if((isNaN(String.fromCharCode(t)) || (t == 32)) && (t != 8 && t != 0)) {
		return false;
	}
}

/**
  * Função que valida o e-mail
  * @param String $value e-mail
  * @return boolean
  **/
function validaEmail(value) {
	expressao = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
    if(!expressao.test(value)) {
        return false;
    }
    return true;
}

/**
  * Função que valida a data
  * @param String $value data
  * @return boolean
  **/
function validaData(campo) {
    var valor = campo.split("/");
    if(valor[0] != "" && valor[1] != "" && valor[2] != "") {
        data = new Date(valor[2], valor[1]-1, valor[0]);
        if (valor[2] != data.getFullYear() || valor[1] !=  data.getMonth()+1 || valor[0] != data.getDate() || valor[2] < 1902 || valor[2] > 2037) {
            return false;
        }
    }
    return true;
}