// -- text.html, file.html, url.html

// variables to be initialized by sws.xslt
var max;
var baseurl;

var optswin;

var profiles = new Array();
var PROFILE_ID = 0;
var PROFILE_LP = 1;
var PROFILE_NM = 2;

function NewWindow(url,name,w,h) {
        myWin= window.open(url, name, "scrollbars=yes,resizable=yes,toolbar=no,width="+w+",height="+h);
        myWin.focus();
        return myWin;
}

function PrintWindow(url,w,h) {
        if (window.myPWin && ! myPWin.closed) { myPWin.close(); }
        myPWin= window.open(url, "print", "scrollbars=yes,resizable=yes,toolbar=no,width="+w+",height="+h);
        myPWin.focus();
}
function Cnt() {
	lenStr = window.document.SourceOfTranslation.text.value.length;
	if(!isNaN(max) && max > 0 && lenStr > max ) {
	window.document.SourceOfTranslation.text.value = window.document.SourceOfTranslation.text.value.substring(0,max);
	//lenStr = 0;
	}
	if(!isNaN(max) && max > 0)
		window.status = lenStr + " characters; max = " + max; //lenStr;
	storecaret(window.document.SourceOfTranslation.text);
} 

function Ins(str)
{
	insertatcaret(window.document.SourceOfTranslation.text, str);
	Cnt();
}

function storecaret(field)
{
      if (field.createTextRange)
  	     field.caretPos = document.selection.createRange().duplicate();
   }

function insertatcaret(field, text)
{
      if (field.caretPos)
   {
        var caretPos = field.caretPos;
  	     caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?  text + ' ' : text;
      }
  	   else
      	 field.value  =  field.value + text;
   	}


function clipb(flag) {
    if (flag) {
        therange= document.SourceOfTranslation.target.createTextRange();
        therange.execCommand("Copy");
    } else {
        therange= document.SourceOfTranslation.text.createTextRange();
        therange.execCommand("Paste");
    }
}

function FillCharList(strUniRange)
{
	charlist = document.getElementById("charlist");
	
	while(charlist.options.length > 0)
		charlist.options.remove(0);
		
	UniRange = strUniRange.split(";");
	for(i = UniRange[0]; i <= UniRange[1]; i++)
	{
		opt = document.createElement("option");
		charlist.options.add(opt);
		opt.innerHTML = "&#" + i + ";";
	}
}

// -- 


function OptionsWindow()
{
	if(!document.SourceOfTranslation.profile || document.SourceOfTranslation.profile.value == "<custom>")
	{
		url = baseurl + '?s=options&lp=' + document.getElementById('lp').value;
		optswin = NewWindow(url, 'options', 700, 620);	
	}
	else
	{
		url = baseurl + '?s=profileedit&profile=' + document.SourceOfTranslation.profile.value + '&pa=options';
		NewWindow(url, 'profile', 700, 620);
	}
}

function UserdictsWindow()
{

	if(!document.SourceOfTranslation.profile || document.SourceOfTranslation.profile.value == "<custom>")
	{
		url = baseurl + '?s=userdicts';
		NewWindow(url, 'dicts', 700, 620);	
	}
	else
	{
		url = baseurl + '?s=profileedit&profile=' + document.SourceOfTranslation.profile.value + '&pa=userdicts';
		NewWindow(url, 'profile', 700, 620);
	}
}

function LPChanged()
{	
	if(optswin)
		if(!optswin.closed)
		{
			url = baseurl + '?s=options&lp=' + document.SourceOfTranslation.lp.value;
			optswin.location.replace(url);
		}
		
	var select = document.SourceOfTranslation.profile;	
	if(profiles.length > 0 && select)
	{
		var selectedValue = select.value;
		var selectedName = "";				
		for(i = 0; i < profiles.length; i++)
			if(profiles[i][PROFILE_ID] == selectedValue)
				selectedName = profiles[i][PROFILE_NM]
				
		select.options.length = 0;
		var optionNum = 0;
		for(i = 0; i < profiles.length; i++)
		{
			if(profiles[i][PROFILE_LP] == document.SourceOfTranslation.lp.value)
			{
				var option = new Option(profiles[i][PROFILE_NM], profiles[i][PROFILE_ID]);
				select.options[optionNum] = option;
				if(profiles[i][PROFILE_NM] == selectedName)
					option.selected = true;
				optionNum++;				
			}
		}
	}
}

function ShowHint(btn, hint)
{
        h = document.getElementById(hint);
        b = document.getElementById(btn);
        var hw=h.offsetWidth;
        var hh=h.offsetHeight;
        var bw=b.offsetWidth;
        var bh=b.offsetHeight
        var x  = b.offsetLeft;
        var y  = b.offsetTop;
        var el = b;
        while (el && el.tagName != 'BODY' && el.tagname != 'HTML') {
                el = el.offsetParent;
                x = x + el.offsetLeft;
                y = y + el.offsetTop;
        }
        h.style.left = x + bh /2 + 15;
        h.style.top = y + bh - 15;
        h.style.display='block';	
}

function HideHint(hint)
{
	document.getElementById(hint).style.display='none';
}

function SelectType(typename)
{
	document.getElementById('sourcetype_' + typename).checked = 'true';
}