﻿var isRegistering = false;

if (!gotEntryPoint)
{
	gotEntryPoint = true;

	$(document).ready(function() {
		initMain(false);

		$('#Tag').val('<previousurl>' + document.referrer + '</previousurl>');

		$('#goBackButton').click(function(event) {
			if (event && typeof(event.preventDefault) != 'undefined')
				event.preventDefault();

			window.history.go(-1);
		});
	});
}

function registerButtonClick(event)
{
	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	$('#registerButton').attr('disabled', 'true');

	validateInput();

	if ($('.error:visible').length > 0)
	{
		$('#registerButton').attr('disabled', null);
		return(false);
	}

	isRegistering = true;

	$('#registerResult').html('Registering, please wait...');
	$('#registerResult').removeClass('invisible');

	$('#CaptchaChallenge').val($('#recaptcha_challenge_field').val());
	$('#CaptchaResponse').val($('#recaptcha_response_field').val());
	
	//DMP
	//alert($('#recaptcha_challenge_field').val());
	
	document.forms['newUserForm'].submit();

	return(true);
}

function onRegisterCallback(success, isSystemError, captchaError)
{
	
	isRegistering = false;

	$('#registerButton').attr('disabled', null);

	if (!success)
	{
		if (!isSystemError)
		{
			$('#registerResult').text('Please correct the highlighted errors and try again.');

			Recaptcha.reload();
		}
		else
			$('#registerResult').text('The system could not register you at this time, please try again later.');
	}
	else
		$('#newUserDetails').hide();
}

function showError(span, value)
{
	if (value)
		$(span).text(value).show();
}

function validateInput()
{
	$('.error').hide();

	if (!$('#agreeTandC').attr('checked'))
		showError('#agreeTandCError', 'You must agree to the terms and conditions before registering');

	if ($('#recaptcha_response_field').val().length < 1)
		showError('#captchaError', 'Text from the security image must be entered');


	/* These should match up with Lib.User.Manager.Create() - otherwise nasty validation mismatches will occur ! */

	if ($('#firstName').val().length < 1)
		showError('#firstNameError', 'First name must be entered');

	if ($('#lastName').val().length < 1)
		showError('#lastNameError', 'Last name must be entered');

	if (!$('#loginName').val().match(/^[A-Za-z0-9]{6,16}$/))
		showError('#loginNameError', 'User name must be 6-16 alphanumeric characters - no spaces or punctuation characters');

	var password = $('#userPassword').val();
	if (password.length < 6 || !password.match(/[^A-Za-z]/) || !password.match(/[A-Z]/) || !password.match(/[a-z]/))
		showError('#userPasswordError', 'Passwords must be at least 6 characters and a mix of upper- and lower-case letters with some numbers or special characters');

	if (password != $('#confirmUserPassword').val())
		showError('#confirmUserPasswordError', 'Passwords must match');

	var emailAddress = $('#emailAddress').val();
	if (!isValidEmailAddress(emailAddress))
		showError('#emailAddressError', 'E-mail address does not appear to be valid');

	if ($('#emailAddress').val() != $('#confirmEmailAddress').val())
		showError('#confirmEmailAddressError', 'E-mail addresses must match');
}

function onRegisterResponseReceived()
{
	
	//document.domain="bl.uk";
	if (!isRegistering)
		return;

	var iFrame = document.getElementById('formSubmitTarget');

	var innerXml;

	if (iFrame.contentDocument)
		{
		//iFrame.contentDocument.domain="bl.uk";
	    innerXml = iFrame.contentDocument;
		}	
	else if (iFrame.contentWindow)
		{
		//iFrame.contentWindow.document.domain="bl.uk";
		innerXml = iFrame.contentWindow.document.documentElement.innerText;	// IE formats XML documents - just use the textual version rather than the (HTML-ised) DOM
		}
	else if (iFrame.document)
		{
		//iFrame.document.domain="bl.uk";
		innerXml = iFrame.document;
		}
	else
		{
		innerXml = '';
		}

	if (innerXml != '')
	{
		$.xslt({
			xml:			innerXml,
			xslUrl:			xslBaseUrl + 'User.xsl',
			xslCache:		isLive,		/* Don't cache if debugging */
			dataTypeXML:	true,
			target:			'#registerResult'
		});
	}
}

