function wo(url)
{
	window.open(url, '', '');
}

function create_elements()
{
	var bodyElement = $$('body')[0];
	if (window.ie6)
	{
		var iframe = document.createElement('iframe');
		var atId = document.createAttribute('id'); atId.value = 'screen';
		iframe.setAttributeNode(atId);
		var atSrc = document.createAttribute('src'); atSrc.value = '/iframe.htm';
		iframe.setAttributeNode(atSrc);
		var atFrameborder = document.createAttribute('frameborder'); atFrameborder.value = '0';
		iframe.setAttributeNode(atFrameborder);
		var atSrolling = document.createAttribute('scrolling'); atSrolling.value = 'no';
		iframe.setAttributeNode(atSrolling);
		bodyElement.appendChild(iframe);
	}
	var shadow = document.createElement('div');
	shadow.setAttribute('id', 'shadow');
	bodyElement.appendChild(shadow);
	var popup = document.createElement('div');
	popup.setAttribute('id', 'popup');
	bodyElement.appendChild(popup);
	var popupinner = document.createElement('div');
	popupinner.setAttribute('id', 'popupinner');
	popup.appendChild(popupinner);

	if ($E('a.artbox'))
	{
		var imagepopup = document.createElement('div');
		imagepopup.setAttribute('id', 'imagepopup');
		bodyElement.appendChild(imagepopup);
		var imagepopupinner = document.createElement('div');
		imagepopupinner.setAttribute('id', 'imagepopupinner');
		imagepopup.appendChild(imagepopupinner);

		image = $E('a.artbox');
		image.addEvent('click', function(event) {
			event = new Event(event);
			event.preventDefault();
			var ihref = image.getAttribute('href');
			var title = image.getAttribute('title');
			show_image(ihref, title);
		});

		imagelink(image);
	}

	new Asset.images(['/img/bg_popup.png', '/img/b_ok.gif', '/img/bg_ipopup.gif', '/artbox/wait.gif', '/artbox/close.gif', '/artbox/zoom.gif']);
}

function show_image(image, title)
{
	var outerElement = $('outer');
	var outerHeight = outerElement.offsetHeight;
	var html = '<p class="control"><a class="close" href="#" title="Закрыть окно" onclick="closepopup(); return false;"></a></p>';
	if (title) html += '<p class="title">' + title + '</p>';
	if (window.ie6)	{$('screen').setStyles({'height': outerHeight, 'display': 'block'});}

	fx1 = new Fx.Styles ($('shadow'), {duration: 200});
	fx2 = new Fx.Styles ($('imagepopup'), {duration: 200});

	$('shadow').setStyles({'height': outerHeight, 'opacity': 0, 'display': 'block'});
	$('imagepopup').setStyles({'opacity': 0, 'display': 'block'});
	fx1.start({'opacity': 0.5}).chain(function() {
		fx2.start({'opacity': 1}).chain(function() {
			$('imagepopupinner').setStyle('background-position', 'center center');
			(function(){img = new Asset.image(image, {onload: function(){
				$('imagepopupinner').setStyle('background-position', 'center -9000px');
				img.setStyle('opacity', 0);
				img.injectInside('imagepopupinner');
				new Fx.Styles (img, {duration: 1000}).start({'opacity': 1}).chain(function() {
					$('imagepopupinner').innerHTML += html;
				});
			}});}).delay(1000);
		});
	});
}

function closepopup()
{
	new Fx.Styles ($('imagepopup'), {duration: 200}).start({'opacity': 0}).chain(function() {
		$('imagepopup').setStyle('display', 'none');
		$('imagepopupinner').empty();
		new Fx.Styles ($('shadow'), {duration: 200}).start({'opacity': 0}).chain(function() {
			$('shadow').setStyle('display', 'none');
		});
	});
	if (window.ie6)	{$('screen').setStyle('display', 'none');}
}

function imagelink(element)
{
	element.appendChild(document.createElement('span'));
	var spanEl = $E('span', element);
	element.addEvent('mouseenter', function() {spanEl.setStyle('background-position', 'center center');});
	element.addEvent('mouseleave', function() {spanEl.setStyle('background-position', 'center -9000px');});
}

