// +---------------------------------------------------------------------------+
// | The OpenBluewhale Project                                                 |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2007  The OpenBW Group (http://www.openbw.org/)             |
// +---------------------------------------------------------------------------+
// |    本原始碼檔案和本軟體計劃 (The OpenBluewhale Project) 的所有程式碼必須  |
// | 遵守三條款式的BSD授權（Three-Clause BSD License），您可以自由下載及使用   |
// | OpenBluewhale，對於任何再散播（redistribution）本軟體的行為，都必須附上   |
// | 這份授權條款全文。 您可以在本專案 /license 目錄下找到授權全文，或是在網站 |
// | http://www.openbw.org/ 取得。                                             |
// +---------------------------------------------------------------------------+
// | 系統 - Ajax 應用 API                                                      |
// |                                                                           |
// +---------------------------------------------------------------------------+
// | Authors: noon <noon@openbw.org>                                           |
// +---------------------------------------------------------------------------+
//
//    $Id: obwajax.js,v 1.8 2008/09/09 00:34:08 noon Exp $

function _OBW_Ajax ()
{
    this.filetype       = 'text'
    this.xmlHttp        = null
    this.url            = ''
    this.createXMLHTTP  = _OBW_createXMLHTTP
    this.readResponse   = _OBW_readResponse
    this.writeResponse  = _OBW_writeResponse

    // tool method
    this.fetchBody      = _OBW_fetchBody
}


function _OBW_createXMLHTTP ()
{
    if (typeof XMLHttpRequest != "undefined"){
        this.xmlHttp = new XMLHttpRequest();
    } else if(typeof window.ActiveXObject != "undefined") {
        try {
            this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0")
        } catch (e) {
            try {
                this.xmlHttp = new ActiveXObject("MSXML2.XMLHTTP")
            } catch (e) {
                try {
                    this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    this.xmlHttp = null;
                }
            }
        }
    }
}

function _OBW_readResponse (url)
{
    if (this.xmlHttp != null) {
        with (this.xmlHttp) {
            open("GET", url, true)
            setRequestHeader("If-Modified-Since", "0")
            setRequestHeader("Cache-Control", "no-cache")
            send(null)
        }
    } else {
        alert("Your browser does not support XMLHTTP.")
    }
}

function _OBW_writeResponse (url, data)
{
    with (this.xmlHttp) {
        open("POST", url, true)
        setRequestHeader("If-Modified-Since", "0")
        setRequestHeader("Cache-Control", "no-cache")
        setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
        setRequestHeader("Content-Type", "text/html; charset=utf-8")
        send(data)
    }
}

function _OBW_fetchBody (content)
{
    var bw_sbody = content.indexOf("<body")
    if(bw_sbody == -1) return content

    bw_sbody = content.indexOf(">", bw_sbody)
    if(bw_sbody == -1) return content

    var bw_ebody = content.lastIndexOf("</body>")
    if(bw_ebody == -1) return content

    return content.slice(bw_sbody + 1, bw_ebody)
}

// openbluewhale platform use
function BWPlatform ()
{
    this.setupHTML    = null
    this.completeHTML = null
    this.changeModule = _OBW_changeModule
    this.showScreen   = _OBW_showScreen
    this.showLoading  = _OBW_showLoading
    this.changeLink   = _OBW_changeLink
}

function _OBW_changeModule (module_sn)
{
    setCookie('DC_MODULE_SN', module_sn);
    this.changeLink(frames['MENU'].document.location, 'menu.php')
    this.showLoading(frames['MENU'].document.body)
    this.showScreen('bwmain', 'main.php', 'main')
}


function _OBW_showScreen (bwajax , url, id)
{
    // read module screen
    bwajax = new _OBW_Ajax()
    bwajax.createXMLHTTP()
    bwajax.url = url
    bwajax.xmlHttp.onreadystatechange = function()
    {
        if (bwajax.xmlHttp.readyState == 1) {
            _OBW_showLoading(frames[id].document.body)
        }
        if (bwajax.xmlHttp.readyState==4) {
            if (bwajax.xmlHttp.status == 200) {
                frames[id].document.body.innerHTML = bwajax.xmlHttp.responseText
            } else {

            }
        }
    }
    bwajax.readResponse(url)
}

function _OBW_showLoading (obj)
{
    try {
        obj.innerHTML += '<table width=100% height=100% style="POSITION: absolute;Z-INDEX:100;top:0;"><tr><td align=center><table bgcolor=white border=1 bordercolor=black><tr><td><img src='+ dcx_theme_path + '/loading.gif width=20 height=20 align=absmiddle> loading...</td></tr></table></td></tr></table>'
    } catch (_OBW_Error) {}
}

function _OBW_changeLink (obj, link)
{
    obj.replace(link)
}

function initializeAjaxBWPlatform ()
{
    BWPlatform = new BWPlatform()
}

initializeAjaxBWPlatform()
