/**
 * Data type functions
 */
function isArray(a)
{
    return isObject(a) && a.constructor == Array;
}
function isObject(a)
{
    return (a && typeof a == 'object') || isFunction(a);
}
function isFunction(a)
{
    return typeof a == 'function';
}
function isNumeric(s)
{
	var i;
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9")))
			return false;
	}
	
	// All characters are numbers.
	return true;
}
function isString(s)
{
	return typeof s == 'string';
}

/**
 * Array functions
 */
function inArray(values_arr, ch_value)
{
	for (var i = 0; i < values_arr.length; i++)
	{
		if (values_arr[i] == ch_value)
			return true;
	}
	
	return false;
}

/**
 * Inheritance
 */
Function.prototype.extend = function(parent)
{
	var aMatch = parent.match(/([^\.]+)$/);
	parent = eval(parent);
	this.prototype[aMatch[1]] = parent;
	for (var m in parent.prototype) {
		this.prototype[m] = parent.prototype[m];
	}
}

/**
 * Document object model DOM accessor
 */
function getElement(id)
{
	if (document.layers)
		return document.layers[id];
	else if (document.all)
		return document.all[id];
	else if (document.getElementById(id))
		return document.getElementById(id);
	
	return null;
}
function getElementsByTagNameClassName(tagName, incClassName, excClassName, doc)
{
	excClassName = excClassName || 0;
	doc = doc || document.body;
	var coll = doc.getElementsByTagName(tagName);
	var elems = new Array();
	for (var i=0; i<coll.length; i++) {
		if (coll[i].className.indexOf(incClassName) != -1 && (!excClassName || coll[i].className.indexOf(excClassName) == -1))
			elems.push(coll[i]);
	}
	return elems;
}

/**
 * Events
 */
function addListener(obj, ev, func)
{
	if (obj.attachEvent) {
		obj.attachEvent(('on' + ev), func);
	}
	if (obj.addEventListener) {
		obj.addEventListener(ev, func, false);
		ev = ev.toUpperCase()
		document.captureEvents(Event[ev]);
	}
}
function removeListener(obj, ev, func)
{
	if (obj.detachEvent) {
		obj.detachEvent("on" + ev, func);
	}
	if (obj.removeEventListener) {
		obj.removeEventListener(ev, func, false);
	}
}

/**
 * Focus accessors
 */
function setFocus(id)
{
	var object = getElement(id);
	if (object)
		object.focus();
}

/**
 * Style accessors
 */
function getStyle(obj)
{
	if (!isObject(obj))
		obj = getElement(obj);
	
	return obj.className;
}

function setStyle(obj, style)
{
	if (!isObject(obj))
		obj = getElement(obj);
	obj.className = style;
}

function triggerStyle(obj, style1, style2)
{
	if (!isObject(obj))
		obj = getElement(obj);
	if (obj.className == style1)
		obj.className = style2;
	else 
		obj.className = style1;
}

/**
 * Form accessors
 */
function getForm(name)
{
	return document.forms[name];
}

function getFormElement(form, element)
{
	var form = getForm(form);
	if (form) {
		return form[element];
	}
	return null;
}

function setFormElementValue(form, element, value)
{
	var element = getFormElement(form, element);
	if (element) {
		element.value = value;
		return true;
	}
	return false;
}

function setFormAction(name, action)
{
	var form = getForm(name);
	if (form) {
		form.action = action;
		return true;
	}
	return false;
}

function submitForm(name)
{
	var form = getForm(name);
	if (form) {
		if (!form.onsubmit || form.onsubmit())
			form.submit();
	}
	return false;
}

/**
 * Submit a form when a key is pressed
 * @param event
 * @param string Form name
 * @param array  Keys, by which to submit
 */
function submitKeysForm(e, name, keys)
{
	// keys by whom to submit
	if (!keys)
		keys = [13, null];
	if (!isArray(keys))
		keys = [keys, null];
	
	// pressed key
	var key;
	if (window.event) {
		key = event.keyCode;
	}
	else {
		key = e.which;
	}

	// onPressed action
	if (inArray(keys, key))
	{
		return submitForm(name);
	}
	
	return null;
}

/**
 * Value accessors
 */
