// JavaScript Document

//Variaveis Globais
corFaltaPreencher = "#FF9F9F"
corPreenchido = "#B8F5B1"

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments;document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

//Mascara
function mascara(e,src,mask) {
	if(window.event) {
		_TXT = e.keyCode;
	} else
		if(e.which) {
		_TXT = e.which;
	}
	if(_TXT > 47 && _TXT < 58) {
		var i = src.value.length;
		var saida = mask.substring(0,1);
		var texto = mask.substring(i);
		if(texto.substring(0,1) != saida) {
			src.value += texto.substring(0,1);
		}
		return true;
	} else {
		if (_TXT != 8) {
		return false;
	} else {
		return true;
		}
	}
}

//Função que oculta Objeto Html
//Paramentro: Id do objeto Html
//Autor: Júnior Oliveira - 14/07/2009
function ocultaObjeto(objeto){
	document.getElementById(objeto).style.display = 'none';
}

//Função que mostra Objeto Html
//Paramentro: Id do objeto Html
//Autor: Júnior Oliveira - 14/07/2009
function mostraObjeto(objeto){
	document.getElementById(objeto).style.display = 'block';
}

//Função que preenche o background do campo com a cor Vermelha
//para chamar a atenção do usuário.
//Parametro: id do campo
//Autor Júnior Oliveira - 15/07/2009
function campoVermelho(campo){
	document.getElementById(campo).style.background = corFaltaPreencher;
}


//Função que preenche o background do campo com a cor Verde.
//Parametro: id do campo
//Autor Júnior Oliveira - 15/07/2009
function campoVerde(campo){
	document.getElementById(campo).style.background = corPreenchido;
}


//Função que verifica se um campo está vazio e se é obrigatorio.
//Paramentros: id do campo e div que exibi a obrigatoriedade.
// Autor: Júnior Oliveira - 14/07/2009
function verificaCampoVazio(campo,div){
	if(document.getElementById(campo).value == ""){
		document.getElementById(campo).style.background = corFaltaPreencher;
		mostraObjeto(div);
		return false;
	}else{
		document.getElementById(campo).style.background = corPreenchido;
		ocultaObjeto(div);
		return true;
	}
	return true;
}

//Função que verifica se um campo(numerico - R$) foi preenchido
//Parametro: id do campo e div que exibi obrigatoriedade
//Autor: Júnior Oliveira - 15/07/2009
function verificaCampoNumerico(campo,div){
	var valor = document.getElementById(campo).value;
	if(valor == "0,00"){
		mostraObjeto(div);
		campoVermelho(campo);
		return false;
	}else{
		ocultaObjeto(div);
		campoVerde(campo);
		return true;
	}
	return true;
}


//Função que verifica campo de acordo com um tipo passado
//Parametro: id do campo, div que exibi a obrigatoriedade e tipo do campo
//Autor: Júnior Oliveira - 15/07/2009
function verificaCampo(campo,div,tipo){
	var valor = document.getElementById(campo).value;
	if(valor == tipo){
		mostraObjeto(div);
		campoVermelho(campo);
		return false;
	}else{
		ocultaObjeto(div);
		campoVerde(campo);
		return true;
	}
	return true;
}

//Função para abrir PopUp
//Parametro: Url da pagina que será aberto o popup, 
//largura ,altura, barra de rolagem, manipulação do tamanho do popup.
function abrirPopUp(url, altura, largura, rolagem, manipulacao){
	window.open(url,'popUp','width='+altura+',height='+largura+',scrollbars='+rolagem+',resizable='+manipulacao);
}


function imprimir(){
	if (!window.print){
		alert("Use o Netscape  ou Internet Explorer \n nas versões 4.0 ou superior!")
		return
	}
	window.print()
}


//Função para validação de formulario de cadastro de estágio,
//esta função deve ser refatora, pois contem codigo duplicado, e foi densenvolvida,
//para resolução de problema emergêncial.
//Autor: Júnior Oliveira - 03/09/2009 as 21:54
//sem musica...
function validaFormularioCadastroEstagio(){ 
	if(verificaCampo('razao_social','campoEmpresa','')
			&& verificaCampo('nomeAluno','campoAluno','')
                        && verificarAuxilioTransporte()
                        && verificarAuxilioAlimentacao()
			&& verificaCampoVazio('data_inicial','data')
                        && verificaCampoVazio('data_final','data')
                        && verificaCampo('atividades','campoAtividades','')
                        && verificarDataResicao()
			//&& verificaCampo('valor_auxilio_alimentacao','campoAlimentacao','')
			//&& verificaCampo('auxilio_alimentacao','campoAlimentacao','dia')
			//&& verificaCampo('auxilio_transporte','campoTransporte','-1')
			//&& verificaCampo('valor_auxilio_transporte','campoTransporte','0,00')
                        ){
			return true;
	}else{
		return false;
	}
}


