function setCookie(vars) {
    today  = new Date();
    ddate = new Date(today.getTime() + 63072000000);
    document.cookie = vars + "; expires=" + ddate.toGMTString()+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
}

function  expandCommentForm(id) {
    $("#"+id+"-text").css("height", "200px");
    $("#"+id+"-text").css("width", "90%");
    $("#"+id+" *").show();
}

function sendFEForm(type, id) {
    query='';
    fields = new Array();
    if (type=='addComment') {
        check = $("#"+id+"-text").attr("value");
        if(check.length>10) {
            //setCookie("usermail="+$("#"+id+"-userEmail").attr("value"));
//            fields = new Array('text', 'userName', 'userEmail', 'entityType', 'itemID', 'ancestor');
            fields = new Array('text', 'userName', 'entityType', 'itemID', 'ancestor');
            /* if ($("#cSubscr").attr("checked")) {
                if ($("#"+id+"-userEmail").attr("value").length>3) {
                    query+='&cSubscr=yes';
                } else {
                    fields = '';
                    alert("Укажите ваш мейл, если хотите подписаться на комментарии");
                }
            } */
        } else {
            alert('Заполните поле "текст"');
        }
    }
    if (fields) {
        jQuery.each(fields, function () {
            value = $("#"+id+"-"+this).attr("value");
            value = value.replace("&", ";amp;");
            query=query+'&'+this+'='+value;
        });
    }
    if (query.length>0) {
        $("#"+id+"-msg").html("Секундочку...");
        $("#"+id+"-fields").hide();
        $.post( '/ajax/FEForm/'+type+'/', query, function (data, textStatus) {
            displaySubmissionResults(data, id, type)
            }, 'json' );
    }
}

function displaySubmissionResults(data, id, type) {
    if(data.result=="ok") {
        switch (type) {
            case 'addComment':
                setTimeout("this.location.reload(true);",1000);
                break;
            default:
                setTimeout("this.location.reload(true);",1000);
        }
    } else {
        alert(data.reason);
        $("#"+id+"-msg").html("");
        $("#"+id+"-fields").show("");
    }
}

/* ------------------- */
function displayCommentsReplyForm(id) {
    $("#comments .replyForm").html('');
    $("#replyForm-"+id).html($("#commentsAddForm").html());
    $("#replyForm-"+id+" .FEForm").css("margin-top", "10px");
    $("#replyForm-"+id+" h3").replaceWith('Ответить на комментарий:');
    $("#replyForm-"+id+" .ancestor").attr("value", id);

}

