var n_cache        = new Array();
var c_cache        = new Array();
var comm_id;
var comm_edit_id;
var s_id;
var e_id;

function IPMenu( m_ip, l1, l2, l3 ){

var menu=new Array();

menu[0]='<a href="http://www.nic.ru/whois/?ip=' + m_ip + '" target="_blank">' + l1 + '</a>';
menu[1]='<a href="' + dle_root + dle_admin + '?mod=iptools&ip=' + m_ip + '" target="_blank">' + l2 + '</a>';
menu[2]='<a href="' + dle_root + dle_admin + '?mod=blockip&ip=' + m_ip + '" target="_blank">' + l3 + '</a>';

return menu;
};

function ajax_cancel_for_edit( news_id )
{
	if ( n_cache[ news_id ] != "" )
	{
		document.getElementById( 'news-id-'+news_id ).innerHTML = n_cache[ news_id ];
	}
	
	return false;
};

function whenCompletedSave(){
n_cache[ e_id ] = '';
};

function ajax_save_for_edit( news_id, event )
{
	var ajax = new dle_ajax();
	var allow_br = 0;

	if (document.getElementById('allow_br_'+news_id).checked) { allow_br = 1; }

	e_id = news_id;
	ajax.onShow ('');
	var news_txt = ajax.encodeVAR( document.getElementById('dleeditnews'+news_id).value );
	var news_title = ajax.encodeVAR( document.getElementById('edit-title-'+news_id).value );
	var varsString = "news_txt=" + news_txt;
	ajax.setVar("id", news_id);
	ajax.setVar("allow_br", allow_br);
	ajax.setVar("title", news_title);
	ajax.setVar("field", event);
	ajax.setVar("action", "save");
	ajax.requestFile = dle_root + "engine/ajax/editnews.php";
	ajax.method = 'POST';
	ajax.element = 'news-id-'+news_id;
	ajax.onCompletion = whenCompletedSave;
	ajax.sendAJAX(varsString);

	return false;
};

function whenCompleted(){

	var post_main_obj = document.getElementById( 'news-id-' + s_id );
	var post_box_top  = _get_obj_toppos( post_main_obj );

			if ( post_box_top )
			{
				scroll( 0, post_box_top - 70 );
			}

};

function ajax_prep_for_edit( news_id, event )
{
	if ( ! n_cache[ news_id ] || n_cache[ news_id ] == '' )
	{
		n_cache[ news_id ] = document.getElementById( 'news-id-'+news_id ).innerHTML;
	}

	var ajax = new dle_ajax();
	s_id = news_id;
	ajax.onShow ('');
	var varsString = "";
	ajax.setVar("id", news_id);
	ajax.setVar("field", event);
	ajax.setVar("action", "edit");
	ajax.requestFile = dle_root + "engine/ajax/editnews.php";
	ajax.method = 'GET';
	ajax.element = 'news-id-'+news_id;
	ajax.execute = true;
	ajax.onCompletion = whenCompleted;
	ajax.sendAJAX(varsString);

	return false;
};


function whenCompletedCommentsEdit(){

	var post_main_obj = document.getElementById( 'comm-id-' + comm_id );
	var post_box_top  = _get_obj_toppos( post_main_obj );

			if ( post_box_top )
			{
				scroll( 0, post_box_top - 70 );
			}

};

function MenuCommBuild( m_id ){

var menu=new Array();

menu[0]='<a onclick="ajax_comm_edit(\'' + m_id + '\'); return false;" href="#">' + menu_short + '</a>';
menu[1]='<a href="' + dle_root + '?do=comments&action=comm_edit&id=' + m_id + '">' + menu_full + '</a>';

return menu;
};

