VPO = {};
VPO.$ = function (obj) {
	if ("string" == typeof obj) {
		if ("object" == typeof VPO.xpath)
			obj = VPO.xpath.evaluate("//*[@id = '" + obj + "']", document).iterateNext();
		else
			obj = document.getElementById(obj);
	}
	return obj;
};
VPO.addEventListener = function(el, ev, func) {
	if (el.attachEvent) el.attachEvent('on' + ev, func);
	else el.addEventListener(ev, func, true);
};
VPO.removeEventListener = function(el, ev, func) {
	if (el.detachEvent) el.detachEvent('on' + ev, func);
	else el.removeEventListener(ev, func, true);
};
String.prototype.add = function (what) {
	if (this.match(new RegExp('\\b' + what + '\\b', 'g'))) return this;
	else return this + ' ' + what;
};
String.prototype.remove = function (what) {
	return this.replace(new RegExp('\\s*\\b' + what + '\\b\\s*', 'g'), ' ').replace(/\s*$/, '');
};
/*
String.prototype.replace = function (what, with) {
	return this.replace(new RegExp('\\s*\\b' + what + '\\b\\s*', 'g'), ' ').replace(/\s*$/, '');
};
*/
VPO.ajax = {};
VPO.ajax.execute = function(response, e) {
	var i;
	var obj;
	for (i in response) {
		obj = response[i];
		if ("object" == typeof obj && "string" == typeof obj.a && "function" == typeof this.handler[obj.a] && "object" == typeof obj.d) {
			if ("undefined" != typeof obj.d.e && obj.d.e == '#') {
				if ("undefined" != typeof e) {
					if (e.length) {
						for (var i=0; i < e.length; i++) {
							obj.d.e = e[i];
							this.handler[obj.a](obj.d);
						}
					}
					else {
						obj.d.e = e;
						this.handler[obj.a](obj.d);
					}
				}
			}
			else {
				this.handler[obj.a](obj.d);
			}
		}
	};
};
VPO.ajax.handler = {
	assign: function(obj) {
		if ("undefined" == typeof obj.e) return;
		obj.e = VPO.$(obj.e);
		if (obj.e && "string" == typeof obj.p && "undefined" != typeof obj.v) obj.e[obj.p] = obj.v;
		//obj.e[obj.p] = obj.v;
	},
	aadd: function(obj) {
		if ("undefined" == typeof obj.e) return;
		obj.e = VPO.$(obj.e);
		if (obj.e && "string" == typeof obj.p && "undefined" != typeof obj.v)
			obj.e[obj.p] = obj.e[obj.p].add(obj.v);
	},
	aremove: function(obj) {
		if ("undefined" == typeof obj.e) return;
		obj.e = VPO.$(obj.e);
		if (obj.e && "string" == typeof obj.p && "undefined" != typeof obj.v)
			obj.e[obj.p] = obj.e[obj.p].remove(obj.v);
	},
	areplace: function(obj) {
		if ("undefined" == typeof obj.e) return;
		obj.e = VPO.$(obj.e);
		if (obj.e && "string" == typeof obj.p && "undefined" != typeof obj.v)
			obj.e[obj.p] = obj.e[obj.p]
				.replace(new RegExp('\\s*\\b' + obj.v + '\\b\\s*', 'g'), ' ' + obj.w + ' ')
				.replace(/^\s*|\s*$/, '');
	},
	style: function(obj) {
		if ("undefined" == typeof obj.e) return;
		obj.e = VPO.$(obj.e);
		if (obj.e && "string" == typeof obj.p && "string" == typeof obj.v) obj.e.style[obj.p] = obj.v;
	},
	className: function(obj) {
		if ("undefined" == typeof obj.e) return;
		obj.e = VPO.$(obj.e);
		if (obj.e && "string" == typeof obj.a && "string" == typeof obj.v) {
			switch (obj.a) {
				case "s": obj.e.className = obj.v; break;
				case "a": obj.e.className.add(obj.v); break;
				case "r": obj.e.className.remove(obj.v); break;
			}
		}
	},
	redirect: function(obj) {
		if ("undefined" == typeof obj.l) return;
		if ("undefined" == typeof obj.t)
			document.location.assign(obj.l);
		else
			setTimeout(function(url) { document.location.assign(obj.l); }, obj.t, obj.l);
	},
/*
	popup: function (obj) {
		if ("undefined" == typeof obj.e) return;
		obj.e = VPO.$(obj.e);
		if (! obj.e) return;
		if ("string" == typeof obj.v && "boolean" == typeof obj.k && "function" == typeof VPO.popup) {
			var popup = new VPO.popup(obj.e); popup.show(obj.v, obj.k);
		}
	},
*/
/*
	div: function (obj) {
		if ("string" == typeof obj.e && "string" == typeof obj.v && "function" == typeof VPO.div)
			var div = new VPO.div(obj.e, obj.v);
	},
*/
/*
	popup: function (obj) {
		if ("string" == typeof obj.e && "object" == typeof obj.d)
			VPO.DOM.popup(obj.e, obj.d);
	},
*/
	css: function (obj) {
		if ("string" == typeof obj.f && "string" == typeof obj.m)
			VPO.CSS.add(obj.f, obj.m, obj.a, obj.p);
	},
	alert: function (obj) {
		if ("undefined" == typeof obj.m) return;
		alert(obj.m);
	},
	focus: function (obj) {
		if ("undefined" == typeof obj.e) return;
		obj.e = VPO.$(obj.e);
		if (obj.e) obj.e.focus();
	},
	script: function (obj) {
		if ("undefined" == typeof obj.s) return;
		eval(obj.s);
	},
	dom: function(obj) {
		if ("undefined" == typeof obj.e) return;
//		if ("object" == typeof obj.a) obj.a = VPO.DOM.animate(obj.a);
		if ("undefined" != typeof obj.d && "string" == typeof obj.f && "function" == typeof VPO.DOM[obj.f])
			VPO.DOM[obj.f](obj)
	},
	cond: function (obj) {
		if ("undefined" != typeof obj.c && "function" == typeof VPO.DOM.cond[obj.c] && "undefined" != typeof obj.d)
			var e = VPO.DOM.cond[obj.c](obj.e, obj.d);
			if (e) {
				if ("undefined" != typeof obj.y) VPO.ajax.execute(obj.y.a, e);
			}
			else if ("undefined" != typeof obj.n) VPO.ajax.execute(obj.n.a, e);
	},
	animate: function(obj) {
		if ("undefined" == typeof obj.e) return;
		if ("object" == typeof obj.d && "string" == typeof obj.f && "function" == typeof VPO.DOM.animate[obj.f])
			VPO.DOM.animate[obj.f](obj)
	}
};

