﻿
$(document).ready(function() {
    initCart();
});

    var expandedSearchId;
    var cartProductToRemove;
    var cartSearchToRemove;
    
    function confirmCartRemoveProduct(id) {
        cartProductToRemove = id;
        $('#dialogConfirmCartRemoveProduct').dialog('open');
    }
    function confirmCartRemoveSearch(id) {
        cartSearchToRemove = id;
        $('#dialogConfirmCartRemoveSearch').dialog('open');
    }

    function editSearch(id) {
        StartLoading();
        
        // get edit form partial
        $.ajax({
            type: "POST",
            url: "Search/Edit/" + id,
            success: function(html) {
                $('#divSearchContainer').html("");
                $('#divSearchContainer').html(html);
                StopLoading();
            },
            error: function(msg) {
                alert('An error occurred');
                StopLoading();
            }
        });
    }

    function initCart() {
        UpdateMessageCenter();
       // $("#divCartContainer img[title]").tooltip('#proddesctip');
        
        // initialize confirm dialog for removing product from cart
        $("#dialogConfirmCartRemoveProduct").dialog({
            bgiframe: true,
            resizable: false,
            height: 140,
            modal: true,
            autoOpen: false,
            overlay: {
                backgroundColor: '#000',
                opacity: 0.5
            },
            buttons: {
                'Remove Product': function() {
                    $(this).dialog('close');
                    StartLoading();
                    $.ajax({
                        type: "POST",
                        url: "Cart/RemoveProduct/" + cartProductToRemove,
                        success: function(html) {
                            $('#divCartContainer').html("");
                            $('#divCartContainer').html(html);
                            initCart();
                            StopLoading();
                        },
                        error: function(msg) {
                            StopLoading();
                            alert('An error occurred while refreshing the data');
                        }
                    });

                },
                Cancel: function() {
                    $(this).dialog('close');
                    return false;
                }
            }
        });
        // initialize confirm dialog for removing search from cart
        $("#dialogConfirmCartRemoveSearch").dialog({
            bgiframe: true,
            resizable: false,
            height: 140,
            modal: true,
            autoOpen: false,
            overlay: {
                backgroundColor: '#000',
                opacity: 0.5
            },
            buttons: {
                'Remove Search': function() {
                    StartLoading();
                    $(this).dialog('close');
                    $.ajax({
                        type: "POST",
                        url: rootURL + "Cart/RemoveApplicant/" + cartSearchToRemove,
                        success: function(html) {
                            $('#divCartContainer').html("")
                            $('#divCartContainer').html(html);
                            initCart();
                            StopLoading();
                        },
                        error: function(msg) {
                            StopLoading();
                            alert(msg);
                        }
                    });
                },
                Cancel: function() {
                    $(this).dialog('close');
                }
            }
        });

        $('.DeleteProduct').click(function() {
            $('#dialogConfirmCartRemoveProduct').dialog('open');
        });

       // $("#tblSearchCart").jExpand();

        
    }
    

