﻿$(document).ready(function() {
$("#DownloadNowButton").click(function(event) {
if ($('.add-to-cart-disclaimer').is(':visible') == true) {
    $(".message").text('You must check the confirmation box to download');
    $(".message").fadeIn(300).delay(700).hide(0, function() {
        $(parent.document).find('.ui-dialog');

    })
}
        if ($('.add-to-cart-disclaimer').is(':hidden') == true) {
            $(".add-to-cart-disclaimer").toggle();
        }
      
        if ($('#ContinueBrowsing').is(':visible') == true) {
            $("#ContinueBrowsing").toggle();
        }
    });
})



$(document).ready(function() {
$("#disclaimerid").click(function(event) {


    $(".termsofServiceClass").toggle();


        
        
    });
})

$(document).ready(function() {
    $("#DisclaimerCheckBox").click(function() {
        var thisCheck = $(this);
        if (thisCheck.is(':checked')) {
          
            if ($('#AjaxLoader').is(':hidden') == true) {
                $("#AjaxLoader").toggle();
            }
            var webMethod = $("#WSUrlHiddenField").val();
            var collID = $("#CollectionDropDownList").val();
            var AssetID = $("#AssetIDsHiddenField").val();
            var UserId = $("#UserIDHiddenField").val();
            var NewCollection = new Object();
            NewCollection.ID = -1;
            var eDownloadType = $("#DownloadTypeHiddenField").val();
            NewCollection.Title = $("#CollectionTitleTextBox").val();
            NewCollection.Description = $("#CollectionDescriptionTextBox").val();
            var o_userId = UserId;

            $.ajax({
                type: "POST",
                url: webMethod + '/Collection_AddUserDownload',
                data: '{ "nUserID":' + UserId + ', "fType":' + eDownloadType + '' + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: DownloadProcessStartSuccess,
                error: DownloadProcessStartError
            });
        }
    });

});

function DownloadProcessStartSuccess(data) {

    var webMethod = $("#WSUrlHiddenField").val();
    var collID = $("#CollectionDropDownList").val();
    var AssetID = $("#AssetIDsHiddenField").val();
    var UserId = $("#UserIDHiddenField").val();
    var NewCollection = new Object();
    NewCollection.ID = -1;
    var eDownloadType = $("#DownloadTypeHiddenField").val();
    var n_download_id;
//    if (redownload == true) {
//        n_download_id = data;
//    }
//    else {
        n_download_id = data.d;
//    }
        
    var o_userId = UserId;
    var a_asset_id = '[' + AssetID + ']';
    $.ajax({
        type: "POST",
        url: webMethod + '/Collection_DownloadAssets',
        data: '{ "aAssetUnitIDs":' + a_asset_id + ', "nUserID":' + o_userId + ', "nUserDownloadID":' + n_download_id + ', "bSaveAsCollection":' + false + '' + '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function() {
        StartDownloadSuccess(webMethod,n_download_id)
        },
        error: StartDownloadError
    });


}


function StartDownloadSuccess(wsurl, n_download_id) {

    var o_download_id = n_download_id;
    CheckDownloadStatus(wsurl,o_download_id);

}
function StartDownloadError(data, status) {

}

function DownloadProcessStartError(data, status) {



}

function CheckDownloadStatus(wsurl,n_download_id) {

    var o_download_id = n_download_id;
    $.ajax({
        type: "POST",
        url: wsurl + '/Collection_GetDownloadStatus',
        data: '{ "nDownloadID":' + o_download_id + '' + '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            CheckDownloadStatusSuccess(data, o_download_id,wsurl)
        },
        error: CheckDownloadStatusError
    });
}
function CheckDownloadStatusSuccess(status, n_download_id,wsurl) {
    var s_status = status.d;
    var o_download_id = n_download_id;
    //alert(s_status);
    if (s_status == 'Processed') {

        GetAssetUnitByID(wsurl,o_download_id);
    }
    else {
        setTimeout('CheckDownloadStatus(wsurl,o_download_id)', 2000);
    }
}

function CheckDownloadStatusError(data) {
   
}

function GetAssetUnitByID(wsurl,asset_unit_id) {
    var collID = $("#CollectionDropDownList").val();
    var AssetID = $("#AssetIDsHiddenField").val();
    var UserId = $("#UserIDHiddenField").val();
    var NewCollection = new Object();
    NewCollection.ID = -1;
    var eDownloadType = $("#DownloadTypeHiddenField").val();
    var o_asset_unit_id = asset_unit_id;
    var o_userId = UserId;
    var a_asset_id = '[' + AssetID + ']';
    $.ajax({
        type: "POST",
        url: wsurl + '/AssetUnit_GetAssetUnitByID',
        data: '{ "nAssetID":' + o_asset_unit_id + ', "bReturnBase":' + false + '' + '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: GetAssetUnitByIDSuccess,
        error: GetAssetUnitByIDError
    });
}
function GetAssetUnitByIDSuccess(data) {
    
    var UserId;
    if ($("#UserIDHiddenField").val() != null) {
        UserId = $("#UserIDHiddenField").val();
    }
    else if ($("[id$='UserIDHiddenField']").val() != null) {
        UserId = $("[id$='UserIDHiddenField']").val();
    }
    var o_userId = UserId;
    $("#AjaxLoader").toggle();
    window.parent.$('.userActivityDialog').dialog('close');
    window.location.href = "Download/" + o_userId + "/" + data.d.FileName;
}
function GetAssetUnitByIDError(data) {

}

