//
// Persian Editor Tools
// Hamid Zarrabi-Zadeh
// Jan 1, 2006
//

//------------------ global variables ---------------------

var menu_buffer = '';
var show_entity = 0;


//------------------ tools functions ---------------------


function submit_form(value)
{
	document.form.edit_param.value = value;
	document.form.submit();
}


function show_open_box(show)
{
	obj1 = document.getElementById('navbar');
	obj2 = document.getElementById('open_box');
	box_text = '<input size="30" type="file" name="userfile" onchange="submit_form(\'open-file\')" />';
	nav_text = '<a class="navbar" title="Back to main menu" href="javascript:show_open_box(0)">Back to Main Menu</a>';
	if (show == 1)
	{
		menu_buffer = obj1.innerHTML;
		obj1.innerHTML = nav_text;
		obj2.innerHTML = box_text;
	}
	else
	{
		obj1.innerHTML = menu_buffer;
		obj2.innerHTML = '';
	}
}


function refreshLangIndicator()
{
	obj = document.getElementById('lang_indicator');
	obj.innerHTML = isiri2901_lang==1 ? '(Fa)' : '(En)';
}


function toggleLang(obj)
{
	isiri2901_lang = 1- isiri2901_lang;
	refreshLangIndicator();
	obj.focus();
}


function Utf8ToEntity(obj)
{
	obj.value = obj.value.replace(/([^\x00-\x7f])/g,
		function(s, c, ofs, all) { c = String(c); return "&#"+c.charCodeAt(0)+";"; } );
}


function EntityToUtf8(obj)
{
	obj.value = obj.value.replace(/&#([0-9]+);/g,
		function(s, n, ofs, all) { return String.fromCharCode(n);} );
}


function showEntity()
{
	show_entity = 1- show_entity;
	obj = document.getElementById('entity_menu');
	entity = '<a class="navbar" title="Convert from UTF-8 to Entity" href="javascript:showEntity()">Entity</a>';
	utf8   = '<a class="navbar" title="Convert from Entity to UTF-8" href="javascript:showEntity()">UTF-8</a>';

	if (show_entity == 1)
	{
		obj.innerHTML = entity;
		EntityToUtf8(textArea);
	}
	else
	{
		obj.innerHTML = utf8;
		Utf8ToEntity(textArea);
	}
	textArea.focus();
}