function classGrey($div)
{
	$sub_div = get_sub_div($div)
	$sub_div.removeClass("blue").addClass("grey");
}

function classBlue($div)
{
	$sub_div = get_sub_div($div)
	$sub_div.removeClass("grey").addClass("blue");
}

function hasClass($div, class_name)
{
	$sub_div = get_sub_div($div)
	return $sub_div.hasClass(class_name);
}

function toggleClass($div)
{
	$sub_div = get_sub_div($div)
	if ($sub_div.hasClass("blue"))
		$sub_div.removeClass("blue").addClass("grey");
	else
		$sub_div.removeClass("grey").addClass("blue");
}

function get_sub_div($div)
{
	$sub_div = $div.find(".inputText");
	return $sub_div;
}

function get_input($div)
{
	return $div.find("input[type=text]");
}

function get_pass_input($div)
{
	return $div.find("input[type=password]");
}

function get_hidden_input($div)
{
	return $div.find("input[type=hidden]");
}

function get_arrow($div)
{
	return $div.find(".arrow");
}

function show_error($div, text)
{
	$div.children(":last").show().html(text);
}

function show_error_order($div, text)
{
	$div.children(":last").children(":last").show().html(text);
}

function hide_error($div)
{
	$div.children(":last").hide();
}

function hide_error_order($div)
{
	$div.children(":last").children(":last").hide();
}

function input_mouseenter(event)
{
	$obj = event.data.obj;
	
	if ($obj.data("focus") == 0 && hasClass($obj, "blue"))
	{
		$obj.data("is_input_modifed", true);
		classGrey($obj);
		$obj.data("temp_value", get_input($obj).val());
		get_input($obj).val($obj.data("default_value"));
	}                    
}

function textarea_mouseenter(event)
{
	$obj = event.data.obj;
	
	if ($obj.data("focus") == 0)
	{
		$obj.data("is_input_modifed", true);
		$obj.data("temp_value", get_textarea($obj).val());
		get_textarea($obj).val('');
		set_textarea_label_text($obj, $obj.data("default_value"));
		get_textarea_label($obj).show();
	}                    
}

function input_mouseleave(event)
{
	$obj = event.data.obj;
	
	if ($obj.data("is_input_modifed") && hasClass($obj, "grey"))
	{
		classBlue($obj);
		get_input($obj).val($obj.data("temp_value"));
	}
}

function textarea_mouseleave(event)
{
	$obj = event.data.obj;
	
	if ($obj.data("is_input_modifed"))
	{
		get_textarea($obj).val($obj.data("temp_value"));
		get_textarea_label($obj).hide();
	}
}

function input_focus(event)
{
	$obj = event.data.obj;
	
	var focus = $obj.data("focus");
	$obj.data("focus", ++focus);
	
	if ($obj.data("is_input_modifed") && hasClass($obj, "grey"))
	{
		classBlue($obj);
		get_input($obj).val($obj.data("temp_value"));
	}
	if (!$obj.data("is_input_modifed") && hasClass($obj, "grey"))
	{
		classBlue($obj);
		get_input($obj).val("");
	}
}

function input_focus_dropdown(event)
{
	$obj = event.data.obj;
	
	if ($obj.data("is_input_modifed") && hasClass($obj, "grey"))
	{
		classBlue($obj);
		get_input($obj).val($obj.data("temp_value"));
	}
}

function textarea_focus(event)
{
	$obj = event.data.obj;
	
	var focus = $obj.data("focus");
	$obj.data("focus", ++focus);
	
	if ($obj.data("is_input_modifed"))
	{
		get_textarea_label($obj).hide();
		get_textarea($obj).val($obj.data("temp_value"));
	}
}

