function openSub(node, display_type) {
	next = node.nextSibling;
	
	 display_type =  display_type ?  display_type : 'block';
	
	if (next.nodeType != document.ELEMENT_NODE) next = next.nextSibling;
	
	if (next.style.display != display_type)
		next.style.display = display_type;
	else
		next.style.display = 'none';
}

function HideShow(id) {
	if (typeof(id) == 'object') {
		if (id.className=="show") id.className="hide";
		else id.className="show";
	} else if (obj=document.getElementById(id)) {
		if (obj.className=="show") obj.className="hide";
		else obj.className="show";
	}
}
function showHideNode(id, style) {
	if (typeof(id) == 'object') {
		if (id.style.display=='none') {
			id.style.visibility = "visible";
			id.style.display = style ? style : "block";
		} else {
			id.style.visibility = "hidden";
			id.style.display = "none";
		}
	} else if (obj=document.getElementById(id)) {
		if (obj.style.display=='none' || (obj.style.display=='' && obj.className.indexOf('hide')>-1)) {
			obj.style.visibility = "visible";
			obj.style.display = style ? style : "block";
		} else {
			obj.style.visibility = "hidden";
			obj.style.display = "none";
		}
	}
}

function showObj(id, style){
	if (id) {
		if (typeof(id) == 'object' && id) {
			id.style.visibility = "visible";
			id.style.display = style ? style : "block";
		} else if (obj = document.getElementById(id)) {
			obj.style.visibility = "visible";
			obj.style.display = style ? style : "block";
		}// else alert('`'+id+'` is not defined');
	}
}
		
function hideObj(id){
	if (id) {
		if (typeof(id) == 'object' && id) {
			id.style.visibility = "hidden";
			id.style.display = "none";
		} else if (obj = document.getElementById(id)) {
			obj.style.visibility = "hidden";
			obj.style.display = "none";
		}
	}
}

function findNextNode(el) {
	if (el) {
		var node = el.nextSibling;
		while (node && node.nodeType != document.ELEMENT_NODE) {
			node = node.nextSibling;
		}
		
		return node;
	}
}

function findPrevNode(el) {
	if (el) {
		var node = el.previousSibling;
		while(node && node.nodeType != document.ELEMENT_NODE) {
			node = node.previousSibling;
		}
		
		return node;
	}
}

function findNextNodeLike(el) {
	if (el) {
		var list = document.getElementsByTagName(el.tagName),
			drop_next = false;
		
		for(var i=0; i<list.length; i++) {
			if (drop_next) {node = list[i]; break;}
			if (list[i] == el) drop_next=true;
		}
		
		return node;
	}
}

function findPrevNodeLike(el) {
	if (el) {
		var list = document.getElementsByTagName(el.tagName),
			drop_next = false;
		
		for(var i=0; i<list.length; i++) {
			if (list[i] == el) drop_next=true;
			if (drop_next) {node = list[i-1]; break;}
		}
		
		return node;
	}
}

function clearObj(id){
	if (id) {
		if (typeof(id) == 'object') {
			id.innerHTML = "";
		} else if (obj = document.getElementById(id)){
			if (obj) obj.innerHTML = "";
		}
	}
}
function removeObj(id) {
	if (id) {
		if (typeof(id) == 'object' && id) {
			id.parentNode.removeChild(id);
		} else if (obj = document.getElementById(id)){
			if (obj) obj.parentNode.removeChild(obj);
		}
	}
}

function changeValue(id, value) {
	var inp = document.getElementById(id);
	if (inp) inp.value=value;
		//else alert("`"+inp+"` is not defined in changeValue()");
}
function getValue(id, func) {
	var inp = document.getElementById(id);
	if (inp) {
        if(inp.value) return inp.value;
    } //else alert('`' + id + '` not defined in getValue() called from '+func);
	
	return false;
}
function moveObj(obj, to_obj) {
	if (obj && to_obj) {
		to_obj.appendChild(obj);
	}
}
function moveBefore(obj, to_obj) {
	if (obj && to_obj) {
		to_obj.parentNode.insertBefore(obj, to_obj);
	}
}
function disableButton(id) {
    var but = getReference(id);
    if (but)
		but.disabled = true;
	else if (typeof(id) != 'undefined' && typeof(id.disabled) != 'undefined')
		id.disabled = true;
}
function enableButton(id) {
    var but = getReference(id);
    if (but)
		but.disabled=false;
	else if (typeof(id.disabled) != 'undefined')
		id.disabled = false;
}

