/// <reference path="jquery.js" />
/// <reference path="json2.js" />

// *** Service Calling Proxy Class
function serviceProxy(serviceUrl)
{
    var _I = this;
    this.serviceUrl = serviceUrl;
 
    // *** Call a wrapped object
    this.invoke = function(method,data,callback,error,bare)
    {
        // *** Convert input data into JSON - REQUIRES Json2.js
        var json = JSON2.stringify(data); 
        // *** The service endpoint URL        
        var url = _I.serviceUrl + method;
 
        $.ajax( { 
                    url: url,
                    data: json,
                    type: "POST",
                    processData: false,
                    contentType: "application/json",
                    timeout: 60000,
                    dataType: "text",  // not "json" we'll parse
                    success: 
                    function(res) 
                    {              
                        if (!callback) return;
                        try
                        {
                            var result = JSON2.parse(res);
                            if (bare)
                            {
                                callback(result); 
                                return; 
                            }
                            for(var property in result)
                            {
                                callback( result[property] );
                                break;
                            }
                        }
                        catch(ex)
                        {
                            if(error) 
                            {
                                error(ex);
                            }
                        }                    
                    },
                    error:  function(xhr) {
                        //alert(xhr.responseText);
                        if (!error) return;
                        try
                        {
                            if (xhr.responseText)
                            {
                                var err = JSON2.parse(xhr.responseText);
                                if (err)
                                    error(err); 
                                else    
                                    error( { Message: "Unknown server error." })
                            }
                            else
                            {
                                error( { Message: "No response received, possible timeout" });
                            }
                   
                        }
                        catch(ex)
                        {
                            error(ex);
                            return;
                        }
                    }
                });   
    }
}

// user service proxy
var userProxy = {

    path: "svc/User.svc/",

    init: function(url) {
        this.proxy = new serviceProxy(url + this.path);
    },

    login: function(email, password, token, optin, f_result) {
        this.proxy.invoke("Login", { "email": email, "password": password, "token": token, "optin": optin }, f_result, this.error);
    },
    logout: function(f_result) {
        this.proxy.invoke("Logout", {}, f_result, this.error);
    },
    changeParentPassword: function(email, oldpassword, newpassword, f_result) {
        this.proxy.invoke("ChangeParentPassword", { "email": email, "oldpassword": oldpassword, "newpassword": newpassword }, f_result, this.error);
    },
    changeChildPassword: function(userid, newpassword, f_result) {
        this.proxy.invoke("ChangeChildPassword", { "userid": userid, "newpassword": newpassword }, f_result, this.error);
    },
    getAccountDetail: function(f_result) {
        this.proxy.invoke("GetAccountDetail", {}, f_result, this.error);
    },
    setChildParentalControls: function(child, f_result) {
        this.proxy.invoke("SetChildParentalControls", { "child": child }, f_result, this.error);
    },
    requestChildTransfer: function(username, password, f_result) {
        this.proxy.invoke("RequestChildTransfer", { "username": username, "password": password }, f_result, this.error);
    },
    acceptChildTransfer: function(email, password, token, f_result) {
        this.proxy.invoke("AcceptChildTransfer", { "email": email, "password": password, "token": token }, f_result, this.error);
    },
    getChildPaymentHistory: function(userid, f_result) {
        this.proxy.invoke("GetChildPaymentHistory", { "childUserId": userid }, f_result, this.error);
    },
    cancelChildSubscription: function(userid, f_result) {
        this.proxy.invoke("CancelChildSubscription", { "userid": userid }, f_result, this.error);
    },
    createParentAccount: function(email, locale, password, birthdate, token, optin, f_result) {
        this.proxy.invoke("CreateParentAccount", { "email": email, "locale": locale, "password":password, "birthDate": birthdate, "token": token, "optin": optin}, f_result, this.error);
    },
    activateParentEmail: function(email, token, f_result) {
        this.proxy.invoke("ActivateParentEmail", { "email": email, "token": token }, f_result, this.error);
    },
    activateChildAccount: function(username, password, f_result) {
        this.proxy.invoke("ActivateChildAccount", { "username": username, "password": password }, f_result, this.error);
    },
    deactivateChildAccount: function(username, password, f_result) {
        this.proxy.invoke("DeactivateChildAccount", { "username": username, "password": password }, f_result, this.error);
    },
    resetChildPassword: function(token, username, password, f_result) {
        this.proxy.invoke("ResetChildPassword", { "token": token, "username": username, "password": password }, f_result, this.error);
    },
    requestPasswordReset: function(username, f_result) {
        this.proxy.invoke("RequestPasswordReset", { "username": username }, f_result, this.error);
    },
    error: function(err) {
        //alert(JSON2.stringify(err));
    }
};

// subscription service proxy
var subscriptionProxy = {

    path: "svc/Subscription.svc/",

    init: function(url) {
        this.proxy = new serviceProxy(url + this.path);
    },

    getSubscriptionDetail: function(f_result) {
        this.proxy.invoke("GetSubscriptionDetail", {}, f_result, this.error);
    },
    maintainAccount: function(acct, f_result) {
        this.proxy.invoke("MaintainAccount", { maintAccount: acct }, f_result, this.error);
    },
    maintainAddress: function(addr, f_result) {
        this.proxy.invoke("MaintainAddress", { maintAddress: addr }, f_result, this.error);
    },
    quoteInvoice: function(billingid, productid, f_result) {
        this.proxy.invoke("QuoteInvoice", { billingAccountId: billingid, productId: productid }, f_result, this.error);
    },
    purchaseSubscription: function(childid, billingid, productid, f_result) {
        this.proxy.invoke("PurchaseSubscription", { childAccountId: childid, billingAccountId: billingid, productId: productid }, f_result, this.error);
    },
    deleteAccount: function(billingid, f_result) {
        this.proxy.invoke("DeleteAccount", { billingAccountId: billingid }, f_result, this.error);
    },
    deleteAddress: function(addressid, f_result) {
        this.proxy.invoke("DeleteAddress", { addressId: addressid }, f_result, this.error);
    },
    getCheckoutUrl: function(childid, productid, f_result) {
        this.proxy.invoke("GetCheckoutUrl", { childAccountId: childid, productId: productid }, f_result, this.error);
    },
    error: function(err) {
        //alert(JSON2.stringify(err));
    }
};

// preference service proxy
var preferenceProxy = {

    path: "svc/Preference.svc/",

    init: function(url) {
        this.proxy = new serviceProxy(url + this.path);
    },

    newsletterSignup: function(emailaddress, country, birthdate, globaloptin, thirdpartyoptin, parentalemail, f_result) {
        this.proxy.invoke("NewsletterSignup", { "emailaddress": emailaddress, "country": country, "birthdate": birthdate, "globaloptin": globaloptin, "thirdpartyoptin": thirdpartyoptin, "parentalemail": parentalemail }, f_result, this.error);
    },
    geoIpCheck: function(locale, f_result) {
        this.proxy.invoke("GeoIpCheck", { "locale": locale }, f_result, this.error);
    },
    error: function(err) {
        //alert(JSON2.stringify(err));
    }
};
