﻿/* Funções para gerenciamento das divs */
function GerenciarFiltroModalidade(modal) {
    if (modal == "internet") {
		GerenciarFiltroModalidade("reset");
        $('hidModalidade').value = "I";
        filtro_area.style.display = "block";
		
        BuscarAreas();
		BuscarTitulos();
	}
    else if (modal == "presencial") {
        GerenciarFiltroModalidade("reset");
        $('hidModalidade').value = "P";

        BuscarCidades($('hidModalidade').value);
		BuscarTitulos();
	}
    else if (modal == "telepresencial") {
        GerenciarFiltroModalidade("reset");
        $('hidModalidade').value = "T";

        BuscarCidades($('hidModalidade').value);
		BuscarTitulos();
	}
    else if (modal == "reset") {
        modal_internet_titulo.className = "boxbg";
        modal_presencial_titulo.className = "boxbg";
        modal_telepresencial_titulo.className = "boxbg";
        modal_internet_texto.className = "box_texto";
        modal_presencial_texto.className = "box_texto";
        modal_telepresencial_texto.className = "box_texto";
        //modal_internet_link.setAttribute("href", "#");
        //modal_presencial_link.setAttribute("href", "#");
        //modal_telepresencial_link.setAttribute("href", "#");
        modal_internet_link.className = "link_modal";
        modal_presencial_link.className = "link_modal";
        modal_telepresencial_link.className = "link_modal";
        $('hidModalidade').value = "";
        filtro_cidade.style.display = "none";
        filtro_area.style.display = "none";
        filtro_tema.style.display = "none";
        lista_cursos.style.display = "none";
		$('cmbCidades').selectedIndex = 0;
		$('cmbAreas').selectedIndex = 0;
		$('cmbTemas').selectedIndex = 0;
    }
}

function GerenciarFiltroCidade() {
    filtro_area.style.display = "block";
    BuscarAreas();
    lista_cursos.style.display = "block";
    BuscarTitulos();
}

function GerenciarFiltroArea() {
    if (filtro_cidade.style.display == "block") {
        filtro_tema.style.display = "block";
        BuscarTemas();
        lista_cursos.style.display = "block";
        BuscarTitulos();
    }
    else {
        filtro_tema.style.display = "block";
        BuscarTemas();
        lista_cursos.style.display = "block";
        BuscarTitulos();
    }
}

function GerenciarFiltroTema() {
    lista_cursos.style.display = "block";
    BuscarTitulos();
}
/**************************************************/


/* Funções de busca no Banco de Dados */
function BuscarCidades(local) {
	filtro_cidade.style.display = "block";
	lista_cursos.style.display = "block";
	$('lista_cursos').innerHTML = '<b>Aguarde, listando os Cursos...</b>';

    pars = 'ordem=1&local=' + local;
    myAjax = new Ajax.Request(
        url,
        { method: 'get',
        parameters: pars,
        onComplete: TratarCidades
    });
}

function BuscarAreas() {
	lista_cursos.style.display = "block";
	$('lista_cursos').innerHTML = '<b>Aguarde, listando os Cursos...</b>';

    var numeroDaOrdem;
    if ($('cmbCidades').value == '')
        numeroDaOrdem = '2';
    else
        numeroDaOrdem = '3';
    pars = 'ordem=2&numeroDaOrdem=' + numeroDaOrdem;
    //url = 'http://www.aasp.org.br/aasp/cursos/crs_ajax.asp';
    myAjax = new Ajax.Request(
        url,
        { method: 'get',
        parameters: pars,
        onComplete: TratarAreas
    });
    //if ($('cmbCidades')[0].value == "S")
      //$('cmbCidades').remove(0);
}

function BuscarTemas() {
	lista_cursos.style.display = "block";
	$('lista_cursos').innerHTML = '<b>Aguarde, listando os Cursos...</b>';

    var numeroDaOrdem;
    if ($('cmbCidades').value == '')
        numeroDaOrdem = '3';
    else
        numeroDaOrdem = '4';    

    pars = 'ordem=3&numeroDaOrdem=' + numeroDaOrdem + '&valorDaArea=' + $('cmbAreas').value;
    //url = 'http://www.aasp.org.br/aasp/cursos/crs_ajax.asp';
    myAjax = new Ajax.Request(
    url,
    { method: 'get',
        parameters: pars,
        onComplete: TratarTemas
    });
}

