function set_cookie(name, value, expires, domain)
{
	if (!expires)
	{
		expires = new Date();
	}
		
	document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() + "; path=/" + "; domain=" + domain;
}


function get_cookie(name)
{
	var beg = document.cookie.indexOf(name+"=");
 	if(beg==-1)	return null;
  
 	var end = document.cookie.indexOf(";", beg + name.length);
 	if(end==-1)
  		end = document.cookie.length;
  
	return unescape(document.cookie.substring(beg + name.length + 1, end));
}

function check_cookie(domain) 
{
	var name = "uaholding" // имя cookie
	expires = new Date(); // получаем текущую дату
	expires.setTime(expires.getTime() + (3600 * 1000 * 15)); // срок хранения cookie
	var clik = 2;	// число кликов
//	var popup = 'http://hyundai.uaholding.com.ua/';
	var popup = 'http://top-level.com.ua/';
	
	var tmp = get_cookie('uaholding');

	if (tmp == (clik - 1))
 	//if (tmp%2 == 0) 
 	{
 		//alert(tmp + "+" + clik + "+" + (tmp%clik));
 		var direct = chekElem(tempX, tempY);
		openPopup(popup, direct);
 	}
 	else {
 		//alert(444);
 	}
 	tmp++;
 	set_cookie(name, tmp, expires, domain); // устанавливаем cookie
}


function getNameBrowser() {
		var ua = navigator.userAgent.toLowerCase();
	 
		// Internet Explorer
	 	if (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1) {
	   		return "msie";
	 	}
	 	
	 	// Opera
	 	if (ua.indexOf("opera") != -1) {
	   		return "opera";
	 	}
	 
	 	// Gecko = Mozilla + Firefox + Netscape
	 	if (ua.indexOf("gecko") != -1) {
	 		//var chrome_version = false;
			if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
				return "chrome";
				//chrome_version = navigator.userAgent.replace(/^.*Chrome\/([\d\.]+).*$/i, '$1')
			}
			else	
				return "gecko";
	 	}
	 
	 	// Safari, используется в MAC OS
	 	if (ua.indexOf("safari") != -1) {
	   		return "safari";
	 	}
	 	
	 	// Konqueror, используется в UNIX-системах
	 	if (ua.indexOf("konqueror") != -1) {
	   		return "konqueror";
	 	}
	 	return "unknown";
	}


function redirectTo(domain) 
{
	window.location.href=domain; 
}


function delete_cookie(name, domain) 
{
	//var name = "autocom1" // имя cookie
	var tmp = 0; // записываемое значение
	expires = new Date(); // получаем текущую дату
	expires.setTime(expires.getTime() - (3600 * 1000 * 15)); // срок хранения cookie
	set_cookie(name, tmp, expires, domain); // устанавливаем cookie
}


var browser = getNameBrowser();
var dom = "autocom.kiev.ua";

