﻿myClientBrowsers = new Array('yandex', 'google'); // Поисковики
myClientwords = new Array(''); // Ключевики
myClientExclusion = new Array('хромбур', 'hormbur', 'hrombur'); // Исключение слов с адреса страницы, с которой пришли
myClientReExclusion = new Array(''); // Исключение слов с адреса страницы НА которую попали
myClientIncludes = new Array(''); // Слова, которые обязательно должны быть на текущей странице
myvphoneScriptRules = [

['(495) 669-32-89', '(495) 783-89-84', '.phone'],
['669-32-89', '783-89-84', '.left p, .bot1'],
];
// Схема: ['виртуальный телефон', 'реальный телефон', 'теги без скобок или .класс или #id, через запятую']запятая, если на след.строке еще один элемент



RegExp.escape = function(text) {
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}

function vphonegetCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function vphoneReplaceArg(myVPhone, thisSitePhone, myHtmlContainers)
{
		$(myHtmlContainers).each(
		function(){
				var myString = $(this).html();
				var myIndexOf = myString.indexOf(thisSitePhone, -1);
				if(myIndexOf > -1)
				{
						var vf = new RegExp(RegExp.escape(thisSitePhone),'gi');
						$(this).html(myString.replace(vf, myVPhone));
				}
		}
		);
}

function vphoneGetDestination(myClientBrowsers, myClientwords, myClientExclusion)
{
	var ws=new Date();// Если нужно удалить куки, меняем цифру на 60 (1 час).
	ws.setMinutes((999999*9)+ws.getMinutes());
	
	var myExclusion = 0;
	myInclusion = 0;
	
	if(myClientExclusion[0] != '')
	{
		for(eu=0; eu <= myClientExclusion.length -1; eu++)
		{
			myExclusionoptimized = encodeURIComponent(myClientExclusion[eu]).replace(RegExp('\%20','gi'), '+');
			
			var myExclusionOf = document.referrer.indexOf(myExclusionoptimized, -1);
			if(myExclusionOf > -1)
			{
					var myExclusion = 1;
			}
		}
	}
	
	if(myClientReExclusion[0] != '')
	{
		for(ru=0; ru <= myClientReExclusion.length -1; ru++)
		{
			myReExclusionoptimized = encodeURIComponent(myClientReExclusion[ru]).replace(RegExp('\%20','gi'), '+');
			
			var myReExclusionOf = document.URL.indexOf(myReExclusionoptimized, -1);
			if(myReExclusionOf > -1)
			{
					myExclusion = 1;
			}
		}
	}

	if(myClientIncludes[0] != '')
	{
		for(iu=0; iu <= myClientIncludes.length -1; iu++)
		{
			myClientIncludesOptimized = encodeURIComponent(myClientIncludes[iu]).replace(RegExp('\%20','gi'), '+');
			
			var myClientIncludesOf = document.URL.indexOf(myClientIncludesOptimized, -1);
			if(myClientIncludesOf > -1)
			{
					myInclusion = 1;
			}
		}
	}	

if( (myClientExclusion[0] == '' || myExclusion != 1) && (myClientReExclusion[0] == '' || myExclusion != 1) && (myClientIncludes[0] == '' || myInclusion == 1) )
{

if(myClientwords[0] != '')
{
			for(i=0; i<= myClientBrowsers.length -1; i++)
			{
					for(u=0; u <= myClientwords.length -1; u++)
					{
						myClientwordsoptimized = encodeURIComponent(myClientwords[u]).replace(RegExp('\%20','gi'), '+');
						var myWordOf = document.referrer.indexOf(myClientwordsoptimized, -1);
						if( myWordOf > -1)
						{
								var myIndexOf = document.referrer.indexOf(myClientBrowsers[i], -1);
								if(myIndexOf > -1)
								{
										 document.cookie ="vphoneCookieStatus=vphoneSet; path=/; expires="+ws.toGMTString();
										 return 'cookieInjected';
								}
						}
					}
			}
}
else
{
			for(i=0; i<= myClientBrowsers.length -1; i++)
			{
								var myIndexOf = document.referrer.indexOf(myClientBrowsers[i], -1);
								if(myIndexOf > -1)
								{
										 document.cookie ="vphoneCookieStatus=vphoneSet; expires="+ws.toGMTString();
										 return 'cookieInjected';
								}
					}
}

}

}

function vphoneScriptExec(myvphoneScriptRules)
{
		for(i=0; i <= myvphoneScriptRules.length; i++)
		{
				vphoneReplaceArg(myvphoneScriptRules[i][0], myvphoneScriptRules[i][1], myvphoneScriptRules[i][2]);
		}
}

function vphoneCheckCookie(myClientBrowsers, myClientwords, myClientExclusion, myClientIncludes, myvphoneScriptRules)
{
if(vphonegetCookie('vphoneCookieStatus') == 'vphoneSet')
		{
		vphoneScriptExec(myvphoneScriptRules);
		}
		else
		{
				vphoneGetDestination(myClientBrowsers, myClientwords, myClientExclusion, myClientIncludes);
				if(vphonegetCookie('vphoneCookieStatus') == 'vphoneSet')
				{
				vphoneScriptExec(myvphoneScriptRules);
				}
		}
}

vphoneCheckCookie(myClientBrowsers, myClientwords, myClientExclusion, myClientIncludes, myvphoneScriptRules);
