// http://akelpad.sourceforge.net/forum/viewtopic.php?p=1994#1994
// Description(1033): Changing the direction of Q <-> A
// 	(for Mnemosyne_XML1-file received from AnyMemo)
// Description(1049): Изменение направления Вопрос <-> Ответ
// 	(для Mnemosyne_XML1-файла, полученного из AnyMemo)
// Version: 1.1 (2015.10.08)
// Author: VladSh 

if (!AkelPad.Include("CommonFunctions.js")) WScript.Quit();

var bSave = false;

var pContent = AkelPad.GetSelText();
if (!pContent) {
	pContent = AkelPad.GetTextRange(0, -1);
	bSave = true;
}

pContent = pContent.replace(/Q>/g, "#>");
pContent = pContent.replace(/A>/g, "Q>");
pContent = pContent.replace(/#>/g, "A>");

var pFileName = AkelPad.GetEditFile(0);
bSave = (bSave && pFileName) ? true : false;
if (bSave) {
	var oFileFormat = getFileFormat(AkelPad.GetEditWnd());
	createFile(oFileFormat, "xml");
}
AkelPad.ReplaceSel(pContent);

if (bSave) {
	var pPrefix = AkelPad.GetArgValue("fileprefix", "Rev ");
	var oFile = separateFile(pFileName);
	oFile.name = pPrefix + oFile.name;
	pFileName = joinFile(oFile);
	AkelPad.SaveFile(AkelPad.GetEditWnd(), pFileName);
	closeFile();
}

closeFile();