﻿
function GetCartList() {

    jQuery.ajax({
        type: 'Post',
        url: "/WebServices/FileListMethods.aspx/GetCartListHtml",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Content-type",
                         "application/json; charset=utf-8");
        },
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(response) {

            var html = response.d;
            if (html.length > 0) {
                jQuery('#divCart').html(html);

            }
            else {
                jQuery("#carts-list-container").hide();
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            jQuery("#carts-list-container").html("error");
        }
    });
};
function GetCartListForDropDown() {

    jQuery.ajax({
        type: 'Post',
        url: "/WebServices/FileListMethods.aspx/GetCartListDropDownOptions",
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Content-type",
                         "application/json; charset=utf-8");
        },
        success: function(response) {
            var html = response.d;
            if (html.length > 0) {
                jQuery('.ddExistingCarts').html(html);
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {


        }
    });
};
function deleteCart(cartId, cartName) {
    if (confirm('Are you sure you want to delete ' + cartName + '?') == true) {
        __doPostBack('CartDeleted', cartId);

    }

};

function deleteSearchCart(searchid, searchname) {
    if (confirm('Are you sure you want to delete ' + searchname + '?') == true) {
        __doPostBack('SearchDeleted', searchid);

    }
};

function GetSearchListForDropDown() {

    jQuery.ajax({
        type: 'Post',
        url: "/WebServices/FileListMethods.aspx/GetSearchListDropDownOptions",
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Content-type",
                         "application/json; charset=utf-8");
        },
        success: function(response) {
            var html = response.d;
            if (html.length > 0) {
                jQuery('.ddlExistingSearch').html(html);
                if (jQuery(".ddlExistingSearch option").size() > 0) {
                    jQuery(".ddlExistingSearch option:first");
                    jQuery("#saveExistingSearch").attr("checked", true);
                }
                else {
                    jQuery("#saveNewSearch").attr("checked", true);
                    jQuery(".saveExistingSearchPanel").hide();
                }
            }
            else {
                jQuery(".saveExistingSearchPanel").hide();
                jQuery("#saveNewSearch").attr("checked", true);
                toggleSearchClick();
            }

        },
        error: function(xhr, ajaxOptions, thrownError) {
            //  alert("error");
        }
    });
};
function GetSearchList() {

    jQuery.ajax({
        type: 'Post',
        url: "/WebServices/FileListMethods.aspx/GetSearchListHTML",
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Content-type",
                         "application/json; charset=utf-8");
        },
        success: function(response) {
            var html = response.d;
            if (html.length > 0) {
                jQuery('#divSearch').html(html);



            }
            else {
                jQuery("#searches-list-container").hide();
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            // alert("error");
        }
    });
};

function swapCartSearches(listToShow) {
   
    jQuery('#carts-btn').removeClass('carts-btn-active');
    jQuery('#searches-btn').removeClass('searches-btn-active');
    jQuery('.right-nav-list').hide();
    jQuery('#' + listToShow + "-list-container").show();
    jQuery('#'+listToShow+ '-btn').addClass(listToShow+'-btn-active');
};

var popupStatus = 0;


function loadCartPopup() {

    if (jQuery(".ddExistingCarts option").size() > 0) {
        jQuery(".ddExistingCarts option:first");
        jQuery("#AddToExisting").attr('checked', true);
    }
    else {
        jQuery("#AddToCartContentAdd").hide()
        jQuery("#AddToNew").attr('checked', true);
    }
    toggleCartClick();
    jQuery('#txtNewCartName').val("");
    jQuery("#cartError").html("");


    jQuery("#popup").modal()



};


function toggleCartClick() {

    str = jQuery("input[name='cartAddType']:checked").val();
    if (str == 'new') {
        jQuery('#AddToCartContentCreateNew').show();
        jQuery('#txtNewCartName').val('');
    }
    else {
        jQuery('#AddToCartContentCreateNew').hide();
    }

};

function toggleSearchClick() {

    str = jQuery("input[name='saveSearchType']:checked").val();

    if (str == 'new') {
        jQuery('#saveNewSearchPanel').show();
        jQuery('#txtNewSearchName').val('');
    }
    else {
        jQuery('#saveNewSearchPanel').hide();
    }

}


