// stores the reference to the XMLHttpRequest object
var xmlHttp2 = createXmlHttpRequestObject2(); 





// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject2() 
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp2;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp2 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp2 = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp2 = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp2 = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp2)
 
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp2;
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function process2()
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp2.readyState == 4 || xmlHttp2.readyState == 0)
  {
    // retrieve the name typed by the user on the form
   // book2 = encodeURIComponent(book);
    // execute the quickstart.php page from the server
	name=document.req.name.value;
	mail=document.req.mail.value;
	phone=document.req.phone.value;
	date=document.req.date.value;
	guests=document.req.guests.value;
	if (document.req.pitanie[0].checked) pitanie="1";
	if (document.req.pitanie[1].checked) pitanie="2";
	
	ship=document.req.ship.value;
	comments=document.req.comments.value;
	
	//alert (name);
    xmlHttp2.open("POST", "/req.php", true);  
	xmlHttp2.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
    // define the method to handle server responses
    xmlHttp2.onreadystatechange = handleServerResponse2;
    // make the server request

    xmlHttp2.send("name=" + encodeURIComponent(name) +"&mail=" + encodeURIComponent(mail) +"&phone=" + encodeURIComponent(phone) +"&date=" + encodeURIComponent(date) +"&guests=" + encodeURIComponent(guests) +"&pitanie=" + encodeURIComponent(pitanie) +"&ship=" + encodeURIComponent(ship) +"&comments=" + encodeURIComponent(comments));
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('process()', 1000);
}

// executed automatically when a message is received from the server
function handleServerResponse2() 
{
  // move forward only if the transaction has completed
  if (xmlHttp2.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp2.status == 200) 
    {
      // extract the XML retrieved from the server
      xmlResponse2 = xmlHttp2.responseXML;
      // obtain the document element (the root element) of the XML structure
      xmlDocumentElement = xmlResponse2.documentElement;
      // get the text message, which is in the first child of
      // the the document element
      //helloMessage = xmlDocumentElement.firstChild.data;
	  
	  xmlRoot2=xmlResponse2.documentElement;
	  msg=xmlRoot2.getElementsByTagName("msg");

	  
	 // xmlRoot=xmlResponse.documentElement;
	  //var basket=xmlRoot.getElementsByTagName("basket");
	  
	  
	  //alert(basket.item(0).firstChild.data);
	  
	  //document.getElementById("basket").innerHTML= '(' + basket.item(0).firstChild.data + ')';
	  
	 alert(msg.item(0).firstChild.data);
	if (msg.item(0).firstChild.data == 'Ваш вопрос отправлен') {
	  }
	  //alert(xmlDocumentElement.firstChild.nodeValue);

      // update the client display using the data received from the server
      //document.getElementById("divMessage").innerHTML = 
                                           
      // restart sequence
      //setTimeout('process()', 1000);
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("В данный момент отправка невозможна: " + xmlHttp.statusText);
    }
  }
}

