/* Variáveis */
var fiUrl   = ""

var cadastros = new Array()
,   links        = new Array();

cadastros['insc'] = 'inscricao';
cadastros['novo'] = 'novo_cadastro';
cadastros['alte'] = 'alterar_cadastro';

links['home'] = 'home';
links['trac'] = 'trackdays';
links['pefr'] = 'perguntas';
links['fovi'] = 'fotos';
links['insc'] = 'inscricoes';
links['cale'] = 'calendario';
links['noti'] = 'noticias';
links['cont'] = 'contato';


/* Funções */

var callFlashFunction = function(pag) {
    var IE = navigator.appName.indexOf("Microsoft") != -1; 
    var meumovie = IE ? document.getElementById("movie") : window.document.movie; 
    meumovie.SetVariable("_root.pagina", pag);

};

function hideCadastro(nom) {
    if (nom != 'insc') $(cadastros['insc']).hide();
    if (nom != 'novo') $(cadastros['novo']).hide();
    if (nom != 'alte') $(cadastros['alte']).hide();
}

function showHideBox(box,id)
{
    var elm = document.getElementById(id);
    elm.style.display = box.checked? "inline":"none";
} 

function hideMsgs() {
    $('e_msg').fade();
    $('s_msg').fade();
}


function isIE() { return  (  (navigator.appName.indexOf('Microsoft') >= 0)
                          || (navigator.appName.indexOf('Internet') >= 0)
                          || (navigator.appName.indexOf('Explorer') >= 0) ); }

// alterado
function load(page, noti, gal) {
    $('content').update('<div class="loading" style="text-align: center;"><img alt="Aguarde, carregando..." src="images/loading.gif" /></div>');
    
    page = page.substr(page.indexOf('?link=')+6);
    if (page == null || page == '') page = 'home';

    // notícia / galerias
    if (noti != null && noti != '') anc = '?anc=id'+noti;
    else                            anc = '';
    
    new Ajax.Request('pages/'+links[page]+'.php'+anc, {
        onFailure: function() { load('?link=home'); },
        onSuccess: function(cnt) { 
           $('content').update(cnt.responseText); 
           if (noti != null && noti != '' && gal != 'S') loadNoticia(noti);
           if (noti != null && noti != '' && gal == 'S') loadGaleria(noti);
        }
    });
}

// criado
function closeNoticia(id) {
    $('noticia'+id+'_fechar').hide();
    if ($('noticia'+id+'_imagem')) new Effect.SlideUp('noticia'+id+'_imagem', { queue: { position: 'end', scope: 'noticia', limit: 2 } });
    new Effect.SlideUp('noticia'+id+'_texto', { queue: { position: 'end', scope: 'noticia', limit: 2 } });
    new Effect.Appear('noticia'+id+'_resumo');
}

// criado
function loadNoticia(id) {
    $('noticia'+id+'_resumo').hide();
    new Effect.SlideDown('noticia'+id+'_texto', { queue: { position: 'end', scope: 'noticia', limit: 2 } });
    if ($('noticia'+id+'_imagem')) new Effect.SlideDown('noticia'+id+'_imagem', { queue: { position: 'end', scope: 'noticia', limit: 2 } });
    new Effect.Appear('noticia'+id+'_fechar');
}

// criado
function loadGaleria(id) {
    $('galeria'+id+'_conteudo').update('<img src="images/loading.gif" />');
    
    new Ajax.Updater('galeria'+id+'_conteudo', 'pages/fotos.php?id='+id);
}

// criado
function closeGaleria(id) {
    $('galeria'+id+'_conteudo').update('');
}

//criado
function paginateNoticia(offset) {
    new Ajax.Updater('noti_cnt', 'pages/noticias.php?pgnt=true&offset='+offset);
}

// Criado
function showVideos(id) {
    $('galeria'+id+'_fotos').hide();
    $('galeria'+id+'_videos').show();
}

// Criado
function showFotos(id) {
    $('galeria'+id+'_fotos').show();
    $('galeria'+id+'_videos').hide();
}

function loadCadastro(cadastro) {
    hideCadastro(cadastro);
    $(cadastros[cadastro]).appear();
}

function growFA(url) {
    $('formalt').appear();
    loadCadastro('alte');
    loading('formalt');
    new Ajax.Updater('formalt', url);
}

function alterarCad(url) {
    
    $('e_msg').hide();
    $('e0_msg').hide();
    $('e00_msg').hide();
    $('s_msg').hide();
    $('mensagem2').hide();
    
    new Ajax.Request(url+'?'+$('formi').serialize(), {
	onFailure: function(t) {
	    $('e_msg').appear();
	},
	onSuccess: function(t) { 
	    if (t.responseText == "true") {
		growFA('pages/form_alterar.php?'+$('formi').serialize());
	    } else {
		$('e00_msg').appear();
	    }
	}
    });
}