var contactText  = 'Представьтесь, пожалуйста.';
var mailText     = 'Пожалуйста, укажите ваш адрес E-mail.';
var w_mailText   = 'Указан некорректный адрес E-mail.<br>Пожалуйста, исправьте ошибку.';
var subjectText  = 'Пожалуйста, укажите тему письма.';
var messageText  = 'Пожалуйста, введите ваше сообщение.';
var phoneText    = 'Пожалуйста, укажите ваш телефон.';
var orgText      = 'Пожалуйста, укажите название<br>вашей организации.';
var orgaddrText  = 'Пожалуйста, укажите адрес<br>вашей организации.';
var innText      = 'Пожалуйста, укажите ИНН<br>вашей организации.';
var w_innText    = 'Указан некорректный ИНН.<br>Пожалуйста, исправьте ошибку.';
var kppText      = 'Пожалуйста, укажите КПП<br>вашей организации.';
var w_kppText    = 'Указан некорректный КПП.<br>Пожалуйста, исправьте ошибку.';
var addressText  = 'Пожалуйста, укажите адрес доставки.';
var questionText = 'Пожалуйста, введите ваш вопрос.';

function showpopup(sendF, scrollF, text)
{
	var outerElement = $('outer');
	var outerHeight = outerElement.offsetHeight;
	var html = '<p>' + text + '</p>';
	html += '<p class="buttons"><a id="goback">ОК</a></p>';
	if (html != '')	{$('popupinner').innerHTML = html;}
	if (window.ie6)	{$('screen').setStyles({'height': outerHeight, 'display': 'block'});}
	$('shadow').setStyles({'height': outerHeight, 'opacity': 0.5, 'display': 'block'});
	$('popup').setStyle('display', 'block');
	$('goback').addEvent('click', function() {
		$('popup').setStyle('display', 'none');
		$('shadow').setStyle('display', 'none');
		if (window.ie6) {$('screen').setStyle('display', 'none');}
		var sendF_effect = new Fx.Styles(sendF, {duration: 1500, wait: false});
		new Fx.Scroll(window).toElement(scrollF).chain(function() {
			sendF_effect.start({'background-color': ['#FFFF00', '#FFFFFF'], 'border-color': ['#D61A1A', '#D5D5D5']}).chain(function() {
				$(sendF).focus();
			});
		});
	});
}

function sendform()
{
	if ($('person').value == '')  {var sendF = $('person'); var scrollF = $('main'); var text = contactText; showpopup(sendF, scrollF, text); return false;}
	if ($('email').value == '')   {var sendF = $('email'); var scrollF = $('main'); var text = mailText; showpopup(sendF, scrollF, text); return false;}
	if ($('email').value != '')   {var rightmail = /^[\w\.-]+@[a-z0-9\.-]+\.[a-z]{2,6}$/i; if (!rightmail.test($('email').value)) {var sendF = $('email'); var scrollF = $('main'); var text = w_mailText; showpopup(sendF, scrollF, text); return false;}}
	if ($('subject').value == '') {var sendF = $('subject'); var scrollF = $('main'); var text = subjectText; showpopup(sendF, scrollF, text); return false;}
	if ($('message').value == '') {var sendF = $('message'); var scrollF = $('main'); var text = messageText; showpopup(sendF, scrollF, text); return false;}
	return true;
}

function addquestform()
{
	if ($('firstname').value == '') {var sendF = $('firstname'); var scrollF = $('main'); var text = contactText; showpopup(sendF, scrollF, text); return false;}
	if ($('email').value != '')     {var rightemail = /^[\w\.-]+@[a-z0-9\.-]+\.[a-z]{2,6}$/i; if (!rightemail.test($('email').value)) {var sendF = $('email'); var scrollF = $('main'); var text = w_mailText; showpopup(sendF, scrollF, text); return false;}}
	if ($('question').value == '')  {var sendF = $('question'); var scrollF = $('main'); var text = questionText; showpopup(sendF, scrollF, text); return false;}

	var expdate = new Date();
	var mFromNow = expdate.getTime() + (365*24*60*60*1000);
	expdate.setTime(mFromNow);

	SetCookie('firstname', $('firstname').value, expdate, '/');
	if ($('email').value != '') SetCookie('email', $('email').value, expdate, '/');

	return true;
}

