function sendEmail()
{
	if (!validateForm()) return false;
	
	document.getElementById("shadow").style.display='block';
	
	setTimeout(sendRequest, 500);
}

function sendRequest()
{	
	var oForm = document.forms[0];
	var sBody = getRequestBody(oForm);

	var oXmlHttp = zXmlHttp.createRequest();
	oXmlHttp.open("post", oForm.action, true);
	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	oXmlHttp.onreadystatechange = function () 
	{
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				saveResult(oXmlHttp.responseText);
			} else {
				saveResult(0);
			}
		}            
	};
	
	oXmlHttp.send(sBody);        
}

function getRequestBody(oForm)
{
	var aParams = new Array();
	
	for (var i=0 ; i < oForm.elements.length; i++) {
		var sParam = encodeURIComponent(oForm.elements[i].name);
		sParam += "=";
		sParam += encodeURIComponent(oForm.elements[i].value);
		aParams.push(sParam);
	} 
	
	return aParams.join("&");        
}

function saveResult(n)
{
	n = parseInt(n);
	if (n) 
	{
		document.getElementById("shadow").style.backgroundImage='url(/img/email_ok.png)';
		document.getElementById("shadow").style.cursor='pointer';
	}
	else 
	{
		alert('ОШИБКА! Не могу отправить сообщение!');
		document.getElementById("shadow").style.display='none';
	}
}

function validateForm()
{
	var oForm = document.forms[0];
	
	for (var i=0 ; i < oForm.elements.length; i++) 
	{
		var sParam = oForm.elements[i].style.borderColor='#6791B9';
	}
	
	var err = 0;
	if (oForm.recipient.selectedIndex<1) {oForm.recipient.style.borderColor='#FF0000'; oForm.recipient.focus(); err=1;}
	if (!/[a-zа-я]{2,}/i.test(oForm.name.value)) {oForm.name.style.borderColor='#FF0000'; oForm.name.focus(); err=1;}
	if (!/[a-zа-я]{3,}/i.test(oForm.message.value)) {oForm.message.style.borderColor='#FF0000'; if(!err) oForm.message.focus(); err=1;}
	if (!isValidEmail(oForm.email.value)) {oForm.email.style.borderColor='#FF0000'; if(!err) oForm.email.focus(); err=1;}
	
	return !err;
}

function isValidEmail (email)
{
 return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}

function clearForm()
{
	document.getElementById('feedback').reset();
	document.getElementById("shadow").style.display='none';
	document.getElementById("shadow").style.backgroundImage='';
}
