var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
	var xmlHttp;

	if (window.ActiveXObject)
	{
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (E)
			{
				xmlHttp = false;
			}
		}
	}
	else
	{
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}

	if (!xmlHttp)
		alert('Error creating XMLHttpRequest.')
	else
		return xmlHttp;
}

function SendMail()
{
	if ((4 == xmlHttp.readyState) || (0 == xmlHttp.readyState))
	{
		document.getElementById("ajax-loader").style.visibility = "visible";		

		var selectTopic = document.getElementById("topic"); 
		var topic       = selectTopic[selectTopic.selectedIndex].text;
		var email       = document.getElementById("email").value;
		var message     = document.getElementById("message").value;

		var paramMail = 'topic=' + encodeURIComponent(topic) + '&email=' + encodeURIComponent(email) + '&message=' + encodeURIComponent(message);   

		xmlHttp.open('POST', 'mail.php?r=' + Math.random(), true);
		xmlHttp.onreadystatechange = handleServerResponse;
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
		xmlHttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

		xmlHttp.send(paramMail);
	}
}

function handleServerResponse()
{
	if (4 == xmlHttp.readyState)
	{
		document.getElementById("ajax-loader").style.visibility = "hidden";

		if (200 == xmlHttp.status)
		{
			try
			{
				var outParam = eval('(' + xmlHttp.responseText + ')');

				if (true == outParam.result)
				{
					alert('Your message to developer was successfully sent!');		
				}
				else
				{
					alert('Unknown error while sending the message! Please, try again.');		
				}
			}
			catch(e)
			{
				alert('Unknown error while sending the message! Please, try again.');		
			}
		}
		else
		{
			alert('Errors of request to a server. ' + xmlHttp.statusText);
		}
	}
}

