﻿// JavaScript Document

var http_request = false;
/*  建置 xmlHttp_requestRequest 物件 start  */
function CreateXMLHttp()
{
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, IE7 ...
		http_request = new XMLHttpRequest();
		 //有些瀏覽器中有BUG，這段就是防止BUG
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // Microsoft
		try {                          //IE 6
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {            //IE 5
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
}
/*  發出 request 需求start  */
function makeRequest(url,arg,showobj,exec) {  //url 網址 , arg 參數 , 欲顯示結果的物件名稱

	CreateXMLHttp();
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = function(){ GetResult(showobj,exec); }  //指呼叫 GetResult() 
	http_request.open('POST', url, true);
	//若使用POST送出，需加下列那行
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	http_request.send(arg);  //arg = 需傳送的參數
}	
/*  伺服器傳回資料後的處理方式  start  */
function GetResult(showobj,exec) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var mesg = http_request.responseText;
			document.getElementById(showobj).innerHTML=mesg;		
			if (exec = 1)  <!--//加入購物車，其他執行-->
			{
				if( document.getElementById('Qry_TopCart1') != undefined) 	{	document.getElementById('Qry_TopCart1').innerText=document.getElementById('vTopCart1').value;	}
				if( document.getElementById('Qry_TopCart2') != undefined)	{	document.getElementById('Qry_TopCart2').innerText=document.getElementById('vTopCart2').value;	}
				if( document.getElementById('qUserBonus') != undefined) 	{	document.getElementById('qUserBonus').innerText=document.getElementById('vUserBo').value;	}
				alert('加入成功!');
			}
		} 
		 else {			
			alert('ajax--Error message:'+http_request.status+'。 There was a problem with the request..');
		}
	}
}

