// Script 13.3 - checkusername.js

/* This page does all the magic for applying
 * Ajax principles to a registration form.
 * The user's chosen username is sent to a PHP
 * script which will confirm its availability.
 */

// Function that starts the Ajax process:
function swap(service) {
	
	// Confirm that the object is useable:
	if (ajax) {
		
		// Call the PHP script.
		// Use the GET method.
		// Pass the username in the URL.
		ajax.open('get', 'get_service.php?service=' + encodeURIComponent(service));
		
		// Function that handles the response:
		ajax.onreadystatechange = handle_check;
		
		// Send the request:
		ajax.send(null);
		
	} else { // Can't use Ajax!
		alert ('Ajax error.');
	}
	
	toggleDisp(service);
	
} // End of check_username() function.

// Function that handles the response from the PHP script:
function handle_check() {
	
	// If everythings OK:
	if ( (ajax.readyState == 4) && (ajax.status == 200) ) {
			
			// Assign the returned value to a document element:
			document.getElementById('service_swap').innerHTML = ajax.responseText;
			
	}
	
} // End of handle_check() function.