﻿function AjaxStart(funcaoDisparar, dadosEnviar) {

    var rnd = Math.random() * 1000000;

    if (window.XMLHttpRequest) { // Mozilla, Safari, IE7...
        var request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE6 and older
        var request = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var url = 'ajaxDesafios.aspx?R=' + rnd;
    request.open("POST", url, true);
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Content-length", dadosEnviar.length);
    request.setRequestHeader("Connection", "close");
    request.send(dadosEnviar);
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            if (request.status == 200) {
                funcaoDisparar(request);
            }
        }
    }
}

function RetornaDesafio(CryptDesafio) {

    var strDados = "";

    if (CryptDesafio != "" && CryptDesafio != null) {
        strDados = "funcao=AjaxRetDesafio&id=" + CryptDesafio;
        $('#ctl00_cphl_upPCarregar').show();
        
        AjaxStart(Pronto, strDados);
    }

    function Pronto(Obj) {
        var xml = Obj.responseXML;
        if (xml != null) {
            var corpoXML = xml.getElementsByTagName('corpo')[0];

            $('#ctl00_cphl_upPCarregar').hide();
            $('#h1NomeDesafio').html(corpoXML.getElementsByTagName('nomeGincana')[0].firstChild.nodeValue);
            $('#ctl00_cphl_imgDesafioAndamento').attr('src', corpoXML.getElementsByTagName('imagem')[0].firstChild.nodeValue);
            $('#spanNome').html(corpoXML.getElementsByTagName('nome')[0].firstChild.nodeValue);
            $('#spanObjetivo').html(corpoXML.getElementsByTagName('objetivo')[0].firstChild.nodeValue);
            $('#spanTipo').html(corpoXML.getElementsByTagName('tipo')[0].firstChild.nodeValue);
            $('#spanNTarefas').html(corpoXML.getElementsByTagName('tarefas')[0].firstChild.nodeValue);
            $('#spanPeriodo').html(corpoXML.getElementsByTagName('periodo')[0].firstChild.nodeValue);
            $('#ctl00_cphl_hlkVisualizarAndamento').attr('href', 'Desafios_detalhes.aspx?id=' + corpoXML.getAttribute('id'));

        }
    }
}

function RetornaDesafioFinalizado(CryptDesafio) {

    var strDados = "";

    if (CryptDesafio != "" && CryptDesafio != null) {
        $('#ctl00_cphl_box_info_finalizado').show();
        strDados = "funcao=AjaxRetDesafio&id=" + CryptDesafio;
        $('#ctl00_cphl_upPCarregar').show();

        AjaxStart(Pronto, strDados);
    }

    function Pronto(Obj) {
        var xml = Obj.responseXML;
        if (xml != null) {
            var corpoXML = xml.getElementsByTagName('corpo')[0];

            $('#ctl00_cphl_upPCarregar').hide();
            $('#h1NomeDesafioFin').html(corpoXML.getElementsByTagName('nomeGincana')[0].firstChild.nodeValue);
            $('#ctl00_cphl_imgDesafioFinalizado').attr('src', corpoXML.getElementsByTagName('imagem')[0].firstChild.nodeValue);
            $('#spanOrgFin').html(corpoXML.getElementsByTagName('nome')[0].firstChild.nodeValue);
            $('#spanObjFin').html(corpoXML.getElementsByTagName('objetivo')[0].firstChild.nodeValue);
            $('#spanTipFin').html(corpoXML.getElementsByTagName('tipo')[0].firstChild.nodeValue);
            $('#spanQtdFin').html(corpoXML.getElementsByTagName('tarefas')[0].firstChild.nodeValue);
            $('#spanPerFin').html(corpoXML.getElementsByTagName('periodo')[0].firstChild.nodeValue);
            $('#ctl00_cphl_hlkVisualizarFinalizado').attr('href', 'Desafios_detalhes.aspx?id=' + corpoXML.getAttribute('id'));

        }
    }
}
