function init() {
        if (self.parent.frames.length != 0) {
                self.parent.location = document.location;
        }
        if (document.getElementById) {
                document.onkeydown = register;
        }
}

function register(e) {
        var code;
        if (!e) var e = window.event;
        if (e.keyCode) code = e.keyCode;
        else if (e.which) code = e.which;

		// shift + ctrl + E
		// ------------------
		// edit current page
		if((code == 69) && (e.ctrlKey == true) && (e.shiftKey == true)) {
			window.open('/hotkey.php?cmd=editpage&r='+escape(window.location.href),'hotkey');
		}
		// shift + ctrl + L
		// ------------------
		// edit current page
		if((code == 76) && (e.ctrlKey == true) && (e.shiftKey == true)) {
			ClickLogin();
		}
}

function PopUp(url, W, H) {
	W+=0;
	H+=0;
	var size="width="+W+",height="+H;
	window.open(url,"","status=no,scrollbars=no,resizable=yes,"+size);
	return false;
}

function ShowPopUp(url, W, H) {
	W+=0;
	H+=0;
	var size="width="+W+",height="+H;
	window.open(url,"","status=no,scrollbars=no,resizable=yes,"+size);
}

function showLogin(islogin)
{
	var f = document.forms['login'];
	if (document.getElementById && f) {
		var iss = getCookie('mkse_login');
		if(!iss || iss == 0) {
			if (islogin == 2) {
				var login = getCookie('mkse');
				islogin = (login && login.length > 1);
				if (islogin) {
					if (f.login.value != login) {
						f.login.value = login;
					}
				}
				f.pswrd.value = '';
			}
			document.getElementById('login-form').style.display = 'block';
			islogin ? f.pswrd.focus() : f.login.focus();
			return false;
		} else {
			return false;
		}
	}
	return true;
}

function hideLogin()
{
	if(document.getElementById('login-form')) document.getElementById('login-form').style.display = 'none';
	return false;
}

function getCookie(n)
{
	var c = document.cookie;
	if (c.length < 1) return false;

	var b = c.indexOf(n + '=');
	if (b == -1) return false;

	b += (n.length + 1);
	var e = c.indexOf(';', b);

	return unescape((e == -1) ? c.substring(b) : c.substring(b, e));
}

function CheckCookie()
{
	var sid = getCookie('Session_id');
	if (!sid) return false;
	var parts = sid.split('.');
	if (parts[2] == '1') return true;
    if (parts[2] == '3') return parseFloat(((new Date().getTime()) - parts[0] * 1000 - parts[1]) / 1000) < 1209600.0;
	return parseFloat(((new Date().getTime()) - parts[0] * 1000 - parts[1]) / 1000) < 7200.0;
}

function CheckSession(rp)
{
	if (!CheckCookie()) return showLogin(2, rp);
}

function ClickLogin()
{
	if (CheckCookie()) {
		return false;
	}
	return showLogin(2);
}

function CheckSearch(form) {
	if (form.q.value == 'поиск') {
		alert("Введите поисковый запрос");			
		form.q.value = '';
		form.q.focus();
	} else {
		form.submit();
	}
}

function checkAuth(form, extended) {
	if (form.UserLogin.value == '') {
		alert("Укажите логин");			
		form.UserLogin.focus();
		if (extended)
			return false;
	} else if (form.UserPassword.value == '') {
		alert("Укажите пароль.\n\nПароль вам был выслан на адрес эл. почты, указанный при регистрации");			
		form.UserPassword.focus();
		if (extended)
			return false;
	} else if (extended && typeof form.UserCaptcha != 'undefined' && form.UserCaptcha.value == '') {
		form.UserCaptcha.focus();
		alert('Введите код, указанный на изображении');
		if (extended)
			return false;
	} else {
		if (extended)
			return true;
		else
			form.submit();
	}
}

var hidEl = null;

function addToBasket(good_id, mode, href) {
	if (hidEl != null) hidEl.style.display = 'block';
	
	var pos = href.getParent().getPosition();
	
	var bcnt = document.getElementById('basketCount');
	bcnt.style.display = 'block';
	if (mode == 'request') {
		bcnt.style.top = pos.y + 2;
		bcnt.style.left = pos.x + (href.getParent().getCoordinates().width / 2) / 2;
	} else {
		bcnt.style.top = pos.y;
		bcnt.style.width = href.getParent().getCoordinates().width;
		bcnt.style.left = pos.x;
	}
	
	href.style.display = 'none';
	
	hidEl = href;

	with(document.basket) {
		GID.value = good_id;
		MODE.value = mode;		
	}	
	
	document.getElementById('gcnt').focus();
}
