/* INTVAL */
function intval(e) // при нажитии кнопки внутри текстового поля вовращает false, если нажаты не 0-9, tab, enter, backspace, delete, left, right
{

	if(e.keyCode)
	{
		var button = e.keyCode;
	}
	else
	{
		var button = e.charCode;
	}

	if (button)
	{
		button_array = new Array('48', '49', '50', '51', '52', '53', '54', '55' ,'56' ,'57' ,'37', '39' ,'8', '13', '9', '46');

		for (i = 0; i < button_array.length; i++)
		{
			if (button_array[i] == button)
			{
				return true;
			}
		}
		return false;
	}
}




/* For sorting */
moveup = function (id)
{
	for (var i=0; i< hash.length; i++)
	{
		if (hash[i].uniq == id)
		{
			break;
		}
	}

	var prefix = hash[i].prefixnum;
	var last = hash[i].lastnum;
	
	if (Number(hash[i].lastnum) != 1)
	{
		
		for (var j=0; j< hash.length; j++)
		{
			if ((hash[j].prefixnum == prefix)&&(hash[j].lastnum == last - 1))
			{
				break;
			}
		}
		
		if (j < hash.length)
		{
		
			
			hash[i].uniq = hash[j].uniq;
			hash[j].uniq = id;

			
			var rowid = 'row';
			if (prefix != '0') rowid = rowid + prefix;

		
			for (var k = 0; k < $(rowid+last).childNodes.length; k++)
			{
				var temp_html = $(rowid+last).childNodes[k].innerHTML;

				$(rowid+last).childNodes[k].innerHTML = $(rowid+hash[j].lastnum).childNodes[k].innerHTML;
				$(rowid+hash[j].lastnum).childNodes[k].innerHTML = temp_html;
			}
		}
	}
	
}

movedown = function (id)
{
	for (var i=0; i< hash.length; i++)
	{
		if (hash[i].uniq == id)
		{
			break;
		}
	}

	var prefix = hash[i].prefixnum;
	var last = hash[i].lastnum;
		
	for (var j=0; j< hash.length; j++)
	{
		if ((hash[j].prefixnum == prefix)&&(hash[j].lastnum == last + 1))
		{
			break;
		}
	}
	
	if (j < hash.length)
	{

		hash[i].uniq = hash[j].uniq;
		
		hash[j].uniq = id;

		var rowid = 'row';
		if (prefix != '0') rowid = rowid + prefix;
	
		for (var k = 0; k < $(rowid+last).childNodes.length; k++)
		{
			var temp_html = $(rowid+last).childNodes[k].innerHTML;

			$(rowid+last).childNodes[k].innerHTML = $(rowid+hash[j].lastnum).childNodes[k].innerHTML;
			$(rowid+hash[j].lastnum).childNodes[k].innerHTML = temp_html;
		}
	}

}

savesort = function()
{
	var str = '';
	for (var i = 0; i < hash.length; i++)
	{
		str = str+ hash[i].uniq+':'+hash[i].lastnum+'=';
	}
	$('sort_order').value = str;
}

/* Сommon */

// Очищает визуал для выпадающих списков
ClearListVisual = function(id)
{
	var str = new String($(id).options[$(id).selectedIndex].text);
	var regexp	= new RegExp('[\->]','g');
	var trim	= new RegExp('\u00a0','g');
	
	str = str.replace(regexp,'');
	str = str.replace(trim,'');

	$(id).options[$(id).selectedIndex].text = str;
}

// Изменяет цвет объекта
ChangeColor = function(ob, color)
{
	ob.style.backgroundColor = color;
}


/* Cookie */

// Выставляет cookie: имя=значение
	setCookie = function (name, value)
	{
		var exp = new Date();
		var e = exp.getTime()+(24*60*60*30*1000);
		exp.setTime(e);
		
		var newCookie = name + "=" + escape(value) + ";path=/;expires="+exp.toUTCString();
		document.cookie = newCookie;
	}

	// Забирает значение cookie с данным именем
	getCookie = function (name)
	{
		var prefix = name + "=";
		var start = document.cookie.indexOf(prefix);
		if (start == -1)
		{
			return false;
		}
		var end = document.cookie.indexOf(";", start + prefix.length)
		if (end == -1)
		{
			end = document.cookie.length;
		}
		return unescape(document.cookie.substring(start + prefix.length, end));
	}


/* Menu */