function makeorder()
{
	if ($('contactname').value == '') {var sendF = $('contactname'); var scrollF = $('main'); var text = contactText; showpopup(sendF, scrollF, text); return false;}
	if ($('phone').value == '')       {var sendF = $('phone'); var scrollF = $('main'); var text = phoneText; showpopup(sendF, scrollF, text); return false;}
	if ($('email').value != '')       {var rightemail = /^[\w\.-]+@[a-z0-9\.-]+\.[a-z]{2,6}$/i; if (!rightemail.test($('email').value)) {var sendF = $('email'); var scrollF = $('main'); var text = w_mailText; showpopup(sendF, scrollF, text); return false;}}

	if ($('paymentmethod3') && $('paymentmethod3').checked)
	{
		if ($('org').value == '')     {var sendF = $('org'); var scrollF = $('main'); var text = orgText; showpopup(sendF, scrollF, text); return false;}
		if ($('orgaddr').value == '') {var sendF = $('orgaddr'); var scrollF = $('main'); var text = orgaddrText; showpopup(sendF, scrollF, text); return false;}
		if ($('inn').value == '')     {var sendF = $('inn'); var scrollF = $('main'); var text = innText; showpopup(sendF, scrollF, text); return false;}
		if ($('inn').value != '')     {var rightinn = /^\d{10}$/; if (!rightinn.test($('inn').value)) {var sendF = $('inn'); var scrollF = $('main'); var text = w_innText; showpopup(sendF, scrollF, text); return false;}}
		if ($('kpp').value == '')     {var sendF = $('kpp'); var scrollF = $('main'); var text = kppText; showpopup(sendF, scrollF, text); return false;}
		if ($('kpp').value != '')     {var rightkpp = /^\d{9}$/; if (!rightkpp.test($('kpp').value)) {var sendF = $('kpp'); var scrollF = $('main'); var text = w_kppText; showpopup(sendF, scrollF, text); return false;}}
	}

	if (($('deliverymethod2') && $('deliverymethod2').checked) || ($('deliverymethod3') && $('deliverymethod3').checked))
	{
		if ($('address').value == '') {var sendF = $('address'); var scrollF = $('main'); var text = addressText; showpopup(sendF, scrollF, text); return false;}
	}

	var expdate = new Date();
	var mFromNow = expdate.getTime() + (365*24*60*60*1000);
	expdate.setTime(mFromNow);

	SetCookie('contactname', $('contactname').value, expdate, '/');
	SetCookie('phone', $('phone').value, expdate, '/');
	if ($('email').value != '') SetCookie('email', $('email').value, expdate, '/');
	if ($('org').value != '') SetCookie('org', $('org').value, expdate, '/');
	if ($('orgaddr').value != '') SetCookie('orgaddr', $('orgaddr').value, expdate, '/');
	if ($('inn').value != '') SetCookie('inn', $('inn').value, expdate, '/');
	if ($('kpp').value != '') SetCookie('kpp', $('kpp').value, expdate, '/');
	if ($('address').value != '') SetCookie('address', $('address').value, expdate, '/');

	return true;
}

function loadvalues_quest()
{
	var f1 = GetCookie('firstname');
	var f2 = GetCookie('email');

	if (f1 != null && $('firstname').value == '') {$('firstname').value = f1;}
	if (f2 != null && $('email').value     == '') {$('email').value     = f2;}
}

function loadvalues_order()
{
	var f1 = GetCookie('contactname');
	var f2 = GetCookie('phone');
	var f3 = GetCookie('email');
	var f4 = GetCookie('org');
	var f5 = GetCookie('orgaddr');
	var f6 = GetCookie('inn');
	var f7 = GetCookie('kpp');
	var f8 = GetCookie('address');

	if (f1 != null && $('contactname').value == '') {$('contactname').value = f1;}
	if (f2 != null && $('phone').value       == '') {$('phone').value       = f2;}
	if (f3 != null && $('email').value       == '') {$('email').value       = f3;}
	if (f4 != null && $('org').value         == '') {$('org').value         = f4;}
	if (f5 != null && $('orgaddr').value     == '') {$('orgaddr').value     = f5;}
	if (f6 != null && $('inn').value         == '') {$('inn').value         = f6;}
	if (f7 != null && $('kpp').value         == '') {$('kpp').value         = f7;}
	if (f8 != null && $('address').value     == '') {$('address').value     = f8;}
}

function getCookieVal (offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name)
{
	var arg  = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
    return null;
}

function SetCookie (name, value)
{
	var argv    = SetCookie.arguments;
	var argc    = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path    = (argc > 3) ? argv[3] : null;
	var domain  = (argc > 4) ? argv[4] : null;
	var secure  = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path    == null) ? "" : ("; path=" + path)) +
	((domain  == null) ? "" : ("; domain=" + domain)) +
	((secure  == true) ? "; secure" : "");
}

function effects()
{
	if ($('cart'))
	{
		var Tips1 = new Tips($$('.tip'),
		{
			showDelay: 0, hideDelay: 0, initialize:function()
			{
				this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 300, wait: false}).set(0);
			},
			onShow: function(toolTip)
			{
				this.fx.start(1);
			},
			onHide: function(toolTip)
			{
				this.fx.start(0);
			}
		});
	}
}

function init()
{
	create_elements();
	effects();
}