$(document).ready(function() {

    $("#DownloadLaterButton").click(function(event) {
        $("#AjaxLoader").toggle();
        var webMethod = $("#WSUrlHiddenField").val();
        var collID = $("#CollectionDropDownList").val();
        var AssetID = $("#AssetIDsHiddenField").val();
        var UserId = $("#UserIDHiddenField").val();
        var o_unit_file_size = $("#AssetUnitFileSizeHiddenField").val();
        var o_file_size_limit = $("#CollectionAndCartFileSizeHiddenField").val();

        var a_asset_id = '[' + AssetID + ']';
        var o_userId = UserId;

        $.ajax({
            type: "POST",
            url: webMethod + '/Collection_GetUserCart',
            data: '{"nUserID":' + o_userId + '' + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                GetUserCartSuccess(data, o_userId, a_asset_id, o_file_size_limit, o_unit_file_size)
            },
            error: GetUserCartError
        });
    });

    function GetUserCartSuccess(data, n_user_id, n_asset_id, n_file_size_limit, n_unit_file_size) {
        var o_user_id = n_user_id;
        var webMethod = $("#WSUrlHiddenField").val();
        var collID = $("#CollectionDropDownList").val();
        var AssetID = $("#AssetIDsHiddenField").val();
        var UserId = $("#UserIDHiddenField").val();
        var a_asset_id = '[' + AssetID + ']';

        var o_userId = UserId;
        var o_total_file_size = data.d.UnitFileSize + parseInt(n_unit_file_size);
        if (o_total_file_size > parseInt(n_file_size_limit * 1000 * 1000)) {
            //alert("Limit reached");
                $(".message").text('You have reached the download file size limit.');
                $(".message").fadeIn(300).delay(1000).show(0, function() {
                        //window.parent.$('.userActivityDialog').dialog('close');
                })
        }
        else {
            $.ajax({
                type: "POST",
                url: webMethod + '/Collection_AddAssetUnitsToCart',
                data: '{ "aAssetUnitIDs":' + a_asset_id + ', "nUserID":' + o_userId + '' + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    AddToCartSuccess(data, o_userId)
                },
                error: AddToCartError
            });
        }

    }
    function GetUserCartError(data) {
        //alert(data.d);
    }


    function AddToCartSuccess(data, n_user_id) {
        var o_user_id = n_user_id;
        var webMethod = $("#WSUrlHiddenField").val();
        var collID = $("#CollectionDropDownList").val();
        var AssetID = $("#AssetIDsHiddenField").val();
        var UserId = $("#UserIDHiddenField").val();
        var a_asset_id = '[' + AssetID + ']';
        var o_userId = UserId;

        $.ajax({
            type: "POST",
            url: webMethod + '/Collection_GetUserCartItemCount',
            data: '{ "nUserID":' + o_user_id + '' + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: GetCartItemCountSuccess,
            error: GetCartItemCountError
        });

    }

    function AddToCartError(data) {
        //alert(data.d);
    }

    function GetCartItemCountSuccess(data) {
        var o_cart_item_count = data.d;
        if ($('#AjaxLoader').is(':visible') == true) {
            $("#AjaxLoader").toggle();
        }
        if ($('.add-to-cart-disclaimer').is(':visible') == true) {
            $(".add-to-cart-disclaimer").toggle();
        }
        $(".message").text('File successfully added');
        $("#ContinueBrowsing").show();
        $('.cart-summary-details').text('Cart Summary:' + o_cart_item_count + ' item(s)');
    }

    function GetCartItemCountError(data) {

        alert(data.d);
    }

});

//Download from download manager



