﻿function el(id) {
    return document.getElementById(id);
}

function getel(doc, id) {
    return doc.getElementById(id);
}

function getElementsById(sId) {
    var outArray = new Array();

    if (typeof (sId) != 'string' || !sId) {
        return outArray;
    };

    if (document.evaluate) {
        var xpathString = "//*[@id='" + sId.toString() + "']"
        var xpathResult = document.evaluate(xpathString, document, null, 0, null);
        while ((outArray[outArray.length] = xpathResult.iterateNext())) { }
        outArray.pop();
    }
    else if (document.all) {

        if (document.all[sId])
            for (var i = 0, j = document.all[sId].length; i < j; i += 1) {
            outArray[i] = document.all[sId][i];
        }

    } else if (document.getElementsByTagName) {

        var aEl = document.getElementsByTagName('*');
        for (var i = 0, j = aEl.length; i < j; i += 1) {

            if (aEl[i].id == sId) {
                outArray.push(aEl[i]);
            };
        };

    };

    if ((outArray.length == 0) && (el(sId)))
        outArray[0] = el(sId);

    return outArray;
}

function element_top(el) {
    var et = 0
    while (el) {
        et += el.offsetTop
        el = el.offsetParent
    }
    return et
}

function matriz(n) {
    this.length = n
    for (var i = 0; i < n; i = i + 1) {
        this[i] = ''
    }
}

function mostraDialogo(html, width, height) {
    ov = el('overlay');
    amp = el('ampliacao');
    ampi = el('ampliInner');
    ct = el('ampliContent');

    ct.innerHTML = '';

    ct.style.height = (height) + 'px';
    ct.style.width = (width) + 'px';
    ampi.style.width = (width) + 'px';

    oh = document.body.scrollHeight > document.body.clientHeight ? document.body.scrollHeight : document.body.clientHeight;
    ov.style.height = (oh) + 'px';
    ov.style.top = (document.body.scrollTop) + 'px';
    ov.style.left = (document.body.scrollLeft) + 'px';

    th = document.body.clientHeight;
    th = ((th - height - 12 - 18 - 3) / 2) - 20;
    if (th < 0)
        th = 0;
    th = th + document.body.scrollTop;
    ampi.style.marginTop = (th) + 'px';

    ov.style.display = 'block';
    amp.style.display = 'block';

    h = document.body.scrollTop;
    document.body.style.overflow = 'hidden';
    document.body.scrollTop = h;

    ct.innerHTML = html;
}

function mostraDialogoFrame(src, width, height) {
    mostraDialogo('<iframe width=' + (width) + ' height=' + (height) + ' frameborder=0 src="' + src + '" scrolling=auto></iframe>', width, height);
}

function mostraDialogoSobrePro() {
    mostraDialogoFrame(htmlBase + 'mypoint/mp.dialogos/infoPro.aspx', 620, 400);
}

function fechaDialogo() {
    ov = el('overlay');
    amp = el('ampliacao');

    ov.style.display = 'none';
    amp.style.display = 'none';

    document.body.style.overflow = '';
}

function showProgressImage(id) {
    pimg = el(id);
    pimg.style.visibility = 'visible';
    pimg.src = pimg.src;
}

var ampliaFotoContent = '';
function ampliaFoto(idFoto) {
    mostraDialogo('<img src="' + htmlBase + 'mypoint/mp.images/loading.gif" id=imgLoading align=absmiddle style="visibility: hidden" /><font color=white> Carregando...</font>', 100, 40);
    showProgressImage('imgLoading');

    url = htmlBase + 'mypoint/fotoAmpliacaoLayer.aspx?idFoto=' + (idFoto) + '&rnd=' + (Math.random());
    req = getAjax();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                ampliaFotoContent = req.responseText;
                el('mpFotoTemp').innerHTML = ampliaFotoContent;
                setTimeout('ampliaFoto2()', 250);
            }
            else {
                fechaDialogo();
                alert('Desculpe, ocorreu uma falha técnica interna durante o processamento de sua solicitação. Por favor tente novamente em alguns instantes.')
            }
        }
    }

    req.open('GET', url, true);
    req.send(null);

}

function ampliaFoto2() {
    img = el('imgFotoAmpli');

    if (img.complete) {
        fechaDialogo();
        w = img.width;
        h = img.height;

        fechaDialogo();
        mostraDialogo(ampliaFotoContent, 520, h + 100);
    }
    else
        setTimeout('ampliaFoto2()', 250);
}

