function adicionarCSS(dir, css){
	var headID = document.getElementsByTagName("head")[0];
	var cssNode = document.createElement('link');
	cssNode.type = "text/css";
	cssNode.rel = "stylesheet";
	cssNode.href = dir+css;
	headID.appendChild(cssNode);
}

function confirmarFoco(event,verif,proximo){
	if(event.keyCode != 8 && event.keyCode != 9 && event.keyCode != 35 && event.keyCode != 36 && event.keyCode != 37 && event.keyCode != 39 && event.keyCode != 46){
		if(verif && proximo != null)
			proximo.focus();
	}
}

var reDigitos = /^\d+$/;

function validarNumeros(numeros){
	if (reDigitos.test(numeros)) {
		return true;
	} else if ((numeros != null && numeros != "") || numeros == "") {
		return false;
	}
}

function ForceNumericInput(event, This, AllowDecimal, AllowMinus){
if(arguments.length == 1){
	var s = This.value;
	// garante que o sinal de "-" seja o primeiro do índice
	var i = s.lastIndexOf("-");
	if(i == -1)
		return;
	if(i != 0)
		This.value = s.substring(0,i)+s.substring(i+1);
		return;
	}
	switch(event.keyCode){
		case 8:     // backspace
		case 9:     // tab
		case 35:    // end
		case 36:    // home
		case 37:    // left arrow
		case 39:    // right arrow
		case 46:    // delete
		event.returnValue = true;
		return;
	}
	if(event.keyCode == 189) {     // sinal de número de negativo
		if(AllowMinus == false){
			CancelEventExecution(event);
			return;
		}
		// aguarda até que o controle tenha sido atualizado
		var s = "ForceNumericInput(document.getElementById('"+This.id+"'))";
		setTimeout(s, 250);
		return;
	}
	if(AllowDecimal && (event.keyCode == 188 || event.keyCode == 110)){
		if(This.value.indexOf(",") >= 0){// restringe a digitação de apenas uma vírgula
			CancelEventExecution(event);
			return;
		}
		event.returnValue = true;
		return;
	}
	// permite caracteres entre 0 e 9
	if((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)){
		event.returnValue = true;
		return;
	}
	CancelEventExecution(event);
}
/*
* Cancela a execução de uma function mapeada por um evento
*/
function CancelEventExecution(event){
	if (navigator.appName == "Netscape"){
		event.preventDefault();
	}else{
		event.returnValue = false;
	}
}

var reCaracteres = /^[ \t\r\n\v\f]|[áàãâÁÀÃÂéèêÉÈÊíìîÍÌÎóòõôÓÒÕÔúùûÚÙÛ]|[a-zA-Z]+$/;

function validarNome(nome){
	if (reCaracteres.test(nome))
		return true;
	else if ((nome != null && nome != "") || nome == "")
		return false;
}

var reAlfaNumericos = /^(\-)|(\,)|(\.)|(°)|(\ª)|[áàãâÁÀÃÂéèêÉÈÊíìîÍÌÎóòõôÓÒÕÔúùûÚÙÛ]|\w+$/;

function validarEndereco(endereco){
	if (reAlfaNumericos.test(endereco))
		return true;
	else if ((endereco != null && endereco != "") || endereco == "")
		return false;
}

var 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}\])$/;

function validarEmail(email){
	if (reEmail.test(email))
		return true;
	else if ((email != null && email != "") || email == "")
		return false;
}

var reHorario = /^([0-1]\d|2[0-3]):[0-5]\d$/;;

function validarHorario(horario){
	if (reHorario.test(horario))
		return true;
	else if ((horario != null && horario != "") || horario == "")
		return false;
}

var reData = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/;

function validarData(data){
	if (reData.test(data))
		return true;
	else if ((data != null && data != "") || data == "")
		return false;
}

var reTelefone = /^\(\d{2}\) \d{4}\-\d{4}$/;

function validarTelefone(telefone){
	if (reTelefone.test(telefone))
		return true;
	else if ((telefone != null && telefone != "") || telefone == "")
		return false;
}

var reCEP = /^\d{2}\.\d{3}\-\d{3}$/;

function validarCEP(cep){
	if (reCEP.test(cep))
		return true;
	else if ((cep != null && cep != "") || cep == "")
		return false;
}

function validarCPF(valcpf){
	var cpf = desmascaraCPF(valcpf);
	if(validarNumeros(cpf) && cpf.length == 11){
		var digitoDigitado = eval(cpf.charAt(9)+cpf.charAt(10));
		var soma1=0;
		var soma2=0;
		var vlr=11;

		for(i=0;i<9;i++){
			soma1+=eval(cpf.charAt(i)*(vlr-1));
			soma2+=eval(cpf.charAt(i)*vlr);
			vlr--;
		}       
		soma1=(((soma1*10)%11)==10 ? 0:((soma1*10)%11));
		soma2=(((soma2+(2*soma1))*10)%11);

		var digitoGerado=(soma1*10)+soma2;
		if(digitoGerado!=digitoDigitado)
			return false;
		else
			return true;
	} else
		return false;
}

