/* ****************************************************************** */
String.prototype.regExpEncode = function() {
	return this.replace(/([\$\(\)\*\+\.\[\]\?\\\^\{\}\|])/g, '\\$1');
}
/* ****************************************************************** */
function AR_get_sibling(obj, obj_name, sibl_name) {
	var s = obj.id.replace(new RegExp('_' + obj_name.regExpEncode() + '$'), '');
	var i = parseInt(s.match(/\d+$/));
	var ar_name = s.replace(/_\d+$/, '');
	return document.getElementById(ar_name + '_' + i + '_' + sibl_name);
}
/* **** Add new row by template ************************************* */
function AR_add(ar_name) {
	var tmplt = document.getElementById(ar_name + '_');
	if (tmplt == null) {
		alert('Не указан шаблон: ' + ar_name + '_');
		return;
	}
//	// find free index
//	if (!window.AR_last_index) window.AR_last_index = new Array();
//	if (!window.AR_last_index[ar_name]) {
//		for (
//			window.AR_last_index[ar_name] = 0;
//			document.getElementById(ar_name + '_' + window.AR_last_index[ar_name] + '_');
//			window.AR_last_index[ar_name] ++
//			);
//	}
//	else window.AR_last_index[ar_name] ++;
//	var index = window.AR_last_index[ar_name];

	// find free index
	var hdLastIndex = document.getElementById(ar_name + '_lastindex');
	if (!hdLastIndex) {
		hdLastIndex = document.createElement('INPUT');
		hdLastIndex.name = hdLastIndex.id = ar_name + '_lastindex';
		hdLastIndex.setAttribute('type', 'hidden');
		hdLastIndex.value = '';
		tmplt.parentNode.appendChild(hdLastIndex);
	}
	var index = parseInt(hdLastIndex.value);
	if (isNaN(index)) index = 0;
	for (; document.getElementById(ar_name + '_' + index + '_'); index ++);
	hdLastIndex.value = index;
	
//	// clone template node
//	var node = tmplt.cloneNode(true);
//	tmplt.parentNode.appendChild(node);
//	// replace id and name with new index
//	AR_recursive_replace(node, ar_name + '_', ar_name + '_' + index + '_');

	// clone template node
	var node = AR_clone_and_replace(tmplt, ar_name + '_', ar_name + '_' + index + '_');
	tmplt.parentNode.appendChild(node);

	// show new row
	if (node.style.display.toLowerCase() == 'none') node.style.display = '';
	if (node.style.visibility.toLowerCase() == 'hidden') node.style.visibility = '';
}
/* ****************************************************************** */
function addEventHandler(o, e, h, c) {
	e = e.replace(/^on/i,'');
	if (o.addEventListener) {
		o.addEventListener(e, h, c || false);
		if (e == 'mousewheel') o.addEventListener('DOMMouseScroll', h, c || false);
	}
	else if (o.attachEvent) {
		o.attachEvent('on' + e, h);
	}
	else {
		var oh = o['on' + e];
		o['on' + e] = oh ? function() { oh(); h(); } : h;
	}
}
/* ****************************************************************** */
function AR_clone_and_replace(tmplt, o, n) {
	if (!tmplt.tagName) {
		return tmplt.cloneNode(true);
	}
	var obj = document.createElement(tmplt.tagName);
	// if script : execute and assign text
	if (tmplt.tagName.toLowerCase() == 'script' && tmplt.text && tmplt.text.length > 0) {
		eval(obj.text = tmplt.text.replace(new RegExp(o.regExpEncode(), 'g'), n));
	}
	// clone attributes
	for (var i = 0; i < tmplt.attributes.length; i++) {
		if (tmplt.attributes[i].specified) {
			var nam = tmplt.attributes[i].name.toLowerCase() == 'class' ? 'className' : tmplt.attributes[i].name;
			var val = eval("tmplt." + nam);
			var typ = typeof(val);
			if (val == null) continue;
//			else if (nam.toLowerCase() == 'name') obj.setAttribute(nam, val);
			else if (typ == 'string')
				obj.setAttribute(nam, val.replace(new RegExp(o.regExpEncode(), 'g'), n));
			else if (typ == 'number')
				obj.setAttribute(nam, val);
			else if (typ == 'function') {
				var str = val.toString().replace(new RegExp(o.regExpEncode(), 'g'), n).replace(/^function[^{]+{\s*/i,'').replace(/\s*}$/i,'');
				obj.setAttribute(nam, str);
				addEventHandler(obj, nam, new Function(str));
			}
		}
	}
	// clone css styles
	for (var nam in tmplt.style) {
		var val = tmplt.style[nam];
		if (typeof(val) == 'string' && val.length > 0) { obj.style[nam] = val; }
	}

	var tn = tmplt.tagName.toLowerCase();
	if (tn == "table" || tn == "tbody" || tn == "tr" || tn == "ul" || tn == "ol" || tn == "dl") {
		for (var i = 0; i < tmplt.childNodes.length; i ++) {
			obj.appendChild(AR_clone_and_replace(tmplt.childNodes[i], o, n));
		}
	}
	else {
		var s = tmplt.innerHTML.replace(new RegExp(o.regExpEncode(), 'g'), n);
		obj.innerHTML = s;
		for(var sl = s.toLowerCase(); sl.length > 0;) {
			var i = sl.indexOf('<script');
			if (i < 0) break;
			sl = sl.substr(i); s = s.substr(i);
			i = sl.indexOf('</script>');
			if (i < 0) i = sl.length;
			var src = s.substr(0, i).replace(/<script[^>]*>/i, '');
			sl = sl.substr(i); s = s.substr(i);
			// Add script to obj and execute it
			var so = document.createElement('script');
			so.setAttribute('type', 'text/javascript');
			eval(so.text = src);
			obj.appendChild(so);
		}
	}
	return obj;
}
/* **** Replace ID and Name of object and childs ******************** */
function AR_recursive_replace(obj, o, n) {
	var l = o.length;
	if (obj.id) {
		var i = obj.id.indexOf(o);
		if (i >= 0){ obj.id = obj.id.substr(0, i) + n + obj.id.substr(l); }
	}
	if (obj.name) {
		var i = obj.name.indexOf(o);
		if (i >= 0){ obj.name = obj.name.substr(0, i) + n + obj.name.substr(l); }
	}
	/**/
	/*if (obj.attributes) {
		for (var i = 0; i < obj.attributes.length; i ++) {
			if (obj.attributes[i].specified && obj.attributes[i].value) {
				obj.setAttribute(
					obj.attributes[i].name,
					//obj.attributes[i].value.replace(o.regExpEncode(), n)
					obj.attributes[i].value.replace(o, n)
				);
				//alert (obj.attributes[i].value.replace(o, n));
			}
		}
	}/**/
	if (obj.childNodes) {
		for (var i = 0; i < obj.childNodes.length; i ++) {
			AR_recursive_replace (obj.childNodes[i], o, n);
		}
	}
}
/* **** Delete row which contains target object ********************* */
function AR_delete_this(ar_name, obj) {
	var ar_name_encoded = str.regExpEncode();
	if (obj == null) return;
	else if (new RegExp('^' + ar_name_encoded + '_[0-9]*_$', 'g').test(obj.id)) obj.parentNode.removeChild(obj);
	else AR_delete_this(ar_name, obj.parentNode);
}
/* ****************************************************************** */
function AR_over(ar_name, e) {
	if (!window.AR_sel_row) window.AR_sel_row = new Array();
	if (typeof(e) == 'string') e = document.getElementById(e);
	if (e != window.AR_sel_row[ar_name])
		e.className = 'tab_row_on';
}
function AR_out(ar_name, e) {
	if (!window.AR_sel_row) window.AR_sel_row = new Array();
	if (typeof(e) == 'string') e = document.getElementById(e);
	if (e != window.AR_sel_row[ar_name])
		e.className = 'tab_row';
}
function AR_sel(ar_name, e) {
	if (!window.AR_sel_row) window.AR_sel_row = new Array();
	if (typeof(e) == 'string') e = document.getElementById(e);
	if (window.AR_sel_row[ar_name])
		window.AR_sel_row[ar_name].className = 'tab_row';
	e.className = 'tab_row_sel';
	window.AR_sel_row[ar_name] = e;
}
/* **** Delete target row ******************************************* */
function AR_delete(ar_name) {
	if(window.AR_sel_row && window.AR_sel_row[ar_name]) {
		window.AR_sel_row[ar_name].parentNode.removeChild(window.AR_sel_row[ar_name]);
	}
}
/* ****************************************************************** */
function AR_count(ar_name) {
	var l = document.getElementById(ar_name + '_lastindex');
	if (l == null || isNaN(l = parseInt(l.value))) return 0;
	var c = 0;
	for (var i = 0; i <= l; i ++)
		if (document.getElementById(ar_name + '_' + i + '_') != null) c++;
	return c;
}
/* ****************************************************************** */

