function addMissing (element)
{
	var name = element.name + "_missing";
	if ((!element.nextSibling) || (element.nextSibling.id != name))
	{
		var span = document.createElement ("span");
		span.id = name;
		span.className = "error";
		span.appendChild (document.createTextNode (" *Missing"));
		if (element.nextSibling)
			element.parentNode.insertBefore (span, element.nextSibling);
		else
			element.parentNode.appendChild (span);
	}
}

function removeMissing (element)
{
	var name = element.name + "_missing";
	if (element.nextSibling && (element.nextSibling.id == name))
		element.parentNode.removeChild (element.nextSibling);
}

function checkMissing (element)
{
	if (element.value.length == 0)
	{
		addMissing (element);
		return true;
	}
	else
	{
		removeMissing (element);
		return false;
	}
}

function submitForm (caller)
{
	function callback (httpRequest)
	{
		if (httpRequest.status == 200)
		{
			var args = {status: false, errorMsg: null};
			try	{ args = JSON.parse (httpRequest.responseText);	}
			catch (ex) {}
			if (args.status)
			{ // The user details are OK - redirect him to PayPal to complete the registration.
				var payPalForm = document.getElementById("PayPalForm");
				payPalForm.custom.value = args.uID;
				payPalForm.submit();
			}
			else
				alert (args.errorMsg ? args.errorMsg :
				 "There was an error subscribing the user.\nPlease try again later.");
		}
	}

	var missing = false;

	missing |= checkMissing (caller.firstName);
	missing |= checkMissing (caller.lastName);
	missing |= checkMissing (caller.email);
	if (caller.country.selectedIndex == 0)
	{
		addMissing (caller.country);
		missing = true;
	}
	else
		removeMissing (caller.country);
//	if (type == 3)
//		missing |= checkMissing (caller.club);
	missing |= checkMissing (caller.username);
	missing |= checkMissing (caller.pass1);
	missing |= checkMissing (caller.pass2);
	missing |= checkMissing (caller.how);

	if (missing)
	{
		alert ("Please fill in the missing details.");
		return false;
	}

	if (caller.pass1.value != caller.pass2.value)
	{
		alert ("The passwords do not match.");
		return false;
	}
	
	var args = [];

	args[0] = JSON.stringify (getFormValues (caller));
	sajax_do_call ("register", args, callback);
}