function getReference(id) {
	var inp = document.getElementById(id);
	return inp;
}
	function $(id) {
		var inp = document.getElementById(id);
		return inp;
	}
function realEscape(str) {
	//Òîâà åñêåèïâà ' è \, à íå êàòî íà æàáàñêðèïò òúïèÿ escape
	if (!str) return false;
	
	str = str.split('\\').join('\\\\');
	str = str.split("'").join("\\'");
	
	return str;
}
function nl2br( str ) {
    return str.replace(/([^>])\n/g, '$1<br/>');
}
function br2nl( str ) {
    var re = /(<br\/>|<br>|<BR>|<BR\/>)/g;
	var s = str.replace(re, "\r\n");
	return s;
}
function _to_utf8(s) {
	var c, d = "";
	
	for (var i = 0; i < s.length; i++) {
		c = s.charCodeAt(i);
		if (c <= 0x7f) {
			d += s.charAt(i);
		} else if (c >= 0x80 && c <= 0x7ff) {
			d += String.fromCharCode(((c >> 6) & 0x1f) | 0xc0);
			d += String.fromCharCode((c & 0x3f) | 0x80);
		} else {
			d += String.fromCharCode((c >> 12) | 0xe0);
			d += String.fromCharCode(((c >> 6) & 0x3f) | 0x80);
			d += String.fromCharCode((c & 0x3f) | 0x80);
		}
	}
	return d;
}
function changeAttribute(id, attribute, value){
    var inp = document.getElementById(id);
	
	value = realEscape(value); //Òîâà åñêåèïâà ' è \, à íå êàòî íà æàáàñêðèïò òúïèÿ escape
	eval("inp."+attribute+"='"+value+"'");
}
function changeCssClass(id, css_class){
	var obj = document.getElementById(id);
	if (obj) obj.className = css_class;
}
function getCssClass(id){
	var obj = document.getElementById(id);
    return obj.className;
}

function GoTo(url) {
	location.href=url;
}
function goto_time(url, time) {
	setTimeout("location.href='"+url+"';",3000);
}
function urlCleanup(url, arr) {
    var new_url = new String;
	var new_url = '';
    url_org = url.split('?');
	
    if (url_org[1]) {
		url = url_org[1].split('#');
        url = url[0].split('&');
    	if (arr && (arr.length > 0)) {
			for(var u=0; u<url.length; u++) {
				if (typeof(url[u]) != 'undefined' && url[u] != '') {
					val = url[u].split('=');
					da_eba_javascript = false;
					for(var i=0; i<arr.length; i++) {
						da_eba_javascript = (val[0] == arr[i]) && (val[1] != '');
						if (da_eba_javascript === true) break;
                    }
					if (da_eba_javascript !== true) {
						new_url = new_url + '&' + val[0] + '=' + val[1];
						i=arr.length;
					}
                }
            }
        }
    }
	
	new_url = url_org[0] +  (new_url != '' ? '?' + new_url.substring(1) : '?'); //
    return new_url;
}

String.prototype.htmlEntities = function () {
   return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}

