//***************************************************************************//
//***************************************************************************//
// Copyright (c) 2011 Matthias Buchholz
// IFIE Ingenieurbüro für innovative Entwicklungen
// Gustav-Meyer-Allee 25, 13355 Berlin Germany
//
// Contact: kontakt@ifie.com
//
// Author: Matthias Buchholz
//
//***************************************************************************//
//***************************************************************************//

function JRequest(options)
{
    var req = {};

    req.controller = options.hasOwnProperty('controller') ? options.controller : '';
    req.command = options.hasOwnProperty('command') ? options.command : '';

    req.activityIndicator =  options.hasOwnProperty('indicator') ? options.indicator : null;


    req._toggleActivity = function(active){ if(this.activityIndicator == null) return; if(active) $(activityIndicator).show(); else $(activityIndicator).hide(); };

    req.onSuccess = function(payload)
    {
        this.processResult(this.controller,this.command,payload.Result);

    };

    req.onError = function(error)
    {
        if(error.message)
            alert(error.message);
    };


    req.processResult = function(controller,command,result)
    {
        //
    };

    req.parameters = function()
    {
        var params = {};
        return params;
    };

    req.execute = function()
    {
        var params = this.parameters();
        params.controller = this.controller;
        params.cmd = this.command;

        var ajaxRequest = {
            url: AjaxHelperUrl,
            data: params,
            type: 'post',
            dataType: 'json',
            subject: this,
            success: function(result)
            {
                if(result.Success == 1)
                {
                    this.subject.onSuccess(result.Payload);
                }else
                {
                    if(result.Exception)
                    {
                        this.subject.onError(result.Exception);
                    }else
                    {
                        this.subject.onError(result.Exception);
                    }
                }
            },
            error: function(xhr,msg)
            {
                this.subject.onError(msg);
            }
        };

        $.ajax(ajaxRequest);
    };

    return req;
}
