var organizer= new Array();

function addCategory(parent, id, name){
	if(!organizer[parent]){
		organizer[parent] = new Array();
	}
	organizer[parent].push(new Array(id, replaceLig2JS(name)));
}

function clearSelectbox(selectEl){
	if(selectEl){
		while(selectEl.length > 0){
		 selectEl.remove(0);
		}
	}
}

function writeSelectBox(level, parent){
	selectEl = document.getElementById('category' + level);
	clearSelectbox(selectEl);
	var cats = organizer[parent];
	if(cats!=null) {
		for(i = 0; i < cats.length; i++){
			var y=document.createElement('option');
			y.text=cats[i][1];
			y.value=cats[i][0];
			try {
				selectEl.add(y, null);
			}
			catch(ex) {
				selectEl.add(y);
			}
		}
	}
}

function selectCategory( level, category ){
	var select = document.getElementById( 'category' + level );
	if(select!=null) for( var i in select.options ){
		var option = select.options[i];
		if(option!=null) {
			option.selected = (option.value == category) ;
		}
	}
}
var _timeout = false;
var _t = 500;

function showHint(msg, interval, elementId)
{
	if (interval && interval > 0) {
		_t = interval;
	}
	if (elementId) {
		doByElementId(msg, elementId);
	} else {
		var links = document.getElementsByTagName("a");
		for (var i=0; i<links.length; i++)
		{
			if (links[i].className == "popup")
			{
				links[i].onmouseover = function()
				{
					clearTimeout(_timeout);
					var popup = document.getElementById("popup");
					if (popup)
					{
						popup.style.display = "block";
						popup.style.top = getRealTop(this) + "px";
						popup.style.left = getRealLeft(this) + 24 + "px";
						popup.innerHTML = msg;
					}
				}
				links[i].onmouseout = function()
				{
					_timeout = setTimeout('displaynone()',_t);
				}
			}
		}
		
		var _popup = document.getElementById("popup");
		if(_popup)
		{
			_popup.onmouseover = function()
			{
				clearTimeout(_timeout);
			}
			_popup.onmouseout = function()
			{
				clearTimeout(_timeout);
				_timeout = setTimeout('displaynone()',_t);
			}
		}
	}
		
}

function doByElementId(msg, elementId) {
	var link = document.getElementById(elementId);
	link.onmouseover = function() {
		clearTimeout(_timeout);
		var popup = document.getElementById("popup");
		if (popup) {
			popup.style.display = "block";
			popup.style.top = getRealTop(this) + "px";
			popup.style.left = getRealLeft(this) + 24 + "px";
			popup.innerHTML = msg;
		}
	}
	link.onmouseout = function() {
		_timeout = setTimeout('displaynone()', _t);
	}
	var _popup = document.getElementById("popup");
	if(_popup)
	{
		_popup.onmouseover = function()
		{
			clearTimeout(_timeout);
		}
		_popup.onmouseout = function()
		{
			clearTimeout(_timeout);
			_timeout = setTimeout('displaynone()',_t);
		}
	}
}

function displaynone()
{
	document.getElementById("popup").style.left = "-1000%";
}

function getRealTop(elem)
{
	var nTop = 0;
	if(elem)
	{
		do
		{
			nTop += elem.offsetTop - elem.scrollTop;
			elem = elem.offsetParent;
		}
		while(elem)
	}
	return nTop;
}

function getRealLeft(elem)
{
	var nLeft = 0;
	if(elem)
	{
		do
		{
			nLeft += elem.offsetLeft - elem.scrollLeft;
			elem = elem.offsetParent;
		}
		while(elem)
	}
	return nLeft;
}

if (window.addEventListener)
	window.addEventListener("load", showHint, false);
else if (window.attachEvent && !window.opera)
	window.attachEvent("onload", showHint);


/* Simple AJAX Code-Kit (SACK) v1.6.1 */
/* c2005 Gregory Wild-Smith */
/* www.twilightuniverse.com */
/* Software licenced under a modified X11 licence,
   see documentation or authors website for more details */

// Improved by Andrey Prudnikov to process form with multiple inputs with the same name


function isArray(obj) {
	return (obj.constructor.toString().indexOf("Array") != -1);
}

