Element.implement({
	addoption: function(){
		Array.flatten(arguments).each(function(element){
			var newOption = new Option(element.text, element.value);
			try {
				this.add(newOption, null);
			} catch (err) {
				this.add(newOption);
			}
		}, this);
		return this;
	}
});

function array_max(input) {
	var i, max;
	for (i in input) {
		if (!max || max < input[i])
			max = input[i];
	}
	return max;
}

function refreshCaptcha() {
	var src = $('captcha_img').get('src');
	$('captcha_img').set('src', src.replace(/\d+$/g, $random(100000, 999999)));
}

// User List

var url = '';
var inPid = '';
var listname = 'userlist';
var userId = 'user_id';

function insertUname(key, val)
{
	$(userId).value = key;
	$(inPid).value = val;
	hideList();
}

function usersByMask(pid, value)
{
	inPid = pid;
	if (value.length == 0)
	{
		$(listname).setStyle('display', 'none');
		$(userId).value = 0;
		return;
	}
	new Request.JSON({
		method: "post",
		url: url,
		onSuccess: function(responseText) {
			$(listname).setStyle('display', 'block');
			$(listname).setStyle('width', $(inPid).getStyle('width').toInt() - 10);
			$(listname).setStyle('position', 'absolute');
			$(listname).setStyle('top', $(inPid).getTop() + $(inPid).getStyle('height').toInt());
			$(listname).setStyle('left', $(inPid).getLeft() + 2);
			$(listname).innerHTML = '';
			if (responseText.aAccounts.length > 0)
			{
				for (var i = 0; i < responseText.aAccounts.length; i++)
					$(listname).innerHTML += '<a href="#" onclick="insertUname(\'' + responseText.aAccounts[i].account_id + '\', \'' + responseText.aAccounts[i].login + '\'); return false">' + responseText.aAccounts[i].login + ' (' + responseText.aAccounts[i].account_id + ')</a><br>';
			}
			else
				$(listname).innerHTML += 'Нет записей<br>';
			$(listname).innerHTML += '[x] <a href="#" onclick="insertUname(\'0\', \'\'); return false">close</a>';
		},
		onFailure: function(status) {alert('AJAX error: ' + status)}
	}).send('mask=' + encodeURIComponent(value));
}

function userList(pid, value)
{
	inPid = pid;
	if (value.length == 0)
	{
		$(listname).setStyle('display', 'none');
		$(userId).value = 0;
		return;
	}
	new Ajax(url, {
		method: 'post',
		postBody: 'mask=' + encodeURIComponent(value),
		onComplete: function(responseText) {
			$(listname).setStyle('display', 'block');
			//$(listname).setStyle('width', $(inPid).getStyle('width').toInt() - 10);
			//$(listname).setStyle('position', 'absolute');
			//$(listname).setStyle('top', $(inPid).getTop() + $(inPid).getStyle('height').toInt());
			//$(listname).setStyle('left', $(inPid).getLeft() + 2);
			$(listname).innerHTML = responseText;
		}
	}).request();
}

function hideList()
{
	$(listname).setStyle('display', 'none');
}

function getX(o)
{
	var iX = 0;
	while (o)
	{
		iX += parseInt(o.offsetLeft);
		o = o.offsetParent;
	}
	return iX;
}

function getY(o)
{
	var iY = 0;
	while (o)
	{
		iY += parseInt(o.offsetTop);
		o = o.offsetParent;
	}
	return iY;
}

function getSelectIndex(oSelect, mValue)
{
	for (var i = 0; i < oSelect.options.length; i++)
	{
		if (oSelect.options[i].value == mValue)
			return i;
	}
	return 0;
}

function getRadioValue(sName)
{
	a = document.getElementsByName(sName);
	for (var i = 0; i < a.length; i++)
	{
		if (a[i].checked == true)
			return a[i].value;
	}
	return 0;
}