function ajax_comm_edit( c_id )
{
	if ( ! c_cache[ c_id ] || c_cache[ c_id ] == '' )
	{
		c_cache[ c_id ] = document.getElementById( 'comm-id-'+c_id ).innerHTML;
	}

	var ajax = new dle_ajax();
	comm_id = c_id;
	ajax.onShow ('');
	var varsString = "";
	ajax.setVar("id", c_id);
	ajax.setVar("action", "edit");
	ajax.requestFile = dle_root + "engine/ajax/editcomments.php";
	ajax.method = 'GET';
	ajax.element = 'comm-id-'+c_id;
	ajax.execute = true;
	ajax.onCompletion = whenCompletedCommentsEdit;
	ajax.sendAJAX(varsString);
	return false;
};

function ajax_cancel_comm_edit( c_id )
{
	if ( n_cache[ c_id ] != "" )
	{
		document.getElementById( 'comm-id-'+c_id ).innerHTML = c_cache[ c_id ];
	}
	
	return false;
};

function whenCompletedSaveComments(){
c_cache[ comm_edit_id ] = '';
}

function ajax_save_comm_edit( c_id )
{
	var ajax = new dle_ajax();
	comm_edit_id = c_id;
	ajax.onShow ('');
	var comm_txt = ajax.encodeVAR( document.getElementById('dleeditcomments'+c_id).value );
	var varsString = "comm_txt=" + comm_txt;
	ajax.setVar("id", c_id);
	ajax.setVar("action", "save");
	ajax.requestFile = dle_root + "engine/ajax/editcomments.php";
	ajax.method = 'POST';
	ajax.element = 'comm-id-'+c_id;
	ajax.onCompletion = whenCompletedSaveComments;
	ajax.sendAJAX(varsString);

	return false;
};

function doFavorites( fav_id, event )
{
	var ajax = new dle_ajax();
	ajax.onShow ('');
	var varsString = "fav_id=" + fav_id;
	ajax.setVar("action", event);
	ajax.setVar("skin", dle_skin);
	ajax.requestFile = dle_root + "engine/ajax/favorites.php";
	ajax.method = 'GET';
	ajax.element = 'fav-id-'+fav_id;
	ajax.sendAJAX(varsString);

	return false;
};

function CheckLogin()
{
	var ajax = new dle_ajax();
	var name = ajax.encodeVAR( document.getElementById('name').value );
	ajax.onShow ('');
	var varsString = "name=" + name;
	ajax.requestFile = dle_root + "engine/ajax/registration.php";
	ajax.method = 'POST';
	ajax.element = 'result-registration';
	ajax.sendAJAX(varsString);

	return false;
};

function doCalendar(month, year){
	var ajax = new dle_ajax();
	ajax.onShow ('');
	var varsString = "";
	ajax.setVar("year", year);
	ajax.setVar("month", month);
	ajax.requestFile = dle_root + "engine/ajax/calendar.php";
	ajax.method = 'GET';
	ajax.element = 'calendar-layer';
	ajax.sendAJAX(varsString);
};

function ShowBild(sPicURL) {
window.open(dle_root + 'engine/modules/imagepreview.php?image='+sPicURL, '', 'resizable=1,HEIGHT=200,WIDTH=200, top=0, left=0, scrollbars=yes');
};

function doRate( rate, id ) {
	var ajax = new dle_ajax();
	ajax.onShow ('');
	var varsString = "go_rate=" + rate;
	ajax.setVar("news_id", id);
	ajax.setVar("skin", dle_skin);
	ajax.requestFile = dle_root + "engine/ajax/rating.php";
	ajax.method = 'GET';
	ajax.element = 'ratig-layer';
	ajax.sendAJAX(varsString);
};

function dleRate( rate, id ) {
	var ajax = new dle_ajax();
	ajax.onShow ('');
	var varsString = "go_rate=" + rate;
	ajax.setVar("news_id", id);
	ajax.setVar("skin", dle_skin);
	ajax.setVar("mode", "short");
	ajax.requestFile = dle_root + "engine/ajax/rating.php";
	ajax.method = 'GET';
	ajax.element = 'ratig-layer-' + id;
	ajax.sendAJAX(varsString);
};

