
/* Função não aceitar somente números */
var r={
	'special':/[\W]/g,
	'quotes':/['\''&'\"']/g,
	'notnumbers':/[^\d]/g
}
function valid(o,w){
	o.value = o.value.replace(r[w],'');
}

//Função que exibe o campo de CPF para inserir um novo participante
function exibeCampoCpf(){
	document.getElementById('trNovo').style.display = 'none';
	document.getElementById('trCpf').style.display = 'inline';
}

/** CARREGA AS CIDADES VIA AJAX **/
function carregaCidades(uf){
	var cidade = $("#idCidade").val();

	$("#carregando").show();
	
	$("#span_cidade").load("RequestMunicipos.do?uf="+uf+"&cidade="+cidade);
	$("#span_cidade").ajaxError(function(request, settings){
		$("#span_cidade").html("<span style='color:#FF0000'>Falha ao buscar as cidades.</span>"); 
		$("#loading").hide();
	});
}


//Exibe ou oculta os campo Letra, no cadastro de obra ao clicar
function escolheFocoMusical(foco){
	if(foco == "cancao"){
		document.getElementById('imgCancao').src = "img/bt/btCancao.jpg";
		document.getElementById('imgInstrumental').src = "img/bt/btInstrumentalPressed.jpg";
		document.getElementById('focoMusicalId').value = "cancao";
		document.getElementById('trLetra').style.display = '';
	}else{
		document.getElementById('imgInstrumental').src = "img/bt/btInstrumental.jpg";
		document.getElementById('imgCancao').src = "img/bt/btCancaoPressed.jpg";
		document.getElementById('focoMusicalId').value = "instrumental";
		document.getElementById('trLetra').style.display = 'none';
	}
}

//Exibe ou oculta os campo Letra, no cadastro de obra ao abrir a tela
function inicializaFocoMusical(foco){
	if(foco == "cancao"){
		document.getElementById('imgCancao').src = "img/bt/btCancao.jpg";
		document.getElementById('imgInstrumental').src = "img/bt/btInstrumentalPressed.jpg";
		document.getElementById('focoMusicalId').value = "cancao";
		document.getElementById('trLetra').style.display = '';
	}else if(foco == "instrumental"){
		document.getElementById('imgCancao').src = "img/bt/btCancaoPressed.jpg";
		document.getElementById('imgInstrumental').src = "img/bt/btInstrumental.jpg";
		document.getElementById('focoMusicalId').value = "instrumental";
		document.getElementById('trLetra').style.display = 'none';
	}else{
		document.getElementById('focoMusicalId').value = "cancao";
	}
}

function exibeBandaDeApoio(valor){
	if(valor == "S"){
		document.getElementById('instrumentos').style.display = '';
	}else{
		document.getElementById('instrumentos').style.display = 'none';
	}
}

function inicializaBandaDeApoio(valor){
	if(valor == "S"){
		document.getElementById('instrumentos').style.display = '';
	}else{
		document.getElementById('instrumentos').style.display = 'none';
	}
}

//Verifica se os dois campos de upload de arquivos estão preenchidos
/*function validaFormArquivo() {
	if($("#uploadMp3").val() == ""){
		alert("Selecione o arquivo MP3.");
		
	}else if($("#uploadPartitura").val() == ""){
		alert("Selecione a partitura ou a letra cifrada.");
		
	}else{
		if (verificaExtensao($("#uploadMp3").val()) && verificaExtensao($("#uploadPartitura").val()) 
				&& (verificaExtensaoMusica($("#uploadMp3").val()))){
			
			if ($("#uploadMp3").val() == $("#uploadPartitura").val()){
				alert("Selecione dois arquivos diferentes.");
			}else {
				$("#loading").show();
				$("#formArquivo").attr("action", "ArquivoAction.do");
				$("#formArquivo").submit();
			}
		}
	}
}*/
function validaFormArquivo(action){
	var status = true;
	var msg = "";
	
	if($("#uploadMp3").length && $("#uploadMp3").val() == ""){
		$(".msgObrigatorio").html("Selecione o arquivo MP3.");
		
	}else if($("#uploadPartitura").length && $("#uploadPartitura").val() == ""){
		$(".msgObrigatorio").html("Selecione a partitura ou letra cifrada.");
		
	}else{
		if($("#uploadMp3").length && !$("#uploadPartitura").length){
			if(verificaExtensao($("#uploadMp3").val()) && (verificaExtensaoMusica($("#uploadMp3").val()))){
				status = false;
			}
			
		}else if(!$("#uploadMp3").length && $("#uploadPartitura").length){
			if(verificaExtensao($("#uploadPartitura").val())){
				status = false;
			}
		}else{
			if(verificaExtensao($("#uploadMp3").val()) && verificaExtensaoMusica($("#uploadMp3").val()) && verificaExtensao($("#uploadPartitura").val())){
				if ($("#uploadMp3").val() == $("#uploadPartitura").val()){
					$(".msgObrigatorio").html("Selecione dois arquivos diferentes.");
				}else {
					status = false;
				}
			}
		}
	}
	
	if(status){
		$("#oculto").fadeOut();
		$("#oculto").fadeIn(300);
		
	}else{
		$("#loading").show();
		$("#formArquivo").attr("action", action);
		$("#formArquivo").submit();
		//alert('Submetendo...');
	}
}


