/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() 
{
	var request_type;
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer")
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	else
		request_type = new XMLHttpRequest();
		
	return request_type;
}

var http = createObject();

/* -------------------------- */
/* JOIN */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;

function join() 
{
	// Optional: Show a waiting message in the layer with ID ajax_response
	document.getElementById('join_response').innerHTML = "Loading..."
	
	// Required: verify that all fileds are not empty. Use encodeURI() to solve some issues about character encoding.
	var email = encodeURI(document.getElementById('email').value);
	
	// Set the random number to add to URL request
	nocache = Math.random();
	
	// Pass the join variables like URL variable
	http.open('get', '/mailing_list.php?email='+email+'&nocache = '+nocache);
	http.onreadystatechange = joinReply;
	http.send(null);
}

function joinReply() 
{
	if (http.readyState == 4)
	{
		var response = http.responseText;
		
		// If join fails
		if (response == 0) 
			document.getElementById('join_response').innerHTML = 'Please enter a valid email';

		// Else if join is OK
		else 
		{
			document.getElementById('join_response').innerHTML = 'Thank you for joining the mailing list';
			document.getElementById('email').value = '';
		}
	}
}