function loading(id) { $(id).update(new Element('div', { 'class': 'align_center' }).insert(new Element('img', { src: 'image/loading.gif' }))); }

function checkEmail($vemail) {
    if (($vemail.length != 0) && (($vemail.indexOf("@") < 1) || ($vemail.indexOf('.') < 7)))
    {
      return false;
    }
    else {
      return true;
    }
}

function sendInscricao(url) {
    
    $('termo_resp').hide();
    $('e_msg').hide();
    $('e0_msg').hide();
    $('e00_msg').hide();
    $('s_msg').hide();
    
    var usuario = $('usuario');
    var senha = $('senha');
    
    var marcado = '0' 
    ,   inputs  = $('formi').getElementsByTagName('input');
        
    for (var i = 0; i < inputs.length; i++) {
       if (inputs[i].type == 'checkbox' && inputs[i].checked) marcado = '1';
    }
    
    if ((usuario.value == '') || (senha.value == '') || (marcado == '0')) 
    {
     	  $('e_msg').appear();
  	  $('s_msg').fade();
    } 
    else
    {
	 new Ajax.Request(url+'?'+$('formi').serialize(), {
		onFailure: function(t) {
			$('e_msg').appear();
			$('s_msg').fade();
		},
		onSuccess: function(t) { 
		    if (t.responseText == "true") {
			$('s_msg').appear();
			$('termo_resp').update('<p class="p1">INSCRIÇÃO REALIZADA!!   </p><a href="pages/termos/'+$('usuario').value+'.pdf" target="_blank">   >>> Clique aqui para imprimir seu Termo de Responsabilidade <<<</a>'); 
		    	$('termo_resp').appear();
			$('e_msg').fade();
			usuario.value = "";
			senha.value = "";
			for (var i = 0; i < inputs.length; i++) {
		           if (inputs[i].type == 'checkbox') inputs[i].checked = false;
    			}
		    } else if (t.responseText == "email") {
		    	
		    	$('termo_resp').update('<p class="p1">INSCRIÇÃO REALIZADA!!   </p><a href="pages/termos/'+$('usuario').value+'.pdf" target="_blank">   >>> Clique aqui para imprimir seu Termo de Responsabilidade <<<</a>'); 
		    	
		    	$('e0_msg').appear();
		    	$('termo_resp').appear();
			$('e_msg').fade();
			$('s_msg').fade();
		    } else {
		        $('e_msg').appear();
			$('s_msg').fade();
		    }
		}
	 });
    }
    return;
}

function marcacampos(formulario) {
   if (formulario == 'I')
   {
	   $('usuario2').style.backgroundColor="#142727";
	   $('senha2').style.backgroundColor="#142727";
	   $('nome').style.backgroundColor="#142727";
	   $('cpf').style.backgroundColor="#142727";
	   $('rg').style.backgroundColor="#142727";
	   $('estadocivil').style.backgroundColor="#142727";
	   $('endereco').style.backgroundColor="#142727";
	   $('cidade').style.backgroundColor="#142727";
	   $('uf').style.backgroundColor="#142727";
	   $('cnh').style.backgroundColor="#142727";
	   $('acidcontato').style.backgroundColor="#142727";
	   $('acidendereco').style.backgroundColor="#142727";
	   $('acidcidade').style.backgroundColor="#142727";
	   $('aciduf').style.backgroundColor="#142727";
	   $('sangue').style.backgroundColor="#142727";
	   $('fonecel').style.backgroundColor="#142727";
	   $('acidfones').style.backgroundColor="#142727";
   } 
   else if (formulario == 'A')
   {
	   $('nome2').style.backgroundColor="#142727";
	   $('cpf2').style.backgroundColor="#142727";
	   $('rg2').style.backgroundColor="#142727";
	   $('estadocivil2').style.backgroundColor="#142727";
	   $('endereco2').style.backgroundColor="#142727";
	   $('cidade2').style.backgroundColor="#142727";
	   $('uf2').style.backgroundColor="#142727";
	   $('cnh2').style.backgroundColor="#142727";
	   $('acidcontato2').style.backgroundColor="#142727";
	   $('acidendereco2').style.backgroundColor="#142727";
	   $('acidcidade2').style.backgroundColor="#142727";
	   $('aciduf2').style.backgroundColor="#142727";
	   $('sangue2').style.backgroundColor="#142727";
	   $('fonecel2').style.backgroundColor="#142727";
	   $('acidfones2').style.backgroundColor="#142727";
   }
   return;
}

