﻿function getCookie(c_name){
    if (document.cookie.length>0){
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1){ 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
                return unescape(document.cookie.substring(c_start,c_end));
        } 
    }
    return "";
}

function setCookie(c_name,value,expiredays){
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie=c_name+ "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function atribuiNota(idPost){
    if(Const.toAction == true){
        var cookie = getCookie('BlogTVPost' + idPost);
        if (cookie != null && cookie != ""){
            Modal.alert('Voc&ecirc; ja votou nesse post hoje!');
        }else{
            var url = '/SalvaRating.aspx?IdPost='+ idPost +'&nota=' + $('score' + idPost).value; 
            Const.toAction = false;
            new Ajax.Request(url, {
                method:'get',
                onSuccess:function(transport) {
                    Windows.closeAll();
                    Const.toAction = true;
//                    $('votos' + idPost).innerHTML = parseInt($('votos' + idPost).innerHTML) + 1;
                    $('votos' + idPost).innerHTML = 'Seu voto foi contabilizado.';
                    setCookie('BlogTVPost' + idPost , 'true', 1);
//                    $('msgVoto' + idPost).innerHTML = '<span style="color:red">Seu voto foi contabilizado.</span>';
                }
            });  
        }
    }else{
        Modal.alert(Const.msgInAction);
    }
}

