var availReq = false;

function makeAvailabilityRequest()
{
	kaango_nickname = document.getElementById("nickname").value;
	kaango_email = document.getElementById("email").value;
	kaango_email = kaango_email.replace(/\./g, "|");
	if ( kaango_nickname != '' && kaango_email != '' )
	{
		url = siteURL+"ajaxRegisterAvailability/"+escape(kaango_nickname)+"/"+escape(kaango_email);
		if(window.XMLHttpRequest)
			availReq = new XMLHttpRequest();
		else if(window.ActiveXObject)
			availReq = new ActiveXObject("Microsoft.XMLHTTP");

		availReq.onreadystatechange = processAvailability;
		availReq.open("GET",url,true);
		availReq.send("");
	}
	return false;
}

function processAvailability()
{
	if(availReq.readyState == 4)
	{
		if(availReq.status == 200)
		{
			if ( availReq.responseText.substring(0,6) != '<html>' ) {
				refreshAvailability();
			}
		}
	}
}

function refreshAvailability()
{
	var my_errs = 0;
	availCode = availReq.responseText;
	// check for existing nickname
	nickAvailCode = availCode.substring(0,1);
	if ( nickAvailCode == 't')
	{
		my_errs++;
		document.getElementById("errors").style.display = 'block';
		document.getElementById("err_nickname").style.display = 'block';
		document.getElementById("row_nickname").style.backgroundColor = "#fff9c3" ;
	}
	// check for existing email
	emailAvailCode = availCode.substring(1,2);
	emailField = document.getElementById("emailAvail");
	emailField.innerHTML = "";
	if ( emailAvailCode == 't')
	{
		my_errs++;
		document.getElementById("errors").style.display = 'block';
		document.getElementById("err_emailUsed").style.display = 'block';
		document.getElementById("row_email").style.backgroundColor = "#fff9c3" ;
		document.getElementById("row_emailConfirm").style.backgroundColor = "#fff9c3" ;
		emailField.innerHTML = "We already have your email on record. It is likely that you have an account and have simply forgotten your password.  Click <a href='/fePasswordReset'>here</a> to reset your password.";
	}
	else if ( emailAvailCode == 'v')
	{
		my_errs++;
		document.getElementById("errors").style.display = 'block';
		document.getElementById("err_emailUsed").style.display = 'block';
		document.getElementById("row_email").style.backgroundColor = "#fff9c3" ;
		document.getElementById("row_emailConfirm").style.backgroundColor = "#fff9c3" ;
		emailField.innerHTML = "<b><font color='#cc0000'>Pending validation</font></b><br /><br />You have already registered with this email, but have not yet validated the email address.  To do so, please click <a href='/feSignUp/2'>here</a>.";
	}
	// submit form if no errors found
	if ( my_errs == 0 )
	{
		document.feSignUp.submit();
	}
}