function avaliarFoto(idFoto, btn) {
    av = el('avaliacao1N').checked ? 1 : 0 +
        el('avaliacao2N').checked ? 2 : 0 +
        el('avaliacao3N').checked ? 3 : 0 +
        el('avaliacao4N').checked ? 4 : 0 +
        el('avaliacao5N').checked ? 5 : 0;

    if (av == 0) {
        alert('Por favor selecione uma das 5 opções!');
        return;
    }

    btn.style.display = 'none';
    el('divEnviando').style.display = 'block';
    setTimeout('showProgressImage(\'imgLoading\')', 100);

    url = htmlBase + 'mypoint/fotoRegistraAvaliacao.aspx?idFoto=' + (idFoto) + '&avaliacao=' + (av) + '&rnd=' + (Math.random());
    req = getAjax();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            el('avaliarBT').style.display = 'block';
            el('divEnviando').style.display = 'none';
            if (req.status == 200) {
                if (req.responseText != "ok")
                    alert(req.responseText)
                else {
                    el('mpFADivAvaliacao').style.display = 'none';
                    alert('Sua avaliação foi registrada. Obrigado!');
                }
            }
            else {
                alert('Desculpe, ocorreu uma falha técnica interna durante o processamento de sua solicitação. Por favor tente novamente em alguns instantes.');
            }
        }
    }

    req.open('GET', url, true);
    req.send(null);


}


function avaliarPost(idPost, btn) {
    av = el('avaliacao1Np').checked ? 1 : 0 +
        el('avaliacao2Np').checked ? 2 : 0 +
        el('avaliacao3Np').checked ? 3 : 0 +
        el('avaliacao4Np').checked ? 4 : 0 +
        el('avaliacao5Np').checked ? 5 : 0;

    if (av == 0) {
        alert('Por favor selecione uma das 5 opções!');
        return;
    }

    btn.style.display = 'none';
    el('divEnviando').style.display = 'block';
    setTimeout('showProgressImage(\'imgLoading\')', 100);

    url = htmlBase + 'mypoint/postRegistraAvaliacao.aspx?idPost=' + (idPost) + '&avaliacao=' + (av) + '&rnd=' + (Math.random());
    req = getAjax();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            el('avaliarBT').style.display = 'block';
            el('divEnviando').style.display = 'none';
            if (req.status == 200) {
                if (req.responseText != "ok")
                    alert(req.responseText)
                else {
                    el('mpPsDivAvaliacao').style.display = 'none';
                    alert('Sua avaliação foi registrada. Obrigado!');
                }
            }
            else {
                alert('Desculpe, ocorreu uma falha técnica interna durante o processamento de sua solicitação. Por favor tente novamente em alguns instantes.');
            }
        }
    }

    req.open('GET', url, true);
    req.send(null);
}

function gravaComentPost(idPost) {
    txt = el('txtComentario').value;

    if (txt.length < 2) {
        alert('Por favor preencha seu comentário!');
        el('txtComentario').focus();
        return;
    }

    btn = el('enviarComentBT');

    btn.style.display = 'none';
    el('divEnviandoComent').style.display = 'block';
    setTimeout('showProgressImage(\'imgLoadingComent\')', 100);

    url = htmlBase + 'mypoint/postRegistraComentario.aspx';
    poststr = "txt=" + encodeURI(txt) + "&idPost=" + idPost;

    if (el('chkReceberFuturos') && (el('chkReceberFuturos').checked))
        poststr += "&receberFuturos=1";

    req = getAjax();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            el('enviarComentBT').style.display = 'block';
            el('divEnviandoComent').style.display = 'none';
            if (req.status == 200) {
                if (req.responseText.substr(0, 2) != "ok")
                    alert(req.responseText)
                else {
                    alert('Seu comentário foi registrado. Obrigado!');
                    idc = req.responseText.substr(3, req.responseText.length - 3);
                    location.href = 'post.aspx?idPost=' + (idPost) + '&ultimaPagina=1&idc=' + (idc) + '#comentario' + (idc);
                }
            }
            else {
                alert('Desculpe, ocorreu uma falha técnica interna durante o processamento de sua solicitação. Por favor tente novamente em alguns instantes.');
            }
        }
    }

    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    // http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // http_request.setRequestHeader("Content-length", poststr.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(poststr);
}

