// Função para criar o cookie.
function GerarCookie(strCookie, strValor, lngDias)
{
    var dtmData = new Date();

    if(lngDias)
    {
        dtmData.setTime(dtmData.getTime() + (lngDias * 24 * 60 * 60 * 1000));
        var strExpires = "; expires=" + dtmData.toGMTString();
    }
    else
    {
        var strExpires = "";
    }
    document.cookie = strCookie + "=" + strValor + strExpires + "; path=/";
}

// Função para ler o cookie.
function LerCookie(strCookie)
{
    var strNomeIgual = strCookie + "=";
    var arrCookies = document.cookie.split(';');

    for(var i = 0; i < arrCookies.length; i++)
    {
        var strValorCookie = arrCookies[i];
        while(strValorCookie.charAt(0) == ' ')
        {
            strValorCookie = strValorCookie.substring(1, strValorCookie.length);
        }
        if(strValorCookie.indexOf(strNomeIgual) == 0)
        {
            return strValorCookie.substring(strNomeIgual.length, strValorCookie.length);
        }
    }
    return null;
}

// Função para excluir o cookie.
function ExcluirCookie(strCookie)
{
    GerarCookie(strCookie, '', -1);
}

//Função para ler a resposta da base de dados
function lerResposta(id_pergunta, metodo, modo, tagId, tagId2, tagId1, tagId3,parametros)
{
if (tagId == "resposta1") {var m_resp = document.form.cx_resposta1.value;}
if (tagId == "resposta2") {var m_resp = document.form.cx_resposta2.value;}
if (tagId == "resposta3") {var m_resp = document.form.cx_resposta3.value;}
if (tagId == "resposta4") {var m_resp = document.form.cx_resposta4.value;}
if (tagId == "resposta5") {var m_resp = document.form.cx_resposta5.value;}
if (tagId == "resposta6") {var m_resp = document.form.cx_resposta6.value;}
if (tagId == "resposta7") {var m_resp = document.form.cx_resposta7.value;}

m_resp = m_resp.replace(/á/, "%E1") 
.replace(/â/, "%E2") 
.replace(/ã/, "%E3") 
.replace(/Á/, "%C1") 
.replace(/Â/, "%C2") 
.replace(/Ã/, "%C3") 
.replace(/É/, "%C9") 
.replace(/é/, "%E9") 
.replace(/ê/, "%EA") 
.replace(/Í/, "%CD") 
.replace(/í/, "%ED") 
.replace(/Ó/, "%D3") 
.replace(/Ô/, "%D4") 
.replace(/Õ/, "%D5") 
.replace(/ó/, "%F3") 
.replace(/õ/, "%F5") 
.replace(/ô/, "%F4") 
.replace(/Ú/, "%DA") 
.replace(/Ü/, "%DC") 
.replace(/ú/, "%FA") 
.replace(/ü/, "%FC") 
.replace(/ç/, "%E7") 
.replace(/Ç/, "%C7") 

document.form.cx_resposta1.value = "";
document.form.cx_resposta2.value = "";
document.form.cx_resposta3.value = "";
document.form.cx_resposta4.value = "";
document.form.cx_resposta5.value = "";
document.form.cx_resposta6.value = "";
document.form.cx_resposta7.value = "";

var m_id = id_pergunta;

    goAjax( "ler_resposta.asp"+"?resposta="+m_resp+"&id="+m_id+"&id_c="+tagId+"&"+parametros+"&rnd"+ Math.random() , metodo, modo , tagId, tagId2, tagId1, tagId3);
}



function goAjax(url, metodo, modo, tagRetorno, tagRetorno2, tagRetorno1, tagRetorno3, parametros) {


xmlhttp = createXmlHttpRequestObject();

        document.getElementById(tagRetorno).innerHTML='<div align="center" class="carregando">Conferindo a resposta...</div>'

            if(metodo == "GET") {
                xmlhttp.open("GET", url, modo);
            } else {
                xmlhttp.open("POST", url, modo);
                xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
                xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
                xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
                xmlhttp.setRequestHeader("Pragma", "no-cache");
            }

            xmlhttp.onreadystatechange = function() {
                if(xmlhttp.readyState == 4) {
                    retorno=xmlhttp.responseText
                    if(retorno == "<font color='#ffffff'>Resposta incorreta, tente novamente!</font>") {
                    document.getElementById(tagRetorno).innerHTML=retorno
                    }

                    if(retorno != "<font color='#ffffff'>Resposta incorreta, tente novamente!</font>") {
                    var x = LerCookie('quiz_arsenal')
                    x = parseInt(x) + 1;
                    GerarCookie('quiz_arsenal',x,7)
                    document.getElementById(tagRetorno1).innerHTML="<img src='chave.png' border='0' height='70' >"
                    document.getElementById(tagRetorno2).style.visibility = 'hidden';
                    document.getElementById(tagRetorno3).style.visibility = 'hidden';
                    
                    m_visivel = "visivel" + (parseInt(x) + 1);
                    if(m_visivel != "visivel8"){document.getElementById(m_visivel).style.visibility = 'visible';}
                    
                    }

		    if(x == 7) {
				//document.getElementById('perguntas').innerHTML="Resposta!!!";
				window.open("resposta.asp","_self");
			}
                    
                    //findScript(retorno)
                }
            }
            if(metodo == "GET") {
                xmlhttp.send(null);
            } else {
                xmlhttp.send(parametros);
            }
}

function createXmlHttpRequestObject()
{
    var xmlhttp = 0;

    try
    {
        xmlhttp = new XMLHttpRequest();
    }
    catch(ee)
    {
        try
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(E)
            {
                xmlhttp = false;
            }
        }
    }
    return xmlhttp;
}

