﻿
var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
 var xmlHttp;
 try{
	xmlHttp = new XMLHttpRequest();
    }
 catch (e)
{
	var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
					"MSXML2.XMLHTTP.5.0",
					"MSXML2.XMLHTTP.4.0",
					"MSXML2.XMLHTTP.3.0",
					"MSXML2.XMLHTTP",
					"Microsoft.XMLHTTP");
 for(var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
 {
   try
   {
	xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
   }
   catch (e) {}
 }
}
if(!xmlHttp)
	alert("Error creating the XMLHttpRequest object.");
else
	return xmlHttp;
}

/////////////////////////////////POST Request function////////////////////////////////////////

var divname ='';
function stDelete_ajax(sn,divId)	/*GuestBook ajax function by POST send*/
{
  if (xmlHttp)
  {
    try
    {
	var randNo = parseInt(Math.random()*999999999);
	xmlHttp.open("POST","../include/stDelete.php?rand=" + randNo, true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	divname = divId;
	xmlHttp.onreadystatechange = baseRequestStateChange;
	xmlHttp.send("sn="+sn);

    }
    catch (e)
    {
	alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function serchProduct_ajax(item_str,item_inp,item_class,item_pg)	/*GuestBook ajax function by POST send*/
{
  if (xmlHttp)
  {
    try
    {
	var randNo = parseInt(Math.random()*999999999);
	xmlHttp.open("POST","./include/serch_product.php?rand=" + randNo, true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.onreadystatechange = product_RequestStateChange;
	xmlHttp.send("sStr=" + item_str + "&inp=" + item_inp + "&class=" + item_class + "&pg=" + item_pg);
    }
    catch (e)
    {
	alert("Can't connect to server:\n" + e.toString());
    }
  }
}
////////////////////////////////////////////////product function///////////////////////////////////
function product_list_ajax(item_id,page)	/*product ajax function*/
{
  if (xmlHttp)
  {
    try
    {
	var randNo = parseInt(Math.random()*999999999);
	xmlHttp.open("GET","./include/show_product.php?class=" + item_id + "&pg=" + page + "&rand=" + randNo, true);
	xmlHttp.onreadystatechange = product_RequestStateChange;
	xmlHttp.send(null);
    }
    catch (e)
    {
	alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function product_RequestStateChange()	/*product ajax function onreadystatechange*/
{
 if (xmlHttp.readyState == 4)
 {
  if(xmlHttp.status == 200)
  {
   try
   {
	document.getElementById('productDiv').innerHTML = xmlHttp.responseText;
	document.getElementById('p_class_title').innerText= p_titleValue;
   }
   catch (e)
   {
	alert("Error reading the response: " + e.toString());
   }
  }
  else
  {
   alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
  }
 }
}
////////////////////////////////////////////////product class function///////////////////////////////////
function product_class_ajax(item_id,item_parent,area)	/*product ajax function*/
{
  if( item_id==item_parent )
  {
	alert("父類別與類別無法選擇同一個喔!!");
	return false;
  }
  if (xmlHttp)
  {
    try
    {
	var randNo = parseInt(Math.random()*999999999);
	xmlHttp.open("GET","../include/p_classParent.php?sn=" + item_id + "&parent=" + item_parent + "&area=" + area + "&rand=" + randNo, true);
	xmlHttp.onreadystatechange = pclass_RequestStateChange;
	xmlHttp.send(null);
    }
    catch (e)
    {
	alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function pclass_RequestStateChange()	/*product ajax function onreadystatechange*/
{
 if (xmlHttp.readyState == 4)
 {
  if(xmlHttp.status == 200)
  {
   try
   {
	alert("變更成功!");
	location.href='list.php';
   }
   catch (e)
   {
	alert("Error reading the response: " + e.toString());
   }
  }
  else
  {
   alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
  }
 }
}
/////////////////////////////////Response function////////////////////////////////////////

function baseRequestStateChange()	/*base ajax function onreadystatechange*/
{
 if (xmlHttp.readyState == 4)
 {
  if(xmlHttp.status == 200)
  {
   try
   {
	var deletedNode = document.getElementById(divname).removeNode(true);
   }
   catch (e)
   {
	alert("Error reading the response: " + e.toString());
   }
  }
  else
  {
   alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
  }
 }
}