function gravaComentFoto(idFoto) {
    txt = el('txtComentario').value;

    if (txt.length < 2) {
        alert('Por favor preencha seu comentário!');
        el('txtComentario').focus();
        return;
    }

    btn = el('enviarComentBT');

    btn.style.display = 'none';
    el('divEnviandoComent').style.display = 'block';
    setTimeout('showProgressImage(\'imgLoadingComent\')', 100);

    url = htmlBase + 'mypoint/fotoRegistraComentario.aspx';
    poststr = "txt=" + encodeURI(txt) + "&idFoto=" + idFoto;

    if (el('chkReceberFuturos') && (el('chkReceberFuturos').checked))
        poststr += "&receberFuturos=1";

    req = getAjax();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            el('enviarComentBT').style.display = 'block';
            el('divEnviandoComent').style.display = 'none';
            if (req.status == 200) {
                if (req.responseText.substr(0, 2) != "ok")
                    alert(req.responseText)
                else {
                    alert('Seu comentário foi registrado. Obrigado!');
                    idc = req.responseText.substr(3, req.responseText.length - 3);
                    location.href = 'fotoPg.aspx?idFoto=' + (idFoto) + '&ultimaPagina=1&idc=' + (idc) + '#comentario' + (idc);
                }
            }
            else {
                alert('Desculpe, ocorreu uma falha técnica interna durante o processamento de sua solicitação. Por favor tente novamente em alguns instantes.');
            }
        }
    }

    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    // http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // http_request.setRequestHeader("Content-length", poststr.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(poststr);
}

var reqs = [];

function saveReqObjs(req, btnPub, btnBlq, divE, idCustom, bloquear, publicar) {
    reqs.length++;
    i = reqs.length - 1;
    reqs[i] = new matriz(2);
    reqs[i].req = req;
    reqs[i].objects = new matriz(6);
    reqs[i].objects.btnPub = btnPub;
    reqs[i].objects.btnBlq = btnBlq;
    reqs[i].objects.divE = divE;
    reqs[i].objects.idCustom = idCustom;
    reqs[i].objects.bloquear = bloquear;
    reqs[i].objects.publicar = publicar;
}

function loadReqObjs(req) {
    for (i = 0; i < reqs.length; i++)
        if (req == reqs[i].req)
            return reqs[i].objects;
}

function postComentarioMudaStatus(btn, idPostComentario, status, validador, bloquear, publicar) {

    btnPub = el('btnPubCm' + (idPostComentario));
    btnBlq = el('btnBlqCm' + (idPostComentario));
    divE = el('divEnviandoCm' + (idPostComentario));

    btnBlq.style.display = 'none';
    btnPub.style.display = 'none';

    divE.style.display = 'block';
    setTimeout('showProgressImage(\'imgLoadingCm' + (idPostComentario) + '\')', 100);

    url = htmlBase + 'mypoint/postComentarioMudaStatus.aspx?idPostComentario=' + (idPostComentario) + '&status=' + (status) + '&valid=' + validador + '&rnd=' + (Math.random());

    req = getAjax();

    saveReqObjs(req, btnPub, btnBlq, divE, idPostComentario, bloquear, publicar);

    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            objs = loadReqObjs(req);
            objs.btnPub.style.display = 'block';
            objs.btnBlq.style.display = 'block';
            objs.divE.style.display = 'none';
            if (req.status == 200) {
                if (req.responseText != "ok")
                    alert(req.responseText)
                else {
                    mpCmadCm = el('mpCmadCm' + (objs.idCustom));
                    lnkCm = el('lnkCm' + (objs.idCustom));
                    lnkCmB = el('lnkCmB' + (objs.idCustom));
                    mpCmadCm.style.color = objs.bloquear ? '#cccccc' : '';
                    lnkCm.style.color = objs.bloquear ? '#cccccc' : '';
                    if (lnkCmB)
                        lnkCmB.style.color = objs.bloquear ? '#cccccc' : '';
                    objs.btnPub.value = objs.publicar ? 'publicado' : 'publicar';
                    objs.btnBlq.value = objs.bloquear ? 'bloqueado' : 'bloquear';
                    objs.btnPub.disabled = objs.publicar;
                    objs.btnBlq.disabled = objs.bloquear;
                    objs.btnPub.style.backgroundColor = objs.publicar ? 'white' : '';
                    objs.btnBlq.style.backgroundColor = objs.bloquear ? 'white' : '';
                    objs.btnPub.style.color = objs.publicar ? 'black' : '';
                    objs.btnBlq.style.color = objs.bloquear ? 'black' : '';
                }
            }
            else {
                alert('Desculpe, ocorreu uma falha técnica interna durante o processamento de sua solicitação. Por favor tente novamente em alguns instantes.');
            }
        }
    }

    req.open('GET', url, true);
    req.send(null);
}