function UpdateNews(id) {
		var ajax = new dle_ajax();

		ajax.onShow ('Обновление времени новости...');
		var varsString = "";
		ajax.setVar("news_id", id);
		ajax.requestFile = "/engine/ajax/updatenews.php";
		//alert(ajax.requestFile);
		ajax.method = 'GET';
		//ajax.element = 'thanks-layer';
		ajax.sendAJAX(varsString);
}

function doAddComments(){

	var form = document.getElementById('dle-comments-form');
		var dle_comments_ajax = new dle_ajax();

	if (dle_wysiwyg == "yes") {
	document.getElementById('comments').value = oEdit1.getXHTMLBody();
		dle_comments_ajax.setVar("editor_mode", 'wysiwyg');
	} else {

	closeall();

	}

	if (form.comments.value == '' || form.name.value == '')
	{
		alert ( dle_req_field );
		return false;
	}

	dle_comments_ajax.onShow ('');
	var varsString = "post_id=" + form.post_id.value;
	dle_comments_ajax.setVar("comments", dle_comments_ajax.encodeVAR(form.comments.value));
	dle_comments_ajax.setVar("name", dle_comments_ajax.encodeVAR(form.name.value));
	dle_comments_ajax.setVar("mail", dle_comments_ajax.encodeVAR(form.mail.value));
	dle_comments_ajax.setVar("skin", dle_skin);

	if ( form.sec_code ) {

		 dle_comments_ajax.setVar("sec_code", form.sec_code.value);

		}

	dle_comments_ajax.requestFile = dle_root + "engine/ajax/addcomments.php";
	dle_comments_ajax.method = 'POST';
	dle_comments_ajax.execute = true;
	dle_comments_ajax.element = 'dle-ajax-comments';
	dle_comments_ajax.sendAJAX(varsString);

};

function dle_copy_quote(qname) 
{
 dle_txt='';

	if (document.getSelection) 
	{
	 dle_txt=document.getSelection();
	}
	else if (document.selection) 
	{
	 dle_txt=document.selection.createRange().text;
	}
	if (dle_txt.replace(" ","") != "")
	{
	 dle_txt='[quote='+qname+']'+dle_txt+'[/quote]\n';
	}
};

function dle_ins(name) 
{
var input=document.getElementById('dle-comments-form').comments;
var finalhtml = "";

	if (dle_wysiwyg == "no") {
		if (dle_txt!= "") {
			input.value += dle_txt;
		}
		else { 
			input.value += "[b]"+name+"[/b],"+"\n";
		}
	} else {
		if (dle_txt!= "") {
			finalhtml = dle_txt;
		}
		else { 
			finalhtml = "<b>"+name+"</b>,"+"<br />";
		}	
	oUtil.obj.focus();	
	oUtil.obj.insertHTML(finalhtml);
	}

};

function SpoilerShowOrHide(d1, d2) {
	if (d1 != ''){
			DoDiv(d1);
	}

	if (d2 != ''){
			DoDiv(d2);
	}
};

function ShowOrHide(d1) {
		if (d1 != '') DoDiv(d1);
};

function DoDiv(id) {
		var item = null;
		if (document.getElementById) {
		item = document.getElementById(id);
		} else if (document.all){
		item = document.all[id];
		} else if (document.layers){
		item = document.layers[id];
		}
		if (!item) {
		}
		else if (item.style) {
		if (item.style.display == "none"){ item.style.display = ""; }
		else {item.style.display = "none"; }
		}else{ item.visibility = "show"; }
};

function ckeck_uncheck_all() {
		var frm = document.pmlist;
		for (var i=0;i<frm.elements.length;i++) {
				var elmnt = frm.elements[i];
				if (elmnt.type=='checkbox') {
						if(frm.master_box.checked == true){ elmnt.checked=false; }
						else{ elmnt.checked=true; }
				}
		}
		if(frm.master_box.checked == true){ frm.master_box.checked = false; }
		else{ frm.master_box.checked = true; }
};

