﻿
function AttemptPurchase(ItemType, ItemID, price) {
    var showBank = false;
    var showCash = false;
    price = GlobalToFloat(price);
    if (GlobalGetBank() >= price) {
        $('#JsBankOption').attr('checked', 'checked');
        showBank = true;
    }
    if (GlobalGetCash() >= price) {
        $('#JsCashOption').attr('checked', 'checked');
        showCash = true;
    }
    if (showBank && showCash) {
        $('#JsBankOption').attr('checked', 'checked');
        ConfirmPurchase(ItemType, ItemID, price);
        $('#PaymentOptions').show();
    } else if (!showBank && !showCash) {
        ShowNoFundsMessage();
    } else {
        ConfirmPurchase(ItemType, ItemID, price);
    }
}

function ConfirmPurchase(ItemType, ItemID, price) {
    $('#UxConfirmPayment').dialog({
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog('close');

                var useBank = false;
                if ($('#JsBankOption:checked').val() == 'bank') {
                    useBank = true;
                }

                switch (ItemType) {
                    case 'vehicleFromShop': PurchaseAjax('PurchaseItem', "{'itemType':'" + ItemType + "', 'itemID':" + ItemID + ",'useBank' :" + useBank + "}", PurchaseVehicleSucces); break;
                    case 'StandardBuilding': PurchaseAjax('PurchaseItem', "{'itemType':'" + ItemType + "', 'itemID':" + ItemID + ",'useBank' :" + useBank + "}", PurchaseBuildingSucces); break;
                    default: throw "Unrecognised item";
                }
            },
            Cancel: function () {
                $(this).dialog('close');
            }
        }
    });
}

function PurchaseAjax(serviceAddress, jsonData, SuccessCallback) {
    $.ajax({ type: "POST", url: "WebService.asmx/" + serviceAddress, contentType: "application/json; charset=utf-8",
        data: jsonData,
        dataType: "json",
        success: SuccessCallback,
        error: PurchaseFailure
    });
}

//Building Specific
function PurchaseBuildingSucces(result) {
    if (result.d.Href != "") {
        var br = GlobalFindObjectWithBuildingIdInArray(BUILDING_ID_OF_INTREST, LAST_BUILDINGS);
        GlobalShowGenericDialog('New Building Aquired!', '<p>Congratulations, you now own \'' + br.Title + '\'</p><br /><center><a class="button" href="' + result.d.Href + '">View</a></center>', { Close: function () { $(this).dialog('close'); }, "Use New Vehicle": function () { window.location = result.d.Href; } });
        GlobalUpdateCash(result.d.Cash);
        GlobalUpdateBank(result.d.Bank);
        SlideItUp();
    } else {
        ShowResultProblemMessage(result.d.FailureString, "Unable to Complete Transaction");
    }
}

//Vehicle Specific
function PurchaseVehicleSucces(result) {
    if (result.d.Href != "") {
        GlobalShowGenericDialog('New Vehicle Aquired!', '<p>Congratulations, your new vehicle is outside<br /><br /><center><a class="button" href="' + result.d.Href + '">Use vehicle</a></p>', { Close: function () { $(this).dialog('close'); }, "Use New Vehicle": function () { window.location = result.d.Href; } });
        GlobalUpdateCash(result.d.Cash);
        GlobalUpdateBank(result.d.Bank);
        SlideItUp();
    } else {
        ShowResultProblemMessage(result.d.FailureString, "Unable to Complete Transaction");
    }
}

// Generic Purchase Messages
function ShowNoFundsMessage() {GlobalShowGenericDialog('Unable to Proceed','<br />Insufficient funds<br /><br />Get more by <a href="Purchase-Credit-and-Cash.aspx">clicking here</a><br />');}
function ShowResultProblemMessage(msg, title) {GlobalShowGenericDialog(title,'<br />' + msg + '<br />');}

// Utilities
function PurchaseFailure(result) {
    x = result.d;
    debugger;
    GlobalShowGenericDialog('Error','<br />Unfortunately this item cannot be purchased as there is a problem with code. Let us know and we\'ll fix it ASAP.');
}