function verificarAuxilioTransporte(){
    var auxilioTranporte = document.getElementById('auxilio_transporte').value;
    if(auxilioTranporte == 'dia' || auxilioTranporte == 'mes' || auxilioTranporte == 'valor'){
    	if(!verificaCampo('valor_auxilio_transporte','campoTransporte','0,00') ){
    		document.dados_estagio.valor_auxilio_transporte.readOnly = false;
    		return false;
    	}else{
    		return true;
    	}
    }else{
        ocultaObjeto('campoTransporte');
        campoVerde('valor_auxilio_transporte');
        //document.dados_estagio.valor_auxilio_transporte.readOnly = true;
        document.dados_estagio.valor_auxilio_transporte.value = "0,00";
        return true;
    }
    return true;
}


function verificarAuxilioAlimentacao(){
    var auxilioAlimentacao = document.getElementById('auxilio_alimentacao').value;
    
    if(auxilioAlimentacao == 'vale_refeicao' || auxilioAlimentacao == 'valor'){
    	if( !verificaCampo('valor_auxilio_alimentacao','campoAlimentacao','0,00') ){
    		document.dados_estagio.valor_auxilio_alimentacao.readOnly = false;
    		return false;
    	}else{
    		return true;
    	}
    }else{
        ocultaObjeto('campoAlimentacao');
        campoVerde('valor_auxilio_alimentacao');
        //document.dados_estagio.valor_auxilio_alimentacao.readOnly = true;
        document.dados_estagio.valor_auxilio_alimentacao.value = "0,00";
        return true;
    }
    return true;
}

 function verificarResicao(idObjeto){
    var objeto = document.getElementById(idObjeto);
    //alert(objeto.checked);
    if(objeto.checked){
      document.getElementById('div_data_resicao').style.display = 'block';
      document.dados_estagio.status.checked = true;
      document.dados_estagio.status.readOnly = true;
      return true;
    }else{
        document.getElementById('div_data_resicao').style.display = 'none';
        document.dados_estagio.data_resicao.disabled = true;
        document.dados_estagio.status.checked = false;
        return false;
    }
}

function verificarDataResicao(){
    var campoResicao = document.getElementById('resindido');
    if(campoResicao.checked){
       return verificaCampoVazio('data_resicao','campoDataResicao');
    }else{
        return true;
    }
    
}


//Função para mostrar ou esconde objeto HTML
//Autor: Júnior Oliveira - 08/10/2009 12:03
//Rage Against The Machine - Township Rebelion
	function mostrarEsconder(objeto){
		var vizibilidade = document.getElementById(objeto).style.display;
		if(vizibilidade == "none"){
			mostraObjeto(objeto);
		}else{
			ocultaObjeto(objeto);
		}
	}

//Função para manipular abas(duas)
//esta função muda a cor de fundo e o estilo da letra da aba
//Parametro: id das abas e id dos objetos que referenteas abas
//Autor: Júnior Oliveira - 15/10/2009
function aba2(aba1,aba2,div1,div2){
	ocultaObjeto(div2);
	document.getElementById(aba1).style.background = "#FEE9A0";
	document.getElementById(aba1).style.fontWeight = "bold";
	document.getElementById(aba2).style.background = "#FFFFFF";
	document.getElementById(aba2).style.fontWeight = "normal";
	mostraObjeto(div1);
}



/**
* Função que executa a ação submit de um formulario
* parametro: formulario - id do formulario
* Autor Júnior Oliveria - 12/11/2009 09:51
*/
//Audio Slave - Out Of Exile
function enviar(form){
	var formulario = document.getElementById(form);
	formulario.submit();
}


/*
 * AINDA EM CONSTRUCAO
 * Funcao generica para validacao de formularios
 * Para a execucao desta funcao é necessario acrescentar um atributo dentro  com o nome de "obrigatorio"
 * e com valor "true", ex: obrigarotio = true 
 */
function validarFormulario(form){
	var formulario = document.getElementById(form);
	var retorno = true;
	for(var i = 0; i < formulario.elements.length; i++){
		var elemento = formulario.elements[i];
		if(elemento.getAttribute("obrigatorio") == 'true'){
			
			if(elemento.type == "radio" || elemento.type == "checkbox"){
				//alert("RADIO ou Checkbox");
				if(elemento.checked == false){
					alert(elemento.name + " Não marcado " + elemento.checked);
					retorno = false;
				}
			}
			
			if(elemento.type == "text" || elemento.type == "textarea"){
				//alert("TEXT OU TEXTAREA");
				if(elemento.value == ""){
					elemento.style.background = corFaltaPreencher;
					retorno = false;
				}
			}
			
			
		}
	}
	return retorno;
}