function BuscarTitulos() {
    var modalidade, cidade, area, tema;

    modalidade = $('hidModalidade').value;
    cidade = $('cmbCidades').value;
    if ($('cmbAreas').value == '')
        area = 'NULL';
    else
        area = $('cmbAreas').value
    if ($('cmbTemas').value == '')
        tema = 'NULL';
    else
        tema = $('cmbTemas').value;

    if (cidade == ' ' || cidade == '')
        cidade = 'NULL';
    
    pars = 'ordem=6&valorDaModalidade=' + modalidade + '&valorDaCidade=' + cidade + '&valorDaArea=' + area + '&valorDoTema=' + tema;
    myAjax = new Ajax.Request(
    url,
    { method: 'get',
        parameters: pars,
        onComplete: TratarTitulos
    });
}

function BuscarCursos(id) {
    var modalidade, cidade, area, tema;

    modalidade = $('hidModalidade').value;
    cidade = $('cmbCidades').value;
    if ($('cmbAreas').value == '')
        area = 'NULL';
    else
        area = $('cmbAreas').value
    if ($('cmbTemas').value == '')
        tema = 'NULL';
    else
        tema = $('cmbTemas').value;

    if (cidade == ' ' || cidade == '')
        cidade = 'NULL';

    if (id > 0) {
        if ($('detCurso_' + id).innerHTML.length == 0) {
            $('detCurso_' + id).innerHTML = 'Buscando dados...';
            //pars = 'ordem=5&idCurso=' + id;
            pars = 'ordem=4&idTitulo=' + id + '&valorDaModalidade=' + modalidade + '&valorDaCidade=' + cidade + '&valorDaArea=' + area + '&valorDoTema=' + tema;
            var myAjax = new Ajax.Updater(
	            'detCurso_' + id,
	            url,
            {
                method: 'get',
                parameters: pars
            });
            $('detCurso_' + id).style.display = 'block';
        } else {
            $('detCurso_' + id).innerHTML = '';
        }
    }
}

function BuscarDadosCursos(id) {
    if (id > 0) {
        if ($('det_' + id).innerHTML.length == 0) {
            $('det_' + id).innerHTML = 'Buscando dados...';
            pars = 'ordem=5&idCurso=' + id;
            var myAjax = new Ajax.Updater(
	            'det_' + id,
	            url,
            {
                method: 'get',
                parameters: pars
            });
            $('det_' + id).style.display = 'block';
        } else {
            $('det_' + id).innerHTML = '';
        }
    }
}
/**************************************************/


/* Funções de tratamento do HTML */
function TratarCidades(req) {
    var retorno = req.responseText;
    $('filtro_cidade').innerHTML = retorno;
}

function TratarAreas(req) {
    var retorno = req.responseText;
    $('filtro_area').innerHTML = retorno;
}

function TratarTemas(req) {
    var retorno = req.responseText;
    $('filtro_tema').innerHTML = retorno;
}

function TratarTitulos(req) {
    var retorno = req.responseText;
	if (retorno.indexOf('<tr>') > -1)
		$('lista_cursos').innerHTML = retorno;
	else
		$('lista_cursos').innerHTML = '<b>Nenhum curso encontrado para os filtros selecionados.</b>';
}

function TratarCursos(req) {
    var retorno = req.responseText;
    $('lista_cursos').innerHTML = retorno;
}

/* Funções para uso do HTML */
function AbrirInscricao(idCurso, nomeCurso) {
    var intLeft, intTop, intWidth, intHeight;
    intWidth = 725;
    intHeight = 480;
    intLeft = eval((screen.width - intWidth) / 2);
    intTop = eval((screen.height - intHeight) / 2);

    var loja = window.open('crs_crt_seleciona.asp?idcurso=' + idCurso + '&nmcurso=' + nomeCurso, 'loja', 'width=' + intWidth + ',height=' + intHeight + ',top=' + intTop + ',left=' + intLeft + ',toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no')
    loja.focus();
}