function gebi(id) {
	return document.getElementById(id);
}

function contato() {
	if($('#contato').height() == 25) {
		$('#contato').animate({ height: '100px' },1000);
	} else {
		$('#contato').animate({ height: '25px' }, 1000);
	}
	
	$('#formContato').slideToggle('slow');
	gebi('nome').focus();
}

function excluir(id) {
	var pergunta = confirm('Tem certeza que deseja excluir?')
	if (pergunta){
		window.location.href='painel/excluir.php?id=' + id; 
	}
}

function excluirE(id) {
	var pergunta = confirm('Tem certeza que deseja excluir?')
	if (pergunta){
		window.location.href='painel/excluirE.php?id=' + id; 
	}
}

function excluirF(id) {
	var pergunta = confirm('Tem certeza que deseja excluir?')
	if (pergunta){
		window.location.href='painel/excluirF.php?id=' + id; 
	}
}

function excluirEquipe(id) {
	var pergunta = confirm('Tem certeza que deseja excluir?')
	if (pergunta){
		window.location.href='painel/excluirEquipe.php?id=' + id; 
	}
}

function excluirObservacao(id) {
	var pergunta = confirm('Tem certeza que deseja excluir?')
	if (pergunta){
		window.location.href='painel/excluirObservacoes.php?id=' + id; 
	}
}

function link2ajax() {
    if ($('a').attr('href') != '#')
		$('a').click(go);
}

function foco(elemento) {
	
	if(elemento.className=='label'){
		$('#'+elemento.id).addClass('lOver');
	}
	
	if(elemento.className=='valor'){
		$('#'+elemento.id).addClass('vOver');
	}
	
	if(elemento.value=='campo' || elemento.value == 'valor') {
		elemento.value = '';
	}
}

function desfoco(elemento) {
	if(elemento.className == 'valor vOver' && (elemento.value == 'valor' || elemento.value=='')) {
		elemento.className = elemento.className.replace(' vOver','');	
	}
	
	if(elemento.className == 'label lOver' && (elemento.value == 'campo' || elemento.value=='')) {
		elemento.className = elemento.className.replace(' lOver','');	
	}
	
	
	if(elemento.value == '' && elemento.className == 'label' || elemento.value == 'campo' && elemento.className == 'label') {
		elemento.value='campo';
	}
	
	if(elemento.value == '' && elemento.className == 'valor' || elemento.value == 'valor' && elemento.className == 'valor') {
		elemento.value='valor';
	}
}

function defineTipo(elemento) {
	var pergunta = confirm('Mudando o tipo de campo, o valor do mesmo será perdido. Deseja continuar?')
	if (pergunta){
		var grupo = elemento.id.replace('tipo','');
		if(elemento.selectedIndex == 0 && gebi('valor'+grupo).type == 'file') {
			gebi('aux'+grupo).innerHTML = '<input type="text" name="valor'+grupo+'" id="valor'+grupo+'" class="valor" onfocus="foco(this)" onblur="desfoco(this)">';
		} else if(elemento.selectedIndex == 1 && gebi('valor'+grupo).type == 'text') {
			gebi('aux'+grupo).innerHTML = '<input type="file" name="valor'+grupo+'" id="valor'+grupo+'" class="valor" />';
		}
		
		gebi('doc'+grupo).innerHTML = '';
	}
}

function addLi() {

	var grupo = document.getElementById('camposEmissoes').getElementsByTagName('li').length;
	var newLi = document.createElement("li");
	
	var newCampo = document.createElement("input");
	newCampo.type = 'text';
	newCampo.id= 'campo'+grupo;
	newCampo.name= 'campo'+grupo;
	newCampo.className= 'label';
	newCampo.value= 'campo';
	//newCampo.setAttribute('onFocus','foco(this)');
    //newCampo.setAttribute('onBlur','desfoco(this)');
    newCampo.onfocus = function() { foco(this); }
	newCampo.onblur = function() { desfoco(this); }
	
	var newSpan = document.createElement("span");
	newSpan.id='aux'+grupo;
	
	var newValor = document.createElement("input");
	newValor.type = 'text';
	newValor.id= 'valor'+grupo;
	newValor.name= 'valor'+grupo;
	newValor.className= 'valor';
	newValor.value= 'valor';
	//newValor.setAttribute('onFocus','foco(this)');
    //newValor.setAttribute('onBlur','desfoco(this)');
    newValor.onfocus = function() { foco(this); }
	newValor.onblur = function() { desfoco(this); }
	
	newSpan.appendChild(newValor);
	
	var newSelect = document.createElement('select');
	newSelect.id = 'tipo'+grupo;
	newSelect.name = 'tipo'+grupo;
	newSelect.onchange = function() { defineTipo(this); }
	//newSelect.setAttribute('onChange','defineTipo(this)');
	
	var Option1 = document.createElement('option'),
	    Option2 = document.createElement('option');
	
	Option1.selected = 'selected';
	Option1.innerHTML = 'Texto';
	Option2.innerHTML = 'Arquivo';
	
	newSelect.appendChild(Option1);
	newSelect.appendChild(Option2);
	
	newLi.appendChild(newCampo);
	newLi.appendChild(newSpan);
	newLi.appendChild(newSelect);
	
	document.getElementById('camposEmissoes').insertBefore(newLi,gebi('adicionar'));
    gebi('campo'+grupo).focus();
}

function aguarde() {
	if(validaExtensao()) {	
		$(".ficha").fadeOut('slow', function() {$(".wait").fadeIn('slow');});
		gebi('formulario').submit();
	} else { 
		alert('Extensão de arquivo inválida. Favor enviar somente arquivos com as seguintes extensões:\n\n- JPG\n- ZIP\n- PDF\n- GIF\n- PNG');
	}
}

function voltar() {
	$('.wait').fadeOut('slow', function() { $('.ficha').fadeIn('slow');});
}

function validaExtensao() {
    var elementosFormulario = gebi('formulario').elements;
    var qtdElementos = elementosFormulario.length;
    var status = true;
    var elemento;

    //Loop para percorrer todos os elementos
    for (var i = 0; i < qtdElementos; i++) {
        //Pega o elemento
        elemento = elementosFormulario[i];
        if (!elemento.disabled) {
            //Trabalha com o elemento caso ele nao esteja desabilitado
            if(elemento.type == 'file') {
                if(elemento.value.match("jpeg")
				||elemento.value.match("jpg")
				||elemento.value.match("zip")
				||elemento.value.match("pdf")
				||elemento.value.match("gif")
				||elemento.value.match("png")
				||elemento.value.match("jpeg")
				||elemento.value == '') {
				    //ok
				} else {
					status = false;
				}
            }
        }
    }
    return status;
}
