/*********************************
Função para buscar option dentro de selectbox
<input type=text name=txt size=30 maxlength=40 onkeyup="javascript:BuscaSelect(this,'id_select_a_ser_pesquisado');">
*********************************/
function BuscaSelect(obj, idform){
	obj2 = document.getElementById(idform);
	tam = obj2.length //numero de elementos (options) do selectbox
	texto = obj.value.toUpperCase(); //passa para caixa alta o  valor digitado
	var item = '';
	for(i=0;i < tam;i++){
		item = obj2.options[i].text.toUpperCase(); //passa para caixa alta o valor do option atual
		if(item.search(texto) != -1){	//se procura na linha(option) nao retornar falso, entao seleciona a opcao
			obj2.options[i].selected = true
			break;
		}
	}
}


/*********************************
Função para fechar popup e carregar a página pai
*********************************/
function carregaPai(){
	window.opener.location.reload();
	window.close();
}


/*********************************
PERMITE SOMENTE NUMEROS NO CAMPO
onkeypress="return somenteNumero(event);"
*********************************/
function somenteNumero(e) {
	if (!e) e = window.event;
	var tecla;		
	if(e.keyCode)    tecla = e.keyCode;
	else if(e.which) tecla = e.which; //Netscape 4.?	

	if ((tecla > 47 && tecla < 58) || (tecla == 44 && campo.value.indexOf(",") == -1) || (tecla == 8) || (tecla == 9))
		return true;
	else
		return false;
}


function blockAspas(e)
{
	if (!e) e = window.event;
	var tecla;		
	if(e.keyCode)    tecla = e.keyCode;
	else if(e.which) tecla = e.which; //Netscape 4.?	
	if (tecla != 39)
		return true;
	else
		return false;
}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){ 
                    return true; 
                }
    }else{
        return false;
        }
}

/*
* The input parameters are: the field name;
* field that holds the number of characters remaining;
* the max. numb. of characters.
*/ 
function textCounter(field, countfield, maxlimit) 
{
	if (field.value.length > maxlimit) // if the current length is more than allowed
		field.value =field.value.substring(0, maxlimit); // don't allow further input
	else
		countfield.value = maxlimit - field.value.length;

}

function textCounterWord(field, countfield, maxlimit) 
{
	var string= field.value;

	var words = string.split(" ").length + string.split("\n").length - 1;
	if(string == "")
		words = 0;
	if (words > maxlimit) // if the current length is more than allowed
		field.value =field.value.substring(0, field.value.length - 1); // don't allow further input
	else
		countfield.value = maxlimit - words;

}


/*********************************
FORMATA CAMPO DATA
onkeydown="formataData(this,event)"
*********************************/
function formataData(campo,e) {
	if (!e) e = window.event;
	var tecla;		
	if(e.keyCode)    tecla = e.keyCode;
	else if(e.which) tecla = e.which; //Netscape 4.?		
	if(e.target) 
		//Firefox
		objEv = e.target;
	else if(e.srcElement) 
		//IE
		objEv = e. srcElement;			
	vr = objEv.value;
	vr = vr.replace(".","");
	vr = vr.replace("/","");
	vr = vr.replace("/","");
	tam = vr.length + 1;
	if (tecla != 9 && tecla != 8) {
		if (tam > 2 && tam < 5) {
			objEv.value = vr.substr(0,tam - 2) + '/' + vr.substr(tam - 2,tam);
		}
		if (tam >= 5 && tam <= 10) {
			objEv.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,3);
		}
	}
}


/*********************************
FORMATA CAMPO HORA
onkeydown="formataHora(this,event)"
*********************************/
function formataHora(campo,e) {
	if (!e) e = window.event;
	var tecla;		
	if(e.keyCode)    tecla = e.keyCode;
	else if(e.which) tecla = e.which; //Netscape 4.?		
	if(e.target) 
		//Firefox
		objEv = e.target;
	else if(e.srcElement) 
		//IE
		objEv = e. srcElement;			
	vr = objEv.value;
	vr = vr.replace(".","");
	vr = vr.replace(":","");
	vr = vr.replace(":","");
	tam = vr.length + 1;
	if (tecla != 9 && tecla != 8) {
		if (tam > 2 && tam < 5) {
			objEv.value = vr.substr(0,tam - 2) + ':' + vr.substr(tam - 2,tam);
		}
		if (tam >= 5 && tam <= 10) {
			objEv.value = vr.substr(0,2) + ':' + vr.substr(2,2) + ':' + vr.substr(4,3);
		}
	}
}


function validaCPF (obj) {
	var CPF = obj.value;
	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
		CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
		CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
		CPF == "88888888888" || CPF == "99999999999"){
		alert("CPF INVÁLIDO");
		obj.value = "";				
		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))){
		alert("CPF INVÁLIDO ");
		obj.value = "";
		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))){
		alert("CPF INVÁLIDO");
		obj.value = "";
		return false;
	}
	return true;
}