﻿function ReceiveServerAck(arg, context)
{
    
    var args = arg.split('|');
    if(odaconsole){
        //odaconsole.debug("ReceiveServerAck: " + args[0] + " with args " + args[1]);
        odaconsole.debug("ReceiveServerAck: " + args[0]);
    }
    switch(args[0])
    {
        case "Alert":
            if (typeof setStatusImage == "function") { setStatusImage(0); }
            var str = "" + arg;
            alert(str.substring(6, str.length));
            //odaAlert(str.substring(6,str.length));
            if (typeof hideLoading == 'function') { hideLoading(); }
            break;
        case "Error":
            if(typeof setStatusImage == "function"){setStatusImage(2);}
            var str = "" + arg;
            if (odaconsole) {
                odaconsole.debug("Server call back failed: " + args[0]);
            }
            eval(str.substring(6, str.length));
            if (typeof hideLoading == 'function') { hideLoading(); }
            break;
        case "Eval":
            if(typeof setStatusImage == "function"){setStatusImage(0);}
            var str = "" + arg;
            eval(str.substring(5,str.length));
            break;
        case "Redirect":
            if(typeof setStatusImage == "function"){setStatusImage(0);}
            var str = "" + arg;
            window.location.href = str.substring(9,str.length);
            break;
        case "NOOP":
            if(typeof setStatusImage == "function"){setStatusImage(0);}
        case "":
            if(typeof setStatusImage == "function"){setStatusImage(0);}
            break;
        default:
            if (typeof setStatusImage == "function") { setStatusImage(0); }
            var str = "" + arg;
            var len = "" + args[0];
            var argsParam = str.substring(len.length + 1, str.length);
            var contextString = context;
            if (contextString != null && !contextString.toString().isJSON()) {
                contextString = "'" + contextString + "'";
            }
            if (argsParam.indexOf('{') > -1 && argsParam.indexOf('}') > -1 && argsParam.toString().isJSON()) {
                eval("if(typeof(" + args[0] + ") == 'function'){" + args[0] + "(" + str.substring(len.length + 1, str.length) + "," + contextString + ");}");
            }
            else {
                eval("if(typeof(" + args[0] + ") == 'function'){" + args[0] + "('" + str.substring(len.length + 1, str.length) + "'," + contextString + ");}");
            }
            break;
    };
    if (context != null && context.toString().split("|")[0] == "CloseContentWindowAfterSave") {
        setTimeout(receiveServerAckCloseAfter, 100, context.split("|")[1]);
    }
}

function receiveServerAckCloseAfter(id) {
    if (window && window.top && window.top.$(id)) {
        window.top.$(id).remove();
    }
}

function CallBackError(arg, context)
{
    if(typeof setStatusImage == "function"){setStatusImage(2);}
    if(odaconsole){
        odaconsole.debug("Server call back failed: " + arg);
    }
    if (typeof hideLoading == "function") { hideLoading(); }
    //alert('Error: Server call back failed!\r\n' + arg);
}

function ServerFunction(arg, context, _causeValidation) {
    if(arg.startsWith("Save")){
        if (window.location.href.toLowerCase().include("content.aspx")) {
            var closeOnSave = getQueryStringValue("CloseOnSave");
            var iframeID = getQueryStringValue("FrameID");
            if (closeOnSave != null && closeOnSave != "null" && closeOnSave == "true" && (context == null || context.toString().length == 0)) {
                ServerFunctionInstance(arg, "CloseContentWindowAfterSave|" + globalReplace(iframeID,"_iframe",""), _causeValidation);
                return;
            }
        }
    }
    ServerFunctionInstance(arg, context, _causeValidation);
}

//**********************************************************************************************
//**********************To Call a Different Page or Web Service**********************************
//**********************************************************************************************

var __functionName
var __passThroughContext
function processReqChange() 
{
    // only if req shows "loaded"
    if (req.readyState == 4) 
    {
        // only if "OK"
        if (req.status == 200) 
        {
			requestReturned(req, __functionName, __passThroughContext);
        }
        else 
        {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function loadXMLDoc(url, returnFunction, context) 
{
    __functionName = returnFunction;
    __passThroughContext = context;
    req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) 
    {
    	try 
    	{
			req = new XMLHttpRequest();
        }
        catch(e) 
        {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    }
    else if(window.ActiveXObject) 
    {
       	try 
       	{
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	}
      	catch(e) 
      	{
        	try 
        	{
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	}
        	catch(e) 
        	{
          		req = false;
        	}
		}
    }
	if(req) 
	{
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

function requestReturned(response,functionName,context)
{
    if(typeof setStatusImage == "function"){setStatusImage(0);}
    eval(functionName + "('" + response.responseText + "', '" + context + "');");
}


///////////////////////////////////////////////////////////////////////////////////////////
/////////       Prototype AJAX CALLS
///////////////////////////////////////////////////////////////////////////////////////////

function ajaxUpdate(IdToUpdate,url,params,onCompleteMethod,context,onFailMethod)
{
    if (typeof setStatusImage == "function") { setStatusImage(1); }

    var p = {};
    p.parameters = params;
    p.evalScripts = true;
    

    if (onCompleteMethod == null) {
        p.onComplete = function(transport) {
            if (typeof setStatusImage == "function") { setStatusImage(0); }
        };
    }
    else {
        p.onComplete = function(transport) {
            if (typeof setStatusImage == "function") { setStatusImage(0); }
            onCompleteMethod(transport.responseText, context);
        };
    }

    if (onFailMethod == null) {
        p.onFailure = function(transport) {
            if (typeof setStatusImage == "function") { setStatusImage(2); }
            odaconsole.debug("Ajax Failed");
        };
    }
    else {
        p.onFailure = function(transport) {
            if (typeof setStatusImage == "function") { setStatusImage(2); }
            onFailMethod(transport.responseText, context);
        };
    }


    new Ajax.Updater(
        { success: IdToUpdate, failure: 'errorNotice' }
        , url
        , p
        );
}

function ajaxRequest(url,params,onSuccessMethod,context){
    if(typeof setStatus == "function"){setStatus(1);}
    new Ajax.Request(url, 
    { 
        method: 'post'
        ,parameters:  params
        ,onSuccess: function(transport){
            if(typeof setStatusImage == "function"){setStatusImage(0);}
            onSuccessMethod(transport.responseText, context);}
             ,onFailure:function(transport){
                    odaconsole.debug(onCompleteMethod + " - Ajax Failed");
                }
    });
}

function getEmbededForm(idToUpdate, entityTypeName, entityID, onCompleteMethod, context, DefaultScreenID)
{
    var params = "EmbeddedEntityType=" + entityTypeName + "&EmbeddedObjectID=" + entityID;
    if(DefaultScreenID != null && DefaultScreenID.toString().length > 0)
    {
        params += "&EmbeddedDefaultScreen=" + DefaultScreenID;
    }
    ajaxUpdate(idToUpdate,'Embed.aspx',params,onCompleteMethod,context);
}

function getPluginForm(idToUpdate, pluginClass, queryString, onCompleteMethod, context)
{
    var params = "PluginClass=" + pluginClass + '&' + queryString
    ajaxUpdate(idToUpdate,'UpdaterPluginHost.aspx',params,onCompleteMethod,context);
}


///////////////////////////////////////////////////////////////////////////////////////////
/////////       Keep Alive
///////////////////////////////////////////////////////////////////////////////////////////

function KeepAlive(args,context)
{
    if(args != "True")
        alert("Your server session has ended.");
    else    
        setTimeout("ServerFunction('KeepAlive',null);", 300000);
}