function sendCadastro(url,oper) {
    
    $('e1_msg').hide();
    $('e2_msg').hide();
    $('e3_msg').hide();
    $('s1_msg').hide();
    
    if( oper == 'I')
    {
	var marcado = '0' 
     	,   inputs  = $('formn').getElementsByTagName('input');

     	for (var i = 0; i < inputs.length; i++) {
	    if (inputs[i].type == 'text' && inputs[i].title.indexOf('*') > 0 && inputs[i].value == '') marcado = '1';
        }

        if (marcado == '1')
        {
	    marcacampos('I');
	    $('e1_msg').appear();
        }
        else
    	{
	    new Ajax.Request(url+'?'+$('formn').serialize(), {
		onFailure: function(t) {
			$('e3_msg').appear();
			$('e1_msg').fade();
			$('e2_msg').fade();
			$('s1_msg').fade();
		},
		onSuccess: function(t) { 
		    if (t.responseText == "usuario") {
			$('e1_msg').fade();
			$('e3_msg').fade();
			$('s1_msg').fade();
			$('e2_msg').appear();
		    } else if (t.responseText == "falta") {
			marcacampos('I');
			$('e2_msg').fade();
			$('e3_msg').fade();
			$('s1_msg').fade();
			$('e1_msg').appear();
		    } else if (t.responseText == "true") {
			$('e1_msg').fade();
			$('e2_msg').fade();
			$('e3_msg').fade();
			$('formins').fade();
			$('mensagem').appear();
			$('s1_msg').appear();
		    } else {
			$('e1_msg').fade();
			$('e2_msg').fade();
			$('s1_msg').fade();
			$('e3_msg').appear();
		    }
		}
	    });
	}
    } else if( oper == 'A')
    {
	var marcado = '0' 
	,   inputs  = $('forma').getElementsByTagName('input');
	 
	for (var i = 0; i < inputs.length; i++) {
	    if (inputs[i].type == 'text' && inputs[i].title.indexOf('*') > 0 && inputs[i].value == '') marcado = '1';
	}

	if (marcado == '1')
	{
	    marcacampos('A');
	    $('e4_msg').appear();
	}
	else
    	{
	    new Ajax.Request(url+'?'+$('forma').serialize(), {
		onFailure: function(t) {
			$('e5_msg').appear();
			$('e4_msg').fade();
			$('s2_msg').fade();
		},
		onSuccess: function(t) { 
		    if (t.responseText == "falta") {
			marcacampos('A');
			$('e5_msg').fade();
			$('s2_msg').fade();
			$('e4_msg').appear();
		    } else if (t.responseText == "true") {
			$('e4_msg').fade();
			$('e5_msg').fade();
			$('formalt').fade();
			$('mensagem2').appear();
			$('s2_msg').appear();
		    } else {
			$('e4_msg').fade();
			$('s5_msg').fade();
			$('e3_msg').appear();
		    }
		}
	    });
	}
    }
    return;
}

function sendContact(url) {
    
    $('e_msg').hide();
    $('s_msg').hide();
    $('e_name_msg').hide();
    $('e_email_msg').hide();
    $('e_message_msg').hide();
	  
    var name = $('name');
    var email = $('email');
    var phone = $('phone');
    var message = $('message');
    
    if (name.value == '')
    {
     	  $('e_msg').fade();
  	  $('s_msg').fade();
	  $('e_name_msg').appear();
	  $('e_email_msg').fade();
	  $('e_message_msg').fade();
	  name.focus();
    } 
    else if(email.value == '')
    {
	  $('e_msg').fade();
	  $('s_msg').fade();
	  $('e_name_msg').fade();
	  $('e_email_msg').appear();
  	  $('e_message_msg').fade();    
	  email.focus();
    }
    else if(!checkEmail(email.value))
    {
	  $('e_msg').fade();
	  $('s_msg').fade();
	  $('e_name_msg').fade();
	  $('e_email_msg').appear();
  	  $('e_message_msg').fade();    
  	  email.focus();
    }
    else if(message.value == '')
    {
	  $('e_msg').fade();
	  $('s_msg').fade();
	  $('e_name_msg').fade();
	  $('e_email_msg').fade();
 	  $('e_message_msg').appear();
	  message.focus();
    }
    else
    {
	 new Ajax.Request(url+'?'+$('formc').serialize(), {
		onFailure: function(t) {
			$('e_msg').appear();
			$('s_msg').fade();
			$('e_name_msg').fade();
			$('e_email_msg').fade();
			$('e_message_msg').fade();
		},
		onSuccess: function(t) { 
		    if (t.responseText == "true") {
			$('s_msg').appear();
			$('e_msg').fade();
			$('e_name_msg').fade();
			$('e_email_msg').fade();
			$('e_message_msg').fade();
			name.value = "";
			email.value = "";
			phone.value = "";
			message.value = "";
		    } else {
			$('e_msg').appear();
			$('s_msg').fade();
			$('e_name_msg').fade();
			$('e_email_msg').fade();
			$('e_message_msg').fade();
		    }
		}
	 });
    }
    return;
}