VPO.ajax.fakeID = function(e, id) {
	ee = VPO.$(id);
	if (ee) {
		ee.removeAttribute('id');
		if ("undefined" != typeof e.fakeID) delete e.fakeID;
	}
	e = VPO.$(e);
	if (e) {
		e.setAttribute('id', id);
		e.fakeID = true;
	}
};

VPO.ajax.getFormValues = function(parent, aFormValues) {
	parent = VPO.$(parent);
	
	if ("undefined" == typeof aFormValues) aFormValues = {};
	
	if (parent && parent.childNodes) {
		this._getFormValues(aFormValues, parent.childNodes);
	}
	
//	alert('X: ' + aFormValues.handle);
	return aFormValues;
};

VPO.ajax._getFormValues = function(aFormValues, children) {
	var iLen = children.length;
	for (var i = 0; i < iLen; ++i) {
		var child = children[i];
		if ('undefined' != typeof child.childNodes) {
			this._getFormValues(aFormValues, child.childNodes);
		}
		this._getFormValue(aFormValues, child);
	}
};

VPO.ajax._getFormValue = function(aFormValues, child) {

	if (!child.name) {
		return;
	}

	if (child.type) {
		if (child.type == 'radio' || child.type == 'checkbox') {
			if (false == child.checked) return;
		}
	}

	var name = child.name;

	var values = [];
	if ('select-multiple' == child.type) {
		var jLen = child.length;
		for (var j = 0; j < jLen; ++j) {
			var option = child.options[j];
			if (true == option.selected) values.push(option.value);
		}
	}
	else {
		values = child.value;
	}
	
	var keyBegin = name.indexOf("[");
	if (0 <= keyBegin) {
		var n = name;
		var k = n.substr(0, n.indexOf("["));
		var a = n.substr(n.indexOf("["));
		if ((typeof aFormValues[k]) == 'undefined') {
			aFormValues[k] = [];
		}
		var p = aFormValues; // pointer reset
		while (a.length != 0) {
			var sa = a.substr(0, a.indexOf("]")+1);
			a = a.substr(a.indexOf("]")+1);
			p = p[k];
			k = sa.substr(1, sa.length-2);
			if (k == "") k = p.length;
			if ((typeof p[k]) == 'undefined') p[k] = [];
		}
		p[k] = values;
	}
	else {
		aFormValues[name] = values;
	}
};

