﻿var BCH_URL = "http://bch.cbd.int";

//============================================================
//
//
//============================================================
function GetRoot()
{
    return BCH_URL+"/";
}

var BchAjaxPlugIn_CssCount = 0
//============================================================
//
//
//============================================================
function  BchAjaxPlugIn_IncludeCss(path)
{
    var sCssID = "BchAjaxPlugInCss"+BchAjaxPlugIn_CssCount;

    document.write("<link id='"+sCssID+"' rel='stylesheet' type='text/css' href='"+ path + "' disabled='true' />");
    
    //Firefox bug: Disable with script
    document.write("<script language='javascript'> var oCss = document.getElementById('"+sCssID+"'); if(oCss!=null) oCss.disabled=true; </script>");
    
    BchAjaxPlugIn_CssCount++;
}

//============================================================
//
//
//============================================================
function  BchAjaxPlugIn_EnableCss()
{
    for(var i=0; i<BchAjaxPlugIn_CssCount; ++i)
    {
        var oCss = document.getElementById("BchAjaxPlugInCss"+i);
        
        if(oCss!=null) oCss.disabled = false;
    }
}

//============================================================
//
//
//============================================================
function BchAjaxPlugIn_IncludeJS(path)
{
    document.write("<script language='javascript' type='text/javascript' charset='utf-8'  src='"+ path +"'></script>")
}


//============================================================
//
//
//============================================================
function BchAjaxPlugIn_LoadDocument(method, parameters, callback)
{
    var oRequest =  new BchAjaxPlugin_HttpRequest(BCH_URL);

    oRequest.onerror = function(description)
    {
        alert(description);
        callback(null);
    }

    oRequest.onload  = function()
    {
        if(oRequest.responseText!=null)
            callback(oRequest.responseText);
        else
            oRequest.onerror("Error loading document");
            
    }

    oRequest.send(method, parameters);
}

//============================================================
//
//
//============================================================
function GenerateRecords(documentType, countryCode, language, enableStyle)
{
    if(CurrentRequest["id"]!=null) GenerateRecord (CurrentRequest["id"], countryCode, language, enableStyle)
    else                           GenerateResults(documentType,         countryCode, language, enableStyle)
}

//============================================================
//
//
//============================================================
function GenerateResults(documentType, countryCode, language, enableStyle)
{

    if(typeof(enableStyle) == "undefined" || enableStyle)
        BchAjaxPlugIn_EnableCss();
    
    var BchDocument = new DocumentContainer(language);
    
    var oParameters = {};

    oParameters["documentType"] = Coalesce(documentType, "all");
    oParameters["country"     ] = Coalesce(countryCode,  "");
    oParameters["language"    ] = Coalesce(language,     "");

    BchAjaxPlugIn_LoadDocument("result", oParameters, function(docText)
    {
        var oUrl = UriBuilder(CurrentRequest.Path);

        BchDocument.write(docText);
        BchDocument.render();
    });
}

//============================================================
//
//
//============================================================
function GenerateRecord(documentID, countryCode, language, enableStyle)
{

    if(typeof(enableStyle) == "undefined" || enableStyle)
        BchAjaxPlugIn_EnableCss();
    
    var BchDocument = new DocumentContainer(language);
    
    var oParameters = {};

    oParameters["id"      ] = Coalesce(documentID, "");
    oParameters["country" ] = Coalesce(countryCode,"");
    oParameters["language"] = Coalesce(language,   "");

    BchAjaxPlugIn_LoadDocument("record", oParameters, function(docText)
    {
        var oUrl = UriBuilder(CurrentRequest.Path);

        BchDocument.write(docText);
        BchDocument.render();
    });
}


//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////


/*
BchAjaxPlugIn_IncludeCss("/common/edit.css");
BchAjaxPlugIn_IncludeCss("/centralportal/ui/offsite/bch-offsite.css");
*/
BchAjaxPlugIn_IncludeCss("js/bch/edit.css");
BchAjaxPlugIn_IncludeCss("js/bch/bch-offsite.css");

BchAjaxPlugIn_IncludeJS ("js/bch/bch-utils.js");
BchAjaxPlugIn_IncludeJS ("js/bch/bch-request.js");
	