// adds the selected asset to cart by cart id.  if cartId is zero, creates a new cart
function ModifyOrCreateCart() {
    str = jQuery("input[name='cartAddType']:checked").val();
    var isValid = true;

    if (str == 'new') {
        isValid = ValidateNewCart();
        if (isValid == true) {
            cartid = 0;
            cartname = jQuery('#txtNewCartName').val();
        }
    }
    else {

        var ddcarts = jQuery("select[id$='_ddExistingCarts']");
        
        cartid = ddcarts.val();
        cartname = ddcarts.find('option').filter(':selected').text();
        
    }
    if (isValid == true) {

        AddAssetsToCart(cartid, cartname);
    }
};
function AddAssetsToCart(cartid, cartname) {
    var parms = '{cart:\'' + cartid + '\',cartname:\'' + cartname + '\',assetlist:\'' + GenerateAssets() + '\'}';

    jQuery.ajax({
        type: 'Post',
        url: "/WebServices/FileListMethods.aspx/AddAssetsToCart",
        data: parms,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (response) {

       
            if (jQuery(".ddlExistingCarts option").size() + jQuery(".ddlExistingSearch option").size() > 0) {
                refreshCartsAndSearches();
                jQuery.modal.close();

            }
            else {

                refreshCartsAndSearches();
            }
            alert("Selected files added to cart");

        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(jQuery("errrr"));
        }
    });
};
function SaveSearch(searchName, searchid) {
    var parms = '{searchName:\'' + searchName + '\',searchid:\'' + searchid + '\'}';

    jQuery.ajax({
        type: 'Post',
        url: "/WebServices/FileListMethods.aspx/SaveSearch",
        data: parms,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(response) {
            alert("Search Saved");
            if (jQuery(".ddlExistingCarts option").size() + jQuery(".ddlExistingSearch option").size() > 0) {
                GetSearchList();
                GetSearchListForDropDown();
                jQuery.modal.close();
            }
            else {
                refreshCartsAndSearches();
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert("An error occured, please try again");
        }
    });
}


function ValidateNewCart() {
    var cartname = jQuery('#txtNewCartName').val().toLowerCase();
    if (cartname.length == 0) {
        jQuery("#cartError").html("Please enter a name for your new cart, or select an existing cart.");
        return false;
    }
    var exists = 0;

    jQuery(".ddExistingCarts option").each(function() {
        newcart = jQuery(this).text().toLowerCase()
        if (newcart == cartname) {
            exists = 1;
            jQuery(".ddExistingCarts").val(jQuery(this).val());
            jQuery("#AddToExisting").attr("checked", true);
            toggleCartClick();
            jQuery('#txtNewCartName').val("");
            jQuery("#cartError").html("A cart with this name already exists");
        }

    });

    if (exists == 1) {

        return false;
    }
    return true;

}

function GenerateAssets() {
    var assetListXml = '<assets>';
    if (g_selectedAssetid == 0) {
        jQuery(".assetCheck").each(function() {
            if (this.checked) {
                var assetid = jQuery(this).attr('ID');
                assetid = assetid.substring(assetid.lastIndexOf('_') + 1, assetid.length);
                assetListXml = assetListXml + '<asset>' + assetid + '</asset>';
            }

        });
    }
    else {
        assetListXml = assetListXml + '<asset>' + g_selectedAssetid + '</asset>';
    }

    return assetListXml = assetListXml + '</assets>';

}
// Search
// performs validations for a new search
function ValidateNewSearch(searchname) {
    var exists = 0;
    var searchName = jQuery('#txtNewSearchName').val().toLowerCase();
    if (searchName.length < 1) {
        jQuery("#saveSearchError").html("Please enter a name for your new search, or select an existing search.");
        return false;

    }
    else {
        jQuery(".ddlExistingSearch option").each(function() {
            newcart = jQuery(this).text().toLowerCase()
            if (newcart == searchname) {
                exists = 1;
                jQuery(".ddlExistingSearch").val(jQuery(this).val());
                jQuery("#saveExistingSearch").attr("checked", true);
                toggleSearchClick();
                jQuery('#txtNewSearchName').val("");
                jQuery("#saveSearchError").html("A search with this name already exists");
            }

        });
    }

    if (exists == 1) {
        return false;
    }
    return true;
};



// overwrite a saved search with the current search
function ModifyOrCreateSearch() {
    var isValid = true;
    var searchname = "";
    var searchid = 0;
    var str = jQuery("input[name='saveSearchType']:checked").val();

    if (str == 'new') {

        searchname = jQuery("#divSaveSearch .addSearch").getValue();
        isValid = ValidateNewSearch(searchname);


    }
    else {
        var ddsearches = jQuery("select[id$='_ddlExistingSearch']");

        searchid = ddsearches.val();
        searchname = ddsearches.find('option').filter(':selected').text();
        //searchname = jQuery(".ddlExistingSearch option:selected").text();


    }
    if (isValid)
        SaveSearch(searchname, searchid);


};

function loadSaveSearchPopup() {
    GetSearchListForDropDown();
    toggleSearchClick();
};

function loadModifySearchPopup() {
    GetSearchShows();

};

function ShowPleaseWait() {
    var WaitBox = jQuery("#pleasewait");

    WaitBox.show();
    AddDot(WaitBox);

};

function HidePleaseWait() {
    var WaitBox = jQuery("#pleasewait");
    if (WaitBox) { WaitBox.style.display = "none"; }
};


function AddDot(WaitBox) {
    WaitBox.append('.');
    WaitBox.html(WaitBox.html().replace('....', ''));

    setTimeout(function() {
        AddDot(WaitBox);
    }, 1000);

};




