

var mss_comments_parent_id = null;


function mss_comments_switch_form(id)
{
	if (mss_comments_parent_id != null)
	{
		document.getElementById("mss_comments_form_container_" + mss_comments_parent_id).innerHTML = "";
	}

	if (id == mss_comments_parent_id)
	{
		mss_comments_parent_id = null;		
	}
	else
	{
		mss_comments_parent_id = id;

		if (mss_comments_parent_id != null)
		{
			document.getElementById("mss_comments_form_parent").value = mss_comments_parent_id;
			mss_comments_refresh_captcha();
			var tmpl = document.getElementById("mss_comments_form_tmpl").innerHTML;
			document.getElementById("mss_comments_form_container_"+mss_comments_parent_id).innerHTML = tmpl;
		}
	}
}

function mss_comments_open_form(id)
{
	console.log("call mss_comments_open_form");
	mss_comments_parent_id = null;
	return mss_comments_switch_form(id);
}


function mss_comments_refresh_captcha()
{
	var img = new Image();
	img.src = "/mss_comments_antirobot.php?rand="+Math.random(1000);

	img.onload = function() 
	{
		var captcha_place = document.getElementById("mss_captcha_place");
		if (captcha_place != null)
		{
			captcha_place.innerHTML = "";
			captcha_place.appendChild(img);
		}
	}
}


function putTagAround(where,what)
{
	insertAroundCursor(document.getElementById(where),"<"+what+">","<\/"+what+">");
}                           


function putTagText(where,what)
{
	insertAtCursor(document.getElementById(where),what);
}


function putTagUrl(where,sPromt) 
{
	var url = "";
	if (url = prompt(sPromt,"http://")) 
	{
        	insertAroundCursor(document.getElementById(where),'<a href="'+url+'">','</a>');
        }
}


function putTagVideo(where,sPromt) 
{
	var url = "";
	if (url = prompt(sPromt,"http://youtube.com/"))
	{
        	insertAtCursor(document.getElementById(where),'<video>'+url+'</video>');
        }
}


function putTagImg(where,sPromt)
{
	var url = "";
	if (url = prompt(sPromt,"http://"))
	{
		insertAtCursor(document.getElementById(where),'<img src="'+url+'" />');
	}
}


function insertAtCursor(myField,myValue)
{
	if (document.selection) 
	{
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	else if (myField.selectionStart || myField.selectionStart == '0') 
	{
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = this.scrollTop;
		myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;
	} 
	else 
	{
		myField.value += myValue;
		myField.focus();
	}
} 


function insertAroundCursor(myField,myValueBefore,myValueAfter)
{
	if (document.selection) 
	{
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValueBefore + sel.text + myValueAfter;
		myField.focus();
	}
	else if (myField.selectionStart || myField.selectionStart == '0') 
	{
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		myField.value = myField.value.substring(0,startPos) + myValueBefore + myField.value.substring(startPos,endPos) + myValueAfter + myField.value.substring(endPos,myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValueBefore.length;
		myField.selectionEnd = endPos + myValueBefore.length;
		myField.scrollTop = scrollTop;
	} 
	else 
	{
		myField.value += myValue;
		myField.focus();
	}
} 


