
// the function handles the validation for any form field
function validate_eat(id,s)
{
	var kusoserverAddress = "catalog/function_eat.php";
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // if we received non-null parameters, we add them to cache in the
    // form of the query string to be sent to the server for validation
    if (id)
    {
      
      cache.push("id=" + id + "&s=" + s);
    }
    // try to connect to the server
    try
    {
      // continue only if the XMLHttpRequest object isn't busy
      // and the cache is not empty
      if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) 
         && cache.length > 0)
      {
        // get a new set of parameters from the cache
        var cacheEntry = cache.shift();
		//message = document.getElementById("loading_kuso");
		//message.innerHTML='<img src="image/ajax-loader.gif" style="margin-top:150px" />';
        // make a server request to validate the extracted data
        xmlHttp.open("POST", kusoserverAddress, true);
        xmlHttp.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = handleRequestStateChangekuso;
        xmlHttp.send(cacheEntry);
      }
    }
    catch (e)
    {
      // display an error when failing to connect to the server
      displayError(e.toString());
    }
  }
}

// function that handles the HTTP response
function handleRequestStateChangekuso() 
{
  // when readyState is 4, we read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // read the response from the server
        readResponsekuso();
      }
      catch(e)
 
      {
        // display error message
        displayError(e.toString());
      }
    }
    else
    {
      // display error message
      displayError(xmlHttp.statusText);
    }
  }
}

// read server's response 
function readResponsekuso()
{
  // retrieve the server's response 
  var response = xmlHttp.responseText;
  // server error?
  if (response.indexOf("ERRNO") >= 0 
      || response.indexOf("error:") >= 0
      || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);

  var html = response;
  //document.getElementById("loading_kuso").style.display="none";
  //message = document.getElementById("msg");
  // show the error or hide the error
  //message.innerHTML ='<img name="eat1" src="images/index_13.jpg"><img name="eat2" src="images/index_14.jpg">';
  //remesg =html;
  separator='xyz';
  remesg =html.split(separator)[0];
  eatnum =html.split(separator)[1];
  notnum =html.split(separator)[2];
  eat1h = document.getElementById("eat1");
  eat2h = document.getElementById("eat2");
  // show the error or hide the error
  eat1h.innerHTML =eatnum;
  eat2h.innerHTML =notnum;
  alert(remesg);
  if(remesg=='恭喜你投票成功'){
    window.location.href = 'index.php';
  }
  // call validate() again, in case there are values left in the cache
  //setTimeout("validate_kuso();", 500);
}

