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

function create_popup_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 warn = document.createElement('div');
	warn.setAttribute('id', 'warn');
	bodyElement.appendChild(warn);
	var preview = document.createElement('div');
	preview.setAttribute('id', 'preview');
	bodyElement.appendChild(preview);

	new Asset.images
	([
		'/img/bgwarn.gif',
		'/img/bgbut50.gif',
		'/img/wait.gif',
		'/img/bgpreview.gif',
		'/img/close.gif'
	]);
}

function showalert(sendF, scrollF, text)
{
	var outerElement = $('outer');
	var outerHeight = outerElement.offsetHeight;
	var html = '<p>' + text + '</p>';
	html += '<p class="buttons"><input id="goback" class="but" type="button" value="ОК"></p>';
	if (html != '')	{$('warn').innerHTML = html;}
	if (window.ie6)	{$('screen').setStyles({'height': outerHeight, 'display': 'block'});}
	$('shadow').setStyles({'height': outerHeight, 'opacity': 0.5, 'display': 'block'});
	$('warn').setStyle('display', 'block');
	$('goback').addEvent('click', function() {
		$('warn').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': ['#CC9900', '#CCCCCC']}).chain(function() {
				$(sendF).focus();
			});
		});
	});
}

function showpreview()
{
	if ($('comment').value == '')
	{
		var sendF   = $('comment');
		var scrollF = $('addcom');
		var text    = commentText;
		showalert(sendF, scrollF, text);
		return false;
	}
	else
	{
		var outerElement = $('outer');
		var outerHeight = outerElement.offsetHeight;

		if (window.ie6)	{$('screen').setStyles({'height': outerHeight, 'display': 'block'});}
		$('shadow').setStyles({'height': outerHeight, 'opacity': 0.5, 'display': 'block'});
		$('preview').setStyle('display', 'block');

		var url = '/netcat/modules/default/preview.php';
		var com = encodeURIComponent($('comment').value);
		var fx = new Fx.Styles ($('preview'), {duration: 1500, transition: Fx.Transitions.Back.easeOut});

		if (window.ie6)
		{
			fx.start({'width': 675, 'height': 300, 'margin-left': -337}).chain(function() {
				$('preview').setStyle('background-position', 'center center');
			});
		}
		else
		{
			fx.start({'width': 675, 'height': 300, 'margin-top': -150, 'margin-left': -337}).chain(function() {
				$('preview').setStyle('background-position', 'center center');
			});
		}

		(function(){new Ajax(url, {
			method: 'post', data: 'comment=' + com, update: 'preview', onComplete: function() {
				$('preview').setStyle('background-position', 'center -9000px')}}).request();}).delay(3000);
	}
}

function closepreview()
{
	$('preview').innerHTML = '';
	if (window.ie6)
	{
		$('preview').setStyles({'background-position': 'center -9000px', 'width': 0, 'height': 0, 'margin-left': 0, 'display': 'none'});
		$('screen').setStyle('display', 'none');
	}
	else
	{
		$('preview').setStyles({'background-position': 'center -9000px', 'width': 0, 'height': 0, 'margin-top': 0, 'margin-left': 0, 'display': 'none'});
	}
	$('shadow').setStyle('display', 'none');
	new Fx.Scroll(window).toElement('addcom').chain(function() {
		$('comment').focus();
	});
}

var firstnameText = 'Представьтесь, пожалуйста.';
var mailText      = 'Пожалуйста, укажите ваш адрес E-mail.';
var badmailText   = 'Указан некорректный адрес E-mail.<br>Пожалуйста, исправьте ошибку.';
var subjectText   = 'Пожалуйста, укажите тему сообщения.';
var messageText   = 'Пожалуйста, введите ваше сообщение.';
var countryText   = 'Пожалуйста, укажите вашу страну.';
var cityText      = 'Пожалуйста, укажите вашу страну и город.';
var commentText   = 'Пожалуйста, введите ваш комментарий.';
var logText       = 'Прекратите хулиганить!';

