function clearEmailField( element )
{
	if( element.value == "Your email address" || element.value == "Recipient's email address" )
	{
		element.value = "";
	}
}

function addToMailingList()
{
	var emailField = document.getElementById( "email" );
	var emailValue = emailField.value;	
	
	if( emailValue.length < 1 )
	{
		alert( "Please enter an email address" );
		emailField.focus();
		return false;
	}

	loadXMLDoc( "/includes/addToMailingList.html?email=" + emailValue );
}

function sendToFriend( newsItemId )
{
	var emailField = document.getElementById( "stfEmail" );
	var emailValue = emailField.value;	
	
	if( emailValue.length < 1 )
	{
		alert( "Please enter an email address" );
		emailField.focus();
		return false;
	}

	loadXMLDoc( "/includes/sendToFriend.html?newsItemId=" + newsItemId + "&email=" + emailValue );
}

var req;

function loadXMLDoc(url) 
{
	// branch for native XMLHttpRequest object
	if( window.XMLHttpRequest )
	{
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	}
	// branch for IE/Windows ActiveX version
	else if( window.ActiveXObject )
	{
		req = new ActiveXObject( "Microsoft.XMLHTTP" );
		if( req )
		{
			req.onreadystatechange = processReqChange;
			req.open( "GET", url, true );
			req.send();
		}
	}
}

function processReqChange() 
{
	// only if req shows "complete"
	if( req.readyState == 4 )
	{
		// only if "OK"
		if( req.status == 200 )
		{
			// ...processing statements go here...
			response = req.responseXML.documentElement;
			functionName = response.getElementsByTagName( 'function' )[0].firstChild.data;
			ok = response.getElementsByTagName( 'ok' )[0].firstChild.data;
			email = response.getElementsByTagName('email')[0].firstChild.data;

			if( ok == "fail_dns" )
			{
				alert( "The email address you entered is not valid, please try again..." );				
			}
			else if( ok == "fail_email" )
			{
				alert( "The email address you entered is not valid, please try again..." );
			}
			else
			{
				if( functionName == "sendToFriend" )
				{
					alert( "Thank you,\n\nThis news item has been sent to " + email + "." );
				}
				else
				{
					alert( "Thank you,\n\n" + email + " has been subscribed to the Rapport Newsletter." );
				}
			}
		}
		else
		{
			alert( "There was a problem:\n" + req.statusText + "\n\nPlease let us know about this error." );
		}
	}
}