function DoConfirmForm(q) {
	if (q == null) {
		var agree=confirm('Вы уверены что хотите удалить комментарий?');
	} else {
		var agree=confirm(q);
	}
		
	if (agree) return true ;
	else return false ;
}

function DoConfirmDeleteVideo() {
	var agree=confirm('Вы уверены что хотите удалить свой видеоклип из очереди на модерацию?');
	if (agree) return true ;
	else return false ;
}

function show(el) {
	var eel = document.getElementById(el);
	if (eel.style.display) { eel.style.display='' }
	else { eel.style.display='none' }
}

function smile (icon)
{
	document.sendmessage.text.value = document.sendmessage.text.value + ' ::' + icon + ':: ';
	document.sendmessage.text.focus();
}

function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2) 
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}

function bbcode (icon)
{
	var txtarea = document.sendmessage.text;
	var theSelection = false;
	
	var clientPC = navigator.userAgent.toLowerCase(); // Get client info
	var clientVer = parseInt(navigator.appVersion); // Get browser version

	var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
	var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
	txtarea.focus();
	
	if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			document.selection.createRange().text = '<' + icon + '>' + theSelection + '</' + icon + '>';
			txtarea.focus();
			theSelection = '';
			return;
		}
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, '<' + icon + '>', '</' + icon + '>');
		return;
	}

	txtarea.value = txtarea.value + '<' + icon + '>' + '</' + icon + '>';
	txtarea.focus();
}

function citate (name)
{
	document.sendmessage.text.value = document.sendmessage.text.value + ' <b>' + name + '</b>, ';
	document.sendmessage.text.focus();
}

function checkAll(oForm, cbName, checked)
{
	for (var i=0; i < oForm[cbName].length; i++) 
		oForm[cbName][i].checked = checked;
}

var selectionStore = false; // Selection data

function emoticon_wospaces_news(text) {
	var txtarea = document.sendmessage.text;
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}

// Catching selection
function catchSelectionNews()
{
	if (window.getSelection)
	{
		selectionStore = window.getSelection().toString();
	}
	else if (document.getSelection)
	{
		selectionStore = document.getSelection();
	}
	else if (document.selection)
	{
		selectionStore = document.selection.createRange().text;
	}
}

// Чистит комменты от BlockQuote
function cleanOtherQuotes(str)
{
	var s = String(str);
	var clientPC = navigator.userAgent.toLowerCase(); // Get client info
	var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
	// Цитаты
	s = s.replace(/<blockquote(.|\s)*?<\/blockquote>/ig, "");
	// Смайлы
	s = s.replace(/<img src=\"images\/smiles\/(\d+)\.gif\" border=\"0\">/ig, "::$1::");
	// Концы строк
	if (is_ie)
		s = s.replace(/<br\s*\/?>/img, "\n");
	else
		s = s.replace(/<br\s*\/?>/img, "");
	return s
}

// Putting selection to the post box
function quoteSelectionNews(name, divName)
{
	if (selectionStore)
	{
		var str = selectionStore;
		emoticon_wospaces_news('[quote="'+name+'"]' + str + '[/quote]\n'); 
		selectionStore = '';
		document.sendmessage.text.focus(); 
		return; 
	}
	else if (divName != '')
	{
		var str = cleanOtherQuotes(document.getElementById(divName).innerHTML);
		emoticon_wospaces_news('[quote="'+name+'"]' + str + '[/quote]\n'); 
		document.sendmessage.text.focus(); 
		return; 
	}
	else
	{ 
		alert('Выделите текст на странице и попробуйте ещё раз');
		return; 
	} 
}

var requestAMVNews;
var destAMVNews;
var callURL;

function processStateChange()
{
	if (requestAMVNews.readyState == 4)
	{
		contentDiv = document.getElementById(destAMVNews);
		if (requestAMVNews.status == 200)
		{
			response = requestAMVNews.responseText;
			contentDiv.innerHTML = response;	
		}
		else
		{
			contentDiv.innerHTML = "Error: Status "+requestAMVNews.status;
		}
	}
}

function getNewHTTPObject()
{
        var xmlhttp;

        /** Special IE only code ... */
        /*@cc_on
          @if (@_jscript_version >= 5)
              try
              {
                  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
              }
              catch (e)
              {
                  try
                  {
                      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                  }
                  catch (E)
                  {
                      xmlhttp = false;
                  }
             }
          @else
             xmlhttp = false;
        @end @*/

        /** Every other browser on the planet */
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
        {
            try
            {
                xmlhttp = new XMLHttpRequest();
            }
            catch (e)
            {
                xmlhttp = false;
            }
        }

        return xmlhttp;
}

function loadHTML(URL, destination)
{
	destAMVNews = destination;
	requestAMVNews = getNewHTTPObject();
	if (requestAMVNews)
	{
		requestAMVNews.onreadystatechange = processStateChange;
		requestAMVNews.open("GET", URL, true);
		requestAMVNews.send(null);
	}
}

function vote_comment(bal, type, pn, id)
{
	callURL = 'http://amvnews.ru/index.php?go='+type+'&file=vote_comment&id='+id+'&bal='+bal;
	divID = 'comment-vote-'+pn+'-'+id;
	loadHTML(callURL, divID);
	//$('#'+divID).load(callURL);
}