﻿function getXmlHttp3(){
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

function updateObj3(obj, data, blink, small) {
	if(blink) data=data.blink();
	if(small) data=data.small();
	if(document.getElementById(obj).tagName=='INPUT') document.getElementById(obj).value=data;
	else document.getElementById(obj).innerHTML = '<font color="red">' + data + '</font>';
} 


function sendForm3(frm){
	var xmlhttp = getXmlHttp3()
	formComment = document.main_form3;
	//Валидация формы 
	if(!formComment.name.value) {updateObj3('alert_name3', 'Имя не введено!',0,1); formComment.name.focus(); return;} else {updateObj3('alert_name3', '',0,0);}
	if(!formComment.email.value){updateObj3('alert_email3', 'Телефон не введен!',0,1); formComment.email.focus(); return;} else {updateObj3('alert_email3', '',0,0);}
	if(!formComment.date.value){updateObj3('alert_date3', 'Заголовок не введен!',0,1); formComment.date.focus(); return;} else {updateObj3('alert_date3', '',0,0);}
	if(!formComment.text.value){updateObj3('alert_text3', 'Сообщение пустое!',0,1); formComment.text.focus(); return;} else {updateObj3('alert_text3', '',0,0);}

	//Формирование строки параметров и их значений
	str='';
	for (i=0; i<formComment.length; i++) if(formComment[i].name) {
		str=str+encodeURIComponent(formComment[i].name)+'='+encodeURIComponent(formComment[i].value)+'&';
		}
	str=str.slice(0,-1);
	
	xmlhttp.open('POST', './forms_back/priem/send_mail.php', true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				if(xmlhttp.responseText) {
					document.getElementById("mess3").innerHTML = xmlhttp.responseText;
				}
			} else {
            alert("Произошла ошибка "+ xmlhttp.status+":\n" + xmlhttp.statusText);
			}			
		}	
	}	
	xmlhttp.send(str);
}
