var http_request = false;
var Html_Root = './';
function makeRequest(strURL, substr)
{
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // 使用 IE 瀏覽器的物件
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}

	if (!http_request) {
		alert('發生錯誤！您的作業系統無法建立 AJAX 物件，已中斷您的操作要求。');
		return false;
	}

	// 使用 GET 方式傳送網址參數，可設定 GET/POST 兩種
	// strURL = 伺服器端要執行的網頁程式路徑，包涵網頁名稱；可輸入實際URL或虛擬URL例：http://xxx.xxx.com.tw/file/xxx.php 或 ./file/xxx.php

	http_request.open('POST', strURL, false);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	// ---------- Loading 寫在這 ------------------- //

	// --------------------------------------------- //

	http_request.send(substr);
	// 若需要直接將結果輸出，需開啟 onreadystatechange 並用函數來輸出 HTML
	// function() {} 可以改成其它函式名稱
	//http_request.onreadystatechange = function() {};

    // ---------- Finish 寫在這 ------------------- //

	// --------------------------------------------- //

	// 將回傳的結果傳出 AJAX 執行區域
	return http_request.responseText
}

// ==========================================================================
// =========================== alert for Ajax ===============================
// ==========================================================================
function alert_Ajax(AX_link, substr)
{
	context = makeRequest(AX_link, substr);
	if(context) ;
	else context = '[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]';
	arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
	  alert(arr[0]);

}
// ==========================================================================


// ==========================================================================
// ======================== innerHTML for Ajax ==============================
// ==========================================================================
function echo_Ajax(AX_obj, AX_link, substr, type, imgload)
{
  // ---------------- Loading 圖示顯示 ------------------------------------------
  if(imgload)
    AX_obj.innerHTML = '<img src="'+Html_Root+'images/loading_'+imgload+'.gif">';
  else
	AX_obj.innerHTML = '<img src="'+Html_Root+'images/loading_ss.gif">';
  // ----------------------------------------------------------------------------
  if(type == 'display')
  {
    if(AX_obj.style.display == 'none')
      AX_obj.style.display = '';
    else
    {
      AX_obj.style.display = 'none';
      return false;
    }
  }

  if(AX_obj.id == '')
  {
	var context = makeRequest(AX_link, substr);
	if(context) ;
	else context = '[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]';
	arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
	AX_obj.innerHTML = arr[0];
  }  
  else
  { 
    setTimeout("echo_Ajax_end('"+AX_obj.id+"', '"+AX_link+"', '"+substr+"', '"+type+"')", 500);
  }               
}

function echo_Ajax_end(AX_obj, AX_link, substr, type)
{
	
	var context = makeRequest(AX_link, substr);
	if(context) ;
	else context = '[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]';
	arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
	document.getElementById(AX_obj).innerHTML = arr[0];
}
// ==========================================================================


// ==========================================================================
// ==================== insertAdjacentHTML for Ajax =========================
// ==========================================================================
function echo_Ajax_Adj(AX_obj, AX_link, substr, type, imgload)
{
  //beforeBegin, afterBegin, beforeEnd, afterEnd
  // ---------------- Loading 圖示顯示 ------------------------------------------
  if(imgload)
    AX_obj.insertAdjacentHTML(type, '<span id="ajax_0917temp"><img src="'+Html_Root+'images/loading_'+imgload+'.gif"></span>');
  else
    AX_obj.insertAdjacentHTML(type, '<span id="ajax_0917temp"><img src="'+Html_Root+'images/loading_ss.gif"></span>');
  // ----------------------------------------------------------------------------
  setTimeout("echo_Ajax_Adj_end('"+AX_obj.id+"', '"+AX_link+"', '"+substr+"', '"+type+"')", 500);
}

