function show_hide(id, show, inline) {
	var elem = document.getElementById(id);
	if (inline===undefined) {
		show_val = 'block';
	}
	else {
		if (inline) {
			show_val = 'inline';
		}
		else {
			show_val = 'block';
		}
	}
	if (elem) {
		if (show===undefined) {
			elem.style.display = (elem.style.display=='none') ? show_val : 'none';
		}
		else {
			if (show) {
				elem.style.display = show_val;
			}
			else {
				elem.style.display = 'none';
			}
		}
	}
}

function get_amount_label(amount, label_template, with_amount) {
	var out = '';

	if (with_amount) {
		out += amount + '&nbsp;';
	}
	var labels = label_template.split('|');
	var decimals = Math.floor(amount / 10);
	if ((decimals==1) || ((decimals % 10)==1)) {
		out += labels[2];
	}
	else {
		var decimals_off = amount % 10;
		if (decimals_off==1) {
			out += labels[0];
		}
		else if ((decimals_off==2) || (decimals_off==3) || (decimals_off==4)) {
			out += labels[1];
		}
		else {
			out += labels[2];
		}
	}

	return out;
}

function count_symbols(text_input, count_text_id, max_symbols) {
	var count_text_block = document.getElementById(count_text_id);
	if (text_input.value.length <= max_symbols) {
		count_text_block.innerHTML = 'You have ' + get_amount_label(max_symbols - text_input.value.length, 'symbol|symbols', true) + ' left.';
		count_text_block.className = '';
	}
	else {
		count_text_block.innerHTML = 'You have ' + get_amount_label(text_input.value.length - max_symbols, 'symbol|symbols', true) + ' more, that available!';
		count_text_block.className = 'imp';
	}
}

function get_decimal_value(str) {
	if (str==".") {
		str = "";
	}

	var rePoint = new RegExp(",", "ig");
	var reNonNumeric = new RegExp("[^0-9\.]", "ig");
	var reDoubleDot = new RegExp("[\.]?([0-9]+[\.]?[0-9]*)[\.]?", "ig");

	str = str.replace(rePoint, ".");
	str = str.replace(reNonNumeric, "");
	str = str.replace(reDoubleDot, "$1");

	return str;
}
function number_format(src, precision) {
	if (precision != null) {
		src = Number(src).toFixed(precision);
	}
	src = src.toString();
	var len = src.length;
	var point = src.indexOf('.');
	point = (point > 0) ? point : len;
	var first = point % 3;
	if (first==point) {
		first = 0;
	}
	var dst = new String();
	var jj = 1;
	for (var ii=0; ii<point; ii++){
		dst += src.charAt(ii);
		if ((jj > first) && (first > 0)){
			first = 0;
			jj = 1;
		}
		if (
			(
				(
					(first > 0)
					&& (jj==first)
				) || (
					(first==0)
					&& ((jj % 3)==0)
				)
			)
			&& (ii!=(point - 1))
		) {
			dst += ' ';
		}
		jj++;
	}
	if (point < len) {
		dst += ',' + src.slice(point + 1);
	}
	return dst;
}
function decimal_format(sender, default_val, default_text)
{
	var src = new String(get_decimal_value(sender.value));
	var dst = number_format(src);
	if (dst!=sender.value) {
		sender.value = dst;
	}
	if (sender.value==default_val) {
		sender.value = default_text;
	}
}
function decimal_focus(sender, default_val, default_text)
{
	if (sender.value==default_text) {
		sender.value = '';
	}
}
function decimal_blur(sender, default_val, default_text)
{
	if ((sender.value==default_val) || (sender.value=='')) {
		sender.value = default_text;
	}
}

function _blink_block(id, color_bg, color_blinked, period) {
	if (block_blinking = document.getElementById(id)) {
		block_blinking.style.backgroundColor = color_bg;
		setTimeout('_blink_block(\''+id+'\', \''+color_blinked+'\', \''+color_bg+'\', '+period+')', period);
	}
}
function blink_block(id, color_bg, color_blinked, period) {
	if (div_blinking = document.getElementById(id)) {
		_blink_block(id, color_bg, color_blinked, period);
	}
}

function get_url_params(url) {
	var params = {};
	$.extend(params, $.urlParser.parse(url).parameters);
	return params;
}
function get_url_base(url, without_server) {
	var source_url = $.urlParser.parse(url);
	var protocol_and_host = source_url.protocol + '\/\/' + source_url.host;
	var path = source_url.path;
	var ret_url = '';
	if ((source_url.host!=$.urlParser.parse().host) || !without_server) {
		ret_url += protocol_and_host;
	}
	ret_url += path;
	return url;
}

function get_object_string(obj, match_string, deepness) {
	var params_text = '';
	if (!deepness) {
		deepness = 0;
	}
	if (deepness <= 5) {
		for (var k in obj) {
			for (ii=0; ii<deepness; ii++) {
				params_text += '    ';
			}
			var show_prop = false;
			if (!match_string) {
				show_prop = true;
			}
			else {
				var reg_ex = new RegExp(match_string, 'i');
				show_prop = k.match(reg_ex);
			}
			if (show_prop) {
				params_text += k + '=' + ((typeof(obj[k])=='object') ? ("{\n" + get_object_string(obj[k], deepness + 1) + "}") : obj[k]) + "\n";
			}
		}
	}
	return params_text;
}

