function isOrdem(ordem,id,tabela){
	url 	= "ajax.php?acao=ordem&ordem=" + ordem + "&id=" + id + "&tabela=" + tabela;
	$.ajax({ url:url, type: "GET", cache: false, success: function(response){
		if (response == "ok"){
			window.location.reload();
		}
	}});
}
function isDestaque(id,tabela){
	
	if($('#destaque-'+id).is(':checked')){
		var check = 1;
	} else {
		var check = 0;
	}
	
	url 	= "ajax.php?acao=destaque&check=" + check + "&id=" + id + "&tabela=" + tabela;
	$.ajax({ url:url, type: "GET", cache: false, success: function(response){
		if (response == "ok"){
			window.location.reload();
		}
	}});
}
function isAtivo(id,tabela){
	if($('#ativo-'+id).is(':checked')){
		var check = 1;
	} else {
		var check = 0;
	}
	url 	= "ajax.php?acao=ativo&check=" + check + "&id=" + id + "&tabela=" + tabela;
	$.ajax({ url:url, type: "GET", cache: false, success: function(response){
		if (response == "ok") window.location.reload();
	}});
}
function isAprovado(id,tabela){
	if($('#aprovado-'+id).is(':checked')) var check = 1; else var check = 0;
	url = "ajax.php?acao=aprovado&check=" + check + "&id=" + id + "&tabela=" + tabela;
	$.ajax({ url:url, type: "GET", cache: false, success: function(response){
		if (response == "ok"){
			window.location.reload();
		}
	}});
}

function remover_espacos(str){
	r = "";
	for(i = 0; i < str.length; i++){
		if(str.charAt(i) != ' '){
			r += str.charAt(i);
		}
	}
	return r;
}
function verificaTexto(campo,limite,contador){
	var conteudo = campo.value;
	
	if (conteudo.length > limite){
		campo.value = campo.value.substr(0,limite);
	}
	document.getElementById(contador).innerHTML = (limite - conteudo.length)+" Caracteres disponíveis.";
}

function url(caminho){
	document.location.href = caminho;
}
function reescreve(campo,valor){
	if(campo.value == ""){
		campo.value = valor;
	}
}
function limpaCampo(campo,texto){
	if (campo.value == texto){
		campo.value = '';
	}
}

function getId(campo){
	return parseInt(campo.attr("id").replace(/[^\d]*/,""));
}

function array_search(findme,array){
	for (i = 0; i < array.length; i++){
		if (findme == array[i]){
			return i;
		}
	}
	return false;
}

function validaMail(email) {
	
	invalidCharsList = " /:,;~#";
	if(email.indexOf('@',0)==-1 || email.indexOf('@',0)== 0 || email.indexOf('.',0)==-1){
		return (false);
	}

	for(i = 0; i < invalidCharsList.length; i++){
		errorChar = invalidCharsList.charAt(i);
		if(email.indexOf(errorChar,0) != -1){
			return (false);
		}
	}   
}
function mask(e,field,mask){
	if (field.val().length > mask.length){
		return false;
	}
	var code = (e.which) ? e.which : e.keyCode;
	// 8 = backspace, 9 = Tab, 37 = seta esquerda, 39 = seta direita, 46 = delete
	if ((code == 8) || (code == 9) || (code == 0) || (code == 37) || (code == 39) || (code == 46)){
		return true;
	}
	
	var value = field.val();
	var length = value.length;
	var nextChar = mask.charAt(length);
	while (nextChar != "A" && nextChar != "9" && length < mask.length){
		value += nextChar;
		nextChar = mask.charAt(++length);
	}
	field.val(value);
	
	if (nextChar == "A" && (code >= 65 && code <= 90)){
		return true;
	} else if (nextChar == "9" && ((code >= 48 && code <= 57) || (code >= 96 && code <= 105))){
		return true;
	} else {
		return false;
	}
}

// JavaScript Document
function check_date(DATA) {
    var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
	var msgErro = 'Formato inválido de data.';
	var vdt = new Date();
	var vdia = vdt.getDay();
	var vmes = vdt.getMonth();
	var vano = vdt.getFullYear();
	if ((DATA.val().match(expReg)) && (DATA.val() != '')){
		var dia = DATA.val().substring(0,2);
		var mes = DATA.val().substring(3,5);
		var ano = DATA.val().substring(6,10);
		if((mes==04 && dia > 30) || (mes==06 && dia > 30) || (mes==9 && dia > 30) || (mes==11 && dia > 30)){
			
			showError(DATA,"Dia incorreto! O mês especificado contém no máximo 30 dias.");
			DATA.focus();
			return false;
		} else{ //1
			if(ano%4!=0 && mes==2 && dia>28){
				showError(DATA,"Dia incorreto! O mês especificado contém no máximo 28 dias.");
				DATA.focus();
				return false;
			} else{ //2
				if(ano%4==0 && mes==2 && dia>29){
					showError(DATA,"Dia incorreto! O mês especificado contém no máximo 29 dias.");
					DATA.focus();
					return false;
				} else{ //3
					return true;
				} //3-else
			}//2-else
		}//1-else                       
	} else { //5
		showError(DATA,msgErro);
		DATA.focus();
		return false;
	} //5-else
}

function validaCPF(cpf){
	
	var campo 	= $("#cpf");
	var retorno = $('.retorno-cadastro');
	var filtro 	= /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
	
	if(!filtro.test(cpf)){
		retorno.html("Preencha seu CPF corretamente!");
		return false;
	}
	
	cpf = remove(cpf, ".");
	cpf = remove(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"){
		retorno.html("Preencha seu CPF corretamente!");
	  	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))){
		retorno.html("Preencha seu CPF corretamente!");
		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))){
		retorno.html("Preencha seu CPF corretamente!");
	 	return false;
	}
	return true;
}
	
function remove(str, sub) {
	i = str.indexOf(sub);
   	r = "";
   	if (i == -1) return str;
   	r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
   	return r;
}
