function popup(url){
	features = "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=490,height=500";
	window.open("/custom/scripts/popup.php?f="+url,"Nuotrauka",features);
}
function body_onload(){
	if (document.getElementById('isearch')){
		document.getElementById('isearch').focus();
	}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function swapPairs(s){
  var res = "";
  for (var i=0; i<s.length; i++){
    var ch = s.charCodeAt(i) ;
     res += String.fromCharCode(
                   ( ch & 0xF0 ) +
                   ((ch & 0x0C)>>2) +
                   ((ch & 0x03)<<2)
                   );
  }
  return res;
}

function getXmlURL(url,fn,json_obj){
     var req;
     if (window.XMLHttpRequest)
     {     req = new XMLHttpRequest();    
	 }
     else if (window.ActiveXObject)
    {    req = new ActiveXObject("Microsoft.XMLHTTP");    }
	if (req){
		req.onreadystatechange = function(){
			if (req.readyState == 4){
				fn(req.responseText);
			}
		};
		req.open('POST',url,true);
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.send(json_obj);
	//	fn(req.responseText);
	}else {
		return "Your browser does not seem to support XMLHttpRequest.";
	}
}

/** naujienos **/
function get_archive_news(a_type){
	getXmlURL('/custom/console/news.php?type='+a_type,put_content);
}

function get_archive_news_lite(a_type){
	getXmlURL('/custom/console/news.php?lite=1&type='+a_type,put_content);
}

function get_all_news_tags(){
	getXmlURL('/custom/console/newstags.php',put_tags_content);
}

function put_tags_content(content){
	if (document.getElementById('naujienuzymes')){
		document.getElementById('naujienuzymes').innerHTML = content;
	}	
}

function put_content(content){
	if (document.getElementById('archive_container')){
		document.getElementById('archive_container').innerHTML = content;
	}
}
/** end naujienos **/
/** komentarai **/
var comment_id; // globalus komentaro id
var cpage = 1;
var ctable = '';
var ctable_id = '';


function comments_init(ctable_, ctable_id_){
	ctable = ctable_;
	ctable_id = ctable_id_;
}

function comments_getlist(page){
	var post = 'page='+page+'&table='+ctable+'&table_id='+ctable_id;
	cpage = page;
	getXmlURL('/custom/console/comments.php?action=list&template=1',comments_refresh,post);
}

function comments_refresh(txt){
	if (document.getElementById('comments')){
		document.getElementById('comments').innerHTML = txt;
	}
}

function comment_rank_refresh(txt){
	if (document.getElementById('comment_rank_'+comment_id)){
		document.getElementById('comment_rank_'+comment_id).innerHTML = txt;
	}
}

function comment_reply_refresh(txt){
	if (document.getElementById('comment_reply_'+comment_id)){
		document.getElementById('comment_reply_'+comment_id).innerHTML = txt;
	}	
}

function comments_increaseRank(cid){
	var post = 'comment_id='+cid+'&increase=1'+'&table='+ctable+'&table_id='+ctable_id;
	comment_id = cid;
	getXmlURL('/custom/console/comments.php?action=rating',comment_rank_refresh,post);
}

function comments_decreaseRank(cid){
	var post = 'comment_id='+cid+'&decrease=1'+'&table='+ctable+'&table_id='+ctable_id;
	comment_id = cid;
	getXmlURL('/custom/console/comments.php?action=rating',comment_rank_refresh,post);
}

function comments_reply(cid){
	var post = 'comment_id='+cid+'&table='+ctable+'&table_id='+ctable_id;
	comment_id = cid;
	getXmlURL('/custom/console/comments.php?action=reply',comment_reply_refresh,post);
}

function comment_save(cid){
	var comment = '';
	var tratilele = '';
	
	if (cid>0){
		comment = document.getElementById('comment_'+cid).value;
		tratilele = document.getElementById('tratilele_'+cid).value;
	}else {
		comment = document.getElementById('comment_').value;
		tratilele = document.getElementById('tratilele_').value;
	}

	if (! comment) {
		alert('Įveskite komentarą');
	} else {
		var post = 'comment='+comment+'&parent_id='+cid+'&page='+cpage+'&table='+ctable+'&table_id='+ctable_id+'&tratilele='+tratilele;
		getXmlURL('/custom/console/comments.php?action=add',comments_refresh,post);
	}
}
function comment_close(cid){
	if (document.getElementById('comment_reply_'+cid)){
		document.getElementById('comment_reply_'+cid).innerHTML = '';
	}
}

function comments_sort(){
	var rb1 = document.getElementById('comment_radb1');
	var rb2 = document.getElementById('comment_radb2');
	var post = '&table='+ctable+'&table_id='+ctable_id;
	if (rb1.checked){
		post += '&sort=asc'
	}
	getXmlURL('/custom/console/comments.php?action=sort',comments_refresh,post);
}

function comments_delete(cid){
	comment_id = cid;
	var post = 'comment_id='+cid+'&table='+ctable+'&table_id='+ctable_id;
	getXmlURL('/custom/console/comments.php?action=delete',comments_refresh,post);
}

function comments_give_warning(cid){
	comment_id = cid;
	var post = 'comment_id='+cid+'&table='+ctable+'&table_id='+ctable_id;
	getXmlURL('/custom/console/comments.php?action=give_warning',comments_refresh,post);
}


function comments_report(cid){
	comment_id = cid;
	var post = 'comment_id='+cid+'&table='+ctable+'&table_id='+ctable_id;
	getXmlURL('/custom/console/comments.php?action=report',comments_refresh,post);
}


function toggleCommentDisplay(obj, sp)
{
	if (obj.style.display == 'none') {
		obj.style.display = 'inline';
		sp.innerHTML = 'Slėpti';
	} else {
		obj.style.display = 'none';
		sp.innerHTML = 'Rodyti';
	}
}

/** end komentarai **/

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// --- calendar control

function showCalendar(content)
{	if (document.getElementById('calendarLayer')) {
		document.getElementById('calendarLayer').innerHTML = content;
	}
} 

function updateCalendar(dateLink)
{
	getXmlURL('/custom/console/calendar.php?mdate=' + dateLink, showCalendar, dateLink);
}
function updateCalendarExactDate(dateLink)
{
	getXmlURL('/custom/console/calendar.php?ddate=' + dateLink, showCalendar, dateLink);
}

function showNewsByDate(link)
{
	getXmlURL(link, updateNewsArchiveList, link);
} 

function updateNewsArchiveList(content)
{
	if (document.getElementById('kaire2')) {
		document.getElementById('kaire2').innerHTML = content;
	}
}
// --- end of calendar control
