var user_id;
var d=document;
var fp_id=0;

function clearChild(el){
	while(el.childNodes[0])el.removeChild(el.firstChild)
	}

function ce(el){
	return(d.createElement(el))
	}

function ac(el,ch){
	el.appendChild(ch);
	return(el)
	}
function ctn(txt){
	return(d.createTextNode(txt))
	}

function sa(el,atr,val){
	el.setAttribute(atr,val);
	return(el)
	}

function unescapeHTML(str) //paartaisa &amp; par &
	{
	var div=ce('div');
	div.innerHTML=str;
	return div.childNodes[0].nodeValue
	}
	
function wordWrap(r,m,b,c){
	var i,j,s;
	if(m>0) {
		for(s=r,r=""; s.length>m; j=c ? m:(j=s.substr(0, m).match(/\S*$/)).input.length-j[0].length	|| j.input.length+(j=s.substr(m).match(/^\S*/)).input.length+j[0].length,r+=s.substr(0,j)+((s=s.substr(j)).length ? b:""));
		r+=s;
		}
	return r
	}
function wordWrap2(what,ttt,letters)
	{
	if (letters!=0)
		{
		bbb=wordWrap(what,letters,'ζ',true);
		bbb=bbb.split("ζ");
		for(bba in bbb)
			{
			ac(ttt,ctn(bbb[bba]));
			if (bbb[bba].length==letters)	ac(ttt,ce('wbr'))
			}
		}
	else ac(ttt,ctn(what))
	}