function sendform() {
if ($('firstname').value == '') {
var sendF   = $('firstname');
var scrollF = $('mainform');
var text    = firstnameText;
showalert(sendF, scrollF, text); return false; }

if ($('mail').value == '') {
var sendF   = $('mail');
var scrollF = $('mainform');
var text    = mailText;
showalert(sendF, scrollF, text); return false; }

if ($('mail').value != '') {
var rightmail = /^[\w\.-]+@[a-z0-9\.-]+\.[a-z]{2,6}$/i;
if (!rightmail.test($('mail').value)) {
var sendF   = $('mail');
var scrollF = $('mainform');
var text    = badmailText;
showalert(sendF, scrollF, text); return false; }}

if ($('subject').value == '') {
var sendF   = $('subject');
var scrollF = $('mainform');
var text    = subjectText;
showalert(sendF, scrollF, text); return false; }

if ($('message').value == '') {
var sendF   = $('message');
var scrollF = $('mainform');
var text    = messageText;
showalert(sendF, scrollF, text); return false; }
return true; }

function addcomform() {
if ($('firstname').value == '') {
var sendF   = $('firstname');
var scrollF = $('addcom');
var text    = firstnameText;
showalert(sendF, scrollF, text); return false; }

if ($('country').value == 'none') {
var sendF   = $('country');
var scrollF = $('addcom');
var text    = countryText;
showalert(sendF, scrollF, text); return false; }

if ($('country').value == '16') {
if ($('city').value == '') {
var sendF   = $('city');
var scrollF = $('addcom');
var text    = cityText;
showalert(sendF, scrollF, text); return false; }}

if ($('mail').value == '') {
var sendF   = $('mail');
var scrollF = $('addcom');
var text    = mailText;
showalert(sendF, scrollF, text); return false; }

if ($('mail').value != '') {
var rightmail = /^[\w\.-]+@[a-z0-9\.-]+\.[a-z]{2,6}$/i;
if (!rightmail.test($('mail').value)) {
var sendF   = $('mail');
var scrollF = $('addcom');
var text    = badmailText;
showalert(sendF, scrollF, text); return false; }}

if ($('comment').value == '') {
var sendF   = $('comment');
var scrollF = $('addcom');
var text    = commentText;
showalert(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, '/');
SetCookie('country', $('country').value, expdate, '/');
SetCookie('city', $('city').value, expdate, '/');
SetCookie('site', $('site').value, expdate, '/');
SetCookie('mail', $('mail').value, expdate, '/');

return true; }

function setflag()
{
	var flagElement = $('flag');
	var starElement = $('townstar');
	if ($('country').value == 'none') {flagElement.setStyle('background-position', '0px 0px');}
	if ($('country').value == '1')    {flagElement.setStyle('background-position', '0px -19px');}
	if ($('country').value == '2')    {flagElement.setStyle('background-position', '0px -38px');}
	if ($('country').value == '3')    {flagElement.setStyle('background-position', '0px -57px');}
	if ($('country').value == '4')    {flagElement.setStyle('background-position', '0px -76px');}
	if ($('country').value == '5')    {flagElement.setStyle('background-position', '0px -95px');}
	if ($('country').value == '6')    {flagElement.setStyle('background-position', '0px -114px');}
	if ($('country').value == '7')    {flagElement.setStyle('background-position', '0px -133px');}
	if ($('country').value == '8')    {flagElement.setStyle('background-position', '0px -152px');}
	if ($('country').value == '9')    {flagElement.setStyle('background-position', '0px -171px');}
	if ($('country').value == '10')   {flagElement.setStyle('background-position', '0px -190px');}
	if ($('country').value == '11')   {flagElement.setStyle('background-position', '0px -209px');}
	if ($('country').value == '12')   {flagElement.setStyle('background-position', '0px -228px');}
	if ($('country').value == '13')   {flagElement.setStyle('background-position', '0px -247px');}
	if ($('country').value == '14')   {flagElement.setStyle('background-position', '0px -266px');}
	if ($('country').value == '15')   {flagElement.setStyle('background-position', '0px -285px');}
	if ($('country').value == '16')
	{
		flagElement.setStyle('background-position', '0px -304px');
		starElement.setStyle('display', 'inline');
	}
	else
	{
		starElement.setStyle('display', 'none');
	}
}

// BB Code
function insert_text(open, close)
{
	msgfield = (document.all) ? document.all.comment : document.forms['addcom']['comment'];
	if (document.selection && document.selection.createRange)
	{
		msgfield.focus();
		sel = document.selection.createRange();
		sel.text = open + sel.text + close;
		msgfield.focus();
	}
	else if (msgfield.selectionStart || msgfield.selectionStart == '0')
	{
		var startPos = msgfield.selectionStart;
		var endPos = msgfield.selectionEnd;
		msgfield.value = msgfield.value.substring(0, startPos) + open + msgfield.value.substring(startPos, endPos) + close + msgfield.value.substring(endPos, msgfield.value.length);
		msgfield.selectionStart = msgfield.selectionEnd = endPos + open.length + close.length;
		msgfield.focus();
	}
	else
	{
		msgfield.value += open + close;
		msgfield.focus();
	}
	return;
}

function insert_url()
{
	var z2 = prompt("Пожалуйста, введите полный URL-адрес:", "http://");
	msgfield2 = (document.all) ? document.all.comment : document.forms['addcom']['comment'];
	if (document.selection && document.selection.createRange)
	{
		msgfield2.focus();
		sel2 = document.selection.createRange();
		sel2.text = '[url='+ z2 +']' + sel2.text + '[/url]';
		msgfield2.focus();
	}
	else if (msgfield2.selectionStart || msgfield2.selectionStart == '0')
	{
		var startPos2 = msgfield2.selectionStart;
		var endPos2 = msgfield2.selectionEnd;
		msgfield2.value = msgfield2.value.substring(0, startPos2) + '[url='+ z2 +']' + msgfield2.value.substring(startPos2, endPos2) + '[/url]' + msgfield2.value.substring(endPos2, msgfield2.value.length);
		msgfield2.selectionStart = msgfield2.selectionEnd = endPos2 + open.length + close.length;
		msgfield2.focus();
	}
	else
	{
		msgfield2.value += '[url='+ z2 +']' + '[/url]';
		msgfield2.focus();
	}
	return;
}

// Cookies
function loadvalues()
{
	var f1 = GetCookie('firstname');
	var f2 = GetCookie('country');
	var f3 = GetCookie('city');
	var f4 = GetCookie('site');
	var f5 = GetCookie('mail');

	if (f1 != null && $('firstname').value == '') {$('firstname').value = f1;}
	if (f2 != null && $('country').value == 'none') {$('country').value = f2;}
	if (f3 != null && $('city').value == '') {$('city').value = f3;}
	if (f4 != null && $('site').value == '') {$('site').value = f4;}
	if (f5 != null && $('mail').value == '') {$('mail').value = f5;}
}

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()
{
	var Tips1 = new Tips($$('.tip'),
	{
		showDelay: 0, hideDelay: 0, initialize:function()
		{
		    this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
		},
		onShow: function(toolTip)
		{
			this.fx.start(1);
		},
		onHide: function(toolTip)
		{
			this.fx.start(0);
		}
	});

	new SmoothScroll();

	if ($('addcom'))
	{
		var txt = $('firstname'), log = $('log');
		var fx  = new Fx.Styles(log, {duration: 3000, wait: false, transition: Fx.Transitions.Quad.easeOut});
		$('firstname').addEvents({
			'keyup': function() {
				if 	(txt.value.contains('Константин Ефимов')) txt.fireEvent('burn', logText);
				else if (txt.value.contains('Aykroyd'))	txt.fireEvent('burn', logText);
			},
			'burn': function(text) {
				txt.value = '';
				log.setHTML(text);
				fx.start({'background-color': ['#FF0000', '#FFFFFF'], 'opacity': [1, 0]})
			}
		});
	}
}

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