function confirmDelete(url){

		var agree=confirm( dle_del_agree );

		if (agree)
		document.location=url;
};

function NewsDelete(url){

		var agree=confirm( dle_del_agree2 );

		if (agree)
		document.location=url;
};

function dle_smiley ( text ){
	doInsert(' ' + text + ' ', '', false);

	document.getElementById('dle_emo').style.visibility = "hidden";
	document.getElementById('dle_emo').style.display    = "none";
	ie_range_cache = null;
};

function DlePageCompleted(){
	hideBusyLayer();
	var post_main_obj = document.getElementById( 'dle-content' );
	var post_box_top  = _get_obj_toppos( post_main_obj );

			if ( post_box_top )
			{
				scroll( 0, post_box_top - 70 );
			}

};

function DlePage( varsString ){
	showBusyLayer();
	if (document.getElementById( 'dle-info' ).innerHTML != '') {
	document.getElementById( 'dle-info' ).innerHTML = '';}

	var ajax = new dle_ajax();
	ajax.onShow ('');
	ajax.requestFile = dle_root + "engine/ajax/pages.php";
	ajax.method = 'GET';
	ajax.execute = true;
	ajax.element = 'dle-content';
	ajax.onCompletion = DlePageCompleted;
	ajax.sendAJAX(varsString);

};

function setNewField(which, formname)
{
						if (which != selField)
						{
				allcleartags();
				fombj    = formname;
								selField = which;

						}
};

function UserMenu( url, m_id, group, uname){

var menu=new Array();

	menu[0]='<a ' + url +' >' + menu_profile + '</a>';

	menu[1]='<a href="' + dle_root + 'index.php?do=pm&doaction=newpm&user=' + m_id + '">' + menu_send + '</a>';
	menu[2]='<a href="' + dle_root + 'index.php?do=lastcomments&userid=' + m_id + '">' + menu_fcomments + '</a>';
	if (group == '1') {
		menu[3]='<a onclick="window.open(\'' + dle_root + dle_admin + '?mod=editusers&action=edituser&id=' + m_id + '\', \'User\',\'toolbar=0,location=0,status=0, left=0, top=0, menubar=0,scrollbars=yes,resizable=0,width=540,height=500\'); return false;" href="#">' + menu_uedit + '</a>';
	}

return menu;
};

function UserNewsMenu( url, news_url, m_id, group, uname ){

var menu=new Array();

	menu[0]='<a ' + url +' >' + menu_profile + '</a>';
	menu[1]='<a ' + news_url +' >' + menu_fnews + '</a>';

	menu[2]='<a href="' + dle_root + 'index.php?do=pm&doaction=newpm&username=' + m_id + '">' + menu_send + '</a>';
	//menu[3]='<a href="' + dle_root + 'index.php?do=repa-stats&user=' + uname + '">Репутация пользователя</a>';
	if (group == '1') {
		menu[3]='<a onclick="window.open(\'' + dle_root + dle_admin + '?mod=editusers&action=edituser&user=' + m_id + '\', \'User\',\'toolbar=0,location=0,status=0, left=0, top=0, menubar=0,scrollbars=yes,resizable=0,width=540,height=500\'); return false;" href="#">' + menu_uedit + '</a>';
	}

return menu;
};

function tag_url_download()
{
		var FoundErrors = '';
		var thesel ='';
		if ( (ua_vers >= 4) && is_ie && is_win)
		{
		thesel = document.selection.createRange().text;
		} else thesel ='Скачать | Download';

		if (!thesel) {
				thesel ='Скачать | Download';
		}

		var enterURL   = prompt(text_enter_url, "http://");
		var enterTITLE = prompt(text_enter_url_name, thesel);

		if (!enterURL) {
				FoundErrors += " " + error_no_url;
		}
		if (!enterTITLE) {
				FoundErrors += " " + error_no_title;
		}

		if (FoundErrors) {
				alert("Error!"+FoundErrors);
				return;
		}

		doInsert("[b][center][url="+enterURL+"]"+enterTITLE+"[/url][/center][/b]", "", false);
}

