var oldText;
function editComment(id)
{
    document.getElementById("editImage"+id).style.visibility = "hidden";
    tempText = document.getElementById("text"+id).innerHTML;
    oldText = tempText;
	tempText = prepareTextForTextarea(tempText);
	var editForm = "<textarea id=editText name=editText style='width:100%; height:100px;' class=comment_input>"+tempText+"</textarea>";
    editForm += "<br><input id=editSubmit name=editSubmit style='Cursor: pointer;' onclick='startEditing("+id+"); return false;' type=submit value='Сохранить' class=comment_button>";
    editForm += "&nbsp;&nbsp;<input id=cancelSubmit name=cancelSubmit style='Cursor: pointer;' onclick='cancelEditing("+id+"); return false;' type=submit value='Отменить' class=comment_button>";
	editForm += "&nbsp;&nbsp;<span id=editError></span>";
    document.getElementById("text"+id).innerHTML = editForm;
    document.getElementById("editSubmit").focus();
}
function prepareTextForTextarea(text)
{
	var ver = navigator.appVersion;
    if (ver.indexOf("MSIE") != -1)
		text = tempText.replace(/<br>/gi, "\n");    // for IE
	else
		text = text.replace(/<br>/gi, "");    // for FF and Opera

    text = text.replace(/<i>/gi, "[i]");
    text = text.replace(/<\/i>/gi, "[/i]");
    text = text.replace(/<span class=quote>/gi, "[quote]");
    text = text.replace(/<span class='quote'>/gi, "[quote]"); // for IE
    text = text.replace(/<span class="quote">/gi, "[quote]"); // for FF
    text = text.replace(/<\/span>/gi, "[/quote]");

	text = text.replace(/<a target="_blank" href=".+">/gi, ""); // fo FF
	text = text.replace(/<A href=".+" target=_blank>/gi, ""); 	// for IE
	text = text.replace(/<\/a>/gi, "");

    return text;
}
function cancelEditing(id)
{
    document.getElementById("editImage"+id).style.visibility = "visible";
    document.getElementById("text"+id).innerHTML = oldText;
}
function startEditing(id)
{
    httpObject = getHTTPObject();
    if (httpObject != null)
    {
    	lockButton("editSubmit");

        var poststr="action="+encodeURIComponent("edit_comment")+
        			"&id="+id+
                    "&text="+encodeURIComponent(document.getElementById("editText").value);
        httpObject.open("POST", "ajax_functions.php", true);
		httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	httpObject.setRequestHeader("Content-length", poststr.length);
      	httpObject.setRequestHeader("Connection", "close");
      	httpObject.send(poststr);
        httpObject.onreadystatechange = function()
        {
   	        if(httpObject.readyState == 4)
   	        {
                if(httpObject.responseText.substring(0,6) == "error:")
                {
                    if(document.getElementById("editSubmit"))
                    	unlockButton("editSubmit");
                    var arr = httpObject.responseText.split('error:');
                    document.getElementById('editError').innerHTML = arr[1];

                }
                else
                {
                    //alert(httpObject.responseText);
                    document.getElementById("editImage"+id).style.visibility = "visible";
	                document.getElementById("text"+id).innerHTML = httpObject.responseText;
				}
   	        }
        }
    }
}
function insert(text)
{
	if (text!="")
    	paste("[quote]"+text+"[/quote]\n", 0);
}

function pasteQuote(name)
{
    if(document.getElementById("editText"))
    {
	    document.getElementById("editText").focus();
	    document.getElementById("editText").value += "2 "+name+"\n";
	}
	else
    {
	    document.getElementById("text").focus();
	    document.getElementById("text").value += "2 "+name+"\n";
    }
}
function paste(text)
{
    if(document.getElementById("editText"))
    {
	    document.getElementById("editText").focus();
	    document.getElementById("editText").value += text;
	}
	else
    {
	    document.getElementById("text").focus();
	    document.getElementById("text").value += text;
    }
}

function get_selection()
{
	if (document.getSelection)
	{
       selection = window.getSelection ? window.getSelection() : document.getSelection
       selection = new String(selection)
       selection = selection.replace(/\r\n\r\n/gi, "_doublecaret_");
       selection = selection.replace(/\r\n/gi, " ");
       while (selection.indexOf("  ") !=-1)
       selection = selection.replace(/  /gi, "");
       selection = selection.replace(/_doublecaret_/gi, "\r\n\r\n");
    }
   	else
	{
		selection = document.selection.createRange().text;
	}
	return selection;
}
function lockButton(id)
{
	var button = document.getElementById(id);
	button.disabled = true;
    //if(id == "submit")
	//    setTimeout("unlockButton("+id+");", 5000);
}
function unlockButton(id)
{
    var button = document.getElementById(id);
    //if(button.disabled == true)
		button.disabled = false;
}
function addComment()
{
    httpObject = getHTTPObject();
    if (httpObject != null)
    {
        lockButton("submit");

        var poststr="action="+encodeURIComponent("add_comment")+
        			"&id="+encodeURIComponent(document.getElementById("news_id").value)+
                    "&text="+encodeURIComponent(document.getElementById("text").value);
		if(document.getElementById("bobo"))
			poststr += "&bobo="+encodeURIComponent(document.getElementById("bobo").value);
		if(document.getElementById("name"))
			poststr += "&name="+encodeURIComponent(document.getElementById("name").value);
		if(document.getElementById("pass"))
			poststr += "&pass="+encodeURIComponent(document.getElementById("pass").value);

        httpObject.open("POST", "ajax_functions.php", true);
		httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	httpObject.setRequestHeader("Content-length", poststr.length);
      	httpObject.setRequestHeader("Connection", "close");
      	httpObject.send(poststr);

        httpObject.onreadystatechange = function()
        {
   	        if(httpObject.readyState == 4)
   	        {
                if(httpObject.responseText.substring(0,6) == "error:")
                {
                    var arr = httpObject.responseText.split('error:');
                    document.getElementById('error').innerHTML = arr[1];
                    unlockButton("submit");
                }
                else if(httpObject.responseText.substring(0,9) == "newcomer:")
                {
					window.location.href = "http://www.biathlon.com.ua/newcomer.php?id="+document.getElementById("news_id").value;
                }
                else
                {
                    if(String(window.location).indexOf("#comment") != "-1")
                    {
                        document.getElementById('text').value = "";
                        window.location.reload();
					}
					else
                    {
                        window.location.hash = "#comment";
                        document.getElementById('text').value = "";
                        window.location.reload();
                    }
				}
   	        }
        }
    }
}
function deleteComment(id_to_del, id, not_upd_news)
{
    if(confirm('Удалить этот комментарий?'))
    {
	    httpObject = getHTTPObject();
	    if (httpObject != null)
	    {
	        httpObject.open("GET", "ajax_functions.php?action=delete_comment&id_to_del="+id_to_del+"&id="+id+"&not_upd_news="+not_upd_news, true);
	        httpObject.send(null);
	        httpObject.onreadystatechange = function()
	        {
	            if(httpObject.readyState == 4)
	            {
	                document.getElementById("com"+id_to_del).parentNode.removeChild(document.getElementById("com"+id_to_del));
	            }
	        };
	    }
	}
}
function getHTTPObject(){
	if (window.ActiveXObject)
	    return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
	    return new XMLHttpRequest();
	else
	{
	    //alert("Your browser does not support AJAX.");
	    return null;
	}
}
