to_open = new Array();
//!!!all data must be in UTF-8


/* photoalbum zip functions */

function base64encode(sStr) {
	var sWinChrs = 'АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя'
	var sBase64Chrs  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
	var arrBase64  = sBase64Chrs.split('')

	var a = new Array();
	var i = 0;
	for(i=0; i<sStr.length; i++ ) {
		var cch=sStr.charCodeAt(i)
		if (cch>127) {
			cch=sWinChrs.indexOf(sStr.charAt(i))+163; if(cch<163) continue; 
		}
		a.push(cch)
	};
	var s=Array(), lPos = a.length - a.length % 3
	for (i=0; i<lPos; i+=3) {
		var t=(a[i]<<16)+(a[i+1]<<8)+a[i+2]
		s.push(arrBase64[(t>>18)&0x3f]+arrBase64[(t>>12)&0x3f]+arrBase64[(t>>6)&0x3f]+arrBase64[t&0x3f] )
	}
	switch (a.length-lPos) {
		case 1 : var t=a[lPos]<<4; s.push(arrBase64[(t>>6)&0x3f]+arrBase64[t&0x3f]+'=='); break
		case 2 : var t=(a[lPos]<<10)+(a[lPos+1]<<2); s.push(arrBase64[(t>>12)&0x3f]+arrBase64[(t>>6)&0x3f]+arrBase64[t&0x3f]+'='); break
	}
	return s.join('')
}

function zip_upload_form (referer, label1, label2, label3, label4) {
	var form = '<form action="upload_arhive" method="post" enctype="multipart/form-data">'+
		'<input type="hidden" name="referer" value="'+referer+'"><p>'+
		label3+'<input type="file" name="zip_arhive" style="width:100%"></p><p>'+
		label4+'<input type="text" name="zip_arhive_src" id="zip_arhive_src" value="" onclick="/*if(this.value==\'\')this.value=\'files/example.zip\'*/" style="width:130px">'+
		' <input type="button" onclick="show_dialog()" style="width:65px;" value="Обзор..."></p>'+
		'<p><input type="submit" style="font-weight:bold;" value="'+label1+'">'+
		' <input type="button" value="'+label2+'" onclick="zip_upload_form(\'\', \'\', \'\', \'\', \'\')"></p></form>';
	var zipDiv = document.getElementById('zip_upload');
	
	if (zipDiv.style.display == 'none') {
		zipDiv.style.display = 'block';
		zipDiv.innerHTML = form;
		
		var bounds = getBounds(document.getElementById('zip_upload_a'))
		
		zipDiv.style.top = (bounds.top + 15) + 'px';
		zipDiv.style.left = bounds.left + 'px';
		
	} else {
		zipDiv.style.display = 'none';
		zipDiv.innerHTML = '';	
	}
}

function show_dialog () {
	umiPopup.getSelf().open();
	
	var form = document.createElement("div");
	form.innerHTML = '<div id="file_manager">'+
		'Поиск Zip-архива:'+
		'<div id="zip_manager" class="zip_manager"></div>'+
		'<p align="center"><input style="width:150px;" type="button" value="Отмена" onclick="umiPopup.getSelf().close();"></p>'+
		'</div>';
	
	umiPopup.getSelf().setContent (form);
	
	reload_manager ('');
}

function load_finished (oRequest) {
	
	hide_loader ();
	
	var new_string = "";
	
	for (i = 0; i < oRequest.length; i++) {
		var file = oRequest[i].file_info;
		
		if (file['type'] == "dir") {
			new_string += '<a href="javascript:void(0)" onclick="reload_manager(\''+base64encode(file['src'])+'\'); return false;" class="folder">/'+file['name']+'</a>';
		} else {
			new_string += '<a href="javascript:void(0)" onclick="put_src(\''+file['src']+'\'); return false;">'+file['name']+'</a>';
		}
	}
	
	document.getElementById('zip_manager').innerHTML = new_string;
}

function reload_manager (path) {
	show_loader ();
	
	jsonRequestsController.getInstance().sendRequest("/admin/content/json_load_zip_folder/?dirpath="+path, load_finished);
}

function put_src (src) {
	document.getElementById('zip_arhive_src').value = src;
	umiPopup.getSelf().close();
}


function show_loader () {
	document.getElementById('zip_manager').innerHTML = '';
	document.getElementById('zip_manager').style.background = 'white url(/images/cms/loading.gif) no-repeat center center';
}
function hide_loader () {
	document.getElementById('zip_manager').style.background = 'white';
}