function validarCNPJ(valcnpj){
	var numeros, digitos, soma, i, resultado, pos, tamanho;
	var cnpj = desmascaraCNPJ(valcnpj);
	if(validarNumeros(cnpj) && cnpj.length == 14){
		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 mascaraHorario(evento,campo){
	var key;
	if(window.event) { key = evento.keyCode; } 
    else if(evento.which) { key = evento.which; }
	var horario = campo.value;
	if(key != 8 && key != 37 && key != 39){
		if (horario.length == 2){
			horario = horario + ':';
			campo.value = horario;
		}
	}
	if (horario.length == 5)
		return validarHorario(horario);
	else
		return false;
}

function mascaraHorarioFoco(evento,campo,proximo){
	var verif = mascaraHorario(evento,campo);
	confirmarFoco(evento,verif,proximo);
	return verif;
}

function mascaraData(evento,campo){
	var key;
	if(window.event) { key = evento.keyCode; } 
    else if(evento.which) { key = evento.which; }
	var data = campo.value;
	if(key != 8 && key != 35 && key != 36 && key != 37 && key != 39){
		if (data.length == 2 || data.length == 5){
			data = data + '/';
			campo.value = data;
		}
	}
	if (data.length == 10)
		return validarData(data);
	else
		return false;
}

function mascaraDataFoco(evento,campo,proximo){
	var verif = mascaraData(evento,campo);
	confirmarFoco(evento,verif,proximo);
	return verif;
}

function desmascaraTelefone(valtelefone){
	var telefone = valtelefone.toString();
	return (telefone.substr(1,2) + telefone.substr(5,4) + telefone.substr(10,4));
}

function mascaraTelefone(evento,campo){
	var key;
	if(window.event) { key = evento.keyCode; } 
    else if(evento.which) { key = evento.which; }
	var telefone = campo.value;
	if(key != 8 && key != 35 && key != 36 && key != 37 && key != 39){
		if (telefone.length == 2){
			telefone = '(' + telefone + ') ';
			campo.value = telefone;
		}
		if (telefone.length == 9){
			telefone = telefone + '-';
			campo.value = telefone;
		}
	}		
	if (telefone.length == 14)
		return validarTelefone(telefone);
	else
		return false;
}

function mascaraTelefoneFoco(evento,campo,proximo){
	var verif = mascaraTelefone(evento,campo);
	confirmarFoco(evento,verif,proximo);
	return verif;
}

function desmascaraCEP(valcep){
	var cep = valcep.toString();
	return (cep.substr(0,2) + cep.substr(3,3) + cep.substr(7,3));
}

function mascaraCEP(evento,campo){
	var key;
	if(window.event) { key = evento.keyCode; } 
    else if(evento.which) { key = evento.which; }
	var cep = campo.value;
	if(key != 8 && key != 35 && key != 36 && key != 37 && key != 39){
		if (cep.length == 2){
			cep = cep + '.';
			campo.value = cep;
		}
		if (cep.length == 6){
			cep = cep + '-';
			campo.value = cep;
		}
	}
	if (cep.length == 10)
		return validarCEP(cep);
	else
		return false;
}

function mascaraCEPFoco(evento,campo,proximo){
	var verif = mascaraCEP(evento,campo);
	confirmarFoco(evento,verif,proximo);
	return verif;
}

function desmascaraCPF(valCPF){
	var cpf = valCPF.toString();
	return (cpf.substr(0,3) + cpf.substr(4,3) + cpf.substr(8,3) + cpf.substr(12,2));
}

function mascaraCPF(evento,campo){
	var key;
	if(window.event) { key = evento.keyCode; } 
    else if(evento.which) { key = evento.which; }
	var cpf = campo.value;
	if(key != 8 && key != 35 && key != 36 && key != 37 && key != 39){
		if (cpf.length == 3 || cpf.length == 7){
			cpf = cpf + '.';
			campo.value = cpf;
		}
		if (cpf.length == 11){
			cpf = cpf + '-';
			campo.value = cpf;
		}
	}
	if (cpf.length == 14)
		return validarCPF(cpf);
	else
		return false;
}

function mascaraCPFFoco(evento,campo,proximo){
	var verif = mascaraCPF(evento,campo);
	confirmarFoco(evento,verif,proximo);
	return verif;
}

function desmascaraCNPJ(valcnpj){
	var cnpj = valcnpj.toString();
	return (cnpj.substr(0,3) + cnpj.substr(4,3) + cnpj.substr(8,3) + cnpj.substr(12,4) + cnpj.substr(17,2));
}

function mascaraCNPJ(evento,campo){
	var key;
	if(window.event) { key = evento.keyCode; } 
    else if(evento.which) { key = evento.which; }
	var cnpj = campo.value;
	if(key != 8 && key != 35 && key != 36 && key != 37 && key != 39){
		if (cnpj.length == 3 || cnpj.length == 7){
			cnpj = cnpj + '.';
			campo.value = cnpj;
		}
		if (cnpj.length == 11){
			cnpj = cnpj + '/';
			campo.value = cnpj;
		}
		if (cnpj.length == 16) {
			cnpj = cnpj + '-';
			campo.value = cnpj;
		}
	}
	if (cnpj.length == 19)
		return validarCNPJ(cnpj);
	else
		return false;
}

function mascaraCNPJFoco(evento,campo,proximo){
	var verif = mascaraCNPJ(evento,campo);
	confirmarFoco(evento,verif,proximo);
	return verif;
}