function fotoComentarioMudaStatus(btn, idFotoComentario, status, validador, bloquear, publicar) {

    btnPub = el('btnPubCm' + (idFotoComentario));
    btnBlq = el('btnBlqCm' + (idFotoComentario));
    divE = el('divEnviandoCm' + (idFotoComentario));

    btnBlq.style.display = 'none';
    btnPub.style.display = 'none';

    divE.style.display = 'block';
    setTimeout('showProgressImage(\'imgLoadingCm' + (idFotoComentario) + '\')', 100);

    url = htmlBase + 'mypoint/fotoComentarioMudaStatus.aspx?idFotoComentario=' + (idFotoComentario) + '&status=' + (status) + '&valid=' + validador + '&rnd=' + (Math.random());

    req = getAjax();

    saveReqObjs(req, btnPub, btnBlq, divE, idFotoComentario, bloquear, publicar);

    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            objs = loadReqObjs(req);
            objs.btnPub.style.display = 'block';
            objs.btnBlq.style.display = 'block';
            objs.divE.style.display = 'none';
            if (req.status == 200) {
                if (req.responseText != "ok")
                    alert(req.responseText)
                else {
                    mpCmadCm = el('mpCmadCm' + (objs.idCustom));
                    lnkCm = el('lnkCm' + (objs.idCustom));
                    lnkCmB = el('lnkCmB' + (objs.idCustom));
                    mpCmadCm.style.color = objs.bloquear ? '#cccccc' : '';
                    lnkCm.style.color = objs.bloquear ? '#cccccc' : '';
                    if (lnkCmB)
                        lnkCmB.style.color = objs.bloquear ? '#cccccc' : '';
                    objs.btnPub.value = objs.publicar ? 'publicado' : 'publicar';
                    objs.btnBlq.value = objs.bloquear ? 'bloqueado' : 'bloquear';
                    objs.btnPub.disabled = objs.publicar;
                    objs.btnBlq.disabled = objs.bloquear;
                    objs.btnPub.style.backgroundColor = objs.publicar ? 'white' : '';
                    objs.btnBlq.style.backgroundColor = objs.bloquear ? 'white' : '';
                    objs.btnPub.style.color = objs.publicar ? 'black' : '';
                    objs.btnBlq.style.color = objs.bloquear ? 'black' : '';
                }
            }
            else {
                alert('Desculpe, ocorreu uma falha técnica interna durante o processamento de sua solicitação. Por favor tente novamente em alguns instantes.');
            }
        }
    }

    req.open('GET', url, true);
    req.send(null);
}

function sdWriteFlash(id, src, width, height, align, salign, wmode, mozila, versao, onmouseover, onmouseout, flashvars) {
    s = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + (versao) + ',0,0,0" width="' +
        (width) + '" height="' + (height) + '" id="' +
        id + '" ' + (align != '' ? 'align="' + align + '"' : '') +
        (onmouseover != '' ? ' onmouseover="' + onmouseover + '"' : '') +
        (onmouseout != '' ? ' onmouseout="' + onmouseout + '"' : '') +
        '><param name="quality" value="high" />' +
        '<param name="allowFullScreen" value="true" />' +
        (src != '' ? '<param name="movie" value="' + src + '" />' : '') +
        (salign != '' ? '<param name="salign" value="' + salign + '" />' : '') +
        ((wmode && (wmode != '')) ? '<param name="wmode" value="' + wmode + '" />' : '') +
        '<param name="menu" value="false" /><param name="scale" value="noscale" />' +
        ((flashvars && (flashvars != '')) ? '<param name="flashvars" value="' + flashvars + '" />' : '');
    if (mozila)
        s += '<embed ' +
            (src != '' ? 'src="' + src + '" ' : '') +
            'loop="false" menu="false" quality="high" scale="noscale" width="' +
            (width) + '" height="' + (height) + '" name="' + id + '" ' +
            (align != '' ? 'align="' + align + '" ' : '') +
            (salign != '' ? 'salign="' + salign + '" ' : '') +
            (onmouseover != '' ? 'onmouseover="' + onmouseover + '" ' : '') +
            (onmouseout != '' ? 'onmouseout="' + onmouseout + '" ' : '') +
            ((wmode && (wmode != '')) ? 'wmode="' + wmode + '" ' : '') +
            ((flashvars && (flashvars != '')) ? 'flashvars="' + flashvars + '" ' : '') +
            'allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" swliveconnect="true" />';
    s += '</object>';
    document.write(s);
}