﻿var Html_Root = '/';
/*
  http_request.readyState:
    0 (尚未初始化; 還沒呼叫 open())
    1 (載入中; http_request 已經設定好了，但是還沒呼叫 send())
    2 (載入完成; http_request 已經送給 server 了，server 已經開始 處理了，你可以取得 content header 了。)
    3 (可以互動的; http_request 已經部份完成了，所以有可能取得 部份處理完的資料)
    4 (完成的; compelte)
*/
/**
 * Ajax Base Function - make
 * */
function makeRequest(href)
{
    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("Microsoft.XMLHTTP");
			} catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
	}

	if (!http_request) {
		alert('發生錯誤！您的作業系統無法建立 AJAX 物件，已中斷您的操作要求。');
		return false;
	}

    //http_request.onreadystatechange = function() {
    //    returnRequest(http_request); };

    //http_request.open('POST', Html_Root+'ctrl.php', true);
	//http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	//http_request.send(href);

    return http_request;
}
/**
 * Ajax Bse Function - send
 * */
function sendRequest(http_request, href)
{
    http_request.open('POST', Html_Root+'bin/ctrl.php', true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(href);

    return false;
}
/**
 * Ajax Bse Function - error message
 * */
function errorCode(code)
{
    var errorMsg;
    switch(code)
    {
    case 500:
        errorMsg = '伺服器錯誤!!';
    break;
    default:
        errorMsg = 'HTTP 發生錯誤, 代碼: '+code+' !';
    break;
    }

    return errorMsg;
}
// ============================================================ //

/**
 * Ajax for Return to some object - send
 * */
function return_Ajax(obj_id, href)
{
    if(typeof(http_request) == 'object' && http_request.readyState != 4)
    {
        setTimeout("alert_Ajax('"+href+"');", 500);
        return false;
    }
    else
    {
        http_request = makeRequest();
        http_request.onreadystatechange = function() {
            return_Ajax_end(obj_id, http_request); };
        sendRequest(http_request, href);
    }
}
/**
 * Ajax for Return to some object - do
 * */
function return_Ajax_end(obj_id, http_request)
{
    //alert(http_request.readyState);
    if (http_request.readyState == 0) {

    } else if (http_request.readyState == 1) {

    } else if (http_request.readyState == 2) {

    } else if (http_request.readyState == 3) {

    } else if (http_request.readyState == 4) {
        if(http_request.status == 200)
        {
            context = http_request.responseText;
            arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
            document.getElementById(obj_id).value = arr[0];
        }
        else if(http_request.status == '0')
            ;
        else
        {
            alert(errorCode(http_request.status));
        }
        http_request = false;
    } else {

    }
}

/**
 * Ajax for alert - send
 * */
function alert_Ajax(href)
{
    //alert(href);
    if(typeof(http_request) == 'object' && http_request.readyState != 4)
    {
        setTimeout("alert_Ajax('"+href+"');", 500);
        return false;
    }
    else
    {
        http_request = makeRequest();
        http_request.onreadystatechange = function() {
            alert_Ajax_end(http_request); };
        sendRequest(http_request, href);
    }
}
/**
 * Ajax for alert - do
 * */
function alert_Ajax_end(http_request)
{
    //alert(http_request.readyState);
    if (http_request.readyState == 0) {

    } else if (http_request.readyState == 1) {

    } else if (http_request.readyState == 2) {

    } else if (http_request.readyState == 3) {

    } else if (http_request.readyState == 4) {
        if(http_request.status == 200)
        {
            context = http_request.responseText;
            alert(context);
            arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
            //alert(arr[0]);
        }
        else if(http_request.status == '0')
            ;
        else
        {
            alert(errorCode(http_request.status));
        }
        http_request = false;
    } else {

    }
}

/**
 * Ajax for innerHTML - send
 * */
function echo_Ajax(target_obj, href, type, imgload)
{
    //alert(href);
    if(typeof(target_obj) == 'string')
        target_obj = document.getElementById(target_obj);

    if(!imgload)
        imgload = '';

    if(type == 'display')
    {
        if(target_obj.style.display == 'none')
            target_obj.style.display = '';
        else
        {
            target_obj.style.display = 'none';
            return false;
        }
    }
    else if(type == 'display_once')
    {
        if(target_obj.style.display == 'none')
            target_obj.style.display = '';
    }
    else
        ;

    if(typeof(http_request) == 'object' && http_request.readyState != 4)
    {
        setTimeout("echo_Ajax('"+target_obj.id+"', '"+href+"', '"+type+"', '"+imgload+"');", 500);
        return false;
    }
    else
    {
        http_request = makeRequest();
        http_request.onreadystatechange = function() {
            echo_Ajax_end(http_request, target_obj, imgload); };
        sendRequest(http_request, href);
    }
}
/**
 * Ajax for innerHTML - do
 * */
function echo_Ajax_end(http_request, target_obj, imgload)
{
    if (http_request.readyState == 0) {

    } else if (http_request.readyState == 1) {
        // ---------------- Loading 圖示顯示 ------------------------------------------
        if(imgload)
            target_obj.innerHTML = '<span id="'+target_obj.id+'_ajax_temp"><img src="'+Html_Root+'images/loading_'+imgload+'.gif"></span>';
        else
            target_obj.innerHTML = '<span id="'+target_obj.id+'_ajax_temp"><img src="'+Html_Root+'images/loading_ss.gif"></span>';
        // ----------------------------------------------------------------------------
    } else if (http_request.readyState == 2) {

    } else if (http_request.readyState == 3) {

    } else if (http_request.readyState == 4) {

        if(http_request.status == 200)
        {
            context = http_request.responseText;
            //alert(context);
            arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
            //alert(arr[0]);
            target_obj.innerHTML = arr[0];
        }
        else if(http_request.status == '0')
            ;
        else
        {
            target_obj.innerHTML = errorCode(http_request.status);
        }
        http_request = false;
    } else {

    }
}

/**
 * Ajax for insertAdjacentHTML - send
 * */
function Adj_Ajax(target_obj, href, imgload)
{
    adj_flag = 'no';
    if(typeof(target_obj) == 'string')
        target_obj = document.getElementById(target_obj);

    if(!imgload)
        imgload = '';

    if(typeof(http_request) == 'object' && http_request.readyState != 4)
    {
        setTimeout("Adj_Ajax('"+target_obj.id+"', '"+href+"', '"+imgload+"');", 500);
        return false;
    }
    else
    {
        http_request = makeRequest();
        http_request.onreadystatechange = function() {
            Adj_Ajax_end(http_request, target_obj, imgload); };
        sendRequest(http_request, href);
    }
}
/**
 * Ajax for insertAdjacentHTML - do
 * */
function Adj_Ajax_end(http_request, target_obj, imgload)
{
    if (http_request.readyState == 0) {

    } else if (http_request.readyState == 1) {
        // ---------------- Loading 圖示顯示 ------------------------------------------
        if(adj_flag == 'yes')
            ;
        else
        {
            adj_flag = 'yes';
            if(imgload)
                insertHtml('beforeEnd', target_obj, '<span id="'+target_obj.id+'_ajax_temp"><img src="'+Html_Root+'images/loading_'+imgload+'.gif"></span>');
            else
                insertHtml('beforeEnd', target_obj, '<span id="'+target_obj.id+'_ajax_temp"><img src="'+Html_Root+'images/loading_ss.gif"></span>');
        }
        // ----------------------------------------------------------------------------
    } else if (http_request.readyState == 2) {

    } else if (http_request.readyState == 3) {

    } else if (http_request.readyState == 4) {
        adj_flag = 'no';
        if(http_request.status == 200)
        {
            context = http_request.responseText;
            arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
            document.getElementById(target_obj.id+'_ajax_temp').style.display = 'none';
            document.getElementById(target_obj.id+'_ajax_temp').id = '';
            insertHtml('beforeEnd', target_obj, arr[0]);
        }
        else if(http_request.status == '0')
            ;
        else
        {
            insertHtml('beforeEnd', target_obj, errorCode(http_request.status));
        }
        http_request = false;
    } else {

    }
}

/**
 * Ajax for eval - send
 * */
function eval_Ajax(href)
{
    if(typeof(target_obj) == 'string')
        target_obj = document.getElementById(target_obj);

    if(typeof(http_request) == 'object' && http_request.readyState != 4)
    {
        setTimeout("eval_Ajax('"+href+"');", 500);
        return false;
    }
    else
    {
        http_request = makeRequest();
        http_request.onreadystatechange = function() {
            eval_Ajax_end(http_request); };
        sendRequest(http_request, href);
    }
}
/**
 * Ajax for eval - do
 * */
function eval_Ajax_end(http_request)
{
    if (http_request.readyState == 0) {

    } else if (http_request.readyState == 1) {

    } else if (http_request.readyState == 2) {

    } else if (http_request.readyState == 3) {

    } else if (http_request.readyState == 4) {
        if(http_request.status == 200)
        {
            context = http_request.responseText;
            //alert(context);
            arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
            //alert(arr[0]);
            eval(arr[0]);
        }
        else if(http_request.status == '0')
            ;
        else
        {
            insertHtml('beforeEnd', target_obj, errorCode(http_request.status));
        }
        http_request = false;
    } else {

    }
}

// 將 AJAX 回傳的結果分兩部份, 前半部回傳給 obj 後半部當作 javascript 執行;
/**
 * Ajax for innerHTML&eval - send
 * */
function echo_eval_Ajax(target_obj, href, type, imgload)
{
    if(typeof(target_obj) == 'string')
        target_obj = document.getElementById(target_obj);

    if(!imgload)
        imgload = '';

    if(type == 'display')
    {
        if(target_obj.style.display == 'none')
            target_obj.style.display = '';
        else
        {
            target_obj.style.display = 'none';
            return false;
        }
    }
    else if(type == 'display_once')
    {
        if(target_obj.style.display == 'none')
            target_obj.style.display = '';
    }
    else
        ;

    if(typeof(http_request) == 'object' && http_request.readyState != 4)
    {
        setTimeout("echo_eval_Ajax('"+target_obj.id+"', '"+href+"', '"+type+"', '"+imgload+"');", 500);
        return false;
    }
    else
    {
        http_request = makeRequest();
        http_request.onreadystatechange = function() {
            echo_eval_Ajax_end(http_request, target_obj, imgload); };
        sendRequest(http_request, href);
    }
}
/**
 * Ajax for innerHTML&eval - do
 * */
function echo_eval_Ajax_end(http_request, target_obj, imgload)
{
    if (http_request.readyState == 0) {

    } else if (http_request.readyState == 1) {
        // ---------------- Loading 圖示顯示 ------------------------------------------
        if(imgload != null)
            target_obj.innerHTML = '<span id="'+target_obj.id+'_ajax_temp"><img src="'+Html_Root+'images/loading_'+imgload+'.gif"></span>';
        else
            target_obj.innerHTML = '<span id="'+target_obj.id+'_ajax_temp"><img src="'+Html_Root+'images/loading_ss.gif"></span>';
        // ----------------------------------------------------------------------------
    } else if (http_request.readyState == 2) {

    } else if (http_request.readyState == 3) {

    } else if (http_request.readyState == 4) {

        if(http_request.status == 200)
        {
            context = http_request.responseText;
            arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
            target_obj.innerHTML = arr[0];
            //alert(arr[0]);
            if(arr[1] != null)
            {
                //alert(arr[1]);
                eval(arr[1]);
            }
        }
        else if(http_request.status == '0')
            ;
        else
        {
            target_obj.innerHTML = errorCode(http_request.status);
        }
        http_request = false;
    } else {

    }
}


/**
 * Ajax for innerHTML&time - send
 * 每1秒確認一次資料是否有傳回, 直到時間結束
 * 傳回的資料陣列第二欄必須是 OK or eval 才會停止
 * 僅可用於 get_session 的功能上
 * */
function echo_time_Ajax(target_obj_id, var_name, timeout, imgload)
{
    href = '_Sys_Flag=get_session&_return_var_name='+var_name;
    //alert('time:' + href);
    //document.getElementById('result_content').insertAdjacentHTML('beforeEnd', var_name+' start!<br />');
    if(typeof(target_obj) == 'string')
        target_obj = document.getElementById(target_obj);

    if(!imgload)
        imgload = '';

    if(typeof(http_request) == 'object' && http_request.readyState != 4)
    {
        //document.getElementById('result_content').insertAdjacentHTML('beforeEnd', http_request.readyState+': '+var_name+' wait!<br />');
        setTimeout("echo_time_Ajax('"+target_obj_id+"', '"+var_name+"', '"+timeout+"', '"+imgload+"');", 500);
        return false;
    }
    else
    {
        http_request = makeRequest();
        http_request.onreadystatechange = function() {
            echo_time_Ajax_end(http_request, target_obj_id, var_name, timeout, imgload); };
        sendRequest(http_request, href);
    }

    return false;
}
function echo_time_Ajax_end(http_request, target_obj_id, var_name, timeout, imgload)
{
    target_obj = document.getElementById(target_obj_id);
    //document.getElementById('result_content').insertAdjacentHTML('beforeEnd', http_request.readyState+ ': ' + var_name+' ready<br />');

    //alert(http_request.readyState);

    if ('http_request.readyState: '+http_request.readyState == 0) {

    } else if (http_request.readyState == 1) {
        // ---------------- Loading 圖示顯示 ------------------------------------------
        if(imgload)
            target_obj.innerHTML = '<span id="'+target_obj.id+'_ajax_temp"><img src="'+Html_Root+'images/loading_'+imgload+'.gif"></span>';
        else
            target_obj.innerHTML = '<span id="'+target_obj.id+'_ajax_temp"><img src="'+Html_Root+'images/loading_ss.gif"></span>';
        // ----------------------------------------------------------------------------
    } else if (http_request.readyState == 2) {

    } else if (http_request.readyState == 3) {

    } else if (http_request.readyState == 4) {
        //alert('http_request.status: '+http_request.status);
        if(http_request.status == 200)
        {
            //alert(timeout);
            parseInt(timeout);

            context = http_request.responseText;
            arr = context.split("[Ajax_abcdefghijklmnopqrstuvwxyz_Ajax_zyxwvutsrqponmlkjihgfedcba_Ajax]");
            //alert(context);

            if(arr[1] == 'OK')
           	{
                //document.getElementById('result_content').insertAdjacentHTML('beforeEnd', var_name+' *** <br />');
                target_obj.innerHTML = arr[0];
           	}
           	else if(arr[1] == 'eval')
           	{
                target_obj.innerHTML = arr[0];
                if(arr[2] != null)
                    eval(arr[2]);
         	}
           	else if(timeout == 0 || timeout == -1)
           	{
                target_obj.innerHTML = '<span style="color:red;">伺服器處理逾時!!!</span>';
           	}
            else if(context.match("<b>Warning</b>:"))
           	{
                target_obj.innerHTML = '<span style="color:red;">系統發生錯誤!!!</span>';
           	}
           	else
           	{
                //document.getElementById('result_content').insertAdjacentHTML('beforeEnd', var_name+' continue <br />');
                timeout = timeout - 1;
                setTimeout("echo_time_Ajax('"+target_obj.id+"', '"+var_name+"', "+timeout+", '"+imgload+"');", 500);
           	}
        }
        else if(http_request.status == '0')
            ;
        else
        {
            target_obj.innerHTML = errorCode(http_request.status);
        }
        http_request = false;
    } else {

    }
}

/**
 * Ajax for Submit Form
 * */
function submitAjax(target_obj, var_name, href, theform, type)
{
	var_name = var_name+'_'+String(Math.floor(Math.random()*10000));
    href=Html_Root+'bin/ctrl.php?'+href+'&_session_var_name='+var_name;
    //alert(href);
    if(type == 'display')
    {
        if(target_obj.style.display == 'none')
            target_obj.style.display = '';
        else
        {
            target_obj.style.display = 'none';
            return false;
        }
    }
    else if(type == 'display_once')
    {
        if(target_obj.style.display == 'none')
            target_obj.style.display = '';
    }
    else
        ;

    if(theform)
	{
		theform.action=href;
		theform.submit();
		theform.action='';
	}
	else
	{
		main_form.action=href;
		main_form.submit();
		main_form.action='';
	}

    echo_time_Ajax(target_obj.id, var_name, -2, 'l')
}
/**
 * Ajax for Submit Form without Result
 * */
function submitAjax_NR(var_name, href, theform)
{
	var_name = var_name+'_'+String(Math.floor(Math.random()*10000));
    href=Html_Root+'bin/ctrl.php?'+href+'&_session_var_name='+var_name;
    //alert(href);
    //alert(theform);

    if(theform)
	{
		theform.action=href;
		theform.submit();
		theform.action='';
	}
	else
	{
		main_form.action=href;     // alert(href);
		main_form.submit();
		main_form.action='';       // alert(target_obj);
	}
}

