
// the function handles the validation for any form field
function validate(inputValue,mode1,sendValue)
{
	var serverAddress = "shopdetail.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 (inputValue)
    {
      // encode values for safely adding them to an HTTP request query string
      inputValue = encodeURIComponent(inputValue);
	  mode = encodeURIComponent(mode1);
	  sendValue = encodeURIComponent(sendValue);
      // add the values to the queue
      cache.push("inputValue=" + inputValue + "&mode=" + mode + "&sendValue=" + sendValue);
    }
    // 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();
        // make a server request to validate the extracted data
        xmlHttp.open("POST", serverAddress, true);
        xmlHttp.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = handleRequestStateChange;
        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 handleRequestStateChange() 
{
  // 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
        readResponse();
      }
      catch(e)
 
      {
        // display error message
        displayError(e.toString());
      }
    }
    else
    {
      // display error message
      displayError(xmlHttp.statusText);
    }
  }
}

// read server's response 
function readResponse()
{
  // 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);
  // get response in XML format (assume the response is valid XML)
  //responseXml = xmlHttp.responseXML;//add
//  xmlRoot = xmlResponse.documentElement; 
  // get the document element
  //xmlRoot = responseXml.documentElement;//add
  /*titleArray = xmlRoot.getElementsByTagName("title");
  isbnArray = xmlRoot.getElementsByTagName("isbn");
  var html = "";  
  // iterate through the arrays and create an HTML structure
  for (var i=0; i<titleArray.length; i++)
    html += '<div><span style="float:left; width:70%">' + titleArray.item(i).firstChild.data + '</span><span style="float:right; width:30%">' + isbnArray.item(i).firstChild.data + '</span></div>'; */ 
 // result = xmlRoot.getElementsByTagName("result")[0].firstChild.data;//add
  // html += "total =" + result;
//  fieldID = xmlDoc.getElementsByTagName("fieldid")[0].firstChild.data;
//  errmsg = xmlDoc.getElementsByTagName("errmsg")[0].firstChild.data;
  // find the HTML element that displays the error
  if(mode == 'additem'){
	  // responseXml = xmlHttp.responseXML;
	  // xmlRoot = responseXml.documentElement;
	   //result = xmlRoot.getElementsByTagName("result")[0].firstChild.data;
	   result = response;
	  if(result==1){
	  alert("商品已放進購物車");
	  }else{
	  alert("商品放進購物車失敗");
	  }
  }else{
  
   html = response;
  message = document.getElementById("car");
  // show the error or hide the error
  message.innerHTML =html;
  }
//  message.className = (result == "0") ? "error" : "error";
  // call validate() again, in case there are values left in the cache
  setTimeout("validate();", 500);
}

// sets focus on the first field of the form
function setFocus()    
{
  document.getElementById("txtUsername").focus();
}