String.prototype.theEscape = function () {
    return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\s/g,'%20');
}
String.prototype.addSlashes = function () {
	return this.replace(/\\/g,"\\"+"\\").replace(/'/g,"\\"+"'").replace(/"/g,'\\'+'"');
}
if (!document.ELEMENT_NODE) {
	document.ELEMENT_NODE = 1;
	document.ATTRIBUTE_NODE = 2;
	document.TEXT_NODE = 3;
	document.CDATA_SECTION_NODE = 4;
	document.ENTITY_REFERENCE_NODE = 5;
	document.ENTITY_NODE = 6;
	document.PROCESSING_INSTRUCTION_NODE = 7;
	document.COMMENT_NODE = 8;
	document.DOCUMENT_NODE = 9;
	document.DOCUMENT_TYPE_NODE = 10;
	document.DOCUMENT_FRAGMENT_NODE = 11;
	document.NOTATION_NODE = 12;
}
document.javascripts = new Array();
document._importNode = function(node, allChildren) {
	switch (node.nodeType) {
		case document.ELEMENT_NODE:
			// IE е тъп като галош и се налага да се създаде нодето по друг "начин"
			if (document.all && node.getAttribute('name') != '')
				var newNode = document.createElement('<' + node.nodeName + ' name="' + node.getAttribute('name') + '" />');
			else
				var newNode = document.createElement(node.nodeName);
				
			var isJS = false;
			/* does the node have any attributes to add? */
			if (node.attributes && node.attributes.length > 0)
				for (var i = 0; i < node.attributes.length;) {
					if (node.attributes[i].nodeName == 'type' && node.getAttribute(node.attributes[i].nodeName) == 'text/javascript') {
						isJS = true;
						break;
						i++;
					} else {
						if (document.all) {
							// IE Ferever
							// IE Shits don't touch
							if (node.attributes[i].specified) {
								if (node.attributes[i].nodeName.indexOf('on') == 0) {
									switch(node.attributes[i].nodeName) {
										case 'onclick': newNode.onclick = node.getAttribute('onclick'); break;
										case 'onblur': newNode.onclick = node.getAttribute('onblur'); break;
										case 'ondblclick': newNode.onclick = node.getAttribute('ondblclick'); break;
										case 'onfocus': newNode.onclick = node.getAttribute('onfocus'); break;
										case 'onkeydown': newNode.onclick = node.getAttribute('onkeydown'); break;
										case 'onkeypress': newNode.onclick = node.getAttribute('onkeypress'); break;
										case 'onkeyup': newNode.onclick = node.getAttribute('onkeyup'); break;
										case 'onmousedown': newNode.onclick = node.getAttribute('onmousedown'); break;
										case 'onmousemove': newNode.onclick = node.getAttribute('onmousemove'); break;
										case 'onmouseout': newNode.onclick = node.getAttribute('onmouseout'); break;
										case 'onmouseover': newNode.onclick = node.getAttribute('onmouseover'); break;
										case 'onmouseup': newNode.onclick = node.getAttribute('onmouseup'); break;
									}
									
								}else if (node.attributes[i].nodeName == 'class')
									newNode.setAttribute('className', node.attributes[i].nodeValue);
								else if (node.attributes[i].nodeName == 'style')
									newNode.style.setAttribute('cssText', node.attributes[i].nodeValue);
								else
									newNode.setAttribute(node.attributes[i].nodeName, node.attributes[i].nodeValue);
							}
						} else newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i].nodeName));
						i++;
					}
				}
			/* are we going after children too, and does the node have any? */
			if (allChildren && node.childNodes && node.childNodes.length > 0)
				for (var i = 0; i < node.childNodes.length;) {
					var new_child = document._importNode(node.childNodes[i], allChildren);
					if (isJS) document.external_scripts[document.external_scripts.length] = new_child.nodeValue;
					 else if (newNode) newNode.appendChild(new_child);
					i++;
				}
			return newNode;
		break;
		
		case document.TEXT_NODE:
		case document.CDATA_SECTION_NODE:
		case document.COMMENT_NODE:
			return document.createTextNode(node.nodeValue);
		
		break;
	}
};
function disableBtn(btn_id, classCss) {
	var btn = getReference(btn_id);
	
	if (btn) {
		btn.className = classCss;
		btn.onclick = function () {return false;};
	} 
}
function undisableBtn(btn_id, classCss) {
	var btn = getReference(btn_id);
	
	if (btn) {
		btn.className = classCss;
		btn.onclick = null;
	} 
}
function checkAjax() {
    url = location.href;
	if (zXmlHttp.isSupported()) { // имаме ajax поддръжка
        xmlRequest('_sender.php?show=ajax', 'window.location.href='+url);
    }
}
function number_format(a, b, c, d) {
	b = b ? b : 0;
	c = c ? c : '.';
	d = d ? d : ',';
	
	if (a) {
		a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
		e = a + '';
		f = e.split('.');
		if (!f[0]) {
			f[0] = '0';
		}
		if (!f[1]) {
			f[1] = '';
		}
		if (f[1].length < b) {
			g = f[1];
			for (i=f[1].length + 1; i <= b; i++) {
				g += '0';
			}
			f[1] = g;
		}
		if(d != '' && f[0].length > 3) {
			h = f[0];
			f[0] = '';
			for(j = 3; j < h.length; j+=3) {
				i = h.slice(h.length - j, h.length - j + 3);
				f[0] = d + i +  f[0] + '';
			}
			j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
			f[0] = j + f[0];
		}
		c = (b <= 0) ? '' : c;
		return f[0] + c + f[1];
	}
}