// uso: onkeypress="return mascaraData(this,event);" maxlength="10"
function mascaraData(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + '/';
	if (vdateValue.value.length == 5) vdateValue.value=vdateValue.value + '/';

	
	return true;
}


//Função que retorna o nome da participante
//mas só uma parte (o primeiro ou o primeiro e o segundo)
										
function cortaNome(nome){
	//divide a frase vinda em espaços (enteres ou quebras de linha também são válidas)
	nomeArr = nome.split(/\s/);
	//pega o primeiro nome
	nomeApresentar = nomeArr[0];
	
	//Se o primeiro nome tem menos de três letras, concatena com o segundo
	//(se este existir)
	if (nomeApresentar.length <= 3){
		nomeApresentar += "&nbsp;"
		nomeApresentar += (nomeArr[1] != undefined && nomeArr[1] != null) ? nomeArr[1] : "";
	}
	
	//se mesmo assim tiver menos de (ou exatamente) três letras
	//concatena com o próximo (se este existir)
	if (nomeApresentar.length <= 3){
		nomeApresentar += "&nbsp;" 
		nomeApresentar +=	(nomeArr[2] != undefined && nomeArr[2] != null) ? nomeArr[2] : "";
	}
	
	//retorna o nome
	return nomeApresentar
}

// mascara de números
// uso: onkeypress="return(mascaraNumeros(this,event));"
function mascaraNumeros(campo,e) {
	var teclaValida = true;
	
	// Pega o código da tecla digitada
	var whichCode = (window.Event) ? e.which : e.keyCode;
	// Transforma o código da tecla digitada em um valor real(ex: cod. 49 -> valor. 1) 
	tecla = String.fromCharCode(whichCode);
	
	/* TECLAS 
	* 0 -> Del
	* 8 -> BackSpace
	* 48-57 -> numeros de 0 a 9
	*/
	if (whichCode == '8' || whichCode == '0') {
		teclaValida = true;
	} else if (whichCode >= '48' && whichCode <= '57') {
		teclaValida = true;
	} else {
		teclaValida = false;
	}

	// Retorno da função (se a tecla for válida, aparece no campo)	
	if (teclaValida == true) return true;
	if (teclaValida == false) return false;
	
}

// Envia um form para ser processado em uma página
// uso: ajax(nomeForm, urlDestino, divRetorno, textoCarregando, callback)
// uso: ajax("form_name", "pagDestino.asp?id=30&fk_cliente=90", "div_recebeEditar", "Carregando Pax", "funcaoRetorno")
function ajax(nomeForm, urlDestino, divRetorno, textoCarregando, callback) {
	
	//se o elemento de retorno não existir,
	//não executa o micox
	if (document.getElementById(divRetorno) == null){
		return false;
	}
	
	
	// texto padrão do carregando
	if (textoCarregando == "") { textoCarregando = "Carregando..."; }

	if (callback != '') {
		ajaxGo({
			form: nomeForm,
			url: urlDestino,
			loading: "<div style='width:400px;'><img src='../../LIB/imagens/icones/loading.gif'style='vertical-align:middle;'/> "+textoCarregando+"</div>",
			elem_return: divRetorno,
			callback: callback
		})
	} else {
		ajaxGo({
			form: nomeForm,
			url: urlDestino,
			loading: "<div style='width:400px;'><img src='../../LIB/imagens/icones/loading.gif'style='vertical-align:middle;'/> "+textoCarregando+"</div>",
			elem_return: divRetorno
		})
	}

}


//include
function include_css(arquivo){
	//By Fabrício Magri e Micox
	//http://elmicox.blogspot.com/2006/12/include-em-javascript.html
	var novo = document.createElement('link');
	novo.setAttribute('rel', 'stylesheet');
	novo.setAttribute('type', 'text/css');
	novo.setAttribute('media', 'screen');
	novo.setAttribute('href', arquivo);
	document.getElementsByTagName('head')[0].appendChild(novo);
	//apos a linha acima o navegador inicia o carregamento do arquivo
	//portanto aguarde um pouco até o navegador baixá-lo. :)
}


