function ajaxFunction()
// returns a XMLHttp object which makes AJAX work
{
	var ajaxRequest;

	try
	{
		ajaxRequest = new XMLHttpRequest();
		// for Opera 8.0+, Firefox and Safari
	}
	
	catch (e)
	{
		try
		{
			ajaxRequest = new ActiveXObject("Msxm12.XMLHTTP");
			// for Internet Explorer
		}
			
		catch (e)
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				// the other alternative for Internet Explorer
			}
			
			catch (e)
			{
				alert("Sorry this web site will not function correctly in your browser. Please try a different one.");
				// Ajax will not work if can not create the ajaxRequest object
				
				return false;
			}
		}
	}
	
	return ajaxRequest;
}

/////

function ajaxFunctionShortenUrl()
// calls the createtinyurl PHP code that generates the new short URL
{
	var ajaxRequest = ajaxFunction();
	// create the AJAX object
	
	var thelongurl = document.getElementById('longurl').value;
	// get the URL the user wants shortened from the form - it has to have its id set as well as its name to work
	
	var querystring = "?longurl="+thelongurl;
	// pass the URL as part of a query string (will be using GET)
	
	ajaxRequest.onreadystatechange = handlestatechange;
	// Function to receive data sent from the server
	// Every time the "ready state" changes, this function will be called
	
	ajaxRequest.open("GET", "createtinyurl.php5"+querystring, true);
	// open the connection to the PHP script on the server
	
	ajaxRequest.send(null);
	// send the request for the PHP code, passing the parameters with the request
	
	function handlestatechange()
	{
		// the readyState can either be processing, downloading or completed ... for now only care about completed requests
		// values 0 .. 4
		
		if (ajaxRequest.readyState == 4)
		// readyState 4 means complete - the server has successfully sent back the data
		{
			var ajaxDisplay = document.getElementById('thenewurls');
			// going to put the result in the div called thenewurls
			
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
			// set the value of innerHTML of the div to the response of the PHP file that processed the request
			
			document.getElementById('submitbutton').value="Shorten My URL";
			// set the text on the button back to "Shorten My URL" from "Please Wait..."
		}
	}
}