
function imageCheckboxClick(vis_name, val_name, img_checked, img_not_checked) {

	var vis_obj = getObject(vis_name);
	var val_obj = getObject(val_name);

	if (vis_obj && val_obj) {

		if (val_obj.value == 1) {
			val_obj.value = 0;
			vis_obj.src = img_not_checked;
		} else {
			val_obj.value = 1;
			vis_obj.src = img_checked;
		}
	}

	return false;
}

function relationalSelectChange(src_name, dst_name, options) {

	var src_obj = getObject(src_name);
	var dst_obj = getObject(dst_name);

	if (src_obj && dst_obj) {
		dst_obj.options.length = 0;

		for (var id = 0; id < options.length; id++) {
			if (options[id][0] == src_obj.value) {
				for (var index = 0; index < options[id][1].length / 2; index++) {
					dst_obj.options[index] = new Option(options[id][1][index * 2 + 1], options[id][1][index * 2]);
				}
			}
		}
	}
}

function fancyRadioSelect(prev_vis_name, val_name, vis_name, value) {

	var val_obj = getObject(val_name);
	var vis_obj = getObject(vis_name);

	if (val_obj) {
		val_obj.value = value;
	}

	try {
		var prev_vis_name_obj = getObject(prev_vis_name);
		if (prev_vis_name_obj) {
			if (prev_vis_name_obj.value) {
				var prev_vis_obj = getObject(prev_vis_name_obj.value);
				if (prev_vis_obj) {
					prev_vis_obj.className = 'fancy_radio';
				}
			}
			prev_vis_name_obj.value = vis_name;
		}
	} catch(e) {

	}

	if (vis_obj) {
		vis_obj.className = 'fancy_radio_selected';
	}
}

/**
*	Kuupäeva valik
*/
function updateDateSelect(date_name, day_name, month_name, year_name) {

	var date_obj		= getObject(date_name);
	var day_obj			= getObject(day_name);
	var month_obj		= getObject(month_name);
	var year_obj		= getObject(year_name);

	if (date_obj && day_obj && month_obj && year_obj) {
		// SQL format
		date_obj.value = year_obj.value+'-'+month_obj.value+'-'+day_obj.value;
	}
}

/**
*	Tekstiväli soovitustega
*
*	Vajab välist funktsiooni - query
*/
function TextSuggestions(name, id) {

	this._name			= name;
	this._text_obj		= getObject(id);
	this._list_obj		= getObject(id + '_list');
	this._list			= new Array();
	this._selection		= -1;
	this._textFocused	= false;
	this._listFocused	= false;

	/**
	*	Attach events to text field
	*/
	this._attachEvents = function() {

		eval("this._text_obj.onkeydown = function("+(window.event ? "" : "event")+") { " + this._name + "._keyPressEvent(event, true); }");
		eval("this._text_obj.onkeyup = function("+(window.event ? "" : "event")+") { " + this._name + "._keyPressEvent(event, false); }");
		eval("this._text_obj.onfocus = function() { " + this._name + ".query(); }");
		eval("this._text_obj.onblur = function() { " + this._name + "._looseTextFocus(); }");
	}

	/**
	*	Set suggestions list
	*/
	this._setList = function(list) {

		this._list = list;
	}

	/**
	*	Show suggestions
	*/
	this._show = function() {

		if (this._list.length <= 0) {
			this._hide();
			return;
		}

		var content = "";

		content += "<ul class=\"suggestion_list\" onmouseover=\"" + this._name + "._changeListFocus(true);\" onmouseout=\"" + this._name + "._changeListFocus(false);\">";
		for (var i = 0; i < this._list.length; i++) {

			content += "<li" + ((i === this._selection) ? " class=\"selected\"" : "");
			content += " onclick=\"" + this._name + ".select(" + i + ")\"";
			content += ">" + this._list[i] + "</li>";
		}
		content += "</ul>";

		writeObjectContent(this._list_obj, content);
		this._list_obj.style.display = "block";

		this._textFocused = true;
		this._listFocused = false;
	}

	/**
	*	Hide suggestions
	*/
	this._hide = function() {

		this._selection = -1;
		this._list_obj.style.display = "none";
		writeObjectContent(this._list_obj, "");
	}

	/**
	*	Loose text focus
	*/
	this._looseTextFocus = function() {

		this._textFocused = false;
		this._checkFocus();
	}

	/**
	*	Change list focus
	*/
	this._changeListFocus = function(focus) {

		this._listFocused = focus;
		this._checkFocus();
	}

	/**
	*	Check focus
	*/
	this._checkFocus = function() {

		if ((!this._textFocused) && (!this._listFocused)) {
			this._hide();
		}
	}

	/**
	*	Choose suggestion
	*/
	this._chooseSelected = function() {

		if (this._selection >= 0) {
			this._text_obj.value = this._list[this._selection];
		}
		this._hide();
	}

	/**
	*	Select one
	*/
	this.select = function(index) {

		if ((index >= 0) && (index < this._list.length)) {

			this._selection = index;
			this._chooseSelected();
		}
	}

	/**
	*	Key press event
	*/
	this._keyPressEvent = function(e, down) {
		var keynum;

		if (window.event) { // IE
			keynum = e.keyCode;
		} else if (e.which) { // Netscape/Firefox/Opera
			keynum = e.which;
		} else {
			return;
		}

		switch (keynum) {

			case 38:
				if ((down) && (this._selection > 0)) {
					this._selection--;
					this._show();
				}
				break;

			case 40:
				if ((down) && (this._selection < this._list.length - 1)) {
					this._selection++;
					this._show();
				}
				break;

			case 13:
				if (down) {
					this._chooseSelected();
				}
				return false;
				break;

			default:
				if (!down) this.query();
				break;
		}

		return true;
	}
}
