/******************************************************************************/
/*                                                                            */
/*             JavaScript Load                                                */
/*                                                                            */
/******************************************************************************/
function LoadJavaScript(url)
{
    if (!document.getElementById(url))
    {
        dojo.require("System.Ajax.JavaScriptLoader");
        var ajaxLoader = new System.Ajax.JavaScriptLoader();
        ajaxLoader.OnLoaded = OnJavaScriptLoaded;
        ajaxLoader.OnError = OnJavaScriptError;
        ajaxLoader.Get(url);
    }//endif
}

function OnJavaScriptLoaded(response, ioArgs)
{                        
    var head    = document.getElementsByTagName("head").item(0);
    var script  = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    script.setAttribute("defer", "defer");
    script.setAttribute("id", ioArgs.url);
    script.text = response;
    head.appendChild(script);                                                                                               
}

function OnJavaScriptError(response, ioArgs)
{
    alert(response);
}

/******************************************************************************/
/*                                                                            */
/*             Minimizing / Maximizing HTML Container                         */
/*                                                                            */
/******************************************************************************/
function CloseContainer(containerId)
{
    var imageContainer      = document.getElementById(containerId + '_Image');
    var linkContainer       = document.getElementById(containerId + '_Link');
    var contentContainer    = document.getElementById(containerId + '_Content');

    //change the images
    //and links
    linkContainer.setAttribute('onclick', 'OpenContainer("' + containerId + '")');
    imageContainer.setAttribute('src', '../theme/common/images/icons/open.gif');

    contentContainer.style.visibility   = 'hidden';
    contentContainer.style.display      = 'none';
}

function OpenContainer(containerId)
{
    var imageContainer      = document.getElementById(containerId + '_Image');
    var linkContainer       = document.getElementById(containerId + '_Link');
    var contentContainer    = document.getElementById(containerId + '_Content');
    
    //change the images
    //and links
    linkContainer.setAttribute('onclick', 'CloseContainer("' + containerId + '")');
    imageContainer.setAttribute('src', '../theme/common/images/icons/close.gif');
    
    contentContainer.style.visibility  	= 'visible';    
    contentContainer.style.display 		= '';       
}

/******************************************************************************/
/*                                                                            */
/*             XHR                                                            */
/*                                                                            */
/******************************************************************************/
function BeginLoading()
{
    var loader = document.getElementById("loader");
    if (loader == null)
    {
        loader = document.createElement("div");        
        loader.id = "loader";
        loader.setAttribute("class", "LoadingContainer");
        loader.innerHTML = "<table class=\"LoadingOverlay\">" + 
                           "     <tbody>" +
                           "         <tr>" +
                           "             <td></td>" +
                           "         </tr>" +
                           "    </tbody>" +
                           "</table>" +
                           "<table class=\"LoadingContent\">" +
                           "    <tbody>" +
                           "        <tr>" +
                           "            <td align=\"center\" valign=\"center\">" +
                           "                <table class=\"Loading\">" +
                           "                    <tbody>" +
                           "                        <tr>" +
                           "                            <td align=\"center\" valign=\"center\">" +
                           "                                <img src=\"/theme/common/images/icons/loading.gif\">" +
                           "                            </td>" +
                           "                        </tr>" +
                           "                    </tbody>" +
                           "                </table>" +
                           "            </td>" +
                           "        </tr>" +
                           "    </tbody>" +
                           "</table>";
        document.body.appendChild(loader);
    }//endif
    loader.style.display = "block";
}

function EndLoading()
{
    var loader = document.getElementById("loader");
    if (loader != null)
    {
        loader.style.display = "none";
    }//endif
}

function Get()
{
    BeginLoading();
    var url  = null;
    var form = null;
    
    url = arguments[0];
    if (arguments.length > 1)
    {
        form = arguments[1];
    }//endif
    
    dojo.require("System.Ajax.PlainTextLoader");
    var ajaxLoader = new System.Ajax.PlainTextLoader();
    ajaxLoader.OnLoaded = OnHtmlLoaded;
    ajaxLoader.OnError = OnHtmlError;
    ajaxLoader.Get(url, form);
}

function Post()
{
    BeginLoading();
    var url  = null;
    var form = null;
    
    url = arguments[0];
    if (arguments.length > 1)
    {
        form = arguments[1];
    }//endif

    dojo.require("System.Ajax.PlainTextLoader");
    var ajaxLoader = new System.Ajax.PlainTextLoader();
    ajaxLoader.OnLoaded = OnHtmlLoaded;
    ajaxLoader.OnError = OnHtmlError;
    ajaxLoader.Post(url, form);                    
}

/******************************************************************************/
/*                                                                            */
/*             Dialog                                                         */
/*                                                                            */
/******************************************************************************/
var g_CurrentDialog = null;
function OpenDialog(title, url)
{
    BeginLoading();    
    dojo.require("dojox.widget.Dialog");
    if (g_CurrentDialog != null)
    {
        g_CurrentDialog.destroyRecursive();
    }//endif           
    g_CurrentDialog = new dojox.widget.Dialog({
        id :            "Dialog",
        title :         title,
        executeScripts: true,
        onHide:         CloseDialog,
        onLoad:         EndLoading,
        href:           url,
        showTitle:      true,
        sizeToViewport: true
    });                             
    g_CurrentDialog.show();    
}//endfunction OpenDialog

function CloseDialog()
{
    if (g_CurrentDialog != null)
    {
        g_CurrentDialog.destroyRecursive();
        g_CurrentDialog = null;
    }//endif
}//endfunction CloseDialog
