function switch_checkbox(chb_id, row_id) {
	// flip checkbox
	if (chb_id != '') {
		var chb = document.getElementById(chb_id);
		chb.checked = (chb.checked ? false : true);
	}
	// flip css style
	if (row_id != '') {
		var tr = document.getElementById(row_id);
		//CssClasses(tr).flip('checked');
		tr.marked = tr.marked ? false : true;
		tr.style.backgroundColor = tr.marked ? '#E1E7CC' : '';
	}
}

var _input_list = null;
function select_checkboxes(chb_mask, tr_mask) {
	if (_input_list == null) {
		_input_list = document.getElementsByTagName('input');
	}
	var ind, id, tr;
	for(var i = 0; i < _input_list.length; i++) {
		ind = _input_list[i].id.indexOf(chb_mask);
		if (ind == 0) {
			_input_list[i].checked = true;
			if (tr_mask != '') {
				id = _input_list[i].id.slice(ind+chb_mask.length);
				if (tr = document.getElementById(tr_mask+id)) {
					tr.marked = true;
					tr.style.backgroundColor = '#E1E7CC';
				}
			}
		}
	}
	return false;
}

function unselect_checkboxes(chb_mask, tr_mask) {
	if (_input_list == null) {
		_input_list = document.getElementsByTagName('input');
	}
	for(var i = 0; i < _input_list.length; i++) {
		ind = _input_list[i].id.indexOf(chb_mask);
		if (ind == 0) {
			_input_list[i].checked = false;
			if (tr_mask != '') {
				id = _input_list[i].id.slice(ind+chb_mask.length);
				if (tr = document.getElementById(tr_mask+id)) {
					tr.marked = false;
					tr.style.backgroundColor = '';
				}
			}
		}
	}
	return false;
}

function setPos(obj, lyr)
{
	var newX = findPosX(obj);
	var newY = findPosY(obj);
	var x = new getObj(lyr);
	x.style.top = newY + 'px';
	x.style.left = newX + 'px';
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function getObj(name)
{
	if (document.getElementById)
	{
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all)
	{
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
	else if (document.layers)
	{
		this.obj = getObjNN4(document,name);
		this.style = this.obj;
	}
}

function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++) {
		if (x[i].id == name) {
			foundLayer = x[i];
		}
		else if (x[i].layers.length) {
			var tmp = getObjNN4(x[i],name);
		}
		if (tmp) {
			foundLayer = tmp;
		}
	}
	return foundLayer;
}


function CookieHandler() {}
CookieHandler.prototype = {
	// Функция установки значения cookie.
	setCookie: function(name, value, path, expires, domain, secure) {
		var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "; path=/") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
		document.cookie = curCookie;
	},

	// Функция чтения значения cookie.
	getCookie: function(name) {
		var prefix = name + "=";
		var cookieStartIndex = document.cookie.indexOf(prefix);
		if(cookieStartIndex == -1) return null;
		var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
		if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length;
		return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
	}
}

function SwitchBlockHandler(id) { this.constructor(id); }
SwitchBlockHandler = newClass(CookieHandler, {
	id  : null,
	obj : null,
	cookName : null,

	constructor : function (id) {
		this.id = id;
		this.cookName = '_block_'+id;
		this.obj = document.getElementById(this.id);
		this.toggle(null);
	},

	// "включить"
	on: function() {
		this.toggle(1);
	},

	// "выключить"
	off: function() {
		this.toggle(0);
	},

	// "переключить"
	toggle: function() {
		var args = arguments;
		var on;
		if (args.length > 0) { on = args[0]; }
		else { on = !this.getFlag(); }
		if (on == null) { on = this.getFlag(); }
		this.obj.style.display = on ? 'block' : 'none';
		this.setFlag(on ? 1 : 0);
	},

	// "установить"
	set: function() {
		var args = arguments;
		var on = null;
		if (args.length > 0) { on = args[0]; }
		if (on == null) { on = this.getFlag(); }
		this.obj.style.display = on ? 'block' : 'none';
		this.setFlag(on);
	},

	setFlag: function(flag) {
		this.constructor.prototype.setCookie.call(this, this.cookName, flag ? 1 : 0, '/', new Date(new Date().getTime()+3600*24*365*1000));
	},

	getFlag: function() {
		return Math.round(this.constructor.prototype.getCookie.call(this, this.cookName));
	}
});

function SwitchBlock(id) {
	return new SwitchBlockHandler(id);
}

function CustomColHandler(id) { this.constructor(id); }
CustomColHandler = newClass(CookieHandler, {

	id  : null,
	obj : null,
	cookName : null,

	constructor : function (id) {
		this.id = id;
		this.cookName = '_'+id;
		this.obj = document.getElementById(this.id);
		this.toggle();
	},

	// "переключить"
	toggle: function() {
		var args = arguments;
		var flag;
		if (args.length > 0) { flag = args[0]; }
		else { flag = this.getFlag(); }
		this.setFlag(flag);
	},

	setFlag: function(flag) {
		this.constructor.prototype.setCookie.call(this, this.cookName, Math.round(flag), '/', new Date(new Date().getTime()+3600*24*365*1000));
	},

	getFlag: function() {
		return Math.round(this.constructor.prototype.getCookie.call(this, this.cookName));
	}

});

function CustomCol(id) {
	return new CustomColHandler(id);
}

Array.prototype.indexOf = function(object) {
	for (var i = 0; i < this.length; i++)
	if (this[i] == object) return i;
	return -1;
};
