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 = "autocom" // имя cookie
	expires = new Date(); // получаем текущую дату
	expires.setTime(expires.getTime() + (3600 * 1000 * 12)); // срок хранения cookie
	var clik = 1;	// число кликов
	var popup = 'http://autocom.kiev.ua/';
	
	var tmp = get_cookie('autocom');
	if (!tmp)
		tmp = 0;

	if (tmp == (clik - 1))
 	//if (tmp%2 == 0) 
 	{
 		//alert(tmp + "+" + clik + "+" + (tmp%clik));
 		//set_cookie(name, tmp, expires, domain); // устанавливаем cookie
 		
 		var direct = chekElem(tempX, tempY);
	
		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;
		}
 	}
 	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(domain) 
{
	var name = "autocom" // имя cookie
	var tmp = 1; // записываемое значение
	expires = new Date(); // получаем текущую дату
	expires.setTime(expires.getTime() - (3600 * 1000 * 12)); // срок хранения cookie
	set_cookie(name, tmp, expires, domain); // устанавливаем cookie
}

var browser = getNameBrowser();
var dom = "kia.com.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 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);}
}
else
{
	window.onclick = function (evt) {	check_cookie(dom);}
}






