﻿
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;
}

//////////////////////////////////////////////// Base Form Post Function ///////////////////////////////////
function base_post_ajax(action)	/*product ajax function*/
{
  if (xmlHttp)
  {
    try
    {
	var randNo = parseInt(Math.random()*999999999);
	sendStr = '';

	switch(action){
	case "cartm_cr" :		/*購物車帳號註冊表單*/
	xmlHttp.open("POST","./include/acc_act.php?action="+action+"&rand=" + randNo, true);
	formObj = document.acc_form;
	break;
	case "cartm_ed" :		/*購物車會員編輯表單*/
	xmlHttp.open("POST","./include/acc_act.php?action="+action+"&rand=" + randNo, true);

	formObj = document.acc_form;
	break;
	case "acc_cr" :		/*帳號註冊表單*/
	xmlHttp.open("POST","./include/acc_act.php?action="+action+"&rand=" + randNo, true);
	formObj = document.acc_form;
	document.getElementById("loading_div").style.display='block';
	break;
	case "acc_ed" :		/*會員編輯表單*/
	xmlHttp.open("POST","./include/acc_act.php?action="+action+"&rand=" + randNo, true);

	formObj = document.acc_form;
	document.getElementById("loading_div").style.display='block';
	break;
	case "acc_login" :	/*登入表單*/
	xmlHttp.open("POST","./include/acc_act.php?action="+action+"&rand=" + randNo, true);

	formObj = document.login_form;
	break;
	case "cont" :		/*聯絡我們表單*/
	xmlHttp.open("POST","./include/other_act.php?action="+action+"&rand=" + randNo, true);

	formObj = document.cont_form;
	document.getElementById("loading_div").style.display='block';
	break;
	case "serviceRequest" :		/*服務需求*/
	xmlHttp.open("POST","./include/other_act.php?action="+action+"&rand=" + randNo, true);

	formObj = document.serviceForm;
	break;
	}
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

	for(x=0; x < formObj.length; x++){
		if( ((formObj.item(x).type == 'radio' || formObj.item(x).type == 'checkbox') && formObj.item(x).checked == false) || formObj.item(x).type == 'button' || formObj.item(x).type == 'image' ){
			continue;
		}
		sendStr += formObj.item(x).name + "=" + formObj.item(x).value + "&";
	  }
	xmlHttp.onreadystatechange = function(){
		base_post_RequestStateChange(formObj, action);
		};
	xmlHttp.send(sendStr);
    }
    catch (e)
    {
	alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function base_post_RequestStateChange(formObj, action)	/*product ajax function onreadystatechange*/
{
 if (xmlHttp.readyState == 4)
 {
  if(xmlHttp.status == 200)
  {
   try
   {
	switch(action){
	case "acc_cr" :
	  splitErr = xmlHttp.responseText.split(";");
		key=0;
		var errAry = new Array();
		while(splitErr[key]){
	  	  errAry_tmp = splitErr[key].split('=');
			errAry[errAry_tmp[0]] = errAry_tmp[1];
		  if(document.getElementById(errAry_tmp[0]+"_alert"))
		  document.getElementById(errAry_tmp[0]+"_alert").innerText = errAry_tmp[1];
		  key++;
		}
			if(errAry["err_flag"] === undefined){
			  if(formObj.tos.checked === false){
				alert('您尚未同意服務條款');
				document.getElementById("loading_div").style.display='none';
			  }else{
	  			formObj.action = "include/acc_act.php?action=acc_register";
	  			formObj.submit();
			  }
			}else{
			document.getElementById("loading_div").style.display='none';
			}
	break;
	case "acc_ed" :
	  splitErr = xmlHttp.responseText.split(";");
		key=0;
		var errAry = new Array();
		while(splitErr[key]){
	  	  errAry_tmp = splitErr[key].split('=');
			errAry[errAry_tmp[0]] = errAry_tmp[1];
		  if(document.getElementById(errAry_tmp[0]+"_alert"))
		  document.getElementById(errAry_tmp[0]+"_alert").innerText = errAry_tmp[1];
		  key++;
		}
			if(errAry["err_flag"] === undefined){
				if(errAry["item_pw_old"] === "OK" &&errAry["item_pw"] === "OK"){
				alert('密碼已變更!請重新登入');
				location.href='include/acc_act.php?action=logout';
				}else{
				alert('修改成功!');
				location.href='?link=login';
				}
			}else{
			document.getElementById("loading_div").style.display='none';
			}
	break;
	case "acc_login" :
	  if(xmlHttp.responseText.search(/error/i) !== -1){
		splitErr = xmlHttp.responseText.split(":");
		document.getElementById("login_alert").innerText = splitErr[1];
	  }else if(xmlHttp.responseText.search(/ok/i) !== -1){
	  formObj.action = "include/acc_act.php?action=login";
	  formObj.submit();
		}
	break;
	case "cont" :
	  splitErr = xmlHttp.responseText.split(";");
		key=0;
		var errAry = new Array();
		while(splitErr[key]){
	  	  errAry_tmp = splitErr[key].split('=');
			errAry[errAry_tmp[0]] = errAry_tmp[1];
		  if(document.getElementById(errAry_tmp[0]+"_alert"))
		  document.getElementById(errAry_tmp[0]+"_alert").innerText = errAry_tmp[1];
		  key++;
		}
			if(errAry["err_flag"] === undefined){

				alert('感謝您的指教!\n客服人員收到您的訊息之後會儘速與您聯絡。');
				location.href=location.href;

			}else{
			document.getElementById("loading_div").style.display='none';
			}
	break;
	case "serviceRequest" :
	  document.getElementById('servRep').innerText = xmlHttp.responseText;
	  formObj.reset();
	break;
	case "cartm_cr" :
	  splitErr = xmlHttp.responseText.split(";");
		key=0;
		var errAry = new Array();
		while(splitErr[key]){
	  	  errAry_tmp = splitErr[key].split('=');
			errAry[errAry_tmp[0]] = errAry_tmp[1];
		  if(document.getElementById(errAry_tmp[0]+"_alert"))
		  document.getElementById(errAry_tmp[0]+"_alert").innerText = errAry_tmp[1];
		  key++;
		}
			if(errAry["err_flag"] === undefined){
	  			formObj.action = "include/acc_act.php?action=acc_register";
	  			formObj.submit();
			}
	break;
	case "cartm_ed" :
	  splitErr = xmlHttp.responseText.split(";");
		key=0;
		var errAry = new Array();
		while(splitErr[key]){
	  	  errAry_tmp = splitErr[key].split('=');
			errAry[errAry_tmp[0]] = errAry_tmp[1];
		  if(document.getElementById(errAry_tmp[0]+"_alert"))
		  document.getElementById(errAry_tmp[0]+"_alert").innerText = errAry_tmp[1];
		  key++;
		}
			if(errAry["err_flag"] === undefined){
				if(errAry["item_pw_old"] === "OK" && errAry["item_pw"] === "OK"){
				alert('密碼已變更!請重新登入');
				location.href='include/acc_act.php?action=logout';
				}else{
				alert('修改成功!');
				location.href='index.php?acPage=memberlogin';
				}
			}
	break;
	}

   }
   catch (e)
   {
	alert("Error reading the response: " + e.toString());
   }
  }
  else
  {
   alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
  }
 }
}

//////////////////////////////////////////////// Account Usage Ajax /////////////////////////////////////////////////
function AccUsage(uid, getMode)
{
  if (xmlHttp)
  {
    try
    {
	var randNo = parseInt(Math.random()*999999999);
	switch(getMode){
	case "accData" :
	xmlHttp.open("POST","./include/AccDataGet.php?action=acc&rand=" + randNo, true);
	break;
	}
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

	xmlHttp.onreadystatechange = AccUsage_RequestStateChange;

	xmlHttp.send("uid=" + uid);
    }
    catch (e)
    {
	alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function AccUsage_RequestStateChange()	/*product ajax function onreadystatechange*/
{
 if (xmlHttp.readyState == 4)
 {
  if(xmlHttp.status == 200)
  {
   try
   {
	splitary = xmlHttp.responseText.split(';');

	for(x in splitary){
		splitKeyValue =splitary[x].split('=');

		document.getElementById(splitKeyValue[0]).value = splitKeyValue[1];
	}
	//alert(xmlHttp.responseText);
   }
   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 (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);
  }
 }
}
////////////////////////////////Addtional Ajax//////////////////////////////////////////////

function addtional_ajax(act,data,type)	/*GuestBook ajax function by POST send*/
{
  if (xmlHttp)
  {
    try
    {
	var randNo = parseInt(Math.random()*999999999);
	xmlHttp.open("POST","../../include/add_act.php?rand=" + randNo, true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.onreadystatechange = addRequestStateChange;
	xmlHttp.send("act="+act+"&data="+data+"&type="+type);
    }
    catch (e)
    {
	alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function addRequestStateChange()	/*base ajax function onreadystatechange*/
{
 if (xmlHttp.readyState == 4)
 {
  if(xmlHttp.status == 200)
  {
   try
   {
	document.getElementById('replyLayer').style.display = 'none';
	callList();
   }
   catch (e)
   {
	alert("Error reading the response: " + e.toString());
   }
  }
  else
  {
   alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
  }
 }
}
///////////////////////////////////////////////////////////////////////////////////////
function taroRandomLucky()
{
  if (xmlHttp)
  {
    try
    {
	var randNo = parseInt(Math.random()*999999999);

	xmlHttp.open("GET","./include/taroRandom.php?rand=" + randNo, true);

	xmlHttp.onreadystatechange =taroRandom_RequestStateChange;

	xmlHttp.send(null);
    }
    catch (e)
    {
	alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function taroRandom_RequestStateChange()	/*product ajax function onreadystatechange*/
{
 if (xmlHttp.readyState == 4)
 {
  if(xmlHttp.status == 200)
  {
   try
   {	
	splitary = xmlHttp.responseText.split('|');

		document.getElementById('taroT1').innerHTML = '';
		document.getElementById('taroT2').innerHTML = '';

	for(x in splitary){
			splitKeyValue =splitary[x].split(',');	

		/*TEXT*/
		var textLink = document.createElement('a');
		textLink.setAttribute('href','index.php?acPage=display&pno='+splitKeyValue[0]);
		textLink.setAttribute('target','_blank');
		textLink.innerText = splitKeyValue[1];

		var textP = document.createElement('p');
		textP.appendChild(textLink);
		/*TEXT*/

		/*IMG*/
		var img = document.createElement('img');
		img.setAttribute('src','upload/p_img/'+splitKeyValue[2]);

		var imgLink = document.createElement('a');
		imgLink.setAttribute('href','index.php?acPage=display&pno='+splitKeyValue[0]);
		imgLink.setAttribute('target','_blank');

		imgLink.appendChild(img);

		var imgP = document.createElement('p');
		imgP.appendChild(imgLink);

		/*IMG*/

		var luckyLi = document.createElement('li');

		luckyLi.appendChild(textP);
		luckyLi.appendChild(imgP);

		if(x < 2){
		document.getElementById('taroT1').appendChild(luckyLi);
		}else{
		document.getElementById('taroT2').appendChild(luckyLi);
		}

	}
	//alert(xmlHttp.responseText);
   }
   catch (e)
   {
	alert("Error reading the response: " + e.toString());
   }
  }
  else
  {
   alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
  }
 }
}
//////////////////////////////////////////////////////////會員搜尋器(禮券用)//////////////////////////////////////////////////////////////////////
function memberSearch(searchStr)
{
  if (xmlHttp)
  {
    try
    {
	var randNo = parseInt(Math.random()*999999999);

	xmlHttp.open("GET","../include/memberSearch.php?str=" + searchStr + "&rand=" + randNo, true);

	xmlHttp.onreadystatechange = memberSearch_RequestStateChange;

	xmlHttp.send(null);
    }
    catch (e)
    {
	alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function memberSearch_RequestStateChange()	/*product ajax function onreadystatechange*/
{
 if (xmlHttp.readyState == 4)
 {
  if(xmlHttp.status == 200)
  {
   try
   {	
	var node = document.getElementById('mSearch').firstChild;
	while(document.getElementById('mSearch').firstChild.childNodes.length > 1){
		node.removeChild(node.childNodes[1]);
	}
	splitary = xmlHttp.responseText.split('|');
	for(x in splitary){
	splitKeyValue =splitary[x].split(',');
	if(splitKeyValue[1] === undefined) break;
	var tb_td1 = document.createElement('td');
	tb_td1.innerHTML = splitKeyValue[0];

	var tb_td2 = document.createElement('td');
	tb_td2.innerHTML = splitKeyValue[1];

	var tb_td3 = document.createElement('td');
	tb_td3.innerHTML = splitKeyValue[2];

	var tb_td4 = document.createElement('td');
	tb_td4.innerHTML = splitKeyValue[3];

	var tb_td5 = document.createElement('td');
	tb_td5.innerHTML = '<input class="btn1" type="button" id="'+splitKeyValue[1]+'" value="選擇" onClick="getValue()">';

	var tb_tr = document.createElement('tr');

	tb_tr.appendChild(tb_td1);
	tb_tr.appendChild(tb_td2);
	tb_tr.appendChild(tb_td3);
	tb_tr.appendChild(tb_td4);
	tb_tr.appendChild(tb_td5);

	document.getElementById('mSearch').firstChild.appendChild(tb_tr);
	}
	//document.write(xmlHttp.responseText);
   }
   catch (e)
   {
	alert("Error reading the response: " + e.toString());
   }
  }
  else
  {
   alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
  }
 }
}