function go_post_form_url(sender_link, params) {
	var parent_form = $(sender_link).parents().find('form:last');

	for (var k in params) {
		if (params[k] instanceof Array) {
			var key = (k.substr(k.length - 2, 2)!='[]') ? (k + '[]') : k;
			var link_inputs = parent_form.find('input[name="' + key + '"]');
			if ($.makeArray(link_inputs).length > 0) {
				link_inputs.remove();
			}
			for (var arr_k in params[k]) {
				var link_input = $('<input/>');
				link_input.attr({
					type: 'hidden',
					name: key
				});
				parent_form.append(link_input);
				link_input.attr('value', params[k][arr_k]);
			}
		}
		else {
			var link_input = parent_form.find('input[name="' + k + '"]');
			if ($.makeArray(link_input).length==0) {
				link_input = $('<input/>');
				link_input.attr({
					type: 'hidden',
					name: k
				});
				parent_form.append(link_input);
			}
			link_input.attr('value', params[k]);
		}
	}

	parent_form.submit();
}

function getOpacityProperty() {
	if (typeof document.body.style.opacity == 'string') // CSS3 compliant (Moz 1.7+, Safari 1.2+, Opera 9)
		return 'opacity';
	else if (typeof document.body.style.MozOpacity == 'string') // Mozilla 1.6 и младше, Firefox 0.8
		return 'MozOpacity';
	else if (typeof document.body.style.KhtmlOpacity == 'string') // Konqueror 3.1, Safari 1.1
		return 'KhtmlOpacity';
	else if (document.body.filters && (navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5)) // Internet Explorer 5.5+
		return 'filter';

	return false; //нет прозрачности
}

// $('smth').opacity(0.85);
$.fn.opacity = function(_value) {
	var opacityProp = getOpacityProperty();

	if (!opacityProp) { // Если браузер не поддерживает ни один из известных функции способов управления прозрачностью
		return $(this);
	}

	_value = Number(_value);

	if (opacityProp=="filter") { // Internet Explorer 5.5+
		_value *= 100;

		// Если уже установлена прозрачность, то меняем её через коллекцию filters, иначе добавляем прозрачность через style.filter
		var oAlpha = $(this).get(0).filters['DXImageTransform.Microsoft.alpha'] || $(this).get(0).filters.alpha;
		if (oAlpha) {
			oAlpha.opacity = _value;
		}
		else { // Для того чтобы не затереть другие фильтры используем "+="*/
			$(this).get(0).style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity=" + _value + ")";
		}
		return $(this);
	}
	else { // Другие браузеры
		return $(this).css(opacityProp, _value);
	}
};

$.fn.getLeft = function() {
	if ($(this).get(0).style.left) {
		return $(this).get(0).style.left;
	}
	else if ($(this).get(0).currentStyle) {
		return $(this).get(0).currentStyle.left;
	}
	else {
		return document.defaultView.getComputedStyle($(this).get(0), null).left;
	}
}
$.fn.getTop = function() {
	if ($(this).get(0).style.top) {
		return $(this).get(0).style.top;
	}
	else if ($(this).get(0).currentStyle) {
		return $(this).get(0).currentStyle.top;
	}
	else {
		return document.defaultView.getComputedStyle($(this).get(0), null).top;
	}
}

function get_scroll_xy() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}
function get_scroll_top() {
	return get_scroll_xy()[1];
}
function get_scroll_left() {
	return get_scroll_xy()[0];
}

function get_doc_height() {
    var test1 = document.body.clientHeight;
    var test2 = document.documentElement.clientHeight;
	var height = 0;
    if(test1 >= test2) {
        height = test1;
    }
    else if (test2 > 0) {
        height = test2;
    }
	return height;
}
function get_doc_width() {
    var test1 = document.body.clientWidth;
    var test2 = document.documentElement.clientWidth;
	var width = 0;
    if(test1 >= test2) {
        width = test1;
    }
    else if (test2 > 0) {
        width = test2;
    }
	return width;
}

function get_window_width() {
	if (window.innerWidth) {
		return window.innerWidth;
	}
	else if (document.documentElement.clientWidth) {
		return document.documentElement.clientWidth;
	}
    else if (document.body.clientWidth) {
		return document.body.clientWidth;
	}
	else if (document.body.offsetWidth) {
		return document.body.offsetWidth;
	}
	return 0;
}
function get_window_height() {
	if (window.innerHeight) {
		return window.innerHeight;
	}
	else if (document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	}
    else if (document.body.clientHeight) {
		return document.body.clientHeight;
	}
	else if (document.body.offsetHeight) {
		return document.body.offsetHeight;
	}
	return 0;
}

