/*******************************************************************************************************************
 * 
 *  These are JavaScript objects that are meant to reflect the server side business objects, and are primarily used
 *  for Ajax calls from the client
 *
 *******************************************************************************************************************/


/*******************************************************************************************************************
 * Distributor Class
 *******************************************************************************************************************/
var Distributor = Class.create();
Distributor.prototype = {
    initialize: function(siteid) {
        this.siteid = siteid;
        this.FirstName = '';
        this.LastName = '';
    }
};


// Attempts to populate a javascript Distributor object based on a username.
// The second parameter is the callback function to call after the load is complete
// and will pass the new distributor as a parameter to the function. null if there is any kind of error
Distributor.LoadFromUsername = function(siteid, username, callback) {
    DoAjaxAction({action: 'distributor.loadfromusername', username: username, siteid: siteid}, function (result) {
        result.Distributor = null;
        if (!result.HasError)
        {
            var sponsordata = result.responseText.parseQuery();
            var sponsor = new Distributor(siteid);
            sponsor.FirstName = sponsordata.firstname;
            sponsor.LastName = sponsordata.lastname;
            result.Distributor = sponsor;
        }
        callback(result.Distributor); // Send back result
    });
};

/*******************************************************************************************************************
 * ShoppingCart Class
 *******************************************************************************************************************/ 
var ShoppingCart = Class.create();
ShoppingCart.prototype = {
    initialize: function () {
    },
    AddItemByPartNumber: function (quantity, partnumber, callback) {
        DoAjaxAction({action: 'shoppingcart.additembypartnumber', cartSession: 'ShoppingCart', quantity: quantity, partnumber: partnumber},
            function (result) {
                if (!result.HasError)
                {
                    if (result.responseText == "nostock")
                    {
                        result.Result = false;
                    }
                    else
                    {
                        result.Result = true;
                    }
                }
                else
                {
                    result.Result = false;
                }
                callback(result);
            });
    },
    AddItemById: function (quantity, itemid) {
        DoAjaxAction({action: 'shoppingcart.additembyid', cartSession: 'ShoppingCart', quantity: quantity, itemid: itemid},
            function (result) {
                if (!result.HasError)
                {
                    result.Result = true;
                }
                else
                {
                    result.Result = false;
                }
                callback(result);
            });        
    }
};



/*******************************************************************************************************************
 * AjaxRequestException Class
 *******************************************************************************************************************/ 
 var AjaxRequestException = Class.create();
 AjaxRequestException.prototype = {
    initialize: function(message) {
        this.Message = message;
    }
 };
 
 /*******************************************************************************************************************
 * AjaxResponse Class
 *******************************************************************************************************************/
 var AjaxResponse = Class.create();
 AjaxResponse.prototype = {
    initialize: function (responseText, exception) {
        this.HasError = false;
        this.Exception = null;
        this.responseText = responseText;
        if (this.responseText == "invalid")
        {
            this.HasError = true;
        }
        
        if (arguments.length == 2) // "Overloading"
        {
            this.HasError = true;
            this.Exception = exception;
        }
    }
 };
 

/*******************************************************************************************************************
 * Common Functions
 *******************************************************************************************************************/ 
 // This is a simple function to encapsulate the Ajax call - AjaxRequest.ashx points to AjaxRequestHandler.cs (See Web Config)
 function DoAjaxAction(parameters, callback)
 {
    new Ajax.Request("AjaxRequest.ashx",
        {
            method: "get",
            parameters: parameters,
            onSuccess: function (transport) {
            //            alert(transport.responseText); //- Result of the Call
                callback(new AjaxResponse(transport.responseText));
            },
            onFailure: function(transport) {
                alert("DoAjaxAction - Failure");
                callback(new AjaxResponse(transport.responseText, new AjaxRequestException(transport.responseText)));
            }
        }
    );  
 }