function getBounds(element)
{
  var left = element.offsetLeft;
  var top = element.offsetTop;
  for (var parent = element.offsetParent; parent; parent = parent.offsetParent)
  {
    left += parent.offsetLeft - parent.scrollLeft;
    top += parent.offsetTop - parent.scrollTop
  }
  return {left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
}

/* end photoalbum zip functions */


function v_onload(b_id) {

	if(b_id == "add_page_wysiwyg")
		return;

	disp = findUCookie(b_id + "_table", "setgroups");
//	t_obj = document.getElementById(b_id + "_table");

	if(disp == "none")
		v_switch(b_id);
/*

	t_obj.style.display = disp;


	if(disp == "none") {
		document.images[b_id + "_img"].src = "/images/cms/sg_arrow_up.gif";
	}
*/
}






function filterState(){
	var state = getCookie('filter');
	var settings = document.getElementById('advanced-settings');
	
	if(typeof settings != "object") return false;
	
	if (state == 'visible'){
		if(settings) {
			settings.style.display = 'block';
		}
	}
}
function checkMultipleInputs(){
		var checkForm =  document.getElementById('page-edit-form')
		var l = checkForm.elements.length;
		for (i = 0; i < l; i++){
			elem = checkForm.elements[i];
			if ((elem.className == "__multiple_guide_input") && (elem.value !== "")){
				var i_id = new String();
				i_id = new String(elem.id);
				u_id = i_id.replace(/multipleGuideInput_/i, "");
				//selectObj = document.getElementById(u_id);
					new multipleGuideInput(u_id, false, elem.value)
				}		
		}
	}

function putInputEvents() {
		if($('name')) {
			new Form.Element.Observer(
				$('name'),
				0.2,
				autoSyncFormInputsPage
			);
		};
		
		var form = $('page-edit-form');
		if(form) {
			form.onsubmit = function () {
				checkMultipleInputs();
				if(checkFieldIsEmpty('name')) return false;
				if(checkFieldIsEmpty('alt-name')) return false;
	
				return true;
			}
			return true;
		}
		
		var form = $('object-edit-form');
		
		if(form) {
			form.onsubmit = function () {
				if(checkFieldIsEmpty('name')) return false;
	
				return true;
			}
			//return true;
		}
		
		if($('data[title]')) {
			new Form.Element.Observer(
				$('data[title]'),
				0.2,
				autoSyncFormInputsData
			);
		}
		
		
		if(form) {		
			if($('login')) {
				var loginObj = $('login');
				if(loginObj.value.length == 0) {
					form.onsubmit = function () {
						if(checkFieldIsEmpty('login')) return false;
						if(checkFieldIsEmpty('password')) return false;
						return true;
					};
				} else {
					form.onsubmit = function () {
						if(checkFieldIsEmpty('login')) return false;
						return true;
					}
				}
			}
		}
	};


	function namesTranslit(value) {
		var value_translit = translit(value, 'R_TO_E');
		if(value_translit.substr(0, 1) == "_") {
			value_translit = value_translit.substr(1, value_translit.length);
		}
		if(value_translit.substr(value_translit.length - 1, 1) == "_") {
			value_translit = value_translit.substr(0, value_translit.length - 1);
		}
		
		
		return value_translit;
	}
	
	function autoSyncFormInputsPage(el, value) {
		var value_translit = namesTranslit(value);
		syncInputValue($('alt-name'), value_translit);
		syncInputValue($('h1'), value);
		syncInputValue($('title'), value);
	}
	
	function autoSyncFormInputsData(el, value) {
		var value_translit = translit(value, 'R_TO_E');
		syncInputValue($('data[name]'), value_translit);
	}
	
	function checkFieldIsEmpty(el_id) {
		var obj = $(el_id);

		if(obj) {
			if(obj.value.length == 0) {
				obj.addClassName("empty");
				obj.focus();
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	function syncInputValue(el, value) {
		if(!el) return true;
		if(value.length > 0) {
			el.removeClassName("empty");
		}
	
		if(el.value.length > 0) {
			if(el.value.length < value.length) {
				if(value.substr(0, el.value.length) != el.value) {
					return false;
				}
			}
			
			if(el.value.length > value.length) {
				if(el.value.substr(0, value.length) != value) {
					return false;
				}
			}
		}
		
		el.value = value;
		return true;
	}


//for cookie
function addCookie(szName,szValue,dtDaysExpires) 
{
//	return 1;
  szName = szName.replace(/\./g, "_");

   var dtExpires = new Date();
   var dtExpiryDate = "";

   dtExpires.setTime(dtExpires.getTime() + 
     dtDaysExpires * 60 * 60 * 1000);

   dtExpiryDate = dtExpires.toGMTString();

   document.cookie = 
    szName + "=" + szValue + "; path=/; expires=" + dtExpiryDate;
}

function findCookie(szName) 
{
  szName = szName.replace(/\./g, "_");


  var i = 0;
  var nStartPosition = 0;
  var nEndPosition = 0;  
  var szCookieString = document.cookie;  

  while(i <= szCookieString.length) 
  {
    nStartPosition = i;
    nEndPosition = nStartPosition + szName.length;

    if(szCookieString.substring( 
        nStartPosition,nEndPosition) == szName) 
    {
      nStartPosition = nEndPosition + 1;
      nEndPosition = 
        document.cookie.indexOf(";",nStartPosition);

      if(nEndPosition < nStartPosition)
        nEndPosition = document.cookie.length;

      return document.cookie.substring( 
          nStartPosition,nEndPosition);  
      break;    
    }
    i++;  
  }
  return "";
}

function addUCookie(vName, vVal, vExp, gName) {	//4093 bytes MAX!!!
	if(!gName)
		gName = "ucookie";

	if(!vExp)
		vExp = 3600*24*365;

	gVal = findCookie(gName);

	nVal = "";

	gArr = gVal.split("*");
	in_array = false;



	for(i = 0; i < gArr.length; i++) {
		tmp = gArr[i];
		cgArr = tmp.split("|");
		cVar = cgArr[0];
		cVal = cgArr[1];

		if(cVar == "")
			continue;

		if(cVar == vName) {
			in_array = true;
			nVal += vName + "|" + vVal + "*";
		} else
			nVal += tmp + "*";

	}

	if(!in_array)
		nVal += vName + "|" + vVal + "*";

	nVal = nVal.substr(0, nVal.length - 1);

	addCookie(gName, nVal, vExp);
}

function findUCookie(vName, gName) {
	if(!gName)
		gName = "ucookie";

	gVal = findCookie(gName);
	gArr = gVal.split("*");

	in_array = false;
	for(i = 0; i < gArr.length; i++) {
		tmp = gArr[i];
		cgArr = tmp.split("|");
		cVar = cgArr[0];
		cVal = cgArr[1];

		if(cVar == vName) {
//			alert(cVar + " = \"" + cVal + "\"");
			return cVal;
		}
	}
	return "";

}

function deleteUCookie(vName, vExp, gName) {
	if(!gName)
		gName = "ucookie";
	if(!vExp)
		vExp = 3600*24*365;

	gVal = findCookie(gName);
	gArr = gVal.split("*");

	nVal = "";


	for(i = 0; i < gArr.length; i++) {
		tmp = gArr[i];
		cgArr = tmp.split("|");
		cVar = cgArr[0];
		cVal = cgArr[1];

		//alert(cVar + " = " + cVal);
		if(cVar != vName)
				nVal += cVar + "|" + cVal + "*";
	}

	nVal = nVal.substr(0, nVal.length - 1);

	addCookie(gName, nVal, vExp);
}

function sitetree_switch(p_id, mode) {


 timg = document.getElementById('il' + p_id + 'li');
 objs = document.getElementById('s' + p_id + 's');
 obji = document.getElementById('i' + p_id + 'i');
 objpm = document.getElementById('p' + p_id + 'm');

if(!objs)
    return true;


 if(objs.style.display == 'none') {
  objpm.src = objpm.src.replace('plus.gif', '') + 'minus.gif';

  objs.style.display = '';
  if(obji)
   obji.style.display = '';

  if(timg)
   timg.style.display = '';

  if(mode != "ONLOAD")
	addUCookie(p_id + "_page", '1');
  else
	return true;

 } else {
  if(mode == "ONLOAD")
	return true;
//  addUCookie(p_id + "_page", '0' , 365);
  deleteUCookie(p_id + "_page", 365);


  objpm.src = objpm.src.replace('minus.gif', '') + 'plus.gif';
  objs.style.display = 'none';

  if(obji)
   obji.style.display = 'none';

  if(timg)
   timg.style.display = 'none';
 }

}

function hide_ugol(p_id) {
//	alert(p_id);
	timg = document.getElementById('il' + p_id + 'li');
	if(timg)
		timg.style.display = 'none';

}

function is_initEditor() {
	if(typeof initEditor == "function") {
		initEditor();
	}
}



/* AUTOFILL: TRANSLIT */
var rusBig = new Array( "Э", "Ч", "Ш", "Ё", "Ё", "Ж", "Ю", "Ю", "\Я", "\Я", "А", "Б", "В", "Г", "Д", "Е", "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Щ", "Ъ", "Ы", "Ь");
var rusSmall = new Array("э", "ч", "ш", "ё", "ё","ж", "ю", "ю", "я", "я", "а", "б", "в", "г", "д", "е", "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "щ", "ъ", "ы", "ь" );
var engBig = new Array("E\'", "CH", "SH", "YO", "JO", "ZH", "YU", "JU", "YA", "JA", "A","B","V","G","D","E", "Z","I","J","K","L","M","N","O","P","R","S","T","U","F","H","C", "W","~","Y", "\'");
var engSmall = new Array("e\'", "ch", "sh", "yo", "jo", "zh", "yu", "ju", "ya", "ja", "a", "b", "v", "g", "d", "e", "z", "i", "j", "k", "l", "m", "n", "o", "p", "r", "s",  "t", "u", "f", "h", "c", "w", "~", "y", "\'");
var rusRegBig = new Array( /Э/g, /Ч/g, /Ш/g, /Ё/g, /Ё/g, /Ж/g, /Ю/g, /Ю/g, /Я/g, /Я/g, /А/g, /Б/g, /В/g, /Г/g, /Д/g, /Е/g, /З/g, /И/g, /Й/g, /К/g, /Л/g, /М/g, /Н/g, /О/g, /П/g, /Р/g, /С/g, /Т/g, /У/g, /Ф/g, /Х/g, /Ц/g, /Щ/g, /Ъ/g, /Ы/g, /Ь/g);
var rusRegSmall = new Array( /э/g, /ч/g, /ш/g, /ё/g, /ё/g, /ж/g, /ю/g, /ю/g, /я/g, /я/g, /а/g, /б/g, /в/g, /г/g, /д/g, /е/g, /з/g, /и/g, /й/g, /к/g, /л/g, /м/g, /н/g, /о/g, /п/g, /р/g, /с/g, /т/g, /у/g, /ф/g, /х/g, /ц/g, /щ/g, /ъ/g, /ы/g, /ь/g);
var engRegBig = new Array( /E'/g, /CH/g, /SH/g, /YO/g, /JO/g, /ZH/g, /YU/g, /JU/g, /YA/g, /JA/g, /A/g, /B/g, /V/g, /G/g, /D/g, /E/g, /Z/g, /I/g, /J/g, /K/g, /L/g, /M/g, /N/g, /O/g, /P/g, /R/g, /S/g, /T/g, /U/g, /F/g, /H/g, /C/g, /W/g, /~/g, /Y/g, /'/g);
var engRegSmall = new Array(/e'/g, /ch/g, /sh/g, /yo/g, /jo/g, /zh/g, /yu/g, /ju/g, /ya/g, /ja/g, /a/g, /b/g, /v/g, /g/g, /d/g, /e/g, /z/g, /i/g, /j/g, /k/g, /l/g, /m/g, /n/g, /o/g, /p/g, /r/g, /s/g, /t/g, /u/g, /f/g, /h/g, /c/g, /w/g, /~/g, /y/g, /'/g);


function translit(input, mode) {
	var textar = input;
	var res = "";

	if(mode == "E_TO_R") {
		if (textar) {
			for (i=0; i<engRegSmall.length; i++) {
				textar = textar.replace(engRegSmall[i], rusSmall[i])  
			}
			for (var i=0; i<engRegBig.length; i++) {
				textar = textar.replace(engRegBig[i], rusBig[i])  
				textar = textar.replace(engRegBig[i], rusBig[i])  
			} 
			res = textar;
		}
	}

	if(mode == "R_TO_E") {
		if (textar) {
			for (i=0; i<rusRegSmall.length; i++) {
				textar = textar.replace(rusRegSmall[i], engSmall[i])  
			}
			for (var i=0; i<rusRegBig.length; i++) {
//				textar = textar.replace(rusRegBig[i], engBig[i])  
				textar = textar.replace(rusRegBig[i], engSmall[i])  
			} 
			res = textar.toLowerCase();
		}
	}

	//
	res = res.replace(/[\/\\'\.,\t\|\+&\?%#@]*/g, "");
	res = res.replace(/[ ]+/g, "_");
	//

	//document.post.message.value = res;
	return res;
}

var g_obj;
var def_value = "";
var def_valueh1 = "";

function tt() {
	obj = g_obj;

	res = translit(obj.value, "R_TO_E");




	df = document.forms['adding_new_page'];
	if (df) {
		if(df.alt_name)
			an = df.alt_name.value;
		else
			an = "";

		if(df.h1)
			h1 = df.h1.value;
		else
			h1 = obj.value;


		if(an == "" || translit(def_value, "R_TO_E") == an) {
			if(df.alt_name)
				df.alt_name.value = res;
		}

		if(h1 == "" || h1 == def_valueh1) {
			if(df.h1)
				df.h1.value = obj.value;
		}
		def_value = res;
		def_valueh1 = obj.value;
		def_alt = an;
	}
}

function go_alt(obj) {
	g_obj = obj;
	setTimeout("tt()", 1);
}



function tt_f() {
	obj = g_obj;
	res = translit(obj.value, "R_TO_E");




	df = document.forms['adding_new_page'];
	if (df) {
		if(df.name)
			an = df.name.value;
		else
			an = "";

		if(an == "" || translit(def_value, "R_TO_E") == an) {
			if(df.name)
				df.name.value = res;
		}

		def_value = res;
		def_alt = an;
	}
}

function go_alt_a(obj) {
	g_obj = obj;
	setTimeout("tt_f()", 1);
}


/* AUTOCHECK FORMS */
var acf_inputs_test = new Array();
var acf_inputs_catch = new Array();
var is_focued_once = 0;


function acf_check(release_outside) {
	acf_name = 'test';

	rt = true;

	if(release_outside)
		is_focued_once = 0;
	if(is_focued_once == 1)
		is_focued_once = 0;
	for(i = 0; i < acf_inputs_test.length; i++) {
		r = filled = acf_field_check(acf_inputs_test[i]);

		if(!r)
			rt = false;
	}

	return rt;

}

function acf_field_check(field) {
	fid = field[0];
	wmsg = field[1];

	f_obj = document.getElementById(fid);
	if(!f_obj)
		return false;

	if(f_obj.value) {
		f_obj.style.border = "#C0C0C0 0.5pt solid";
		return true;
	} else {
		f_obj.style.border = "#A00 0.5pt solid";
		if(is_focued_once == 0) {
			f_obj.focus();
			is_focued_once = 1;
		}
		return false;
	}

}


function acf_catch_input(e) {

	var is_ie = Prototype.Browser.IE;

	if(is_ie)
		c = event.keyCode;
	else
		c = e.keyCode;

	is_focued_once = 0;
	if(c != 13)
		is_focued_once = -1;
	res = acf_check(0)
	if(c == 13) {
		return res;
	}
	return true;

}

function acf_onload() {
	if(!acf_inputs_catch)
		return true;
	for(i = 0; i < acf_inputs_catch.length; i++) {
		field = acf_inputs_catch[i];

		f_obj = document.getElementById(field);
		if(f_obj) {
			f_obj.onkeypress = acf_catch_input;
		}
	}
	return true;
}

function if_sured(link, inp_text) {
	var href = link.href;

	var callback = function () {
		if(href) {
			window.location = href;
		}
	};

	var contDiv = document.createElement("div");
	var html = "<div style='margin: 15px;'><h1>Вы уверены, что хотите удалить эту страницу?</h1>";
	html += "<p>Вы собираетесь удалить страницу. Если вы уверены, нажмите 'Удалить'.<br />";
	html += "После удаления страница попадет в <a href='/admin/data/trash/'>корзину удаленных страниц</a>, откуда ее сможет восстановить администратор сайта.</p>";
	html += "<p align='right'><input type='button' value='Удалить' style='width: 100px; font-weight: bold;' onclick='javascript: umiPopup.getSelf().close(); umiPopup.getSelf().callCallbackFunction();' id='confirmDeleteButton' /> <input type='button' value='Отменить' style='width: 100px;' onclick='javascript: umiPopup.getSelf().close();' /></p>";
	html += "</div>";

	contDiv.innerHTML = html;

	var up = new umiPopup();
		up.setSize(430, 130);
	up.setCallbackFunction(callback);
	up.open();
	up.setContent(contDiv);

	var delButton = document.getElementById('confirmDeleteButton');
	if(delButton) {
		delButton.focus();
	}


	return false;
}



function json_option_changed(iObj) {
	var elId = iObj.value;	
	var url = "/admin/catalog/get_subelements/" + elId + "/" + iObj.parentNode.id + "/";
	execInternalScript(url);
	iObj.parentNode.innerHTML = "<option>loading...</option>";
}

function lin_array(val, arr) {
	for(i = 0; i < arr.length; i++) {
		if(arr[i] == val) return true;
	}
	return false;
}

function jsonSelectCallback(elArr, parentNodeId, relId, val) {
	var iObj = document.getElementById(parentNodeId);
	var res = "";

	res += "<option></option>\r\n";

	var valArr = val.split("|");

	res += "<option value=\"" + relId + "\" onclick=\"javascript: json_option_changed(this);\">&lt;Go back&gt;</option>\r\n";
	for(var i = 0; i < elArr.length; i++) {
		if(elArr[i][2] == '0') {
			res += "<option value=\"" + elArr[i][0] + "\" onclick=\"javascript: json_option_changed(this);\">#" + elArr[i][1] + "</option>\r\n";
		} else {
//			if(val && val == elArr[i][0]) {
			if(val && lin_array(elArr[i][0], valArr)) {
				res += "<option value=\"" + elArr[i][0] + "\" selected=\"selected\">" + elArr[i][1] + "</option>\r\n";
			} else {
				res += "<option value=\"" + elArr[i][0] + "\">" + elArr[i][1] + "</option>\r\n";
			}
		}
	}
	iObj.innerHTML = res;
}

function json_select_load(objId, val) {
	var iObj = document.getElementById(objId);

	var url = "/admin/catalog/get_subelements_onload/" + objId + "/" + val + "/";
	execInternalScript(url);
	iObj.innerHTML = "<option>loading...</option>";
}

function execInternalScript(url) {
	var scriptObj = document.createElement("script");
	scriptObj.src = url;
	var placerObj = document.body.firstChild;

	do {
		if(placerObj.nodeType == 1) {
			placerObj.appendChild(scriptObj);
			break;
		}
	} while(placerObj = placerObj.nextSibling);
}





function json_cat_getter_load(objId, val, is_cat_only) {
	if(!is_cat_only) is_cat_only = 0;

	var iObj = document.getElementById(objId);
	var url = "/admin/catalog/get_subelements_onload_cat/" + objId + "/" + val + "/" + is_cat_only + "/";

//	execInternalScript(url);
//	iObj.innerHTML = "<option>loading...</option>";

	iObj.innerHTML = "";
	var o = document.createElement("option");
	o.innerHTML = "<option>loading...</option>";
	iObj.appendChild(o);

	execInternalScript(url);
}


function json_option_changed_cat(iObj, is_cat_only) {
	if(!is_cat_only) is_cat_only = 0;
	var is_ie = Prototype.Browser.IE;

	var elId = iObj.value;


	if(is_ie) {
		var url = "/admin/catalog/get_subelements/" + elId + "/" + iObj.id + "/1/" + is_cat_only + "/";
	} else {
		var url = "/admin/catalog/get_subelements/" + elId + "/" + iObj.parentNode.id + "/1/" + is_cat_only + "/";
	}

	execInternalScript(url);

	if(is_ie) {
		iObj.innerHTML = "";
		var o = document.createElement("option");
		o.innerHTML = "loading...";
		iObj.appendChild(o);
	} else {
		iObj.parentNode.innerHTML = "<option>loading...</option>";
	}
}


function jsonSelectCallback_cat(elArr, parentNodeId, relId, val, cats_only) {
	if(!cats_only) cats_only = 0;

	var is_ie = Prototype.Browser.IE;

if(is_ie) {

	var iObj = document.getElementById(parentNodeId);
	iObj.innerHTML = "";

	iObj.appendChild( document.createElement("option") );

	var valArr = val.split("|");

	var o = document.createElement("option");
	o.value = relId;
	var __cats_only = cats_only;
	o.innerHTML = "&lt;Go back&gt;";
	iObj.appendChild( o );

	for(var i = 0; i < elArr.length; i++) {
		var o = document.createElement("option");
		o.value = elArr[i][0];
		o.innerHTML = "[" + elArr[i][1] + "]";
		o.is_object = "no";

		if(elArr[i][2] == '0') {
			o.className = "jsonIcat";
		} else {
			if(val && lin_array(elArr[i][0], valArr)) {
				o.className = "jsonIobj";
				o.selected = true;
			} else {
				o.innerHTML = elArr[i][1];
				o.className = "jsonIobj";
				o.is_object = "yes";
			}
		}

		iObj.appendChild(o);
	}

	var __cats_only = cats_only;
	iObj.ondblclick = function () {
		json_option_changed_cat(this, __cats_only);
	};

} else {

	var iObj = document.getElementById(parentNodeId);
	var res = "";

	res += "<option></option>\r\n";

	var valArr = val.split("|");

	res += "<option value=\"" + relId + "\" ondblclick=\"javascript: json_option_changed_cat(this, '" + cats_only + "');\">&lt;Go back&gt;</option>\r\n";
	for(var i = 0; i < elArr.length; i++) {
		if(elArr[i][2] == '0') {
			res += "<option class=\"jsonIcat\" value=\"" + elArr[i][0] + "\" ondblclick=\"javascript: json_option_changed_cat(this, '" + cats_only + "');\">" + elArr[i][1] + "</option>\r\n";
		} else {
			if(val && lin_array(elArr[i][0], valArr)) {
				res += "<option class=\"jsonIobj\" value=\"" + elArr[i][0] + "\" selected=\"selected\">" + elArr[i][1] + "</option>\r\n";
			} else {
				res += "<option class=\"jsonIobj\" value=\"" + elArr[i][0] + "\">" + elArr[i][1] + "</option>\r\n";
			}
		}
	}

	iObj.innerHTML = res;
}

}

function cat_getter_del(iId) {
	var obj, obj2, nobj;

	if(obj = document.getElementById(iId)) {
		if(obj2 = document.getElementById('inp_' + iId)) {
			obj2.parentNode.removeChild(obj2);
		}

		nobj = document.createElement("input");
		nobj.type = "hidden";
		nobj.name = "cat_del[]";
		nobj.value = iId;

		obj.parentNode.appendChild(nobj);

		obj.parentNode.removeChild(obj);
	}


	return false;
}

function json_cat_addItems(iId) {
	var obj = document.getElementById(iId);
	if(!obj) return false;

	var l = obj.options.length;
	var i;
	for(i = 0; i < l; i++) {
		if(obj.options[i].selected) {
			var val = obj.options[i].value;
			var txt = obj.options[i].innerHTML;
			var is_cat = (obj.options[i].className == 'jsonIcat') ? true : false;

			json_cat_addItem(iId, val, txt, is_cat);
		}
	}
}

function json_cat_addItem(iId, val, txt, is_cat) {
	var obj = document.getElementById(iId + "_list");
	if(!obj) return false;

	var nO = document.createElement("div");
	nO.className = "cat_getter_i";
	nO.id = val;

	if(document.getElementById(val)) return false;

	var nh = "";

	var img_name = (is_cat) ? "ico_news_lent" : "ico_news_item";
	nh += "<div style=\"float: left;\"><img src=\"/images/cms/admin/full/" + img_name + ".gif\" />&nbsp;";
	nh += txt;
	nh += "</div><div style=\"float: right;\">";
	nh += "<a href=\"/\" onclick=\"javascript: return cat_getter_del('" + val + "');\">";
	nh += "<img src=\"/images/cms/admin/full/ico_del.gif\" border=\"0\" /></a></div>";
	nh += "<input type=\"hidden\" name=\"cat_getter_items[]\" value=\"" + val + "\" id=\"inp_" + val + "\" />";
	nh += "<div style=\"clear: both;\" /></div>";

	nO.innerHTML = nh;

	obj.appendChild(nO);
}



var onLoadFunctions__ = new Array();

function addOnLoadEvent(h) {
	if(typeof h != "function") {
		alert("addOnLoadEvent:: Handler must be a valid function");
		return false;
	}
	onLoadFunctions__[onLoadFunctions__.length] = h;
	return true;
}

function runOnLoadEvents() {
	var i;
	for(i = 0; i < onLoadFunctions__.length; i++) {
		var h = onLoadFunctions__[i];
		h();
	}
}




function get_tip(name, title, tip) {
	var callback = function () {
	};

	var contDiv = document.createElement("div");
	var html = "<div style='margin: 15px;'><h1>Подсказка для поля \"" + title + "\".</h1>";
	html += "<p>" + tip + "</p>";
	html += "<p align='right'><input type='button' value='Закрыть' style='width: 100px;' onclick='javascript: umiPopup.getSelf().close();' /></p>";
	html += "</div>";

	contDiv.innerHTML = html;

	var up = new umiPopup();
	up.setSize(430, 115);
	up.setCallbackFunction(callback);
	up.open();
	up.setContent(contDiv);


	return false;
}


function if_sured_unrestorable(link, inp_text) {
	var href = link.href;

	var callback = function () {
		if(href) {
			window.location = href;
		}
	};

	var contDiv = document.createElement("div");
	var html = "<div style='margin: 15px;'><h1>" + inp_text + "</h1>";
	html += getLabel("js-confirm-unrecoverable-del");
	html += "<p align='right'><input type='button' value='" + getLabel("js-confirm-unrecoverable-yes") + "' style='width: 100px; font-weight: bold;' onclick='javascript:umiPopup.getSelf().close(); umiPopup.getSelf().callCallbackFunction();' id='confirmDeleteButton' /> <input type='button' value='" + getLabel("js-confirm-unrecoverable-no") + "' style='width: 100px;' onclick='javascript:umiPopup.getSelf().close();' /></p>";
	html += "</div>";

	contDiv.innerHTML = html;

	var up = new umiPopup();
		up.setSize(430, 110);
	up.setCallbackFunction(callback);
	up.open();
	up.setContent(contDiv);

	var delButton = document.getElementById('confirmDeleteButton');
	if(delButton) {
		delButton.focus();
	}


	return false;
}

function helpViewerState() {
	var infoBlock = document.getElementById('info-block');
	var infoBlockState = getCookie('info-block');
	var setWidth = document.getElementById('contentWidthBehavior');

	if (typeof infoBlock == "object") {
		if (infoBlockState != 'hide') {
			if(infoBlock) {
				infoBlock.parentNode.style.display = '';
			}

			if(setWidth) {
				setWidth.style.width = '100%';
			}
		}
	}
}

function helpViewerSwitch() {
	var infoBlockState = getCookie('info-block');
	var infoBlock = document.getElementById('info-block');
	var setWidth = document.getElementById('contentWidthBehavior');
	if (infoBlock.parentNode.style.display == 'none') {
		infoBlock.parentNode.style.display = '';
		
		if(setWidth) {
			setWidth.style.width = '75%';
		}

		setCookie('info-block', 'visible');
	} else {
		infoBlock.parentNode.style.display = 'none';
		
		if(setWidth) {
			setWidth.style.width = '100%';
		}
		setCookie('info-block', 'hide');
	}
}

function checkVisible(id) {
	var blockId = id + '_table';
	var block = document.getElementById(blockId);
	if (block.style.display == 'none') {
		v_switch(id)
	}
	
}


function show_tip(caller, name, title, tip) {
	var is_ie = Prototype.Browser.IE;

	var oDiv = document.createElement("div");
	oDiv.style.width = '280px';
	oDiv.style.backgroundColor = '#FFF';
	oDiv.style.border = '#000 1px solid';
	oDiv.style.color = '#000';
	oDiv.style.padding = '5px';

	oDiv.style.position = "absolute";
	oDiv.style.zIndex = 300;

	var currWidth = document.body.clientWidth;
	if((document.lmouse.x + 280 + 30) > currWidth) {
		oDiv.style.left = (document.lmouse.x - 280 - 20) + 'px';
	} else {
		oDiv.style.left = (15 + document.lmouse.x) + 'px';
	}

	if(is_ie) {
		oDiv.style.top = (document.body.scrollTop + document.lmouse.y + 15) + 'px';
	} else {
		oDiv.style.top = (document.lmouse.y + 15) + 'px';
	}


	var r = "", i;
	var obj = screen;
	for(i in obj) {
		r += i + " = " + obj[i] + "\n";
	}

	title = title.replace(/</g, '&lt;').replace(/>/g, '&gt;');
	oDiv.innerHTML = "<b>" + title + "</b><br />" + tip;
	document.body.appendChild(oDiv);
	
	caller.onmouseout = function () {
		oDiv.parentNode.removeChild(oDiv);
		this.onmouseout = function () {};
	}
}


function returnNewTag(id, tag, tagObj) {
	var obj = document.getElementById(id);
	if(obj) {
		if(obj.value.length == 0) {
			obj.value = tag;
		} else {
			obj.value += ", " + tag;
		}
	}
	if(tagObj) {
		tagObj.onclick = function () {};
		tagObj.className = "disabledTag";
	}
}

function focusTagsInput(id) {
	var obj = document.getElementById(id);
	if(obj) {
		obj.focus();
	}
}

function checkDiscountSize(el, value) {
		if(value < 0) el.value = 0;
		if(value > 100) el.value = 100;
		return true;
	};
function checkProc() {
		var obj = $$('input#proc');
		if(obj) {
			if(obj[0]) {
				obj = obj[0];
				
				new Form.Element.Observer(
					$(obj),
					0.2,
					checkDiscountSize
				);
			} else {
				return false;
			}
		} else {
			return false;
		}
	};