function sack(file) {
	this.xmlhttp = null;

	this.resetData = function() {
		this.method = "POST";
  		this.queryStringSeparator = "?";
		this.argumentSeparator = "&";
		this.URLString = "";
		this.encodeURIString = true;
  		this.execute = false;
  		this.element = null;
		this.elementObj = null;
		this.requestFile = file;
		this.vars = new Object();
		this.responseStatus = new Array(2);
  	};

	this.resetFunctions = function() {
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompletion = function() { };
  		this.onError = function() { };
		this.onFail = function() { };
	};

	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}

		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	};

	this.setVar = function(name, value){
		if (isArray(value)) {
			var val = new Array();
			for (i=0; i < value.length; i++)
				val.push(value.toString());
		}
		else {
			var val = Array(value.toString());
		};
		if (typeof this.vars[name] == "undefined") {
			this.vars[name] = Array(val, false);
		}
		else {
			var arr = this.vars[name][0];
			arr = arr.concat(val);
			this.vars[name] = Array(arr, false);
		};
	};

	this.encVar = function(name, value, returnvars) {
		if (isArray(value)) {
			var val = new Array();
			for (i=0; i < value.length; i++)
				val.push(encodeURIComponent(value[i].toString()));
		}
		else {
			var val = Array(encodeURIComponent(value.toString()));
		};
		if (typeof this.vars[name] == "undefined") {
			value = Array(val, true);
		}
		else {
			var arr = this.vars[name][0];
			arr = arr.concat(val);
			value = Array(arr, true);
		};
		if (true == returnvars) {
			return Array(encodeURIComponent(name), val);
		} else {
			this.vars[encodeURIComponent(name)] = value;
		}
	}

	this.processURLString = function(string, encode) {
		encoded = encodeURIComponent(this.argumentSeparator);
		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
		varArray = string.split(regexp);
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split("=");
			if (true == encode){
				this.encVar(urlVars[0], urlVars[1]);
			} else {
				this.setVar(urlVars[0], urlVars[1]);
			}
		}
	}

	this.createURLString = function(urlstring) {
		if (this.encodeURIString && this.URLString.length) {
			this.processURLString(this.URLString, true);
		}

		if (urlstring) {
			if (this.URLString.length) {
				this.URLString += this.argumentSeparator + urlstring;
			} else {
				this.URLString = urlstring;
			}
		}

		// prevents caching of URLString
		this.setVar("rndval", new Date().getTime());

		urlstringtemp = new Array();
		for (key in this.vars) {
			if (false == this.vars[key][1] && true == this.encodeURIString) {
				encoded = this.encVar(key, this.vars[key][0], true);
				delete this.vars[key];
				this.vars[encoded[0]] = Array(encoded[1], true);
				key = encoded[0];
			}

			for (i=0; i < this.vars[key][0].length; i++) {
				urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0][i];
			};
		}
		if (urlstring){
			this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
		} else {
			this.URLString += urlstringtemp.join(this.argumentSeparator);
		}
	}

	this.runResponse = function() {
		eval(this.response);
	}

	this.runAJAX = function(urlstring) {
		if (this.failed) {
			this.onFail();
		} else {
			this.createURLString(urlstring);
			if (this.element) {
				this.elementObj = document.getElementById(this.element);
			}
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
//					totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
					totalurlstring = this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
					try {
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					} catch (e) { }
				}

				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState) {
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;
						case 3:
							self.onInteractive();
							break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;

							if (self.execute) {
								self.runResponse();
							}

							if (self.elementObj) {
								elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input"
								|| elemNodeName == "select"
								|| elemNodeName == "option"
								|| elemNodeName == "textarea") {
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							if (self.responseStatus[0] == "200") {
								self.onCompletion();
							} else {
								self.onError();
							}

							self.URLString = "";
							break;
					}
				};

				this.xmlhttp.send(this.URLString);
			}
		}
	};

	this.reset();
	this.createAJAX();
}



function whenLoading () {
}

function whenLoaded () {
}

function whenInteractive () {
}

function whenCompleted () {
}

var ajaxDIV = null
var ajaxURL = null

function whenError () {
	oajax = null
	oajax = new sack();
	if (ajaxDIV) if (typeof(ajaxDIV) == 'object') ajaxDIV.innerHTML = 'Can\'t load: ' + ajaxURL;
}

function request (_url, _method, _OnComplete_functionName) {
	oajax = null
	oajax = new sack();
	if (_method) oajax.method = _method;
	else oajax.method = 'GET';
	oajax.onLoading = whenLoading;
	oajax.onLoaded = whenLoaded; 
	oajax.onInteractive = whenInteractive;
	oajax.onCompletion = whenCompleted;
	oajax.onError = whenError;
	oajax.onFail = whenError;
	if (_OnComplete_functionName) oajax.OnCompleteFunctionCall = _OnComplete_functionName;
	else oajax.OnCompleteFunctionCall = whenCompleted
	oajax.runAJAX(_url);
}

function unescapeXML(str) {
	return str.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&').replace(/&quot;/g, '"').replace(/&apos;/g, "'");
};

function requestForm(_formid, _unescape, _func) {
	form = document.getElementById(_formid);
	oajax = new sack(form.action);
	oajax.method = 'POST';
	oajax.onLoading = whenLoading;
	oajax.onLoaded = whenLoaded; 
	oajax.onInteractive = whenInteractive;
	oajax.onCompletion = whenCompleted;
	oajax.onError = whenError;
	oajax.onFail = whenError;
	if (typeof _func == 'function') oajax.onCompletion = _func;
	for (i=0; i<form.elements.length; i++) {
		if (form.elements[i].type == 'checkbox') {
			var val = form.elements[i].checked?'true':'false';
			oajax.setVar(form.elements[i].name, _unescape?unescapeXML(val):val);
		}
		else {
			if (form.elements[i].tagName.toLowerCase() == 'select' && form.elements[i].multiple) {
				for (j=0; j < form.elements[i].options.length; j++) {
					if (form.elements[i].options[j].selected) {
						val = form.elements[i].options[j].value;
						oajax.setVar(form.elements[i].name, _unescape?unescapeXML(val):val);
					};
				};
			}
			else {
				var val = form.elements[i].value;
				oajax.setVar(form.elements[i].name, _unescape?unescapeXML(val):val);
			};
		};
	};
	oajax.runAJAX();
};

