﻿/* Menu Scripts */
var parentMenu;

function BuildMenuEvents(parentMenuId)
{
    //Set click event to show the related link menu on the main menu image
    parentMenu = "#" + parentMenuId;
    $(parentMenu + " li ul").hide();
    $(parentMenu + ' li a.mainLink').click(function()
    {
        var childUl = "#" + $(this).next().attr('id');
        $(parentMenu + " li ul").hide();
        $(childUl).show("normal");
        return false;
    });
    $(parentMenu + " li ul").mouseover(function()
    {
        var currentId = $(this).attr('id');
        $(parentMenu + " li ul").each(function()
        {
            if (!$(this).is(':hidden') && $(this).attr('id') != currentId)
            {
                $(this).hide();
            }
        });

        if ($(this).is(':hidden'))
        {
            $(this).show('normal');
        }
        return false;
    });
    $(parentMenu + " li ul").bind('mouseleave', (function()
    {

        $(this).hide('normal');
        return false;
    }));
};

/* Popup Script */
function newWindow(page, height, width, menubar, scrollbars)
{
    var parameters = format("status=no,toolbar=no,directories=no,menubar={2},scrollbars={3},resizable=yes,width={0},height={1},top=20,left=100", width, height, menubar, scrollbars);
    var OpenWin = this.open("", "CtrlWindow", parameters);
    OpenWin.location = page;
};

/* Format String */
function format(str)
{
    for (var i = 1; i < arguments.length; i++)
    {
        str = str.replace('{' + (i - 1) + '}', arguments[i]);
    }
    return str;
};

// this is to toggle the inventory status dialog from only opening once.
var isStatusDialogClosed = true;

$(document).ready(function () {
    var statusDialog = null;
    var helpDialog = null;
    var helpPopupHTML = '<div><img src="/Images/ajaxLoader.gif" alt="Processing"/></div>';
    var statusPopupHTML = '<div><img src="/Images/ajaxLoader.gif" alt="Processing"/></div>';

    // Header Help Dialog setup
    $('#helpDialog').click(function () {
        if (helpDialog == null) {
            helpPopupHTML = '<div>' + $('#helpContent #mainDiv').parent().html() + '</div>';
            helpDialog = $(helpPopupHTML).dialog({
                autoOpen: false,
                width: 467,
                height: 501,
                modal: true,
                resizable: false,
                position: [600, 30]
            });
        }
        helpDialog.dialog('open');
        return false;
    });
    // end help

    $("#checkAll").click(function () {
        var checked_status = this.checked;
        $("input[name=checkAllChild]").each(function () {
            this.checked = checked_status;
        });
    });

    //Inventory status legend dialog
    $('.inventoryStatus').click(function () {
        // check if it was already opened or not
        // HACK: needed because this .click was firing more than once causing dialogs to 'stack'
        if (isStatusDialogClosed) {
            if (statusDialog == null) {
                statusDialog = $(statusPopupHTML)
                .appendTo('body')
                .load('/InventoryStatus')
                .dialog({
                    width: 750,
                    modal: true,
                    resizable: false,
                    autoOpen: false,
                    close: function (event, ui) { isStatusDialogClosed = true; }
                });
            }
            statusDialog.dialog('open').css({ minHeight: 0, width: '675px' });
            isStatusDialogClosed = false;
        }
        //prevent the browser to follow the link
        return false;
    });

    // Unobtrusive js for print
    $('#Print').show();
    $('#Print a').click(function () {
        window.print();
        return false;
    });

    var tutorial = $('[href$="/Tutorial"]');
    tutorial.attr('href', 'javascript:openTutorialVideos();');
    tutorial.removeAttr('target');

});

function findPos(findObj)
{
    var obj = document.getElementById(findObj);
    var posTop = 0;
    var posLeft = 0;

    if (obj.offsetParent)
    {
        posLeft = obj.offsetLeft;
        posTop = obj.offsetTop;
        while (obj = obj.offsetParent)
        {
            posLeft += obj.offsetLeft;
            posTop += obj.offsetTop;
        }
    }

    return [posLeft, posTop];
};

function strReplace(str1, str2, str3)
{
    str1 = str1.split(str2).join(str3);
    return str1;
};

/*  
    JQuery Validation Custom Methods
*/
$.validator.addMethod('regexp', function (value, element, param) {
return this.optional(element) || value.match(param);
}, 'This value doesn\'t match the correct format.');
/* End JQuery Validation Custom Methods*/

(function($)
{
    $.fn.numeric = function()
    {
        this.keydown(function(event)
        {
            if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) // 0-9 or numpad 0-9
            {
            }
            else if (event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39) // not esc, del, left or right
            {
                event.preventDefault();
            }
        });
    };
})(jQuery);


// retrieve dialog buttons
function getjQueryDialogUIButton(jqUIdialog, button_names) {
    if (typeof button_names == 'string') {
        button_names = [button_names];
    }

    var buttons = jqUIdialog.parent().find('.ui-dialog-buttonpane button');
    for (var i = 0; i < buttons.length; i++) {
        var jButton = $(buttons[i]);
        for (var j = 0; j < button_names.length; j++) {
            if (jButton.text() == button_names[j]) {
                return jButton;
            }
        }
    }
    return null;
}

// enable/disable dialog buttons
function enablejQueryUIDialogButton(jqUIdialog, button_names, enable) {
    var button = getjQueryDialogUIButton(jqUIdialog, button_names);
    if (button == null) {
        alert('button not found: ' + button_names);
    }
    else {
        if (enable) {
            button.removeAttr('disabled').removeClass('ui-state-disabled');
        }
        else {
            button.attr('disabled', 'disabled').addClass('ui-state-disabled');
        }
    }
}

function openTutorialVideos() {
    OpenWin = this.open("/Tutorial", "CtrlWindow", "status=no,toolbar=no,directories=no,menubar=no,scrollbars=no,resizable=yes,width=1010,height=620");
}

function openPattersonDentalTips() {
    OpenWin = this.open("http://pattersonsupport.custhelp.com/app/answers/list/p/1993%2c2055%2c5%2c", "CtrlWindow", "location=yes,status=no,toolbar=yes,directories=yes,menubar=yes,scrollbars=yes,resizable=yes,width=1080,height=620");
}