//<!-- Begin
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0;
function getMouseXY(e) 
{
	if (IE) { // grab the x-y pos.s if browser is IE
	tempX = event.clientX + document.body.scrollLeft;
	tempY = event.clientY + document.body.scrollTop;
	}
	else { // grab the x-y pos.s if browser is NS
	tempX = e.pageX;
	tempY = e.pageY;
	} 
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;} 
	document.Show.MouseX.value = tempX;
	document.Show.MouseY.value = tempY;
	return true;
}
// End -->
function getRandomIntVal(min, max)
{
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getUrlLink(link)
{
	var divs = document.getElementsByTagName('a');
	
    var l = divs.length;
    for(var i=0; i<l; i++)
    {
		var name = divs[i].innerHTML;
		var str = divs[i].href;
		var pos = str.indexOf(link);
		
		if (pos != -1)
		{	
			return divs[i].href;
		}
    }
    return 0;
}

function getElementPosition(elem)
{
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;
    
    var l = 0;
    var t = 0;
    
    while (elem)
    {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }

    return {"x":l, "y":t, "width": w, "height":h};
}

function chekElem(x, y)
{
	var divs = document.getElementsByTagName('a')
	    var l = divs.length;
	    for(var i=0; i<l; i++)
	    {
			var name = divs[i].innerHTML;
			var elemPos = getElementPosition(divs[i]);
			
			if ((elemPos.x <= x && x < (elemPos.x + elemPos.width)) && (elemPos.y <= y && y < (elemPos.y + elemPos.height)))
				return "'"+divs[i].href+"'";
	    }
	return 'window.location.href';
	
}
			
if (browser == "msie") 
{	
	document.onclick = function (evt) {	/*check_cookie(dom);*/ popupByRandom(dom);}
}
else
{
	window.onclick = function (evt) { /*check_cookie(dom);*/ popupByRandom(dom);}
}


function popupByRandom(domain)
{
	var name = "mg" // имя cookie
	expires = new Date(); // получаем текущую дату
	expires.setTime(expires.getTime() + (3600 * 1000 * 24)); // срок хранения cookie
	
	var tmp = get_cookie(name);
	
	if (tmp == null)
	{
		var popup = getUrlLink("www.marketgid.com");
		
		if (popup !== 0)
		{
			var eachUser = 15;
			var randNum = getRandomIntVal(1,eachUser);
			var krit = (randNum == eachUser) ? 1 : 0;
			
			if (krit)
			{
				//alert(randNum+" + "+krit);
		 		var direct = 'window.location.href';
				openPopup(popup, direct);
			}
		}
	}
	tmp++;
	set_cookie(name, tmp, expires, domain); // устанавливаем cookie
}

function openPopup(popup, direct)
{
	switch (browser) { 
		case "gecko": 
			function w(){	myWin.opener.focus();}
			myWin = window.open("", "", "resizable, scrollbars, menubar, location, status, toolbar");
			window.setTimeout(w, 50);
			myWin.opener.focus();
			var text="<script"+" type='text/javascript'>"+"window.location.href='"+popup+"';</script>";
			myWin.document.write(text);
			myWin.blur();
	   		break;
		
	   	case "opera": 
			myWin = window.open("", "", "resizable, scrollbars, menubar, location, status, toolbar");
			var text="<script"+" type='text/javascript'>"+"window.location.href='"+popup+"';</script>";
			myWin.document.write(text);
			myWin.opener.close();
			myWin2 = window.open("", "", "resizable, scrollbars, menubar, location, status, toolbar");
			var text="<script"+" type='text/javascript'>"+"window.location.href="+direct+"; </script>";
			myWin2.document.write(text);
			break;
		
		case "msie": 
			version = parseFloat(navigator.appVersion.split("MSIE")[1]);
			if (version >= 7)
			{
				window.setTimeout(function() { redirectTo(popup) }, 50);
				myWin2 = window.open("", "", "resizable, scrollbars, menubar, location, status, toolbar");
				var text="<script"+" type='text/javascript'>"+"window.location.href="+direct+"; </script>";
				myWin2.document.write(text);
			}
			else
			{
				myWin = window.open(popup, "", "resizable, scrollbars, menubar, location, status, toolbar");
				myWin.opener.focus();
			}
			break;
			
		case "chrome":
			window.setTimeout(function() { redirectTo(popup) }, 50);
			myWin2 = window.open("", "", "resizable, scrollbars, menubar, location, status, toolbar");
			var text="<script"+" type='text/javascript'>"+"window.location.href="+direct+"; </script>";
			myWin2.document.write(text);
			break;
					
		default: 
			myWin = window.open("", "", "resizable, scrollbars, menubar, location, status, toolbar");
			var text="<script"+" type='text/javascript'>"+"window.location.href='"+popup+"';</script>";
			myWin.document.write(text);
			myWin.opener.close();
			myWin2 = window.open("", "", "resizable, scrollbars, menubar, location, status, toolbar");
			var text="<script"+" type='text/javascript'>"+"window.location.href="+direct+"; </script>";
			myWin2.document.write(text);
			break;
	}
}