VPO.ajax.JSON = function () {

	function f(n) {	return n < 10 ? '0' + n : n; }

	Date.prototype.toJSON = function (key) { return this.getUTCFullYear() + '-' + f(this.getUTCMonth() + 1) + '-' + f(this.getUTCDate()) + 'T' + f(this.getUTCHours()) + ':' + f(this.getUTCMinutes()) + ':' + f(this.getUTCSeconds()) + 'Z'; };

	String.prototype.toJSON = Number.prototype.toJSON = Boolean.prototype.toJSON = function (key) { return this.valueOf(); };

	var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, escapeable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, gap, indent, meta = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\' }, rep;

	function quote(string) {
		escapeable.lastIndex = 0;
		return escapeable.test(string) ?
		'"' + string.replace(escapeable, function (a) {
			var c = meta[a];
			if (typeof c === 'string') return c;
			return '\\u' + ('0000' + (+(a.charCodeAt(0))).toString(16)).slice(-4);
		}) + '"' :
		'"' + string + '"';
	}

	function str(key, holder) {
		var i, k, v, length, mind = gap, partial, value = holder[key];
		if (value && typeof value === 'object' && typeof value.toJSON === 'function') value = value.toJSON(key);
		if (typeof rep === 'function') value = rep.call(holder, key, value);
		switch (typeof value) {
		case 'string':
			return quote(value);
		case 'number':
			return isFinite(value) ? String(value) : 'null';
		case 'boolean':
		case 'null':
			return String(value);
		case 'object':
			if (!value) return 'null';
			gap += indent;
			partial = [];
			if (typeof value.length === 'number' && !(value.propertyIsEnumerable('length'))) {
				length = value.length;
				for (i = 0; i < length; i += 1) {
					partial[i] = str(i, value) || 'null';
				}
				v = partial.length === 0 ? '[]' : gap ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' : '[' + partial.join(',') + ']';
			    gap = mind;
			    return v;
			}
			if (rep && typeof rep === 'object') {
				length = rep.length;
				for (i = 0; i < length; i += 1) {
					k = rep[i];
					if (typeof k === 'string') {
						v = str(k, value, rep);
						if (v) partial.push(quote(k) + (gap ? ': ' : ':') + v);
					}
				}
			} else {
				for (k in value) {
					if (Object.hasOwnProperty.call(value, k)) {
						v = str(k, value, rep);
						if (v) partial.push(quote(k) + (gap ? ': ' : ':') + v);
					}
				}
			}

			v = partial.length === 0 ? '{}' : gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' : '{' + partial.join(',') + '}';
			gap = mind;
			return v;
		}
	}

	return {
		stringify: function (value, replacer, space) {
			var i; gap = ''; indent = '';
			if (typeof space === 'number') {
				for (i = 0; i < space; i += 1) {
					indent += ' ';
				}
			} else if (typeof space === 'string') {
				indent = space;
			}

			rep = replacer;
			if (replacer && typeof replacer !== 'function' && (typeof replacer !== 'object' || typeof replacer.length !== 'number'))
			    throw new Error('JSON.stringify');

			return str('', {'': value});
		},
        parse: function (text, reviver) {
			var j;
			function walk(holder, key) {
				var k, v, value = holder[key];
				if (value && typeof value === 'object') {
					for (k in value) {
						if (Object.hasOwnProperty.call(value, k)) {
						    v = walk(value, k);
						    if (v !== undefined) value[k] = v;
						    else delete value[k];
						}
					}
				}
				return reviver.call(holder, key, value);
			}
			cx.lastIndex = 0;
			if (cx.test(text)) text = text.replace(cx, function (a) { return '\\u' + ('0000' + (+(a.charCodeAt(0))).toString(16)).slice(-4);});
			if (/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
				j = eval('(' + text + ')');
				return typeof reviver === 'function' ? walk({'': j}, '') : j;
			}
			throw new SyntaxError('JSON.parse');
		}
	};
}();

