Function.prototype.bind = function(o){
	var method = this;
	return function() {
		return method.apply(o, arguments);
	}
}

function in_array(needle, haystack, argStrict){
	var key = '', strict = !!argStrict;

	if (strict){
		for (key in haystack){
			if (haystack[key] === needle){
				return true;
			}
		}
	} else {
		for (key in haystack){
			if (haystack[key] == needle){
				return true;
			}
		}
	}

	return false;
}

function hasClass(ele, cls){
	return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}
function addClass(ele, cls){
	if (!hasClass(ele,cls)) ele.className += ' ' + cls;
}
function removeClass(ele, cls){
	if (hasClass(ele, cls)){
		var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
		ele.className = ele.className.replace(reg, ' ');
	}
}

function setcookie(name, value, expires, path, domain, secure){
	expires instanceof Date ? expires = expires.toGMTString() : typeof(expires) == 'number' && (expires = (new Date(+(new Date) + expires * 1e3)).toGMTString());
	var r = [name + '=' + escape(value)], s, i;
	for(i in s = {expires: expires, path: path, domain: domain}){
		s[i] && r.push(i + '=' + s[i]);
	}

	return secure && r.push('secure'), document.cookie = r.join(';'), true;
}
function getcookie(name){
	if (name){
		var start = document.cookie.indexOf(name + '=');
		var len = start + name.length + 1;
		if ((!start) && (name != document.cookie.substring(0, name.length))){
			return null;
		}
		if (start == -1){
			return null;
		}

		var end = document.cookie.indexOf(';', len);
		if (end == -1){
			end = document.cookie.length;
		}

		return unescape(document.cookie.substring(len,end));
	} else {
		return null;
	}
}

function trim(str, charlist){
	charlist = !charlist ? ' \\s\\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
	var re = new RegExp('^[' + charlist + ']+|[' + charlist + ']+$', 'g');
	return str.replace(re, '');
}


function number_format(number, decimals, dec_point, thousands_sep){
	var i, j, kw, kd, km;

	// input sanitation & defaults
	if (isNaN(decimals = Math.abs(decimals))){
		decimals = 2;
	}
	if (dec_point == undefined){
		dec_point = ",";
	}
	if (thousands_sep == undefined){
		thousands_sep = ".";
	}

	i = parseInt(number = (+number || 0).toFixed(decimals)) + '';

	if ( (j = i.length) > 3 ){
		j = j % 3;
	} else {
		j = 0;
	}

	km = (j ? i.substr(0, j) + thousands_sep : '');
	kw = i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thousands_sep);
	kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : '');

	return km + kw + kd;
}

function miniAction(url, func){
	$.ajax({
		url: url,
		type: 'POST',
		data: {},
		cache: false,
		async: false,
		error: function (event, request, settings){
			alert('При запросе на сервер возникла ошибка!');
		},
		success: function(data, textStatus){
			if (!data) return;

			if (typeof(func) == 'function'){
				func($.parseJSON(data));
			} else if (typeof(func) == 'string'){
				window[func]($.parseJSON(data));
			}
		}
	});
}
