﻿				function parseUri (str) {
					var	o   = parseUri.options,
						m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
						uri = {},
						i   = 14;

					while (i--) {
					    uri[o.key[i]] = m[i] || "";
					}

					uri[o.q.name] = {};
					uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
						if ($1) uri[o.q.name][$1] = $2;
					});

					return uri;
				};

				parseUri.options = {
					strictMode: false,
					key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
					q:   {
						name:   "queryKey",
						parser: /(?:^|&)([^&=]*)=?([^&]*)/g
					},
					parser: {
						strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
						loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
					}
				};
				
				function phone_createCookie(name,value,days) {
					if (days) {
						var date = new Date();
						date.setTime(date.getTime()+(days*24*60*60*1000));
						var expires = "; expires="+date.toGMTString();
					}
					else var expires = "";
					document.cookie = name+"="+value+expires+"; path=/; domain=.contact-polis.ru";
					//document.cookie = name+"="+value+expires+"; path=/;";
				}

				function phone_readCookie(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 phone_eraseCookie(name) {
					phone_createCookie(name,"",-1);
				}

				function format_phone(phone,format) {
					phone_array=phone.split(" ");
					if (format=="www.contact-polis.ru") {
						//phone_number=phone_array.join(" ");

						phone_number = "<span style='font-size:14pt;font-family:arial;'>(" + phone_array[1] + ")</span><span style='font-size:24pt;font-family:arial;'> " + phone_array[2] + "-" + phone_array[3] + "-" + phone_array[4] + "</span><span style='display:none;'>-</span>";
					} else if (format=="lid.contact-polis.ru") {
						phone_array.shift();
						phone_array.shift();
						phone_number=phone_array.join("-");
					} else if (format=="lead.contact-polis.ru") {
						phone_array.shift();
						phone_array.shift();
						phone_number=phone_array.join("-");
					} else if (format=="kasko.contact-polis.ru") {
						phone_array.shift();
						phone_array.shift();
						phone_number=phone_array.join("-");
					}  else if (format=="-") {
						phone_array.shift();
						phone_array.shift();
						phone_number=phone_array.join("-");
					} else if (format==" ") {
						phone_array.shift();
						phone_array.shift();
						phone_number=phone_array.join(" ");
					} else if (format=="empty") {
						phone_array.shift();
						phone_array.shift();
						phone_number=phone_array.join("");
		            }
		            //alert(phone_number);
					return phone_number;
				}
				
				function msk_phone(format) {
					if(!format) {
						format=document.location.host;
					}					
					phone_cookie=phone_readCookie('_phone_traffic_source');
					if (phone_cookie=="direct") {
						phone_number="8 495 784 66 39";
					} else if (phone_cookie=="yandex_search_organic") {
						phone_number="8 495 784 76 89";
					} else if (phone_cookie=="yandex_search_cpc") {
						phone_number="8 495 784 76 85";
					} else if (phone_cookie=="yandex_search_mkb") {
						phone_number="8 495 784 66 39";
					} else if (phone_cookie=="google_search_organic") {
						phone_number="8 495 784 76 89";
					} else if (phone_cookie=="google_search_cpc") {
						phone_number="8 495 784 76 85";
					} else if (phone_cookie=="yandex_partners") {
						phone_number="8 495 784 76 82";
					} else if (phone_cookie=="google_partners") {
						phone_number="8 495 784 76 82";
					} else if (phone_cookie=="referrer") {
						phone_number="8 495 784 66 39";
					} else if (phone_cookie=="other_utm") {
						phone_number="8 495 784 66 39";
					} else {
						phone_number="8 495 784 66 39";
					}
					
					if ((document.location.hostname=="lead.contact-polis.ru") || (document.location.hostname=="lid.contact-polis.ru")) {
						phone_number="8 495 784 76 80";
					}
					
					return format_phone(phone_number,format);
				}
				
			
				/*
				*** _phone_traffic_source ***
				yandex_search_organic - домен yandex.ru, нет utm
				yandex_search_cpc - домен yandex.ru, utm_medium=cpc
				yandex_search_mkb - домен yandex.ru, utm_medium=mkb
				google_search_organic - домен www.google.com или www.google.ru, нет utm
				google_search_cpc - домен www.google.com или www.google.ru, utm_medium=cpc
				yandex_partners - домен не yandex.ru, utm_medium=cpc, utm_source=yandex_direct
				google_partners - домен не www.google.ru, не www.google.com, utm_medium=cpc, utm_source=adwords
				other_utm - есть utm_source
				direct - нет referrer
				referrer - нет utm, есть referrer
				*/
				function detect_phoneCookie(d_location,d_referrer) {
					var ref_o = parseUri.options;
					var ref_items = parseUri(d_referrer);
					var ref_query = ref_items[ref_o.q.name];

					var self_domain = d_location.hostname;

					var loc_o = parseUri.options;
					var loc_items = parseUri(d_location);
					var loc_query = loc_items[loc_o.q.name];

			


//					alert(decodeURI(ref_items[ref_o.key[0]]));
//					alert(decodeURI(loc_items[ref_o.key[0]]));

//					var d_cookie_info_unsplitValue = decodeURI(ref_items[ref_o.key[0]]) + ' ::: ' + decodeURI(loc_items[ref_o.key[0]]);
					saveDirectInfo_ref(decodeURI(ref_items[ref_o.key[0]]));
					saveDirectInfo_loc(decodeURI(loc_items[ref_o.key[0]]));
				
					if (phone_readCookie('_phone_traffic_source')==null) 
					{
						if (d_referrer=="") {
							phone_createCookie('_phone_traffic_source','direct',120);					
						} else if ((ref_items['host']=='yandex.ru') && (loc_query['utm_source']==null)){
							phone_createCookie('_phone_traffic_source','yandex_search_organic',120);
						} else if ((ref_items['host']=='yandex.ru') && (loc_query['utm_medium']=='cpc')){
							phone_createCookie('_phone_traffic_source','yandex_search_cpc',120);
						} else if ((ref_items['host']=='yandex.ru') && (loc_query['utm_medium']=='mkb')){
							phone_createCookie('_phone_traffic_source','yandex_search_mkb',120);
						} else if (((ref_items['host']=='www.google.ru') || (ref_items['host']=='www.google.com')) && (loc_query['utm_source']==null)){
							phone_createCookie('_phone_traffic_source','google_search_organic',120);
						} else if (((ref_items['host']=='www.google.ru') || (ref_items['host']=='www.google.com')) && (loc_query['utm_medium']=='cpc')){
							phone_createCookie('_phone_traffic_source','google_search_cpc',120);
						} else if ((loc_query['utm_source']=='yandex_direct') && (loc_query['utm_medium']=='cpc')){
							phone_createCookie('_phone_traffic_source','yandex_partners',120);
						} else if ((loc_query['utm_source']=='utm_adwords') && (loc_query['utm_medium']=='cpc')){
							phone_createCookie('_phone_traffic_source','google_partners',120);
						} else if (loc_query['utm_source']==null){
							phone_createCookie('_phone_traffic_source','referrer',120);					
						} else if (!(loc_query['utm_source']==null)){
							phone_createCookie('_phone_traffic_source','other_utm',120);
						}				
					} else {
						if ((ref_items['host']=='yandex.ru') && (loc_query['utm_source']==null)){
							phone_createCookie('_phone_traffic_source','yandex_search_organic',120);
						} else if ((ref_items['host']=='yandex.ru') && (loc_query['utm_medium']=='cpc')){
							phone_createCookie('_phone_traffic_source','yandex_search_cpc',120);
						} else if ((ref_items['host']=='yandex.ru') && (loc_query['utm_medium']=='mkb')){
							phone_createCookie('_phone_traffic_source','yandex_search_mkb',120);
						} else if (((ref_items['host']=='www.google.ru') || (ref_items['host']=='www.google.com')) && (loc_query['utm_source']==null)){
							phone_createCookie('_phone_traffic_source','google_search_organic',120);
						} else if (((ref_items['host']=='www.google.ru') || (ref_items['host']=='www.google.com')) && (loc_query['utm_medium']=='cpc')){
							phone_createCookie('_phone_traffic_source','google_search_cpc',120);
						} else if ((loc_query['utm_source']=='yandex_direct') && (loc_query['utm_medium']=='cpc')){
							phone_createCookie('_phone_traffic_source','yandex_partners',120);
						} else if ((loc_query['utm_source']=='adwords') && (loc_query['utm_medium']=='cpc')){
							phone_createCookie('_phone_traffic_source','google_partners',120);
						}
					}
	}


	function saveDirectInfo_ref(d_cookieValue) {
	    if (phone_readCookie('client_source_info_ref') == null) {
	        phone_createCookie('client_source_info_ref', d_cookieValue, 90);
	    }
	} 
	function saveDirectInfo_loc(d_cookieValue) {
	    if (phone_readCookie('client_source_info_loc') == null) {
	        phone_createCookie('client_source_info_loc', d_cookieValue, 90);
	    }
	}
	function getCityCode(codeName) {
	    var cName = codeName.toString().toLowerCase();
	    if (cName == 'msk') {
	        return '(495)';
	    }
	    else if (codeName == 'spb') {
	        return '(812)';
	    }
	    return '(095)';
	}
	function getCityPhone(codeName) {
	    var cName = codeName.toString().toLowerCase();
	    if (cName == 'msk') {
	        return '';
	    }
	    else if (codeName == 'spb') {
	        return '';
	    }
	    return '';
	}

	detect_phoneCookie(document.location, document.referrer);