//Valida os tipos de extensão dos arquivos de upload
function verificaExtensao(campo) {   
    var validos = /(exe|bat|sh|batch|dll)/;   
    var ext = campo.substring(campo.length-3,campo.length);
    ext = ext.toLowerCase();
    if ( validos.test(ext) ) {
    	$(".msgObrigatorio").html("Arquivo com extensão não permitida: ." + ext);
        return false;
    }   
    return true;   
}

//Verifica se no campo expecifico está com o tipo de arquivo especifico
function verificaExtensaoMusica(campo) {   
    var validos = /(mp3|wma)/;   
    var ext = campo.substring(campo.length-3,campo.length);
    ext = ext.toLowerCase();
    if ( !validos.test( ext ) ) {
    	$(".msgObrigatorio").html("Arquivo de Música com extensão não permitida: ." + ext);
        return false;
    }   
    return true;   
}

//Verifica se no campo expecifico está com o tipo de arquivo especifico
function verificaExtensaoPartitura(campo) {   
    var validos = /(exe|bat|sh)/;   
    var ext = campo.substring(campo.length-3,campo.length);
    ext = ext.toLowerCase();
    if (validos.test(ext) ) {
    	$(".msgObrigatorio").html("Arquivo de Partitura com extensão não permitida: ." + ext);
        return false;   
    }   
    return true;   
}

function excluirParticipante(cpf){
	if(confirm("Deseja realmente excluir o participante?")){
		document.location="UsuarioAction.do?operacao=excluir&cpfPart=" + cpf; 
	}
}

function getRadioCheckedValue(radio_name)
{
   var oRadio = document.forms[0].elements[radio_name];
   for(var i = 0; i < oRadio.length; i++)
   {
      if(oRadio[i].checked)
      {
         return oRadio[i].value;
      }
   }
   return '';
}


function validaFormDispPalco(){
	var status = false;
	var msg;
	
	if($("#txtNomeGrupo").val() == ""){
		msg = "Digite o nome do grupo ou intérprete.";
		$("#txtNomeGrupo").focus();
		status = true;

	}
	else if($("#txtQtdIntegrantes").val() == ""){
		msg = "Digite o número de integrantes.";
		$("#txtQtdIntegrantes").focus();
		status = true;

	}
	else if($("#txtQtdIntegrantes").val() == "0"){
		msg = "O número de integrantes deve ser maior que 0 (zero).";
		$("#txtQtdIntegrantes").focus();
		status = true;
	
	}	
	else if($("#txtDisposicao").val() == ""){
		msg = "Digite a disposição em palco.";
		$("#txtDisposicao").focus();
		status = true;
	}
	else if( ($("#motivoApoio").val() == "") && (getRadioCheckedValue("bandaApoio") == "S") ){
		msg = "Informe o motivo pelo qual irá necessitar de banda de apoio.";
		$("#motivoApoio").focus();
		status = true;
	}
	
	if(status){
		$(".msgObrigatorio").hide();
		$("#oculto").fadeOut();
		$("#oculto").fadeIn(300);
		$(".msgObrigatorio").html(msg);
		
	}else{
		$("#formDisposicaoPalco").attr("action","InscricaoFestival.do");
		$("#formDisposicaoPalco").submit();
	}
}

