function apenasNumeros(e){
	/* how to use
		onKeyPress="return apenasNumeros(event);"
	*/
	navegador = /msie/i.test(navigator.userAgent);
	
	if (navegador){
		var tecla = event.keyCode;
	}else{
		var tecla = e.which;
	}

	if(tecla > 47 && tecla < 58 || tecla ==0) {// numeros de 0 a 9
		return true;
	}else{
		if (tecla != 8){ // backspace
			return false;
		}else{
			return true;
		}
	}
}

/* inicio Valida Cpf */
function validaCPF(cpf) {
	if (cpf.replace(/^\s*|\s*$/g,"").length == 11) {
		var cpf1 = limpaCPF(cpf);
		if(checaCPF(cpf1)){ return true; }else if(cpf1!=""){ return false; }
	} else {
		return false;
	}
}

function limpaCPF(cpf) {
	l = cpf.length;
	st2 = "";
	for (i = 0; i < l; i++) {
		caracter = cpf.substring(i,i+1);
		if ((parseInt(caracter) >= 0) && (parseInt(caracter) <= 9))
			st2 = st2 + caracter;
	}
	return st2;
}
		
function checaCPF(CPF) {
	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" || CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||	CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" || CPF == "88888888888" || CPF == "99999999999" || CPF == "01234567890")
		return false;
	soma = 0;
	for (i=0; i < 9; i ++)
		soma += parseInt(CPF.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(9)))
		return false;
	soma = 0;
	for (i = 0; i < 10; i ++)
		soma += parseInt(CPF.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(10)))
		return false;
	return true;
}
/* fim valida cpf */

/* inicio valida email */
function validaEMail(texto) {
	var msgauxem=false;
	if (texto.length >= 0) {
		if(validate1(texto) == false) {
			msgauxem=true;
			erro=true;
		}
		c=0;
		for(i=0; i<=texto.length;i++) {
			if(texto.substring(i,i+1) == "@") {
				c++;
			}
		}
		if(c!=1) {
			msgauxem=true;
			erro=true;
		}
		if(texto.substring(texto.length-1,texto.length) == "-" || texto.substring(texto.length-1, texto.length) == "_") {
			msgauxem=true;
			erro=true;
		}
		for(i=0;i<=texto.length;i++) {
			if(texto.substring(i,i+2) == "-." || texto.substring(i,i+2) == "_." || texto.substring(i,i+2)== "..") {
				msgauxem=true;
				erro=true;
			}
		}
		for(i=0;i<=texto.length;i++) {
			if(texto.substring(i,i+2) == ".-" || texto.substring(i,i+2) == "._") {
				msgauxem=true;
				erro=true;
			}
		}
		p=texto.indexOf('@');
		if (p<1 || p>(texto.length-6)) {
			msgauxem=true;
			erro=true;
		}
		texto1=texto.substring((p+1),texto.length);
		t=texto1.indexOf('.');
		texto4=texto1.substring(0,t);
		if (t<1 || t>(texto1.length-3)) { //
			msgauxem=true;
			erro=true;
		}
		if(texto1.substring(0,1) == "_" || texto1.substring(0,1) == "-") {
			msgauxem=true;
			erro=true;
		}
		if(texto.substring(0,1)=="_" || texto.substring(0,1) == "-" ) {
			msgauxem=true;
			erro=true;
		}
		texto2=texto.substring(p-1,p);
		if(texto2 == "." || texto2 == "-" || texto2 == "_") {
			msgauxem=true;
			erro=true;
		}
		r=texto.indexOf('.');
		if (r<1) {
			msgauxem=true;
			erro=true;
		}
		texto3=texto.substring(r+1,texto.length);
		u=texto3.indexOf('.');
		if(u>(texto3.length-3)) {
			msgauxem=true;
			erro=true;
		}
	}
	
	if (msgauxem == true) { return false; } else { return true; }
	
}

function validate1(texto) {
	var valid = "abcdefghijklmnopqrstuvxyzwABCDEFGHIJKLMNOPQRSTUVXYZW1234567890-_.@"
				var ok = "yes";
	var temp;
	for (var i=0; i<=texto.length; i++) {
		temp = "" + texto.substring(i, i+1);
		if (valid.indexOf(temp) == "-1")
			ok = "no";
	}
	
	if (ok == "no") {
		return false;
	}
	
	return true;
}

/* fim valida email */

/* inicio valida form */
function validar(form){
	for (var i = 0; i < form.elements.length; i++) {
	
		if (form.elements[i].type == "text" ) {
			if (form.elements[i].value=="" ){			
				if (form.elements[i].title !=""){
						alert("Preencha o Campo " + form.elements[i].title);
						form.elements[i].focus();
						return false;
				}
			}else if(form.elements[i].title.indexOf("CPF")>=0){
					
				if(!validaCPF(form.elements[i].value)){
					alert("CPF inválido");
					form.elements[i].focus();
					return false;
				}

			}else if(form.elements[i].title.indexOf("E-mail")>=0){
				if(!validaEMail(form.elements[i].value)){
					alert("E-mail inválido");
					form.elements[i].focus();
					return false;
				}
			}
		}else if(form.elements[i].type == "textarea"){
			if (form.elements[i].value=="" ){
				if (form.elements[i].title !=""){
					alert("Preencha o Campo " + form.elements[i].title);
					form.elements[i].focus();
					return false;
				}
			}
		}else if(form.elements[i].type == "select-one"){
			if (form.elements[i].title !=""){
				if(form.elements[i].value ==""){
					alert("Escolha um(a) " + form.elements[i].title);
					form.elements[i].focus();
					return false;
				}
			}
		}
	}
	
	
	form.submit();
	
	
}

/* fim valida form */
 function ajaxJs(){
    try {
         aj = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(e) {
         try {
                aj = new ActiveXObject("Msxml2.XMLHTTP");
         } catch(ex) {
                try {
                   aj = new XMLHttpRequest();
                }catch(exc) {
                   alert("Esse browser não tem recursos para uso do Ajax");
                   aj = null;
                }
         }
        }

        return aj;
 }
 
function formatar(src, mask, e) {
	if(apenasNumeros(e)==true){
	  var i = src.value.length;
	  var saida = mask.substring(0,1);
	  var texto = mask.substring(i)
	   
	   if (texto.substring(0,1) != saida)   {
			src.value += texto.substring(0,1);
	   }
   }else{
	   return false;
   }
}


 function addRemove(pes_id,n){
      ajaxItens = ajaxJs();
      obj = document.getElementById('chk_' + n);
      obj2 = document.getElementById('eve_id');
      eve_id = obj2.options[obj2.selectedIndex].value
      acao = "";
      if(obj.checked){
              var acao = "add";
      }else{
              var acao = "remove";
      }

     
     ajaxItens.open("POST", "c_participante_add.php?xid=<?=$_GET['xid']?>", true);
     ajaxItens.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     ajaxItens.send("pes_id="+ pes_id +"&acao=" + acao +"&eve_id="+ eve_id);
     
   }
 