VPO.ajax.call = function(url, func, args) {
	var oScript = document.getElementById('vpo.ajax.script');
	var head = document.getElementsByTagName("head").item(0);
	if (oScript) head.removeChild(oScript);
   	oScript = document.createElement("script");
	oScript.setAttribute("src", url + '?json=' + encodeURIComponent(this.JSON.stringify({func: func, args: args})));
	oScript.setAttribute("id",'vpo.ajax.script');
    head.appendChild(oScript);
	return true;
};
VPO.ajax.response = function(response) {
	if ("object" == typeof response && "undefined" != typeof response.length)
		VPO.ajax.execute(response);
};

VPO.CSS = {};
VPO.CSS.add = function (file, media, additional) {
	var t;
	if ("undefined" != typeof additional && "undefined" != typeof additional.target) eval('t = ' + additional.target);
	else t = document;
	var head = t.getElementsByTagName("head").item(0);
	var links = head.getElementsByTagName("link");
	var exists = false;
	for (i in links) {
		if (links[i].rel == 'stylesheet' && links[i].href == file)
			exists = true;
	}
	if (!exists) {
		var css;
		if ("undefined" != typeof additional) {
			for (k in additional) {
				if (k == 'prepend') {
					css = t.createElement('style');
					css.setAttribute('type', 'text/css');
					if ("undefined" == typeof css.styleSheet)
						css.appendChild(t.createTextNode(additional[k]));
					else
						css.styleSheet.cssText = additional[k];
					head.appendChild(css);
				}
			};
		}
		css = t.createElement('link');
		css.setAttribute('type', 'text/css');
		css.setAttribute('rel', 'stylesheet');
		css.setAttribute('href', file);
		css.setAttribute('media', media);
	    head.appendChild(css);
	}
};

VPO.CSS.getStyle = function(el, css) {
	var style;
	if (document.defaultView && document.defaultView.getComputedStyle)
		style = document.defaultView.getComputedStyle(el, "").getPropertyValue(css);
	else if (el.currentStyle)
		style = el.currentStyle[this.camelize(css)];
	return style;
};
VPO.CSS.camelize = function(string) {
	var oStringList = string.split('-');
	if (oStringList.length == 1) return oStringList[0];
	
	var camelizedString = string.indexOf('-') == 0
		? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1)
		: oStringList[0];
	
	for (var i = 1, len = oStringList.length; i < len; i++) {
		var s = oStringList[i];
		camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
	}
	return camelizedString;
};
VPO.CSS.getWidth = function(el) {
	return (parseInt(el.style.width||this.getStyle(el,'width'))||(el.offsetWidth- (parseInt(this.getStyle(el,'padding-left'))||0) - (parseInt(this.getStyle(el,'padding-right'))||0)
	- (parseInt(this.getStyle(el,'border-left-width'))||0) - (parseInt(this.getStyle(el,'border-right-width'))||0))||0);
};
VPO.CSS.getHeight = function(el) {
	return (parseInt(el.style.height||this.getStyle(el,'height'))||(el.offsetHeight- (parseInt(this.getStyle(el,'padding-top'))||0) - (parseInt(this.getStyle(el,'padding-bottom'))||0)	- (parseInt(this.getStyle(el,'border-top-width'))||0) - (parseInt(this.getStyle(el,'border-bottom-width'))||0))||0);
};
VPO.CSS.getScrollHeight = function(el) {
	return el.scrollHeight
		- (parseInt(this.getStyle(el,'padding-top'))||0) - (parseInt(this.getStyle(el,'padding-bottom'))||0)
		- (parseInt(this.getStyle(el,'border-top-width'))||0) - (parseInt(this.getStyle(el,'border-bottom-width'))||0);
};

