﻿
// Product Grid or List Pages

$(document).ready(function () {

    $('.btnselectpb').click(function () {
        SelectPB();
    });

    selectProjectBuilderChange();
});

function addItem(pid, bSetPB) {
    if (pid > 0)
        $('#pbaction').val("add");
    else
        $('#pbaction').val("set");

    var did = $('#didpopupmenu').val();
    var cpbid = $('#cpbid').val();

    // Need to log in
    if (did > 0) {
        $('#pid').val(pid);

        // Current Project Builder ID
        if (cpbid > 0 && (!bSetPB || bSetPB == false)) {
            setPBItem("add");

            return false;
        } else {
            // Popup dialog
            $("#dialog-form").modal({
                position: [57, ],
                onShow: function (dialog) {
                    $(".processingbar").slideUp('fast', function () {
                        $(".projectbuilders").slideDown('fast');
                    });
                    $("#simplemodal-container").css("height", "auto");
                },
                maxHeight: 400
            });
            return false;
        }
    }
    else {
        if ($("#loginbox").length > 0) {
            //run the effect
            $("#loginbox").slideToggle('fast');

            //scroll to top
            window.scrollTo(0, 0);

            return false;
        }
    }
}

function SelectPB() {

    var action = $('#pbaction').val();
    if (!action || action == '')
        action = "set";

    setPBItem(action, $("#txtNewPBName").val(), true);

    return false;
}

function removeItem(pid) {
    $('#pid').val(pid);

    setPBItem("delete");
}

function selectProjectBuilderChange() {
    $("input[name='PBItemSelect']").change(function () {
        if ($("input[name='PBItemSelect']:checked").val() == '0') {

            $("#txtNewPBName").removeAttr('disabled');
            $("#txtNewPBName").focus();

        } else {
            $("#txtNewPBName").attr('disabled', 'disabled');
        }
    });
}

function validateInput(action) {

    if (action == 'add' && $('#cpbid').val() > 0)
        return true;

    var checked = $("input[name='PBItemSelect']:checked");
    if (!checked || checked.length != 1)
        return false;

    var selected = $("input[name='PBItemSelect']:checked").val();
    if (selected.length <= 0)
        return false;

    if (selected > 0) {
        $('#cpbid').val(selected);
        return true;
    }

    if (selected == 0) {
        if ($("#txtNewPBName").val().length <= 0)
            return false;
        else
            return true;
    }

    return false;
}

function callString(action, pbid, pid, pbnamenew) {
    var qstring = '';
    if (pbid && pbid > 0)
        qstring += '&pbid=' + pbid;
    if (pid && pid > 0)
        qstring += '&pid=' + pid;
    if (pbnamenew)
        qstring += '&name=' + pbnamenew;

    return '/Handler/ProjectBuilder.ashx' + '?action=' + action + qstring;

}

function setPBItem(action, pbnamenew, refresh) {

    if (validateInput(action) == false) {
        alert($.ProjectBuilderPopup.ValidateInputAction_errorMessage);
        return false;
    }

    $("#simplemodal-container button").addClass('disabled');


    $(".projectbuilders").slideUp(500, function () {
        $(".processingbar").slideDown(500);
    });

    var pbid = $('#cpbid').val();

    $.ajax({
        url: callString(action, pbid, $('#pid').val(), pbnamenew),
        success: function (data) {
            var retData = JSON.parse(data);
            if (retData && retData.ReturnCode >= 0) {
                if (retData.PBId > 0) {
                    $('#cpbid').val(retData.PBId);
                    if ($('#active-project .proj').text().indexOf("Change") > 0) {
                        $('#active-project a:first-child').text(retData.PBName);
                    }
                    else {
                        $('#active-project .proj').text(retData.PBName);
                    }


                }
            }
            if (refresh)
                window.location = window.location;
            else {
                $.modal.close();
            }

            $('.pbitem' + retData.PId).toggle();
        },
        error: function (request, status, error) {
            $.modal.close();
        }
    });
}