function setValue(obj, value)
{
	if (!isObject(obj))
		obj = getElement(obj);
	if (obj) {
		obj.value = value;
		return true;
	}
	return false;
}

function getValue(obj)
{
	if (!isObject(obj))
		obj = getElement(obj);
	if (obj)
		return obj.value;
	return false;
}

/**
 */
function getHTML(obj)
{
	if (!isObject(obj))
		obj = getElement(obj);
	return obj.innerHTML;
}

/**
 */
function setHTML(obj, html)
{
	if (!isObject(obj))
		obj = getElement(obj);
	obj.innerHTML = html;
}

function loadHTML(obj, htmlAddress, loadMethod)
{
	var xml_http = new XMLHttp();
	xml_http.onLoad = function(data) {
		setHTML(obj, data);
	}
	return xml_http.load(htmlAddress, loadMethod);
}

function redirect(loc)
{
	window.location = loc;
	return false;
}

XMLHttp = function()
{
	try { this.obj = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) {
		try { this.obj = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch (e) { this.obj = false; }
	}
	if (!this.obj && typeof XMLHttpRequest != 'undefined') {
		try { this.obj = new XMLHttpRequest(); }
		catch (e) { this.obj = false; }
	}
	if (!this.obj && window.createRequest) {
		try { this.obj = window.createRequest(); }
		catch (e) { this.obj = false; }
	}
}
XMLHttp.prototype.avail = function()
{
	return (this.obj != false);
}
XMLHttp.prototype.load = function(htmlAddress, loadMethod, ret)
{
	loadMethod = loadMethod || "GET";
	if (this.obj) {
		var self = this;
		var xmlhttp = this.obj;
		xmlhttp.open(loadMethod, htmlAddress, true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				if (ret == "XML") self.onLoad(xmlhttp.responseXML);
				else self.onLoad(xmlhttp.responseText);
			}
		}
		xmlhttp.send(null);
		return true;
	}
	return false;
}
XMLHttp.prototype.isStarted = function()
{
	return this.obj.readyState > 0;
}
XMLHttp.prototype.isLoaded = function()
{
	return this.obj.readyState == 4 && this.obj.status == 200;
}

/**
 * Cookie accessors
 */
function setCookie(name, value, expires, path, domain)
{
	if (!expires) {
		expires = new Date ();
		expires.setTime(expires.getTime() + 60*60*24*30); 
	}
	
	if (!path) {
		path = "/";
	}
	
	if (!domain) {
		domain = location.host;
	}
	
	var cookieString = name + " = " + value + "; expires=" + expires.toGMTString() + "; path=" + path + ";";// domain=" + domain;
	document.cookie = cookieString;
	
	return true;
}

/**
 * Get a cookie value
 */
function getCookie()
{
}

/**
 * Name, pathname input automatization
 */
function getFilename(path)
{
	var backSlash = path.split('\\');
	var forwSlash = path.split('/');
	var slash = (backSlash.length > forwSlash.length) ? '\\' : '/';
	return path.substring((path.lastIndexOf(slash) + 1), path.lastIndexOf('.'));
}

function copy(value, id, replace_)
{
	if (!getValue(id) || replace_) {
		setValue(id, value);
		return true;
	}
	return false;
}

/**
 * Other common functions
 */
function getDisplay(obj)
{
	if (!isObject(obj))
		obj = getElement(obj);

	if (obj.style.display == 'none')
		return false
	else
		return true;
}

function setDisplay(obj, display)
{
	if (!isObject(obj))
		obj = getElement(obj);
	if (!display)
		obj.style.display = 'none';
	else
		obj.style.display = '';
	return true;
}

function triggerDisplay(obj)
{
	if (!getDisplay(obj))
		setDisplay(obj, true);
	else
		setDisplay(obj, false);
}

function getVisibility(obj)
{
	if (!isObject(obj))
		obj = getElement(obj);
	
	if (obj.style.visibility == 'hidden')
		return false;
	else
		return true;
}

function setVisibility(obj, visible)
{
	if (!isObject(obj))
		obj = getElement(obj);
	
	if (!visible)
		obj.style.visibility = 'hidden';
	else
		obj.style.visibility = 'visible';
}

function triggerVisibility(id)
{
	if (!getVisibility(id))
		setVisibility(id, true);
	else
		setVisibility(id, false);
}

