/*
	JS Extending Library
	DOM functions.
	build 200806051950
*/

var dom = {
	getElementsByClass: function(searchClass, node, tag) {
		var classEl = [];
		if (! isset(node)) node = document;
		if (! isset(tag)) tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		
		for (var i = 0; i < elsLen; i++) {
			if (els[i].className == searchClass) classEl.push(els[i]);
		}

		return classEl;
	},
	
	addOption: function(oList, text, value, isDfltSel, isSel) {
		var oOpt = document.createElement("option");
		oOpt.appendChild(document.createTextNode(text));
		oOpt.setAttribute("value", value);

		if (isDfltSel) oOpt.defaultSelected = true;
		else {
			if (isSel) oOpt.selected = true;
		}

		oList.appendChild(oOpt);
	},
		
	getElementComputedStyle: function (elem, prop) {
		if (typeof elem != "object") var elem = $(elem);
	  
	// external stylesheet for Mozilla, Opera 7+ and Safari 1.3+
		if (document.defaultView   &&   document.defaultView.getComputedStyle) {
				if (prop.match(/[A-Z]/)) prop = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
				return document.defaultView.getComputedStyle(elem, '').getPropertyValue(prop);
		}
	  
	// external stylesheet for Explorer and Opera 9
		if (elem.currentStyle) {
			var i;
			while ((i = prop.indexOf("-")) != -1) prop = prop.substr(0, i) + prop.substr(i + 1, 1).toUpperCase() + prop.substr(i + 2);
			return elem.currentStyle[prop];
		}
	  
		return '';
	}
};

function $F(id, what) {
	switch (typeof(id)) {
		default:
			return false;
		break;
		case 'string':
			var o = $(id);
		break;
		case 'object':
			var o = id;
		break;
	}
	if (! o) return false;
	switch (o.tagName) {
		case 'TEXTAREA':
			return o[isset(what) ? what : 'value'];
		break;
		case 'INPUT':
			if (o.type == 'checkbox') return o[isset(what) ? what : 'checked'];
			return o[isset(what) ? what : 'value'];
		break;
		case 'SELECT':
			if (o.selectedIndex > -1) return o.options[o.selectedIndex][isset(what) ? what : 'value'];
		break;
	}
	return false;
}

function getCheckedValues(id) {
	var o, i = 0, res = [];
	o = $(id + i);
	while (o) {
		if (o.tagName == 'INPUT'   &&   o.type == 'checkbox'   &&   o.checked) res.push(o.value);
		++i;
		o = $(id + i);
	}
	return res;
}

function toggle(id, focus) {
	if (typeof(id) == 'string') var o = $(id);
	else var o = id;

	if (o) {
		if (o.style.display == 'none') {
			o.style.display = 'block';
			if (isset(focus)) {
				var focusO = $(focus);
				if (focusO) focusO.focus();
			}
		} else o.style.display = 'none';
	}
	return id;
}

function toggleOn(id, visibility) {
	if (! isset(visibility)) var visibility = false;
	if (typeof(id) == 'string') var o = $(id);
	else var o = id;
	
	if (o) {
		if (visibility) o.style.visibility = 'visible';
		else o.style.display = 'block';
	}
	return id;
}

function toggleOff(id, visibility) {
	if (! isset(visibility)) var visibility = false;
	if (typeof(id) == 'string') var o = $(id);
	else var o = id;
	
	if (o) {
		if (visibility) o.style.visibility = 'hidden';
		else o.style.display = 'none';
	}
	return id;
}

function toggleAll(id_prefix, id_postfix, start_from) {
	if (! isset(id_prefix)) return;
	if (! isset(id_postfix)) var id_postfix = '';
	if (! isset(start_from)) var start_from = 0;
	else start_from = parseInt(start_from);
	
	var o;
	
	for (var i = start_from;true;i++) {
		o = $(id_prefix + i + id_postfix);
		if (o) {
			if (o.style.display == 'none') o.style.display = 'block';
			else o.style.display = 'none';
		} else break;
	}
}

function checkAll(id, check, i, useList) {
	// Checks all checkboxes: id0, id1, id2,...
	if (isset(useList)) {
		for (var i = 0; i < useList.length; i++) $(useList[i]).checked = check;
		return;
	}
	
	var o;
	if (! isset(i)) var i = 0;
	if (! isset(check)) var check = true;
	while (o = $(id + i.toString())) {
		o.checked = check;
		i++;
	}
}
/**
 * Converts XML object into multi-dimensional array.
 *
 * @param OBJECT XML object.
 * @return OBJECT
 */
function xml2array(xml) {
	var nv, el, SO, i, cn = xml.childNodes, res = {};

	if (xml.attributes) {
		SO = xml.attributes.length;
		for (i = 0; i < SO; ++i) res[xml.attributes[i].nodeName] = xml.attributes[i].nodeValue;
	}
	SO = cn.length;
	for (i = 0; i < SO; ++i) {
		nv = cn[i].nodeValue;
		if (nv != null) {
			res['txt'] = nv;
			return res;
		} else res[cn[i].nodeName] = xml2array(cn[i]);
	}

	return res;
}
