var maxSymbolsInQuote = 2500;
var maxSymbolsInComment = 2;	

var lcpID;
var lcpText;
function SetLastCommentSourceElement(e)
{
/*	var postID = e.id.split("_")[1];
	if (postID != null)
	{
		var text = e.document.selection.createRange().htmlText;
			
		if (lcpID == null)
			lcpID = postID;
						
		if (lcpID != postID)
			lcpID = postID;
		
		lcpText = text;
	}*/
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
String.prototype.truncate = function(maxLength) 
{ 
	if (this.length > maxLength)
		return this.substring(0, maxLength);
	else
		return this;
};

function SetQuote(objName)
{		
        $("#CommentForm").show("slow");
	
	var obj  = document.getElementById("Comment_"+objName);
	var objAuthor = document.getElementById("AutorQuote_"+objName);
	var objSet = document.getElementById("descr");
				
	if (lcpID != objName) 
	{
		lcpID = objName;
		lcpText = obj.innerHTML;
	}
				
	var quote = RemoveQuoteFromTextByCSS(lcpText);
	quote = UnlinkText(quote).truncate(maxSymbolsInQuote);
				
	if ( objSet != null )								
	{
		var pos = objSet.value.indexOf("[quote]");
		var lastpos = objSet.value.lastIndexOf("[/quote]");
		if (pos != -1 && lastpos != -1 )
		{
			var remove = objSet.value.substring (pos,lastpos+9);
			objSet.value = objSet.value.replace(remove,"");
		}
		if (IsIE()== true)
		{										
			var textToInsert = "[quote][autor]"+objAuthor.innerText+"[/autor]";
			textToInsert += quote + "[/quote]\n"
			objSet.focus();
			var sel = document.selection.createRange();
			sel.text = textToInsert;
		}
		else
			objSet.value += "[quote][autor]"+objAuthor.textContent+"[/autor]"+ quote  +"[/quote]\n"

		objSet.focus();					
	}

	return false;
}

function RemoveQuoteFromTextByCSS(text)
{
	text =  text.replace(/<BR>/g,"\n");	
	var word = Del(text);
							
	return word;
}

function Del(Word) 
{
	a = Word.indexOf("<");
	b = Word.indexOf(">");
	len = Word.length;
	c = Word.substring(0, a);
	if(b == -1)
		b = a;
	d = Word.substring((b + 1), len);
	Word = c + d;
	tagCheck = Word.indexOf("<");
	if(tagCheck != -1)
		Word = Del(Word);
	return Word;
}

function UnlinkText(text)
{
	var re = new RegExp("<(a|/a)[^>]*>", "gi");
	var arr = text.match(re);
	if (arr != null)
		for (i=0; i<arr.length; i++)
	text = text.replace(arr[i], "");
	return text;
}

function IsIE()
{
	if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) 
		return true;
	else
		return false;
}