$(document).ready(function() {
    $(".cart-bttn-checkout").click(function(event) {
        //if ($("#disclaimer").find(':checkbox').is(':checked')) {
        if (true) {

            if ($("[id$='DisclaimerCheckBox']").is(':checked') == false) {
                $("#dialog-message").text("Please confirm that you read the Disclaimer and Terms and Conditions for Use.");
                $("#dialog-message").dialog({
                    modal: true,
                    buttons: {
                        Ok: function() {
                            $(this).dialog("close");
                        }
                    }
                });
            }
            else {
                $("#AjaxLoader").toggle();

                var o_s_wsurl = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(0)").val();
                var o_a_download_ids = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(1)").val();
                var o_n_user_id = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(2)").val();
                var o_e_download_type = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(3)").val();

                $.ajax({
                    type: "POST",
                    url: o_s_wsurl + '/Collection_AddUserDownload',
                    data: '{ "nUserID":' + o_n_user_id + ', "fType":' + o_e_download_type + '' + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: DownloadAllProcessStartSuccess,
                    error: DownloadAllProcessStartError
                });
            }
            
        }
        else {
            $("#dialog-message").dialog({
                modal: true,
                buttons: {
                    Ok: function() {
                        $(this).dialog("close");
                    }
                }
            });

            //alert("Please confirm that you read the Disclaimer and Terms and Conditions for Use");
        }
    });
});


function DownloadAllProcessStartSuccess(data, status) {

    var o_s_wsurl = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(0)").val();
    var o_a_download_ids = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(1)").val();
    var o_n_user_id = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(2)").val();
    var o_e_download_type = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(3)").val();
    var n_download_id = data.d;
    var a_asset_id = '[' + o_a_download_ids + ']';
    $.ajax({
        type: "POST",
        url: o_s_wsurl + '/Collection_DownloadAssets',
        data: '{ "aAssetUnitIDs":' + a_asset_id + ', "nUserID":' + o_n_user_id + ', "nUserDownloadID":' + n_download_id + ', "bSaveAsCollection":' + false + '' + '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            StartDownloadAllSuccess(data, n_download_id)
        },
        error: StartDownloadAllError
    });

}
function DownloadAllProcessStartError(data, status) {


}

function StartDownloadAllSuccess(data, n_download_id) {

    var o_download_id = n_download_id;
    CheckDownloadAllStatus(o_download_id);

}
function StartDownloadAllError(data, status) {

}
function CheckDownloadAllStatus(n_download_id) {

    var o_s_wsurl = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(0)").val();
    var o_a_download_ids = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(1)").val();
    var o_n_user_id = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(2)").val();
    var o_e_download_type = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(3)").val();

    var o_download_id = n_download_id;
    $.ajax({
        type: "POST",
        url: o_s_wsurl + '/Collection_GetDownloadStatus',
        data: '{ "nDownloadID":' + o_download_id + '' + '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            CheckDownloadAllStatusSuccess(data, o_download_id)
        },
        error: StartDownloadAllError
    });
}

function CheckDownloadAllStatusSuccess(status, n_download_id) {
    var s_status = status.d;
    var o_download_id = n_download_id;
    //alert(s_status);
    if (s_status == 'Processed') {

        GetAllAssetUnitByID(o_download_id);
    }
    else {
        setTimeout('CheckDownloadAllStatus(o_download_id)', 2000);
    }
}

function StartDownloadAllError(data) {

}

function GetAllAssetUnitByID(asset_unit_id) {
    var o_s_wsurl = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(0)").val();
    var o_a_download_ids = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(1)").val();
    var o_n_user_id = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(2)").val();
    var o_e_download_type = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(3)").val();

    var o_asset_unit_id = asset_unit_id;
    $.ajax({
        type: "POST",
        url: o_s_wsurl + '/AssetUnit_GetAssetUnitByID',
        data: '{ "nAssetID":' + o_asset_unit_id + ', "bReturnBase":' + false + '' + '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: GetAllAssetUnitByIDSuccess,
        error: GetAllAssetUnitByIDError
    });
}
function GetAllAssetUnitByIDSuccess(data) {
    var o_n_user_id = $("#DownloadAjaxHiddenValues > input[type='hidden']:eq(2)").val();

    $("#AjaxLoader").toggle();
    setTimeout('location.reload()', 500);
    window.location.href = "Download/" + o_n_user_id + "/" + data.d.FileName;

}
function GetAllAssetUnitByIDError(data) {

}

//Redownload

$(document).ready(function() {
    $(".reprocessdownload").click(function() {
        var downloadID = this.id;
        if (downloadID != null) {
            Redownload(downloadID);
        }
    });
});

function Redownload(downloadid) {
        $("#AjaxLoader").toggle();
        var webMethod = $("[id$='WSUrlHiddenField']").val();
        var UserId = $("[id$='UserIDHiddenField']").val();
        var o_userId = UserId;
        
        $.ajax({
            type: "POST",
            url: webMethod + '/Collection_RegenerateDownload',
            data: '{ "nUserDownloadID":' + downloadid + ', "nUserID":' + o_userId + '' + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
            RedownloadSuccess(data, downloadid)
            },
            error: RedownloadError
        });
}

function RedownloadSuccess(data, downloadid) {
    var webMethod = $("[id$='WSUrlHiddenField']").val();
    CheckDownloadStatus(webMethod,downloadid);

}
function RedownloadError(data) {

}