function input_blur(event)
{
	$obj = event.data.obj;
	
	$obj.data("focus", 0);
	
	if (get_input($obj).val().length == 0)
	{
		classGrey($obj);
		get_input($obj).val($obj.data("default_value"));
		$obj.data("is_input_modifed", false);
		if ($obj.data("children").length != 0)
			disable_children($obj);
	}
	else
	{
		if ($obj.data("children").length != 0)
			enable_children($obj);
	}
}
/* Для пароля */
function input_pass_mouseenter(event)
{
	$obj = event.data.obj;
	
	if ($obj.data("focus") == 0 && hasClass($obj, "blue"))
	{
		$obj.data("is_input_modifed", true);
		classGrey($obj);
		get_pass_input($obj).hide();
		get_input($obj).val($obj.data("default_value")).show();
	}                    
}

function input_pass_mouseleave(event)
{
	$obj = event.data.obj;
	
	if ($obj.data("is_input_modifed") && hasClass($obj, "grey"))
	{
		classBlue($obj);
		get_input($obj).val($obj.data("default_value")).hide();
		get_pass_input($obj).show();
	}
}

function input_pass_focus(event)
{
	$obj = event.data.obj;
	
	var focus = $obj.data("focus");
	$obj.data("focus", ++focus);
	
	if ($obj.data("is_input_modifed") && hasClass($obj, "grey"))
	{
		classBlue($obj);
		get_input($obj).hide();
		get_pass_input($obj).show().focus();
	}
	if (!$obj.data("is_input_modifed") && hasClass($obj, "grey"))
	{
		get_input($obj).hide();
		get_pass_input($obj).show().focus();
		classBlue($obj);
	}
}

function input_pass_blur(event)
{
	$obj = event.data.obj;
	
	$obj.data("focus", 0);
	
	if (get_pass_input($obj).val().length == 0)
	{
		classGrey($obj);
		get_pass_input($obj).hide();
		get_input($obj).val($obj.data("default_value")).show();
		$obj.data("is_input_modifed", false)
	}
}

function parse_sex(data) 
{
	return $.map(data, function(row) {
		return {
			data: row,
			value: row[0],
			result: row[1]
		}
	});
}

function parse_object(data) 
{
	return $.map(data, function(row) {
		return {
			data: row,
			value: row.id,
			result: row.name
		}
	});
}

function no_highlight(value, term) 
{
	return value;
}

function disable_element($obj)
{
	get_input($obj).attr("disabled", "true");
	get_arrow($obj).hide();
}

function enable_element($obj)
{
	get_input($obj).removeAttr("disabled");
	get_arrow($obj).show();
}

function disable_children($obj)
{
	var childrens = $obj.data("children");
	if (childrens == undefined)
		return;
	$(childrens).each(function(i, $item) 
	{
		disable_children($item);
		disable_element($item);
	});
}

function enable_children($obj)
{
	var childrens = $obj.data("children");
	if (childrens == undefined)
		return;
	$(childrens).each(function(i, $item) 
	{
		enable_element($item);
	});
}

function get_textarea($obj)
{
	return $obj.find("textarea");
}

function get_textarea_label($obj)
{
	return $obj.find(".textareaFlow");
}

function set_textarea_label_text($obj, text)
{
	$label = get_textarea_label($obj);
	$label.find("td").html(text);
}

function hide_scrolls()
{
	var scroll_top = $(window).scrollTop();
	var scroll_left = $(window).scrollLeft();
	$('html, body').css("overflow", "hidden");
	$('html, body').scrollTop(scroll_top);
}

function show_scrolls()
{
	var scroll_top = $(window).scrollTop();
	var scroll_left = $(window).scrollLeft();
	$('html, body').css("overflow", "visible");
	$('html, body').scrollTop(scroll_top);
}

function disable_scroll()
{
	$(window).unbind("mousewheel").bind("mousewheel", function(e) 
	{
		return false;
	});
	
	$(".ac_even, .ac_odd").live("mousewheel", function(e) 
	{
		e.stopPropagation();
		return true;
	});
}

function enable_scroll()
{
	$(window).unbind("mousewheel");
}