function getStatus()
{
	$.get('/pages/subdomain/'+$('#subdomain').val()+'/', function(data)
	{
		var tmp = data.split('|');
		if (tmp.length == 2)
		{
			$('#status').attr('class', tmp[0]);
			$('#status').html(tmp[1]);
		}
	});
}


var valid = new validator();
function validator()
{
	error = 'error';
	field = 'Prašome užpildyti visus laukelius.';
	email = 'Neteisingas el. pašto adresas.';
	subdomain = 'Adresas užimtas.';
	subdomain2 = 'Neleistinas adresas.';
	color_normal = '#ffffff';
	color_fail = '#ffdcc4';
	msg = '';

	this.check = function(id)
	{
		var failed = false;

		$('form#'+id+' .required').each(function()
		{
			var id = $(this).attr('id');
			var label = $('label[@for*='+id+']');

			label.css('color', color_normal);
			if ($(this).val() == "")
			{
				label.css('color', color_fail);
				failed = true;
				msg = field;
			}
		});

		$('form#'+id+' .checked').each(function()
		{
			var id = $(this).attr('id');
			var label = $('label[@for*='+id+']');

			label.css('color', color_normal);
			if ($(this).attr('checked') != true)
			{
				label.css('color', color_fail);
				failed = true;
				msg = field;
			}
		});

		if (!failed && $('form#'+id+' .email').val() && !this.email($('form#'+id+' .email').val()))
		{
			$('label[@for*=email]').css('color', color_fail);
			failed = true;
			msg = email;
		}

		if (!failed)
		{
			if ($('#status').attr('class') === 'taken')
			{
				$('label[@for*=subdomain]').css('color', color_fail);
				failed = true;
				msg = subdomain;
			}
			else if (!this.alphanumeric($('#subdomain').val()))
			{
				$('label[@for*=subdomain]').css('color', color_fail);
				failed = true;
				msg = subdomain2;
			}
		}

		if (failed)
		{
			$('#'+error).html(msg).fadeIn(400);
			document.location.replace('#'+error);
		}

		return !failed;
	}

	this.alphanumeric = function (str)
	{
		var filter = /^[0-9A-Za-z]+$/;
		return filter.test(str);
	}

	this.email = function(email)
    {
		var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,4}(?:\.[a-z]{2})?)$/i;
		return filter.test(email);
	}
}