function add_links(where,sourc,letters)
	{
 	rr=sourc.split(' ');
	var urlRegex = /\b(https?:\/\/[^\s\"\<\>\(\)]+)/ig;
	for (kk in rr)
		{
		urlRegex.exec('');
		if (match=urlRegex.exec(rr[kk]))
			{
			elmLink = ce('a'); 
			wordWrap2(match[0].substring(7,match[0].length),sa(sa(sa(elmLink,'href',match[0]),'title',match[0]),'target','_blank'),letters);
			
			wordWrap2(RegExp.leftContext,where,letters);
			
			wordWrap2(RegExp.rightContext,ac(where,elmLink),letters);
			}
		else wordWrap2(rr[kk],where,letters);
		ac(where,ctn(' '))
		}
	}


var utf_8=[
	"aā", "eē", "iī", "uū", "ļļ", "ņņ", 
	"Ā", "Č", "Ē", "Ģ", "Ī", "Ķ", "Ļ", "Ņ", "Š", "Ū", "Ž",
	"ā", "č", "ē", "ģ", "ī", "ķ", "ļ", "ņ", "š", "ū", "ž"
	];

var exceptions=[ /^([ps]aa[^a]|nee[^\be])/i , 3,
				/^[mb]iljon/i, 6,
				/^[mb]iljard/i, 7,
				/buljon/i, 6,
				/(^|[^ghdr])iee[^\be]/i,3,
				/sheem/i, 2,
				/shem/i, 3,
				/vakuum/i,6
	  ];

var tl_prog="JS/translit";

var default_encoding="utf_8";
var debug=false;


function decodeChars(in_string,cp_reference)
{
	sym_count=cp_reference.length;
	result=in_string;
	
	if (typeof(in_string)!="string")
	{
		if (debug)
			alert(tl_prog+"error decodeChars: non-string given");
		return null
	}
	
	for (counter=0;counter< sym_count; counter++)
		result=result.replace(translit[counter],cp_reference[counter]);
	return result
}

function decodeTranslit(input_string,encoding)
{
	var URI_definition=/((https?|ftp|telnet):\/\/|mailto:)[A-z0-9%:@#=_\/\.\?\+\-]+/;
	var latinword = /[qwxy]/i;

	var cp_reference=eval(encoding);
	
	if (cp_reference == null)
	{
		if (debug) alert(tl_prog+" error: Encoding "+encoding+" not found!");
		return null
	}
	if (cp_reference.length != translit.length)
	{
		if (debug) alert(tl_prog+" error: Encoding "+encoding+" has different "+
			"size than the translit table, not all chars defined to be changed!");
		return null
	}
	var words=input_string.split(" ");
	var wc=words.length;
	var excount=exceptions.length;
	var dontAnalyze;
	var changeStart,changeEnd;
	var result= new Array();
	for (i=0; i< wc; i++)
	{
		resultword="";
		dontAnalyze = new Array();
		if (words[i].search(URI_definition)==-1 && words[i].search(latinword)== -1)
		{

			if (debug) alert("Analyzing: "+words[i]);
			for (j=0;j<excount;j+=2)
			{
				excplace=words[i].search(exceptions[j]);
				if (excplace != -1)
				{
					if (debug) alert("Found exception "+exceptions[j]+" length: "+exceptions[j+1]);
					dontAnalyze.push(excplace, excplace+exceptions[j+1])
				}
			}
			var daCount=dontAnalyze.length;
			changeStart=0;
			changeEnd=words[i].length;
			if (daCount > 0)
			{
				if (debug) alert("Found "+(daCount / 2)+" exceptions in "+words[i]+"\n"+dontAnalyze.join(","));
				resultword+=decodeChars(words[i].slice(0,dontAnalyze[0]),cp_reference);
				for (j=0; j<daCount; j+=2)
				{
					tmpvar1=words[i].slice(dontAnalyze[j],dontAnalyze[j+1]);
					if (debug) alert("processing exception, adding "+tmpvar1);
					resultword+=tmpvar1;
					changeStart=dontAnalyze[j+1];
					if (j+2 >= daCount)
					{
						changeEnd=words[i].length;
						break
					}
					else
						changeEnd=dontAnalyze[j+2];
					tmpvar1=words[i].slice(changeStart,changeEnd);
					if (debug) alert("exception: adding processed "+tmpvar1);
					resultword+=decodeChars(tmpvar1,cp_reference)
				}
			}
			tmpvar1=words[i].slice(changeStart,changeEnd);
			if (debug) alert("adding the rest of the word:"+tmpvar1);
			resultword+=decodeChars(tmpvar1,cp_reference)
		}
		else
			resultword=words[i];
		result.push(resultword)
	}
	return result.join(" ")
}


function doTranslit()
{
	if (typeof(arguments)!="object")
	{
		alert(tl_prog+" no object passed to doTranslit()");
		return false
	}
	if (arguments.length < 1)
	{
		alert(tl_prog+" too few arguments to doTranslit");
		return false
	}
	var encoding=(arguments.length > 1 ? arguments[1] : default_encoding);
	var input_object=arguments[0];
	
	if (! (input_object.type=="text" || input_object.type=="textarea"))
	{
		alert(tl_prog+" non-text form element passed to doTranslit? Can't "+
			" get value of "+input_object);
		return false
	}
	
	var result=decodeTranslit(input_object.value,encoding);
	
	if (debug) alert(tl_prog+" final result, got: "+result);
	input_object.value=result;
	
	return false
}

function externalLinks() {
 if (!d.getElementsByTagName) return;
 var anchors = d.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       (anchor.getAttribute("rel") == "nofollow" || anchor.getAttribute("rel") == "follow")
		&& anchor.getAttribute("rel") != "int")
     anchor.target = "_blank"
 }
}

function stils(el,stils)
	{
	sa(el,'style',stils);
	el.style.cssText=stils
	}

function sms() {
	if (d.getElementById("sms_length"))
		{
		smslength=d.getElementById("sms_length");
		ac(smslength,ctn('('+(d.getElementById('sms_text').value.length+d.getElementById('sign').value.length)+' no 800)'))
  	}
}

function ikt() {
	if (d.getElementById("ikt_length"))
		{
		smslength=d.getElementById("ikt_length");
		ac(smslength,ctn('('+(d.getElementById('zina_ir-ko-teikt').value.length)+' no 250)'))
  	}
}

function arrow(u){
  if(!u){
    if(window.event ){
      u = window.event;
      targ=u.srcElement
    }else{
      return
    }
  }
  else targ=u.target;
  if(typeof(u.keyCode)=='number') u=u.keyCode;
  else
  	if(typeof(u.which)=='number') u=u.which;
 		else if(typeof(u.charCode)=='number') u=u.charCode;
  else return;
  if (targ.id=="chatbarText")
  	switch (u){
  		case 38:
  		if (hist.length!=0){
  			hn++;
  			if (hn>hist.length) hn=1;
  			d.getElementById("chatbarText").value=hist[hist.length-hn]
  		}
  		break;
  		case 40:
  		if (hn!=0){
  			hn--;
  			if (hn==0) hn=hist.length;
  			d.getElementById("chatbarText").value=hist[hist.length-hn]
  		}
	  	break;
	  	case 13:
  		if (d.getElementById("chatbarText").value) sendComment();
	  	break;
	  	default:
	  	hn=0;
  	}
  if (targ.id=="sms_text" || targ.id=="sign")
  	{
  	smsbody=d.getElementById('sms_text').value;
  	sign=d.getElementById('sign').value.length;
  	if (smsbody.length+sign>800) d.getElementById('sms_text').value=smsbody.substring(0,800-sign);
  	smslength=d.getElementById("sms_length");
  	smslength.removeChild(smslength.firstChild);
  	ac(smslength,ctn('('+(d.getElementById('sms_text').value.length+sign)+' no 800)'))
  	}
  if (targ.id=="zina_ir-ko-teikt")
  	{
  	iktbody=d.getElementById('zina_ir-ko-teikt').value;
  	if (iktbody.length>250) d.getElementById('zina_ir-ko-teikt').value=iktbody.substring(0,250);
  	iktlength=d.getElementById("ikt_length");
  	iktlength.removeChild(iktlength.firstChild);
  	ac(iktlength,ctn('('+(d.getElementById('zina_ir-ko-teikt').value.length)+' no 250)'))
  	}
	}
function enter(){
	sendComment();
	return(false)
	}
	
	
	
function comment_urls()
	{
	if (d.getElementById('com_fieldset'))
		{
		elem = d.getElementById('com_fieldset').getElementsByTagName('p');
		for(i in elem)
			{
			if (elem[i].id!=null && elem[i].id.substr(0,3)=='com')
				{
				
				kr=elem[i].childNodes;
				de=kr.length;
				for(c=0;c<de;c++)
					{
					if (kr[c].nodeName=='#text')
						{
						span=ce('span');
						add_links(span,kr[c].nodeValue,0);
							{
							elem[i].insertBefore(span,kr[c].nextSibling);
							elem[i].removeChild(kr[c])
							}
						}
					}
				}
			}
		} 
	}
	
function ikt_urls()
	{
	if (d.getElementById('ikt_box'))
		{
		elem = d.getElementById('ikt_box').getElementsByTagName('strong');
		for(i in elem)
			{
			if (elem[i].id!=null)
				{
				kr=elem[i].childNodes;
				de=kr.length;
				for(c=0;c<de;c++)
					{
					if (kr[c].nodeName=='#text')
						{
						span=ce('span');
						add_links(span,kr[c].nodeValue,0);
							{
							elem[i].insertBefore(span,kr[c].nextSibling);
							elem[i].removeChild(kr[c])
							}
						}
					}
				}
			}
		} 
	}
	

	
function load_ajax()
	{
	if(typeof getHTTPObject == 'function')
		{
		if(typeof init_new_mail == 'function') init_new_mail();
		if(typeof highlight == 'function') highlight();
		if(typeof checkName == 'function') checkName();
		if(typeof receiveChatText == 'function') 
			{
			receiveChatText();
			sa(d.forms['chatForm'].elements['chatbarText'],'autocomplete','off');
			clearChild(d.getElementById("chat_load"))
			}
		if(typeof receiveUO == 'function') receiveUO();
		if(typeof rA == 'function') rA()
		}
	else setTimeout('load_ajax();',750)
	}
	
function hide_buttons()
	{
	if (d.getElementById('js_hide_button'))stils(d.getElementById(('js_hide_button')),'display:none;z-index:0')
	}
function upd_url(id1,form1)
	{
	form1.action=form1.action+'?place='+id1
	}

function font_size()
	{
	if (screen.width<1024) 
		{
		htmltag=d.getElementsByTagName('html');
		stils(htmltag[0],'font-size:68.75%')
		}
	}

var shjd=true;	
function shj()
	{
	if (d.getElementById('shj'))
		{
		stils(d.getElementById('shj'),'height:0');
		d.getElementById('share_joy_txt').onfocus=function(){if (shjd) for(var a=1;a<100;a++) setTimeout("stils(d.getElementById('shj'),('height:"+a+"px'));",a*10);shjd=false};
		}
	}
var shrinksms;
var shsmsd=true;	
function shsms()
	{
	if (d.getElementById('shsms') && !shrinksms)
		{
		stils(d.getElementById('shsms'),'height:0');
		d.getElementById('ph').onfocus=function(){if (shsmsd) for(var a=1;a<170;a++) setTimeout("stils(d.getElementById('shsms'),('height:"+a+"px'));",a*10);shsmsd=false};
		d.getElementById('sms_text').onfocus=function(){if (shsmsd) for(var a=1;a<170;a++) setTimeout("stils(d.getElementById('shsms'),('height:"+a+"px'));",a*10);shsmsd=false};
		}
	}

function addtosms()
	{
		//alert (d.getElementById('pantins').text);
	var pp=d.getElementById('pantins');
	var pr=d.getElementById('sms_text');
	for( var x = 0; pp.childNodes[x]; x++ ) if (pp.childNodes[x].nodeType==3) 
			{
			ac(pr,ctn(pp.childNodes[x].nodeValue));
			ac(pr,ctn(' '))
			}
  smsbody=pr.value;
  sign=d.getElementById('sign').value.length;
  if (smsbody.length+sign>800) d.getElementById('sms_text').value=smsbody.substring(0,800-sign);
  smslength=d.getElementById('sms_length');
  smslength.removeChild(smslength.firstChild);
  ac(smslength,ctn('('+(pr.value.length+sign)+' no 800)'));
  keepattention(true)
	}	
		
function keepattention(on)
	{
	if (on &&	shsmsd) 
		{
		stils(d.getElementById('sms_text'),'color:#f00;');
		setTimeout('keepattention(false)',500)
		}
	else 
		{
		stils(d.getElementById('sms_text'),'color:#000;');
		setTimeout('keepattention(true)',500)
		}
	}

		
function loadfunctions()
	{
	shj();
	shsms();
	font_size();
	hide_buttons();
	externalLinks();
	sms();
	//ikt();
	load_ajax();
	//ikt_urls();
	//comment_urls();
	if(typeof showcal == 'function') 
		{
		if (d.getElementById('cal')) showcal(ct,'cal');
		if (d.getElementById('cal1')) showcal(ct,'cal1');
		if (d.getElementById('cal2')) showcal(ct,'cal2')
		}
	}


window.onload = loadfunctions;
d.onkeyup=arrow;