// Вспоминает какие группы меню были открыты
	load_menu = function (template_dir)
	{
		var menu_cookie = getCookie('menu');
		if (menu_cookie != '')
		{
			var menu_array = new Array();
			menu_array = menu_cookie.split('[delim]');
			for (var i = 0; i < menu_array.length; i++)
			{
				if ($('menu-group_'+menu_array[i]))
				{
					$('menu-group_'+menu_array[i]).style.display = 'none';
					$('menu-image_' + menu_array[i]).src = 'templates/' + template_dir + '/images/menu_open.gif';
					$('menu-image_' + menu_array[i]).alt = '+';
				}
			}
		}
	}

	// Сохраняет текущее изменение меню при каждом изменении
	change_menu = function (block_name, template_dir)
	{
		var menu_cookie = getCookie('menu');
		
		var menu_array = new Array()
		if (menu_cookie)
		{
			menu_array = menu_cookie.split('[delim]');
		}

		block_name = block_name.replace('menu-group_','');
	
		if ($('menu-group_' + block_name))
		{
			if ($('menu-group_' + block_name).style.display == 'none')
			{
				$('menu-group_' + block_name).style.display = '';
				$('menu-image_' + block_name).src = 'templates/' + template_dir + '/images/menu_close.gif';
				$('menu-image_' + block_name).alt = '-';

				var i = 0;
				while ((menu_array[i] != block_name)&&(i <  menu_array.length)) i++;
				menu_array.splice(i, 1);
			}
			else
			{
				menu_array.push (block_name);
				$('menu-group_' + block_name).style.display = 'none';
				$('menu-image_' + block_name).src = 'templates/' + template_dir + '/images/menu_open.gif';
				$('menu-image_' + block_name).alt = '+';
			}
		}

		menu_string = menu_array.join ('[delim]');
		setCookie('menu', menu_string);
	}	

/* Security */
check_password = function (ob)
{
	var level = 0;
	var msg = '';
	var color = '';

	var str = new String(ob.value);

	var regexp	= new RegExp('[0-9]','g');

	if (str.match (regexp))
	{
		level++;
	}
	
	regexp = new RegExp('[A-Z]','g');

	if (str.match (regexp))
	{
		level++;
	}

	if (str.length > 10)
	{
		level = level + 2;
	}
	else if (str.length > 8)
	{
		level++;
	}

	if (str.length < 4)
	{
		level = -1;
	}
	else if (str.length < 6)
	{
		level--;
	}

	switch (level)
	{
		case -1 : color = 'red'; msg = 'Некорректный пароль'; break;
		case 0 : color = 'red'; msg = 'Очень слабый пароль (level 1)'; break;
		case 1 : color = 'blue'; msg = 'Слабый пароль (level 2)'; break;
		case 2 : color = 'blue'; msg = 'Нормальный пароль (level 3)'; break;
		case 3 : color = 'green'; msg = 'Хороший пароль (level 4)'; break;
		case 4 : color = 'green'; msg = 'Превосходный пароль (level 5)'; break;
	}

	$('check_pass_result').innerHTML = 	msg;
	$('check_pass_result').style.color = color;

	if (level < 0)
	{
		ob.value = '';
	}
}


function Ask($question)
{
	if(!$question)
	{
		$question ='Вы уверены?';
	}
	if(confirm($question))
	{
		return true;
	}
	return false;
}

/* Are you shure */
function check()
{
	if (document.getElementById("username").value.length == 0)
	{
		alert("{LANG_LOGIN_IS_MISSING}");
	}
	else
	{
		document.getElementById('login_form').action = '{SITE_HOME}/admin/send.php';
		document.getElementById('login_form').submit();
	}
}

/* Change row colors */
var marked_row = new Array;

function change_color(theRow, color, num, theAction)
	{
	// set variable for change checkbox
	var form_el;
	form_el = eval('document.getElementById("chk' + num + '")');

	// set theRow if calls from td
	if(theAction == 'click') theRow = eval('document.getElementById("row' + num + '")');
	if (typeof(document.getElementsByTagName) != 'undefined')
		{
		theCells = theRow.getElementsByTagName('td');
		}
	else if (typeof(theRow.cells) != 'undefined')
		{
		theCells = theRow.cells;
		}
	else
		{
		return false;
		}

	var rowCellsCnt  = theCells.length;
	var domDetect    = null;
	var currentColor = null;
	var newColor     = null;

	if(theAction == 'click')
		{
		if(typeof(marked_row[num]) == 'undefined' || !marked_row[num])
			{
			newColor = "D1CFCF";
			marked_row[num] = true;
			form_el.checked=true;
			}
		else
			{
			newColor = (color) ? color : "D1CFCF";
			marked_row[num] = null;
			form_el.checked=false;
			}
		}
	else if((theAction == 'over' || theAction == 'out') && (typeof(marked_row[num]) == 'undefined' || !marked_row[num]))
		{
		newColor = color;
		}

	if (newColor)
		{
		var c = null;
		if (domDetect)
			{
			for (c = 0; c < rowCellsCnt; c++)
				{
				theCells[c].setAttribute('bgcolor', newColor, 0);
				}
			}
		else
			{
			for (c = 0; c < rowCellsCnt; c++)
				{
				theCells[c].style.backgroundColor = newColor;
				}
			}
		}
	return true;
	}


/* For check boxes */
function setCheckboxes(the_form, do_check)
{
	
	var elts = document.forms[the_form].elements;
	var elts_cnt  = elts.length;

	

	for (var i = 0; i < elts_cnt; i++)
	{
		
		if ((i % 2) == 0)
		{
			color = "FFFFFF";
		}
		else
		{
			color = "F9F7F7";
		}
		
		
		if ((typeof(marked_row[elts[i].name]) == 'undefined' || !marked_row[elts[i].name]) && do_check == true)
		{
			change_color('', color, elts[i].name, 'click');
		}
		else if (marked_row[elts[i].name] == true && do_check == false)
		{
			change_color('', color, elts[i].name, 'click');
		}
		
	}

	return true;
}
