function checkSelection()
{
	var txt = '';
	if (window.getSelection)
	{
		txt = window.getSelection();
	}
	else if (document.getSelection)
	{
		txt = document.getSelection();
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;
	}
	return txt;
}
function quickQuote(post_id,mode)
{
	var data='';
	if(typeof mode=='undefined'){
		mode='quick';
	}
	var sel=checkSelection();

	if(typeof sel=='undefined'){
		sel='';
	}

	var reply = $E('message')?true:false;

	var file = _f('quickquote');
	var h=$H({pid:post_id,text:sel,mode:mode});

	if(mode == 'vrn') {
		var file = _f('comment');
		var h=$H({id:post_id, text:sel, mode:'quote'});
	}

	new Ajax.Request(file,
		{
			method:'post',
			parameters:h.toQueryString(),
			onSuccess:function(xh){
				var resp=xh.responseText;
				if(resp.substr(0,5)=='error'){
					alert(_("Error")+':\n'+resp.substring(6));
				}else{
					eval('data='+resp);
					data=data.shift();
					if(data.mode=='quick'){
						if(reply===true){
							var msg_area=$('message');
							if(msg_area.value!=''){
								msg_area.value+="\n\n";
							}
							msg_area.value+=data.text+"\n\n";
							msg_area.focus();
							Element.scrollTo("new_post_div");
						}else{
							window.location.href=last_page+"#new";
						}
					}else{
						if(reply===true){
							if(!document.getElementById('quote_list_div')){
								var newHtml='1 '+_('One saved quote')+' (<a href="#" onclick="switchQuotelist();return false;" id="qsLink">'+_('Show text')+'</a>)';
								newHtml+='<div id="quote_list_div" style="display:none;"><ul id="quote_list">';
								newHtml+='<li id="q'+data.id+'">'+newQuoteLi(data)+'</li>';
								newHtml+='</ul></div></div>';
								$('saved_quotes_div').innerHTML=newHtml;
								Element.show('saved_quotes_div');
							}else{
								var newItem=document.createElement('li');
								newItem.id='q'+data.id;
								newItem.innerHTML=newQuoteLi(data);
								$('quote_list').appendChild(newItem);
								var anz=$('quote_list').childNodes.length;
								var newHtml=anz+' '+_('Some saved quotes')+' (<a href="#" onclick="switchQuotelist();return false;" id="qsLink">'+_('Show text')+'</a>)';
								newHtml+='<div id="quote_list_div" style="display:none;">';
								newHtml+=$('quote_list_div').innerHTML;
								newHtml+='</div>';
								$('saved_quotes_div').innerHTML=newHtml;
							}
						}
						setTimeout("fadeIn('"+post_id+"')",1000);
					}
				}
			}
		}
	);
	return false;
}
function storeQuote(post_id){
	var qlink=$('ql'+post_id).firstChild;
	$('dp'+post_id).style.display='none';
	fadeOut(qlink);
	return quickQuote(post_id,'store');
}
function fadeOut(elem){
	var myEff=new Fx.Style(elem,'opacity',{duration:300});
	myEff.custom(1,.6);
}
function fadeIn(post_id){
	var qlink=$('ql'+post_id).firstChild;
	var myEff=new Fx.Style(qlink,'opacity',{duration:300});
	myEff.custom(.6,1);
	$('dp'+post_id).style.display='';
}
function newQuoteLi(data){
	var newHtml='';
	newHtml+='<b>'+_('Quote from')+' '+data.author+' - '+data.time+'</b> ';
	newHtml+='(<a href="#" onclick="insertSavedQuote(\''+data.id+'\');return false;">'+_('insert')+'</a> &middot; <a href="#" onclick="deleteSavedQuote(\''+data.id+'\');return false;">'+_('delete')+'</a>)<br/>';
	newHtml+=data.text;
	return newHtml;
}
function switchQuotelist(){
	Element.toggle('quote_list_div');
	$('qsLink').innerHTML=($('qsLink').innerHTML==_('Show text'))?_('Hide text'):_('Show text');
	return false;
}
function insertSavedQuote(quoteId){
	var anz=$('quote_list').childNodes.length;
	if(anz<=0||!document.getElementById('q'+quoteId)||!document.getElementById('message')){return false;}
	var file=_f('quickquote');
	new Ajax.Request(file,
		{
			method:'post',
			parameters:'mode=insert&qid='+quoteId,
			onSuccess:function(xh){
				var resp=xh.responseText;
				if(resp.substr(0,5)=='error'){
					alert(_("Error")+':\n'+resp.substring(6));
				}else{
					eval('data='+resp);
					data=data.shift();
					insertUBB(data.text,'','message');
					Element.remove('q'+quoteId);
					var newanz=anz-1;
					if(newanz<=0){$('saved_quotes_div').innerHTML='';Element.hide('saved_quotes_div');}
					else{
					var qtext=newanz+' ';
					qtext+=(newanz==1)?_('One saved quote'):_('Some saved quotes');
					qtext+=' (<a href="#" onclick="switchQuotelist();return false;" id="qsLink">'+_('Show text')+'</a>)';
					var newList='<div id="quote_list_div">';
					newList+=$('quote_list_div').innerHTML;
					newList+='</div>';
					$('saved_quotes_div').innerHTML=qtext+newList;
					}
				}
			}
		}
	);
	return false;
}
function deleteSavedQuote(quoteId){
	var anz=(!document.getElementById('quote_list'))?0:$('quote_list').childNodes.length;
	if(anz<=0||!document.getElementById('q'+quoteId)||!document.getElementById('message')){return false;}
	var file=_f('quickquote');
	new Ajax.Request(file,
		{
			method:'post',
			parameters:'mode=delete&qid='+quoteId,
			onSuccess:function(xh){
				var resp=xh.responseText;
				if(resp.substr(0,5)=='error'){
					alert(_("Error")+':\n'+resp.substring(6));
				}else{
					Element.remove('q'+quoteId);
					var newanz=anz-1;
					if(newanz<=0){$('saved_quotes_div').innerHTML='';Element.hide('saved_quotes_div');}
					else{
						var qtext=newanz+' ';
						qtext+=(newanz==1)?_('One saved quote'):_('Some saved quotes');
						qtext+=' (<a href="#" onclick="switchQuotelist();return false;" id="qsLink">'+_('Hide text')+'</a>)';
						var newList='<div id="quote_list_div">';
						newList+=$('quote_list_div').innerHTML;
						newList+='</div>';
						$('saved_quotes_div').innerHTML=qtext+newList;
					}
				}
			}
		}
	);
	return false;
}