var syntax = /(^|.|\r|\n)(\[(\w+)\])/;
var comment = null;

Event.observe(window, 'load', function() {
    if(typeof __comment != 'undefined')
        comment = new CComment(__comment);

	if((f = $('loginForm')) != null) {
		f.observe('submit', function(ev) {
			ev.stop();
			f	= $('loginForm');

			if(f.u.value.search(/^\S{4,31}$/) == -1) {
				alert('username must be 4 to 31 characters long and can not contain spaces.');
				f.u.activate();
				return;
			}

			if(f.p.value.search(/^\S{6,100}$/) == -1) {
				alert('password must contain at least 6 characters and no spaces.');
				f.p.activate();
				return;
			}

			$('loginLoader').show();

			new Ajax.Request('/ajax/login.php', {
				parameters:	f.serialize(),
				onSuccess:	function(t) {
					try {
						js	= t.responseText.evalJSON(true);
					} catch(e) {
						alert('Received invalid JSON:\n' + t.responseText);
						return;
					}

					if(js.m == 'login') {
						if(js.r) {
							document.location.reload(true);
						} else {
							alert(js.d);
						}
					} else if(js.m == -1) {
						alert(js.r);
					} else {
						alert(t.responseText);
					}
				},
				onComplete: function() {
					$('loginLoader').hide();
				}
			});
		}); 
	}

	if((l = $('loginCreate')) != null) {
		l.observe('click', function(ev) {
			ev.stop();

			cb	= $('createAccountBox');

			os	= ev.element().cumulativeOffset();
			wd	= cb.getWidth();
			cb.setStyle({
				left:	(os.left - wd) + 'px',
				top:	os.top + 'px'
			});
			$('captcha').src	= '/captcha.php?' + Math.random();
			cb.show();
		});
	}

	if((f = $('createForm')) != null) {
		f.observe('submit', function(ev) {
			ev.stop();

			f	= ev.element();
			u	= f.u.value.strip();
			p	= f.p.value.strip();
			cp	= f.cp.value.strip();
			em	= f.em.value.strip();
			c	= f.c.value.strip();

			if(u.search(/^\S{4,31}$/) == -1) {
				alert('username must be 4 to 31 characters long and can not contain spaces.');
				f.u.activate();
				return;
			}

			if(p.search(/^\S{6,}$/) == 1) {
				alert('password must contain at least 6 characters and no spaces.');
				f.p.activate();
				return;
			}

			if(p != cp) {
				alert('Passwords do not match.');
				f.p.activate();
				return;
			}

			if(em != '' && em.search(/^([-\w]+\.)*[-\w]+@([-\w]+\.)+[A-Za-z]{2,4}$/) == -1) {
				alert('Invalid e-mail address.');
				f.em.activate();
				return;
			}

			if(c.search(/^[23456789bcdfghjkmnpqrstvwxyzBCDFGHJKMNPRSVWXZ]{7}$/) == -1) {
				alert('Invalid CAPTCHA code. Try again.');
				$('captcha').src	= '/captcha.php?' + Math.random();
				f.c.activate().clear();
				return;
			}

			$('createLoader').show();

			new Ajax.Request('/ajax/create.php', {
				parameters:	f.serialize(),
				onSuccess:	function(t) {
					try {
						js	= t.responseText.evalJSON(true);
					} catch(e) {
						alert('Received invalid JSON:\n' + t.responseText);
						return;
					}

					if(js.m == 'create' && js.r == true) {
						alert("Thanks for signing up. You can start using your account as soon as the page reloads.\n" +
								"For those who submitted an e-mail address, you can use your account immediately, but you will need to confirm your e-mail address by clicking the link sent to you. Don't forget to check your spam folder.");
						document.location.reload(true);
					} else if(js.m == 'captcha') {
						alert('Invalid CAPTCHA code. Try again.');
						$('captcha').src	= '/captcha.php?' + Math.random();
						$('createForm').c.activate().clear();
					} else if(js.m == -1) {
						alert(js.r);
					} else {
						alert(t.responseText);
					}
				},
				onComplete:	function() {
					$('createLoader').hide();
				}
			});
		});
	}

	Ajax.Responders.register({
		onFailure: function() {
			alert('There was an error connecting to the server. Please try again.');
		}
	});

	$('cover').remove();
});
