// handle the action of a user clicking to 
// check for the availability of a username
function handleCheckAvailabilityClick(messageDivName, usernameTextBoxID, usernameRequiredValidatorID, 
	usernameAlphaNumericValidatorID, usernameAvailableValidatorID, usernamePeriodValidatorID, requestHandlerUrl) {

	// clear any message that might still be hanging around
	document.getElementById(messageDivName).innerHTML = "";

	// clear any validation errors that might be hanging around on the page	
	usernameRequiredValidator = document.getElementById(usernameRequiredValidatorID);
	usernameRequiredValidator.style.display = "none";

	// clear any validation errors that might be hanging around on the page	
	usernameAvailableValidator = document.getElementById(usernameAvailableValidatorID);
	usernameAvailableValidator.style.display = "none";
	
	// clear any validation errors that might be hanging around on the page	
	usernameAlphaNumericValidator = document.getElementById(usernameAlphaNumericValidatorID);
	usernameAlphaNumericValidator.style.display = "none";

	// clear any validation errors that might be hanging around on the page	
	usernamePeriodValidator = document.getElementById(usernamePeriodValidatorID);
	usernamePeriodValidator.style.display = "none";
		
	// Valid all the validators for this control only.  The Page_IsValid is not necessary for this ajax postback.
	ValidatorValidate(usernameRequiredValidator);
	ValidatorValidate(usernameAvailableValidator);
	ValidatorValidate(usernameAlphaNumericValidator);
	ValidatorValidate(usernamePeriodValidator);
	
	// are there no validation errors on the username field?
	if (usernameRequiredValidator.isvalid && usernameAvailableValidator.isvalid && usernameAlphaNumericValidator.isvalid && usernamePeriodValidator.isvalid)
	{
		// activate the message that tells the user
		// that their request is being processed
		activateProcessingMessage(messageDivName);
		
		// make the check to see if the username is available or not
		checkUsernameAvailability(requestHandlerUrl, usernameTextBoxID);
	}
}


// send text back to the browser to tell the user whether
// a username is available or not
function checkUsernameAvailability(url, usernameTextBoxID)
{
	// add a querystring var to the url that specifies what
	// the username entered into the text box was
	var usernameTextBox = document.getElementById(usernameTextBoxID);
	url = url + "?username=" + usernameTextBox.value;
	
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.onreadystatechange = checkUsernameAvailability_StateChange
		xmlhttp.open("GET", url, true);
		xmlhttp.send(null);
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		if (xmlhttp)
		{
			xmlhttp.onreadystatechange = checkUsernameAvailability_StateChange
			xmlhttp.open("GET", url, true)
			xmlhttp.send()
		}
	}
}


// plug whatever html is returned it into the
// appropriate div tag on the page
function checkUsernameAvailability_StateChange()
{
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4)
	{
		// if "OK"
		if (xmlhttp.status==200)
		{
			document.getElementById("userNameAvailableMessage").innerHTML = xmlhttp.responseText;
		}
		else
		{
			alert("Problem retrieving data:" + xmlhttp.statusText)
		}
	}
}

// Activate a message that lets a user know that
// the event is taking time to process	
function activateProcessingMessage(messageDivName) {

	document.getElementById(messageDivName).innerHTML = "<br /><span class='HighlightOrange'>Checking Username... Please Wait</span>";
}
