// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject(); 
var showErrors = true;
var cache = new Array();
// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}
// the function handles the validation for any form field
function vote_answer()
{
	var pigserverAddress = "catalog/function_insert.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
	//content = encodeURIComponent(document.form1.content.value);
	secode = encodeURIComponent(document.form1.secode.value);
	real_name = encodeURIComponent(document.form1.real_name.value);
	tel = encodeURIComponent(document.form1.tel.value);
	e_mail = encodeURIComponent(document.form1.e_mail.value); 	
	address = encodeURIComponent(document.form1.address.value);
	content = encodeURIComponent(document.form1.content.value);
      
      cache.push("secode="+secode+"&real_name="+real_name+"&tel="+tel+"&e_mail="+e_mail+"&address="+address+"&content="+content);
    
    // 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_pig");
		//message.innerHTML='<img src="image/ajax-loader.gif" style="margin-top:150px" />';
        // make a server request to validate the extracted data
        xmlHttp.open("POST", pigserverAddress, true);
        xmlHttp.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = handleRequestStateChangepig;
        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 handleRequestStateChangepig() 
{
  // 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
        readResponsepig();
      }
      catch(e)
 
      {
        // display error message
        displayError(e.toString());
      }
    }
    else
    {
      // display error message
      displayError(xmlHttp.statusText);
    }
  }
}

// read server's response 
function readResponsepig()
{
  // 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);

  xmlResponse = xmlHttp.responseText;
	  if(xmlResponse !=''){
	  alert(xmlResponse);
	    if(xmlResponse=='恭喜你發問成功'){
	     window.location.href = 'index.php';
		}
	  }else{
	  alert('nothing');
	  }
  // call validate() again, in case there are values left in the cache
  //setTimeout("validate_pig();", 500);
}

function displayError($message)
{
  // ignore errors if showErrors is false
  if (showErrors)
  {
    // turn error displaying Off
    showErrors = false;
    // display error message
 
    alert("Error encountered: \n" + $message);
    // retry validation after 10 seconds
    //setTimeout("validate_glinkster();", 10000);
  }
}