/**
* (SnS) AJAX Quick Quote
* for IP.Board 2.3.x
*
* @package		(SnS) AJAX Quick Quote
* @author		Oleg "Sannis" Efimov
* @copyright	2007 Sannis
* @link			http://sannis.ru
*/


function ajax_quick_quote( pid, selText )
{
	//----------------------------------
	// Using fancy js?
	//----------------------------------
	
	if ( !use_enhanced_js )
	{
		return false;
	}
	
	/*--------------------------------------------*/
	// Main function to do on request
	/*--------------------------------------------*/
	
	do_request_function = function( )
	{
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading();
			return;
		}
		
		xmlobj.hide_loading();
		
		//----------------------------------
		// Process
		//----------------------------------
		
		var raw_text = xmlobj.xmlhandler.responseText;
		
		if ( raw_text == 'nopermission' )
		{
			alert( js_error_no_permission );
		}
		else if ( raw_text == 'error' )
		{
			alert( 'error' );
		}
		else 
		{
			ajax_quick_quote_add(raw_text, selText);	
		}
	}
	
	//----------------------------------
	// LOAD XML
	//----------------------------------
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	var url = ipb_var_base_url+'act=xmlout&do=get-post-quickquote&p='+pid+'&t='+ipb_input_t+'&f='+ipb_input_f;
	xmlobj.process(url);
	
	return false;
}

function ajax_quick_quote_add( raw_text, selText )
{
	if ( raw_text.replace(' ', '') != '')
	{
		raw_text = ipsclass.un_htmlspecialchars(raw_text);
		
		my_show_div(document.getElementById('qr_open'));
		my_hide_div(document.getElementById('qr_closed'));
		
		IPS_Lite_Editor['fast-reply'].editor_check_focus();
		
//	        var selectedText = document.getSelection();

//		alert(selectedText);
		IPS_Lite_Editor['fast-reply'].insert_text(IPS_Lite_Editor['fast-reply'].get_selection() + raw_text + selText + "\n[/quote]\n");
//	        IPS_editor['ed-0'].editor_check_focus();
//    		IPS_editor['ed-0'].insert_text(IPS_editor['ed-0'].get_selection() + raw_text);
		IPS_Lite_Editor['fast-reply'].editor_check_focus();
		
		document.getElementById('qr_open').scrollIntoView();
	}
}