VPO.DOM = {};
VPO.DOM.build = function (list, el, before) {
	if (list.length) {
		for (var i in list) {
			var ea = false;
			if ("string" == typeof list[i])
				ea = document.createTextNode(list[i]);
			else if ("object" == typeof list[i]) {
				var err;
				try { // IE hack for radio buttons, pfuj jebenti!
					if (list[i].t == 'input' && "undefined" != typeof list[i].a['type'] && list[i].a['type'] == 'radio') {
						ea = '<input';
						for (var a in list[i].a) ea += ' ' + a + '="' + list[i].a[a] + '"';
						ea = document.createElement(ea + '>');
					}
					else throw true;
				}
				catch (err) {
					ea = document.createElement(list[i].t);
					VPO.DOM.attrs(ea, list[i].a);
				}
			}
			if (ea) {
				var m;
				if (before) el.insertBefore(ea,before);
				else el.appendChild(ea);
				if ("undefined" != typeof list[i].c) this.build(list[i].c, ea);
			}
		}
	}
};
VPO.DOM.attrs = function(e, attrs) {
	if ("object" == typeof attrs) {
		var attrsold = {};
		for (var i=0; i < e.attributes.length; i++) {
			attrsold[e.attributes[i].name] = true;
		}
		for (var a in attrs) {
			if (m = a.match(/^on(.*)$/))
				eval('e.' + a + ' = function(e) {' + attrs[a] + '};');
			else if (a == 'style' && "undefined" != typeof e.style.setAttribute) // IE hack
				e.style.setAttribute('cssText', attrs[a]);
			else {
				var attr = document.createAttribute(a);
				attr.nodeValue = attrs[a];
				e.setAttributeNode(attr);
			}
			if ("undefined" != attrsold[a]) delete attrsold[a];
		}
		for (var a in attrsold) {
			e.removeAttribute(a);
		}
	}
};
VPO.DOM.cond = {
	haschild: function(e, obj) {
		e = VPO.$(e);
		var children = [];
		if (e) {
			var childNodes;
			if ("undefined" != typeof obj.t)
				childNodes = e.getElementsByTagName(obj.t);
			else
				childNodes = e.childNodes;
			for (var i=0; i < childNodes.length; i++) {
				children[children.length] = childNodes[i];
			};
		}
		return (children.length ? children[0] : false);
	},
	xpath: function(e, obj) {
		e = VPO.$(e);
		var list = [];
		if (e) {
			var nodeIterator = VPO.xpath.evaluate(obj.p, e);
			var node;
			while (node = nodeIterator.iterateNext()) {
				list[list.length] = node;
			}
		}
		return (list.length ? list : false);
	},
	confirm: function(e, obj) {
		if ("string" == typeof obj.t) return confirm(obj.t);
	}
};

