function create_sess_cookie(name, value) {
	document.cookie = name + "=" + value + "; path=/";
}

function read_cookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') {
			c = c.substring(1, c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length, c.length);
		}
	}
	return null;
}

function show_phones() {
	var engines = [
		'yandex.ru',
		'google.ru',
		'google.com',
		'images.google.ru',
		'images.yandex.ru',
		'blogs.yandex.ru',
		'blogsearch.google.ru',
		'go.mail.ru',
		'gogo.ru',
		'nova.rambler.ru',
		'images.rambler.ru',
		'list.mail.ru',
		'search.qip.ru',
		'webalta.ru',
		'search.livetool.ru',
		'nigma.ru',
		'aport.ru',
		'poisk.ru',
		'go.km.ru',
		'liveinternet.ru',
		'quintura.ru',
		'ru.search.yahoo.com',
		'ru.yahoo.com',
		'google.com.ua'
	];
	var phone = {
		"default": "+7 (495) 540-51-68",
		"direct": "+7 (495) 363-21-90",
		"adwords": "+7 (495) 540-51-68",
		"organic": "8 (495) 540-51-68"
	};
	var type = read_cookie('__type');
	if (null == type) {
		// if no cookie set
		var utm_source = location.search.match(new RegExp("[\?|\&]utm_source=.[^\&|$]+"));
		if (null !== utm_source) {
			// get type by utm_content
			utm_source = utm_source[0].split('=')[1];
			if (utm_source == 'direct_yandex_ru') {
				type = 'direct';
			}
		}
		else {
			// get type by referrer
			var referer = document.referrer.match(new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im'));
			if (null !== referer) {
				referer = referer[1];
				for (var i = 0; i < engines.length; i++) {
					if (referer.indexOf(engines[i]) >= 0) {
						type = 'organic';
					}
				}
			}
		}
		if (null == type) {
			type = 'default';
		}
		// set session cookie
		create_sess_cookie('__type', type);
	}
	$('#phone_1, #phone_2, #phone_3').html(phone[type]);
}