function include_js(arquivo){
	var novo = document.createElement('script');
	novo.setAttribute('type', 'text/javascript');
	novo.setAttribute('src', arquivo);
	document.getElementsByTagName('head')[0].appendChild(novo);
}


function validaEmail(nformElement){
	
	if (nformElement.value == "") {
		alert("Informe seu e-mail.");
		nformElement.focus();
		nformElement.select();
		return false;
	} else {
		prim = nformElement.value.indexOf("@")
		if(prim < 2) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf("@",prim + 1) != -1) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf(".") < 1) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf(" ") != -1) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf("zipmeil.com") > 0) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf("hotmeil.com") > 0) {
			//alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf(".@") > 0) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf("@.") > 0) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf(".com.br.") > 0) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf("/") > 0) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf("[") > 0) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf("]") > 0) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf("(") > 0) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf(")") > 0) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
		if(nformElement.value.indexOf("..") > 0) {
			alert("E-mail informado invalido.");
			nformElement.focus();
			nformElement.select();
			return false;
		}
	}
		return true;

}



function validaCampo(nformElement,nomeCampo){
	
	if (nformElement.value == "") {
		alert("Campo "+nomeCampo+" obrigatorio.");
		nformElement.focus();
		nformElement.select();
		return false;
	}
	
	return true;
}


// MOSTRA DIV COM UMA MENSAGEM TEMPORÁRIA
//USO: iAjax_msg({tempo:5, status:'ok', mensagem:'mensagem', titulo:'titulo' })
function iAjax_msg(parametros){

	var tempo = parametros["tempo"];
	var status = parametros["status"];
	var mensagem = parametros["mensagem"];
	var titulo = parametros["titulo"];
	
	var iAjax_msg = document.getElementById("mensagem");
	var iAjax_imagem = document.getElementById("mensagem_imagem");
	var mensagem_texto = document.getElementById("mensagem_texto");
	var mensagem_titulo = document.getElementById("mensagem_titulo");
	//tratamentos
	tempo = parseInt(tempo);
	
	// se o tempo nao for passado, padrão de 3 segundos
	if(tempo == ""){
		tempo = 3000
	}else{
		tempo = tempo*1000
	}
	
	// se o tempo nao for passado, padrão de 3 segundos
	if(status == "ok"){
		corFundo = "#87C53C";
		corTexto = "#000";
		imagem = "ico_msg_ok.png";
		if(titulo == undefined || titulo == "") titulo = "Sucesso";
	}else if(status == "erro"){
		corFundo = "#BE3F3F";
		corTexto = "#FFF";
		imagem = "ico_msg_erro.png";
		if(titulo == undefined || titulo == "") titulo = "Erro";
	}else if(status == "normal"){
		corFundo = "#D7D7D7";
		corTexto = "#FFF";
		imagem = "ico_msg_normal.png";
		if(titulo == undefined || titulo == "") titulo = "Alerta";
	}

	$('#mensagem').slideDown();
	iAjax_msg.style.backgroundColor = corFundo;
	mensagem_texto.style.color = corTexto;
	mensagem_titulo.style.color = corTexto;
	
	$('#mensagem_imagem').slideDown();
	iAjax_imagem.style.background="url('../../LIB/images/"+imagem+"') no-repeat"; 
	
	$('#mensagem_titulo').html(titulo);
	$('#mensagem_texto').html(mensagem);
	
	setTimeout(" $('#mensagem').fadeOut('slow');$('#mensagem_imagem').fadeOut('slow');", tempo);
}

function retira_acentos(palavra) {  
		com_acento = 'áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ';
		sem_acento = 'aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC';  
		nova='';  
		for(i=0;i<palavra.length;i++) {  
			if (com_acento.search(palavra.substr(i,1))>=0) {  
			nova+=sem_acento.substr(com_acento.search(palavra.substr(i,1)),1);  
			}  
			else {  
			nova+=palavra.substr(i,1);  
			}  
		}  
		return nova;  
}