//подсчет символов при наборе (site/inc_comment.php)
function DisplayLength(MesText, MaxLength, rchid) {
	var rch=document.getElementById(rchid);
	if ( MesText.value.length > MaxLength ) {
		MesText.value = MesText.value.substr(0, MaxLength );
	}
	if (rchid!="no") {
		rch.innerHTML = MaxLength - MesText.value.length;
	}
};

//ссылка "обновить" капчу (site/inc_comment.php)
function reload(){
	var src=document.captcha.src;
	document.captcha.src="loading.gif";
	document.captcha.src=src+"?rand="+Math.random();
};

//конвертация в трансит текста text и выводит в value объекта obj_to (admin/add_article.php)
var rusChars = new Array('а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х','ч','ц','ш','щ','э','ю','\я','ы','ъ','ь', ' ', '\'', '\"', '\#', '\$', '\%', '\&', '\*', '\,', '\:', '\;', '\<', '\>', '\?', '\[', '\]', '\^', '\{', '\}', '\|', '\!', '\@', '\(', '\)', '\-', '\=', '\+', '\/', '\\','.');
var transChars = new Array('a','b','v','g','d','e','yo','zh','z','i','y','k','l','m','n','o','p','r','s','t','u','f','kh','ch','ts','sh','sch','e','yu','ya','y','', '', '_', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '','_');
function convert(text,obj_to){
	var to = new String();
	text = text.toLowerCase();
	var len = text.length;
	var character, isRus;
	for(i=0; i < len; i++){
		character = text.charAt(i,1);
		isRus = false;
			for(j=0; j < rusChars.length; j++){
				if(character == rusChars[j]){
					isRus = true;
					break;
				}
			}
			to += (isRus) ? transChars[j] : character;
	}
document.getElementById(obj_to).value = to+'.php';
//document.m_form.message.focus();
};

//подтверждение на удаление статьи (admin/edit_article.php)
function verify_del(page) {
	if (confirm("Вы действительно хотите удалить статью?")) {
		window.location.href='"./admin.php?mod=del_article&page='+page+'"';
	}
	else {
		alert ("Не хотите, как хотите :)");
	}
};

//показ выбранного изображения (admin/edit_article.php)
function view_image(from){
	var image = from.options[from.selectedIndex].value;
	image = image.split(/[|]/);
	var filename_image = image[image.length-3] + image[image.length-2];
	var title_image = image[image.length-1];
	document.form_edit.img_filename.value=filename_image;
	document.form_edit.img_title.value=title_image;
	document.view_img.src=filename_image;
	document.view_img.alt=title_image;
	document.view_img.title=title_image;
};


//заполняет компоненты  формы в зависимости от выбранной щаписи в списке (admin/edit_image_info.php)
function set_image_info(from){
	var image = from.options[from.selectedIndex].value;
	image = image.split(/[|]/);
	var path_thumb = image[0] + "thumbs/thumb_";
	var path_image = image[0];
	var filename = image[1];// + "." + image[3];
	var ext_file = image[2];
	var ext_thumb = image[3];
	var author = image[4];
	var publisher = image[5];
	var title = image[6];
	var date_create = image[7];
	var date_publish = image[8];
	var noext_filename = image[9];
	
	//поля формы по выбранной картинке
	document.form_edit.noext_filename.value = noext_filename.substr(0,noext_filename.length-4);
	document.form_edit.filename.value = filename;
	document.form_edit.ext_file.value = ext_file;
	document.form_edit.ext_thumb.value = ext_thumb;
	document.form_edit.author.value = author;
	document.form_edit.publisher.value = publisher;
	document.form_edit.title.value = title;
	document.form_edit.date_create.value = date_create;
	document.form_edit.date_publish.value = date_publish;

	//для ссылки на открытие оригинала выбранной картинки
	document.getElementById("link_img").href = path_image + noext_filename;
	
	//для вывода выбранной картинки
	document.thumb_img.src = path_thumb + noext_filename;
	document.thumb_img.alt = title;
	document.thumb_img.title = title;
};

//замена bbcode в text (admin/edit_article.php)
function code_img (bbcode) {
	var image = document.form_edit.filename_images.options[document.form_edit.filename_images.selectedIndex].value;
	image = image.split(/[|]/);
	image = image[image.length-2];
	
	var startpos = document.form_edit.text.selectionStart;
	var endpos = document.form_edit.text.selectionEnd;
	document.form_edit.text.value = document.form_edit.text.value.substring(0,startpos) + "[img_" +bbcode + "]" + image + "[/img]" + document.form_edit.text.value.substring(endpos,document.form_edit.text.length);
};

// --------- вставка bb кодов при написании комментариев ((site/inc_comment.php)) -------- //
var txt = '';
var LeftText = '';
var RightText = '';
var selStart;

var bbtags = {
	bold:			{0:"[b]",1:"[/b]"},
	italic:			{0:"[i]",1:"[/i]"},
	underline:		{0:"[u]",1:"[/u]"}
};

function bbcode() {
	var L = '';
	var R = '';
	var a = bbcode.arguments;
    var code = a[0].name;
	L = bbtags[code][0];
	R = bbtags[code][1];
    TextArea.selected = true;
    if (isMSIE) {
		if (!TextArea.caretPos) {IEOP();}
		TextArea.caretPos.text = L + TextArea.caretPos.text + R;
		TextArea.caretPos = null;
    } else if (isMozilla || isOpera) {
    		SelectedText = NNMOZ();
            TextArea.value = LeftText + L + SelectedText + R + RightText;
			
			if (isMozilla)
				{L = L.replace(/\r/g, '');}
			else if (isOpera && L.indexOf('\r') == -1)
				{L = L.replace(/\n/g, '\r\n');}
			
			var newPos = selStart + L.length + SelectedText.length + R.length;
			TextArea.selectionStart		= newPos;
			TextArea.selectionEnd		= newPos;
    }
	SelectedText = txt = '';
}

function NNMOZ() {
		var selLength	= TextArea.textLength;
		selStart		= TextArea.selectionStart;
        var selEnd		= TextArea.selectionEnd;
        if (selEnd == 1 || selEnd == 2)
                selEnd = selLength;

        LeftText		= (TextArea.value).substring(0,selStart);
        RightText		= (TextArea.value).substring(selEnd, selLength);
        return  (TextArea.value).substring(selStart, selEnd);
}

function IEOP() {
	if (TextArea.createTextRange) {
		TextArea.caretPos = document.selection.createRange().duplicate();
	}
}
// --------- вставка bb кодов при написании комментариев -------- //
