
function ShowAccept(text_correct_id, status)
{

    $("#POINTS_FORM_DIV").show();
    jQuery("#points_form").validate();
}

function getStatusMes(status)
{
    if (status == 1)
        return "Are you sure you completed the correction?";

    if (status == 0)
        return "Reopen correction?";

    alert("Wrong Status!");

}


function SetStatus(text_correct_id, status, text_id)
{
	
    //if (window.confirm(getStatusMes(status)))
    $.getJSON("/ajax_set_text_correction_status/"+text_correct_id+"/"+status, {}, function (){location.href="/correct/"+text_id+"/page1"});
}


function CreateCorrect(text_id)
{
	
    //if (window.confirm(getStatusMes(status)))
    $.getJSON("/ajax_create_correction/"+text_id, {}, function (){location.href="/correct/"+text_id+"/page1"});
}

function build_on_save_done(pid)
{
	return function onSaveDone(data)
	{
	    //if (data.result != "ok")
	    //    alert("Save error!");
	    //alert('save_done'+pid);
		
		$.get("/get_marked_paragraf/"+pid, {},function(data){$("#paragraph_"+pid).html(data); });
    	$.get("/get_marked_correct/"+pid, {},function(data){$("#cparagraph_"+pid).html(data);$("#paragraphc_"+pid).hide();$("#cparagraph_"+pid).show()});
	}
}

function saveCorrect(paragraf_id, ctext)
{
    //alert("save_"+paragraf_id+"_"+ctext);
    $.post("/ajax_save_paragraf/"+paragraf_id, {"text":ctext}, build_on_save_done(paragraf_id));
}

function remove_markers(pid, chars_count)
{
    for (var i=0;i<chars_count;i++)
    {
        var cid = "char"+pid+"_"+i;
        $("#"+cid).removeClass();
    }
}

function onApplyDone(data)
{


    remove_markers(start_block_id, data.chars)

    for (mid in data.data)
        draw_Marker(data.data[mid]);
}

var marker_div = document.getElementById('red_marker_div');

function check_to_spelling()
{
    marker_type = 0;
    $("#red_marker_div").show();
    $("#lastik_marker_div").hide();
    marker_div = document.getElementById('red_marker_div');

}

function check_to_lasting()
{
    marker_type = -1;
    $("#red_marker_div").hide();
    $("#lastik_marker_div").show();
    marker_div = document.getElementById('lastik_marker_div');

}

function check_to_cursor()
{
    marker_type = null;
    $("#red_marker_div").hide();
    $("#lastik_marker_div").hide();


}

function markers_move(event)
{


    var mc = mouseCoords(event)
    marker_div.style.top = mc.y+5+'px';
    marker_div.style.left = mc.x+5+'px';

    //marker_div.style.top = mc.y+5;
    //marker_div.style.left = mc.x+5;


}


var marker_type = null;
var pushed = false;
var start_block_id;
var start_char_pos;
function d(block_id,char_pos)
{

    pushed = true;
    //alert('start sel'+block_id+char_pos);
    start_block_id = block_id;
    start_char_pos =  char_pos;
}

function u(block_id,char_pos)
{

    //alert('end sel'+block_id+char_pos);
    if (start_block_id!=block_id)
        return;
    if (pushed == false)
        return;

    pos2 = Math.max(char_pos,start_char_pos);
    pos1 = Math.min(char_pos,start_char_pos);
    //alert("/ajax_apply_marker/"+block_id+"/"+pos1+"/"+pos2+"/0");
    if (marker_type!=null)
        $.getJSON("/ajax_apply_marker/"+block_id+"/"+pos1+"/"+pos2+"/"+marker_type, {}, onApplyDone);

    pushed = false;
}

function prepare_text(ids)
{
    //alert("start");
    for (id in ids)
    {
        var text = $("#paragraph_"+ids[id]).text();

        var t = "";
        //arr = [];
        for (var i=0;i<text.length;i++)
        {
            var ch = text.charAt(i);
            var html= "<span id='char"+ids[id]+"_"+i+"' onmousedown='d("+ids[id]+","+i+");' onmouseup='u("+ids[id]+","+i+");'>"+ch+"</span>"
            //arr.push(html);
            t = t.concat(html);

        }
        //t = arr.join("");
        $("#paragraph_"+ids[id]).empty();
        $(t).appendTo("#paragraph_"+ids[id]);
        //alert(t);
    }
}

function init_data(data)
{
    //alert("get");
    for (pid in data)
        for (k in data[pid])
            draw_Marker(data[pid][k]);
}

function draw_Marker(marker)
{
    var m = marker;
    //alert("draw_marker"+m.from+m.to+m.type+m.pid);
    for (var i=m.from;i<=m.to;i++)
    {
        var cid = "char"+m.pid+"_"+i;
        //alert(cid);
        var cls = "";
        if (m.type == 0)
            cls = "red_marker";
        else
            cls = "yellow_marker";

        $("#"+cid).toggleClass(cls);
    }
    //alert($("#paragraphc_99").value);
}



function remove_paragraph_markers(pid)
{
    function on_remove_markers(data)
    {
        remove_markers(data.pid, data.chars);
    }
    $.getJSON("/ajax_remove_markers/"+pid, {}, on_remove_markers);
}

function checkTextAreaEmpty(id)
{
    alert(id);
    alert($("#paragraphc_"+id).val());
}

function checkTextAreas(ids)
{
    for (id in ids)
    {
        checkTextAreaEmpty(ids[id])
    }
}


