/*
Latin to Cyrillic Convertor version 4.84
Copyright (C) 1999 - 2004 Petko Yotov http://5ko.free.fr/

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


// ***  НЕ ПРОМЕНЯЙТЕ  ПО-НАДОЛУ, ОСВЕН АКО ЗНАЕТЕ КАКВО ПРАВИТЕ! ***
// ***  NE PROMENIAYTE PO-NADOLU, OSVEN AKO ZNAETE KAKVO PRAVITE! ***

// Същинският скрипт


function c5_vars()
{
	this.dni = 30;
	this.help = "templates/nastroiki4.html";


	var nav = navigator.userAgent;
	var v_o = nav.indexOf("Opera");
	if(v_o>-1)v_o = parseFloat(nav.substring(v_o+6, v_o+11));
	this.opera = v_o;

	var v_ie = nav.indexOf("MSIE");
	if(v_ie>-1 && v_o<0)v_ie = parseFloat(nav.substring(v_ie+5, v_ie+10));
	this.msie = v_ie;

	this.skip0 = "";
	this.skip1 = ""
		+ "\\[quote[^\\]]*\\](\\n|\\r|.)+?\\[\\/quote\\]"
		+ "|\\[code\\](\\n|\\r|.)+?\\[\\/code\\]"
		+ "|\\[b\\](\\n|\\r|.)+?\\[\\/b\\]"
		+ "|\\[url[^\\]]*\\](\\n|\\r|.)+?\\[\\/url\\]"
		+ "|\\[i\\](\\n|\\r|.)+?\\[\\/i\\]"
		+ "|\\[u\\](\\n|\\r|.)+?\\[\\/u\\]"
    + "|\\[img[^\\]]*\\](\\n|\\r|.)+?\\[\\/img\\]"
		+ "|\\[table\\](\\n|\\r|.)+?\\[\\/table\\]"
		+ "|\\[google\\](\\n|\\r|.)+?\\[\\/google\\]"
		+ "|\\[hide[^\\]]*\\](\\n|\\r|.)+?\\[\\/hide\\]"
		+ "|\\[list[^\\]]*\\](\\n|\\r|.)+?\\[\\/list\\]"
				
		+ "|\\[\/?([bui]|list[^\\]]*)\\]"
		+ "|\\[(smile|sad|cool|question|idea)\\]"
		+ "| [:;]\\S+" //smilies

		+ "|\\[font=[^\\]]+\\]|\\[\\/font\\]"
		+ "|\\[color=[^\\]]+\\]|\\[\\/color\\]"
		+ "|<link>[^<]+<\\/link>"
		+ "|<program>[^<]+<\\/program>";

	if(v_o <7.02 && v_ie < 5.5)
	{	this.skip1 = this.skip1.replace(/\.\+\?/g, "[^\\]]+");
		this.skip1 = this.skip1.replace(/\+\?/g, "+");
	}

	this.skip2 = "";

	this.skip3 = "\\b[a-z]{3,6}:\\/\\/\\S+\\b" // http://.+, ftp://
		+ "|\\bwww\\.\\S+\\b" // www.site.com
		+ "|\\b[a-z0-9\\._-]+@[a-z0-9\\.-]+\\b|\\t+"; //email

	this.ogra = "[]";
	this.def_azb = "a.b.w.g.d.e.v.z.i.j.k.l.m.n.o.p.r.s.t.u.f.h.c.ch.sh.sht.y.x.ju.ja...".split(".");

	this.rxs = new RegExp();
	this.rxs.compile(/([\^\|\*\(\)\[\]\{\}\.\+\?\$\/\\-])/);

	this.clearSep = new RegExp();
	this.clearSep.compile(this.skip1 + "|" + this.skip3, "gi");

   	this.azb = "абвгдежзийклмнопрстуфхцчшщъьюяыэё";
	this.def_q = "................................";

	this.q = "";
	this.azbuka = "";
	this.azb_frm = "";
	this.special = "";
	this.cnt = "";
	this.cyr = "";
	this.q_ogra = "";
	this.ogra_f = "";
	this.og1 = "";
	this.og2 = "";
	this.azblen = "";
	this._last = "";
	this.fullRegExp = "";

	return this;
}

c5 = new c5_vars();

function cyr5ko() { } //main container

function cyr5ko_controls(obj, names)
{
	var fn = -1;
	with (document)
	{	for(var f=0; f<forms.length; f++)
		{	for(var e=0; e<forms[f].length; e++)
			{	if(forms[f].elements[e].name.indexOf("cyr5ko") == 0) {fn = f; break;}
			}
			if(fn >= 0) break;
		}
	}
	if(fn < 0){	alert("Control element not found. Exiting."); return;	}

	var key = obj.options[obj.selectedIndex].value;
	var k = -1;
	if(key == 'toCyr') k = 0;
	if(key == 'toLat') k = 1;
	if(key == 'clearSep') k = 2;
	if(key == 'doubleSep') k = 3;

	var formfields = names.split(",");

	if(k >= 0)
	{
		for(var f = 0; f<formfields.length; f++)
		{
			eval("var d = document.forms[" + fn + "]."  + formfields[f] + ";");
			if(d && d.value.length>0) with(d){value = cyr5ko.transform(value, k);}
		}
	}
	else
	{
		if(key == 'selectall') eval("document.forms["+ fn +"]."+ formfields[0] +".select();");
		if(key == 'clearform')
			if(confirm('Наистина ли искате да изтриете всичко?'))
				eval("document.forms["+ fn +"]."+ formfields[0] +".value = '';");

		if(key == 'settings')
		{	var nastr = window.open(c5.help ,"nastr", "width=600,height=400,resizable,scrollbars,status");
			if(nastr) nastr.focus();
			else alert("За да ползвате тази функция, трябва да разрешите\nизскачащите прозорчета за тази страница.\n\n"
				+ "To use this function you must allow pop-up windows for this page.");
		}
		if(key == 'clearCookies') if(confirm('Желаете ли да изтриете настройките си?')){cyr5ko.clearQs(); cyr5ko.init();}
	}
	obj.selectedIndex = 0;
}

cyr5ko.transform = function(tx, command) // 0: toCyr; 1: toLat; 2: clearSep; 3: doubleSep
{
	if(tx.length == 0) return "";
	if(! command) command = 0;

	if(command < 2)cyr5ko.resave();

	var all = tx.match(c5.fullRegExp);
	if(all != null)	{ tx = tx.replace(c5.fullRegExp, "\t"); }

	if(command < 2) tx = cyr5ko.prevod(tx, command);
	if(all != null)
	{
		var tx_split = tx.split('\t');
		var tx2 = "";
		for(var j=0; j<tx_split.length; j++)
		{
			tx2 += tx_split[j];
			if(j < all.length)tx2 += (command >= 2)? cyr5ko.clearSep(all[j], command) : all[j];
		}
		return tx2;
	}
	else 	return tx;
}

cyr5ko.prevod = function(text, to_lat)
{
	for(var i=c5.azbuka.length-1; i>=0 ; i--)	//отзад напред - първо sht, после sh.

	{	if(c5.azbuka[i].length > 0 && c5.azbuka[i] != " ")
		{
			var _from = (to_lat)? c5.cyr[i] : c5.azbuka[i];
			var _to = (to_lat)? c5.azbuka[i] : c5.cyr[i];

         p = new RegExp(_from, "g");
			text = text.replace(  p, _to );
         p = new RegExp(_from, "gi");
			text = text.replace(  p, _to.toUpperCase() );
		}
	}
	return text;
}

// 2 funkcii ot K&R :))
cyr5ko.permute = function(a, b)
{	var c = c5.cnt[a];
	var d = c5.azbuka[a];
	var e = c5.cyr[a];

	c5.cnt[a] = c5.cnt[b];
	c5.azbuka[a] = c5.azbuka[b];
	c5.cyr[a] = c5.cyr[b];

	c5.cnt[b] = c;
	c5.azbuka[b] = d;
	c5.cyr[b] = e;
}
cyr5ko.quicksort = function(_left, _right)// K&R quicksort
{
	if (_left >= _right) return;
	cyr5ko.permute(_left, Math.floor((_left + _right) / 2));
	c5._last = _left;
	for (i=_left+1; i <=_right; i++)
		if (c5.cnt[i] < c5.cnt[_left]) cyr5ko.permute(++c5._last, i);
	cyr5ko.permute(_left, c5._last);
	cyr5ko.quicksort(_left, c5._last-1);
	cyr5ko.quicksort(c5._last+1, _right);
}

cyr5ko.setQ = function(ime, stoinost, dni)
{	if(dni == null)var ex = "";
	else
	{	var t = new Date();
		t.setTime(t.getTime() + (1000*60*60*24*dni));
		var ex = "; expires=" + t.toGMTString();
	}
	document.cookie = "" + ime + "=" + escape(stoinost) + ex;
}

cyr5ko.getQ = function(ime)
{
	var nachalo = document.cookie.indexOf(ime + "=");
		if (nachalo == -1) return "";
		// else
		var ind1 = nachalo + 1 + ime.length;
	ind2 = document.cookie.indexOf(";", ind1);
	if(ind2 == -1)ind2 = document.cookie.length;
	return unescape(document.cookie.substring(ind1, ind2));

}
cyr5ko.clearSep = function(tx, n)
{
	if (tx == null) return;
	var found = tx.match(c5.clearSep);
	if(found != null)	{ return tx; }

	if(n == 2)
	{	//tx = "clearsep: " + tx;
		var p1 = c5.og1 + "([^" + c5.og2 + "]*)" + c5.og2;
		var p = new RegExp(p1, "gi");
		tx = tx.replace(  p, "$1" );// + p.toString()
	}
	if(n == 3)
	{
		var p3 = "" + c5.og1 + c5.og1;
		var p4 = "" + c5.og2 + c5.og2;
		var p = new RegExp(p3, "gi");
		tx = tx.replace(  p, c5.og1.charAt(c5.og1.length - 1) );
		p = new RegExp(p4, "gi");
		tx = tx.replace(  p, c5.og2.charAt(c5.og2.length - 1) );
	}
	return tx;
}
cyr5ko.init = function ()
{
   c5.q = cyr5ko.getQ("c5ko4");

	// има ли промени в Кукито
   var tmp = (c5.q.length>0)? c5.q : c5.def_q;
   var user_settings = tmp.split(".");
	c5.azbuka = new Array();
	c5.azb_frm = new Array();

	var sj = 0;
	c5.special = new Array();

   c5.cnt = new Array();
   c5.cyr = new Array();

   c5.q_ogra = cyr5ko.getQ("o5ko");
   c5.ogra_f = (c5.q_ogra.length>1)? c5.q_ogra : c5.ogra;
   c5.og1 = cyr5ko.rxSafe(c5.ogra_f.charAt(0));
   c5.og2 = cyr5ko.rxSafe(c5.ogra_f.charAt(1));

   for(i=0; i<c5.def_azb.length; i++)
   {
      c5.azbuka[i] = (user_settings[i].length>0)? user_settings[i] : c5.def_azb[i];

		c5.azb_frm[i] = c5.azbuka[i];
		c5.cyr[i] = c5.azb.charAt(i);
		if(c5.azbuka[i].indexOf("/"))
		{
			var temparr = c5.azbuka[i].split("/");
			c5.azbuka[i] = temparr[0].length ? temparr[0] : c5.def_azb[i];
			for(k = 1; k<temparr.length; k++)
			{	c5.special[sj++] = new Array(c5.cyr[i], temparr[k]);
			}
		}
   }

	for(sj=0; sj<c5.special.length; sj++)
	{
		c5.cyr[i] = c5.special[sj][0];
		c5.azbuka[i] = c5.special[sj][1];
		i++;
	}

   c5.azblen = c5.azbuka.length;
   for(i=0; i<c5.azblen; i++)
   {	c5.cnt[i] = c5.azbuka[i].length;
		c5.azbuka[i] = cyr5ko.rxSafe(c5.azbuka[i]);
   }
   cyr5ko.quicksort(0, c5.azblen-1);

	c5.skip0  = "" + c5.og1 + c5.og1+ "|" + c5.og2 + c5.og2; // "[[" or "]]"
	c5.skip2 = "" + c5.og1+ "[^" + c5.og2 + "]*" + c5.og2 + ""; // [na latinica]
	c5.fullRegExp = new RegExp();
	c5.fullRegExp.compile(c5.skip0 + "|" + c5.skip1 + "|" + c5.skip2 + "|" + c5.skip3, "gi");
}

cyr5ko.resave = function()//за още (30) дни, само ако посетителят направи превод
{	if(c5.q) cyr5ko.setQ("c5ko4", c5.q, c5.dni);
	if(c5.q_ogra) cyr5ko.setQ("o5ko", c5.q_ogra, c5.dni);
}

cyr5ko.rxSafe = function(str)
{
	if(c5.opera>0) {if("a-^|*()[{]}.+?$/\\".indexOf(str)) return "\\" + str; return str;}
	return str.replace(c5.rxs, "\\$1");
}
cyr5ko.clearQs = function(){ cyr5ko.setQ("o5ko", '', -1); cyr5ko.setQ("c5ko4", '', -1);}
cyr5ko.whattime = function(){var t = new Date();return t.getTime();}// тест за скорост

cyr5ko.init();