VPO.DOM.popup = function (obj) {
	var e = VPO.$(obj.e);
	if (e) e.parentNode.removeChild(e);
	var a = {id:obj.e,style:''}; // ,style:'display:none'
	if (obj.cn) a['class'] = obj.cn;
	if (obj.al) {
		obj.al.e = VPO.$(obj.al.e);
		if (obj.al.e) {
			var x = 0, y = 0;
			var o = obj.al.e;
			while (o.tagName != 'BODY') {
				x += o.offsetLeft;
				y += o.offsetTop;
				o = o.offsetParent;
			}
			if (obj.al.s == 'right') x = document.body.clientWidth - (x + obj.al.e.offsetWidth);
			a.style += 'top:' + (y + obj.al.e.offsetHeight) + 'px;' + obj.al.s + ':' + x + 'px;';
		}
	}
/*	if ("object" == typeof obj.a) a.style += obj.a.getStyle() + ';';*/
	var b = [];
	var c = [];
	if (obj.pr) {
		for (var i=0; i < obj.pr.length; i++) {
			var e = VPO.$(obj.pr[i]);
			if (e) e.parentNode.removeChild(e);
			b[b.length] = {t:'div',a:{id:obj.pr[i]}};
			c[c.length] = obj.pr[i];
		};
	}
	b[b.length] = {t:'div',a:a,c:obj.d};
	var t;
	if ("undefined" != typeof obj.a && "undefined" != typeof obj.a.target) eval('t = ' + obj.a.target);
	else t = document.body;
	this.build(b,t);
	e = VPO.$(obj.e);
	for (var i=0; i < c.length; i++) {
		c[i] = VPO.$(c[i]);
	};
	c[c.length] = e;
	if (obj.al && obj.al.e.fakeID) e.clearID = obj.al.e;
	e.close = function (list) {
		return function() {
			for (var i=0; i < list.length; i++) {
				if (list[i].clearID) list[i].clearID.removeAttribute('id');
				list[i].parentNode.removeChild(list[i]);
			};
			if ("function" == typeof obj.clickhandler)
				VPO.removeEventListener(document.body, 'click', this.clickhandler);
		};
	}(c);
	if (obj.c) {
		e.clickhandler = VPO.DOM.popup[obj.c](e);
		setTimeout(function() { VPO.addEventListener(document.body, 'click', e.clickhandler); }, 500);
	}
};
VPO.DOM.popup.k = function (ev,obj,k) {
	if (! ev) ev = window.event;
	var target = ev.srcElement ? ev.srcElement : ev.target;
	var stop = false;
	while (target.tagName != 'BODY' && !( stop = (target == obj))) {
		target = target.parentNode;
	}
	if (stop == k) obj.close();
};
VPO.DOM.popup.c = function(obj) {
	return function (ev) { VPO.DOM.popup.k(ev,obj,true); };
};
VPO.DOM.popup.o = function(obj) {
	return function (ev) { VPO.DOM.popup.k(ev,obj,false); };
};
VPO.DOM.popup.a = function(obj) {
	return function (ev) { obj.close(); };
};
VPO.DOM.replace = function (obj) {
	obj.e = VPO.$(obj.e);
	if (obj.e) {
		while (obj.e.firstChild) obj.e.removeChild(obj.e.firstChild);
		this.build(obj.d,obj.e);
	}
};

VPO.DOM.remove = function (obj) {
	obj.e = VPO.$(obj.e);
	if (obj.e) obj.e.parentNode.removeChild(obj.e);
};

VPO.DOM.update = function (obj) {
	obj.e = VPO.$(obj.e);
	if (obj.e) {
		if (obj.e.tagName.toLowerCase() == obj.d[0].t.toLowerCase()) {
			VPO.DOM.attrs(obj.e, obj.d[0].a);
/*
			for (var i=0; i < obj.e.attributes.length; i++) {
				if ("undefined" == typeof obj.d[0].a[obj.e.attributes[i].name] || obj.d[0].a[obj.e.attributes[i].name] != obj.e.attributes[i].value) {
					obj.e.setAttribute(obj.e.attributes[i].name, obj.d[0].a[obj.e.attributes[i].name]);
					if ("undefined" != typeof obj.d[0].a[obj.e.attributes[i].name])
						delete obj.d[0].a[obj.e.attributes[i].name];
				}
			};
			for (i in obj.d[0].a) {
				obj.e.setAttribute(i, obj.d[0].a[i]);
			};
*/
			while (obj.e.firstChild) obj.e.removeChild(obj.e.firstChild);
			this.build(obj.d[0].c,obj.e);
			if ("function" == typeof obj.e.onchange) obj.e.onchange();
		}
	}
};