function tag_url_mirror()
{
		var FoundErrors = '';
		var thesel ='';
		if ( (ua_vers >= 4) && is_ie && is_win)
		{
		thesel = document.selection.createRange().text;
		} else thesel ='Зеркало | Mirror';

		if (!thesel) {
				thesel ='Зеркало | Mirror';
		}

		var enterURL   = prompt(text_enter_url, "http://");
		var enterTITLE = prompt(text_enter_url_name, thesel);

		if (!enterURL) {
				FoundErrors += " " + error_no_url;
		}
		if (!enterTITLE) {
				FoundErrors += " " + error_no_title;
		}

		if (FoundErrors) {
				alert("Error!"+FoundErrors);
				return;
		}

		doInsert("[b][center][url="+enterURL+"]"+enterTITLE+"[/url][/center][/b]", "", false);
}

function tag_password()
{
		var FoundErrors = '';

		var enterURL   = prompt(text_enter_url, "");

		if (!enterURL) {
				FoundErrors += " " + error_no_url;
		}

		if (FoundErrors) {
				alert("Error!"+FoundErrors);
				return;
		}

		doInsert("[hide][b][center][COLOR=#FF0000]Пароль | Password: "+enterURL+"[/COLOR][/center][/b][/hide]", "", false);
}

function GetLink(id)
{
var linkimg = document.getElementById("getlinkimg_"+id);
var state = toggleimg(linkimg);

if(state == 1){
	//document.getElementById("link_"+id).innerHTML = "";
	//document.getElementById("link_"+id).style.visibility = "hidden";
	document.getElementById("link_"+id).style.display = "none";
} else {
	//document.getElementById("link_"+id).innerHTML = "asdasda2";
	//document.getElementById("link_"+id).style.visibility = "visible";
	document.getElementById("link_"+id).style.display = "";
}

}
function toggleimg(img)
{
	img_re = new RegExp("_collapsed\\.gif$");
	if (img.src.match(img_re))
	{
		img.src = img.src.replace(img_re, '.gif');
		return 1;
	}
	else
	{
		img_re = new RegExp("\\.gif$");
		img.src = img.src.replace(img_re, '_collapsed.gif');
		return 0;
	}
}

function SimilarNewsMenu( a )
{
alert(a[0]);
var menu=new Array();

for( i=0; i < sizeof(a); i++) {
	menu[i] = '<a href="?mod=editnews&action=editnews&id=' + a[i][0] + '" target="_blank">' + a[i][1] + '</a>';
}
return menu;
};
function dle_news_delete( m_id ){

		var agree=confirm( dle_del_agree );

		if (agree)
		document.location= dle_root + 'index.php?do=deletenews&id=' + m_id + '&hash=' + dle_login_hash;
};

function MenuNewsBuild( m_id, event ){

var menu=new Array();

menu[0]='<a onclick="ajax_prep_for_edit(\'' + m_id + '\', \'' + event + '\'); return false;" href="#">' + menu_short + '</a>';
menu[1]='<a href="' + dle_root + dle_admin + '?mod=editnews&action=editnews&id=' + m_id + '" target="_blank">' + menu_full + '</a>';

if (allow_dle_delete_news) {

menu[2]='<a onclick="dle_news_delete (\'' + m_id + '\'); return false;" href="#">' + dle_del_news + '</a>';

}

return menu;
};

function openWindow(url, width, height, windowid)
{
	return window.open(
		url,
		(typeof windowid == 'undefined' ? 'dlePopup' : windowid),
		'statusbar=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes'
		+ (typeof width != 'undefined' ? (',width=' + width) : '') + (typeof height != 'undefined' ? (',height=' + height) : '')
	);
};