function echo_Ajax_Adj_end(AX_obj, AX_link, substr, type)
{
  var context = makeRequest(AX_link, substr);
  if(context) ;
  else context = '[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]';
  arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
  ajax_0917temp.style.display = 'none';
  ajax_0917temp.id = '';
  if(type == null)
    type = 'afterBegin';
  document.getElementById(AX_obj).insertAdjacentHTML(type, arr[0]);

}
// ==========================================================================

// ==========================================================================
// ========================== eval for Ajax =================================
// ==========================================================================
function eval_Ajax(AX_link, substr)
{
  context = makeRequest(AX_link, substr);
  if(context) ;
  else context = '[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]';
  arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");

  eval(arr[0]);
}
// ==========================================================================


// ==========================================================================
// =================== innerHTML & eval for Ajax ============================
// ==========================================================================
// 將 AJAX 回傳的結果分兩部份, 前半部回傳給 obj 後半部當作 javascript 執行;
function echo_eval_Ajax(AX_obj, AX_link, substr, imgload)
{
  // ---------------- Loading 圖示顯示 ------------------------------------------
  if(imgload)
    AX_obj.innerHTML = '<img src="'+Html_Root+'images/loading_'+imgload+'.gif">';
  else
	AX_obj.innerHTML = '<img src="'+Html_Root+'images/loading_ss.gif">';
  // ----------------------------------------------------------------------------

  if(AX_obj.id == '')
  {
    var context = makeRequest(AX_link, substr);
    if(context) ;
	else context = '[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]';
    //使用獨一無二的分割字串
    arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
	AX_obj.innerHTML = arr[0];
    eval(arr[1]);
  }
  else
    setTimeout("echo_eval_Ajax_end('"+AX_obj.id+"', '"+AX_link+"', '"+substr+"')", 500);
}

function echo_eval_Ajax_end(AX_obj, AX_link, substr)
{
  var context = makeRequest(AX_link, substr);
  if(context) ;
  else context = '[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]';
  //使用獨一無二的分割字串
  arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
  document.getElementById(AX_obj).innerHTML = arr[0];
  //alert(AX_link+'?'+substr);
  eval(arr[1]);
}
								
// ==========================================================================


// ==========================================================================
// =================== innerHTML & time for Ajax ============================
// ==========================================================================
// 使用範例 6: 倒數每2秒確認一次資料是否有傳回
function echo_time_Ajax(AX_obj, AX_link, substr, timeout, imgload)
{
  // ---------------- Loading 圖示顯示 ------------------------------------------
  if(imgload)
    document.getElementById(AX_obj).innerHTML = '<img src="'+Html_Root+'images/loading_'+imgload+'.gif">';
  else
	document.getElementById(AX_obj).innerHTML = '<img src="'+Html_Root+'images/loading_ss.gif">';
  // ---------------------------------------------------------------------------- 
  parseInt(timeout);
  setTimeout("echo_time_Ajax_end('"+AX_obj+"', '"+AX_link+"', '"+substr+"', "+timeout+", '"+imgload+"')", 1000);  
}
function echo_time_Ajax_end(AX_obj, AX_link, substr, timeout, imgload)
{
	var context = makeRequest(AX_link, substr);
	if(context) ;
	else context = '[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]';
	  
	parseInt(timeout);
	
	//使用獨一無二的分割字串
	//alert(context);
	arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
	//alert(arr[1]);
	if(arr[1] == 'OK')
	{
	  document.getElementById(AX_obj).innerHTML = arr[0];
	}
	else if(arr[1] == 'eval')
	{
	  document.getElementById(AX_obj).innerHTML = arr[0];
	  eval(arr[2]);
	}
	else if(timeout == 0 || timeout == -1)
	{
	  document.getElementById(AX_obj).innerHTML = '<span style="color:red;">伺服器處理逾時!!!</span>';
	}
	else
	{	
	  timeout = timeout - 2;
	  setTimeout("echo_time_Ajax_end('"+AX_obj+"', '"+AX_link+"', '"+substr+"', "+timeout+", '"+imgload+"')", 2000);
	}  
}
// ==========================================================================
