/*************************************************************
 *  Quick Quote Mod by Char
 *	For MFGG
 *************************************************************/
function StripTags(strMod){
	// SRC: http://www.georgehernandez.com/h/aaBlog/post.asp?PostID=431
    if(arguments.length<3) strMod=strMod.replace(/<\/?(?!\!)[^>]*>/gi, '');
    else{
        var IsAllowed=arguments[1];
        var Specified=eval("["+arguments[2]+"]");
        if(IsAllowed){
            var strRegExp='</?(?!(' + Specified.join('|') + '))\b[^>]*>';
            strMod=strMod.replace(new RegExp(strRegExp, 'gi'), '');
        }else{
            var strRegExp='</?(' + Specified.join('|') + ')\b[^>]*>';
            strMod=strMod.replace(new RegExp(strRegExp, 'gi'), '');
        }
    }
    return strMod;
}

function quick_quote(post_id, username, url)
{
	var message_name = 'message_' + post_id;
	var posturl = url + '/viewtopic.php?p='+post_id+'#p'+post_id;
	var theSelection = '';
	var thePost = '';
	var finalSelection = '';
	var qrcon = false;
	var divarea = false;
	var quicktb = false;

	if (document.all)
	{
		divarea = document.all[message_name];
		quicktb = document.all['quickreplytb'];
		qrcon = document.all['qrc'];
	}
	else
	{
		divarea = document.getElementById(message_name);
		quicktb = document.getElementById('quickreplytb');
		qrcon = document.getElementById('qrc');
	}

	// Get text selection - not only the post content :(
	if (window.getSelection)
	{
		theSelection = window.getSelection().toString();
	}
	else if (document.getSelection)
	{
		theSelection = document.getSelection();
	}
	else if (document.selection)
	{
		theSelection = document.selection.createRange().text;
	}

	if (divarea.innerHTML)
	{
		thePost = divarea.innerHTML.replace(/<br>/ig, '\n');
		thePost = thePost.replace(/<br\/>/ig, '\n');
		thePost = thePost.replace(/&lt\;/ig, '<');
		thePost = thePost.replace(/&gt\;/ig, '>');
		thePost = thePost.replace(/&amp\;/ig, '&');
	}
	else if (document.all)
	{
		thePost = divarea.innerText;
	}
	else if (divarea.textContent)
	{
		thePost = divarea.textContent;
	}
	else if (divarea.firstChild.nodeValue)
	{
		thePost = divarea.firstChild.nodeValue;
	}
	
	if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null || StripTags(thePost).indexOf(theSelection) == -1)
		finalSelection = StripTags(thePost);
	else
		finalSelection = StripTags(theSelection);

	if (finalSelection)
	{
		if (qrcon.style.display == 'none')
			qrcon.style.display = '';
		quicktb.value += '[quote="[url='+posturl+']' + username + '[/url]"]' + finalSelection + '[/quote]\n';
		window.location.hash = "quickreplyanchor";
		quicktb.focus();
	}

	return;
}

function showQROpts() {
	if (document.getElementById('qr1') != undefined)
		document.getElementById('qr1').style.display = '';
	document.getElementById('qr2').style.display = '';
	document.getElementById('qr3').style.display = 'none';
}

/* Ripped off from somewhere */
function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
	while(1) 
	{
	  curleft += obj.offsetLeft;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.x)
	curleft += obj.x;
return curleft;
}

function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
	while(1)
	{
	  curtop += obj.offsetTop;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.y)
	curtop += obj.y;
return curtop;
}

// Show forum desc 
var box;

function showPopup(iam, text) {
	me = iam.childNodes[0];
	var x = findPosX(me);
	var y = findPosY(me);
	box = document.createElement("div");
	box.style.position = "absolute";
	box.style.left = (x-2)+"px";
	box.style.top  = (y+me.height-1)+"px";
	box.className = "popup";
	box.innerHTML = text;
	document.body.appendChild(box);
}
