﻿// JScript File

function confirmDelete(obj, msg) {
    jQuery(obj).closest('tr').addClass('hiliteDelete');

    if (confirm(msg)) {
        return true;
    } else {
        jQuery(obj).closest('tr').removeClass('hiliteDelete');
        return false;
    }
}

var playerFormatting = function(text) {
    var newText = text;
    //array of find replaces
    var findplayers = [
				{ find: /^([^\-]+) \- /g, player: '<span class="ui-selectmenu-item-header">$1</span>' },
				{ find: /([^\|><]+) \| /g, player: '<span class="ui-selectmenu-item-content">$1</span>' },
				{ find: /(\([^\|><]+\))$/g, player: '<span class="ui-selectmenu-item-footer">$1</span>' }
			];

	for (var i in findplayers) {
	    newText = newText.replace(findplayers[i].find, findplayers[i].player);
    }
    return newText;
}

function f2d(sVal) {
    var dRet = 0;
    if (sVal.indexOf('/') > 0 && sVal.indexOf(' ') > 0) {
        var dWhole = 0; var dFrac = 0;
        var aFrac = sVal.split(' ');
        dWhole = parseInt(aFrac[0]);
        if (aFrac.length == 2) {
            var sFrac = aFrac[1];
        } else {
            var sFrac = sVal.substring(dWhole.toString().length).replace(' ', '');
        }
        var aRmdr = sFrac.split('/');
        dFrac = parseInt(aRmdr[0])/parseInt(aRmdr[1]);
        dRet = dWhole + dFrac;
    } else {
        dRet = parseFloat(sVal);
    }
    return dRet;
}



if (typeof jQuery != 'undefined') {
    jQuery(document).ready(function ($) {
        $.ajaxSetup({
            cache: false
        });

        $('#jQTabs, .jQTabs').tabs();

        $('#jQAccordion, .jQAccordion').accordion({ autoHeight: false });

        if (jQuery().selectmenu) {
            $('.selectMenu').selectmenu();
        }

        $('div.makeInlineDialog').each(function () {
            $(this).wrap('<div />');
            var oWrapper = $(this).parent();
            oWrapper.addClass('ui-inline-dialog ui-widget ui-widget-content ui-corner-all');
            var oTitle = $('<div />').addClass('ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix').append('<span>' + $(this).attr('title') + '</span>').addClass('ui-dialog-title');
            oWrapper.prepend(oTitle);
        });

        $('.makeButtonPane').addClass('ui-dialog-buttonpane ui-widget-content ui-helper-clearfix');
        $('input[type="submit"], input[type="button"], button, a', $('.makeButtonPane')).not('.processed').button().addClass('processed');
        $('a.makeAddButton').button({ icons: { primary: 'ui-icon ui-icon-plusthick'} }).addClass('processed');
        $('a.makeAddButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-plusthick' }, text: false }).addClass('processed');
        $('a.makeEditButton').button({ icons: { primary: 'ui-icon ui-icon-pencil'} }).addClass('processed');
        $('a.makeEditButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-pencil' }, text: false }).addClass('processed');
        $('a.makeNotesButton').button({ icons: { primary: 'ui-icon ui-icon-comment'} }).addClass('processed');
        $('a.makeDocBButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-document-b' }, text: false }).addClass('processed');
        $('a.makeNotesButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-comment' }, text: false }).addClass('processed');
        $('a.makeDeleteButton').button({ icons: { primary: 'ui-icon ui-icon-trash'} }).addClass('processed').live('click', function (e) {
            var sConfirm = $(this).attr('confirmmsg');
            var bRet = true;
            if (sConfirm != '' && sConfirm != null) {
                bRet = confirm(sConfirm);
                if (!bRet) e.preventDefault();
            }
            return bRet;
        });
        $('a.makeDeleteButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-trash' }, text: false }).addClass('processed').live('click', function (e) {
            var sConfirm = $(this).attr('confirmmsg');
            var bRet = true;
            if (sConfirm != '' && sConfirm != null) {
                bRet = confirm(sConfirm);
                if (!bRet) e.preventDefault();
            }
            return bRet;
        });
        $('a.makeAlertButton').button({ icons: { primary: 'ui-icon ui-icon-alert'} }).addClass('processed');
        $('a.makeAlertButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-alert' }, text: false }).addClass('processed');
        $('a.makeSaveButton').button({ icons: { primary: 'ui-icon ui-icon-disk'} }).addClass('processed');
        $('a.makeSaveButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-disk' }, text: false }).addClass('processed');
        $('a.makeCancelButton').button({ icons: { primary: 'ui-icon ui-icon-arrowreturnthick-1-w'} }).addClass('processed');
        $('a.makeCancelButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-arrowreturnthick-1-w' }, text: false }).addClass('processed');
        $('a.makeReorderButton').button({ icons: { primary: 'ui-icon ui-icon-arrow-4' }, text: false }).addClass('processed').css('width', '1.2em').css('height', '1.2em').css('cursor', 'move');
        $('a.makeMoveRightButton').button({ icons: { primary: 'ui-icon ui-icon-arrowthick-1-e' }, text: false }).addClass('processed').css('width', '1.2em').css('height', '1.2em');
        $('a.makeMoveLeftButton').button({ icons: { primary: 'ui-icon ui-icon-arrowthick-1-w' }, text: false }).addClass('processed').css('width', '1.2em').css('height', '1.2em');
        $('a.makeMoveUpButton').button({ icons: { primary: 'ui-icon ui-icon-arrowthick-1-n' }, text: false }).addClass('processed').css('width', '1.2em').css('height', '1.2em');
        $('a.makeMoveDownButton').button({ icons: { primary: 'ui-icon ui-icon-arrowthick-1-s' }, text: false }).addClass('processed').css('width', '1.2em').css('height', '1.2em');
        $('a.makeExpandButton, .makeNextButton').button({ icons: { primary: 'ui-icon ui-icon-triangle-1-e'} }).addClass('processed');
        $('a.makeCollapseButton').button({ icons: { primary: 'ui-icon ui-icon-triangle-1-s'} }).addClass('processed');
        $('a.makeExpandButtonIcon, .makeNextButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-triangle-1-e' }, text: false }).addClass('processed');
        $('a.makeCollapseButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-triangle-1-s' }, text: false }).addClass('processed');
        $('a.makePreviousButton').button({ icons: { primary: 'ui-icon ui-icon-triangle-1-w'} }).addClass('processed');
        $('a.makePreviousButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-triangle-1-w' }, text: false }).addClass('processed');
        $('a.makeTinyButton').button().addClass('processed')
        $('a.makeImageButton').button().addClass('processed').css('width', '2.2em').css('height', '2.2em').css('margin-top', '0.1em').find('span.ui-button-text').css('padding', '0.5em');
        $('a.makeImageButtonIcon').button({ text: false }).addClass('processed').css('width', '2.2em').css('height', '2.2em').find('span.ui-button-text').css('padding-top', '0.4em').css('padding-left', '0.4em');
        $('a.makeOpenLinkButton').button({ icons: { primary: 'ui-icon ui-icon-extlink'} }).addClass('processed');
        $('a.makeOpenLinkButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-extlink' }, text: false }).addClass('processed');
        $('a.makeResetButton').button({ icons: { primary: 'ui-icon ui-icon-arrowrefresh-1-s'} }).addClass('processed');
        $('a.makeResetButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-arrowrefresh-1-s' }, text: false }).addClass('processed');
        $('a.makeFavoriteButton').button({ icons: { primary: 'ui-icon ui-icon-star'} }).addClass('processed');
        $('a.makeFavoriteButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-star' }, text: false }).addClass('processed');
        $('a.makePreviewButton').button({ icons: { primary: 'ui-icon ui-icon-zoomin'} }).addClass('processed');
        $('a.makePreviewButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-zoomin' }, text: false }).addClass('processed');
        $('a.makePublishButton').button({ icons: { primary: 'ui-icon ui-icon-signal-diag'} }).addClass('processed');
        $('a.makeCheckButton').button({ icons: { primary: 'ui-icon ui-icon-check'} }).addClass('processed');
        $('a.makeCheckButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-check' }, text: false }).addClass('processed');
        $('a.makeGearButton').button({ icons: { primary: 'ui-icon ui-icon-gear'} }).addClass('processed');
        $('a.makeGearButtonIcon').button({ icons: { primary: 'ui-icon ui-icon-gear' }, text: false }).addClass('processed');
        $('.makeButton:not(.processed), .jqButton:not(.processed), .jQButton:not(.processed)').button();
        $('a.emailLink, a.makeEmailButton').button({ icons: { primary: 'ui-icon ui-icon-mail-closed'} });
        $('div.ui-invertednavtabs ul.ui-tabs-nav li').each(function () {
            $(this).addClass('ui-corner-bottom ui-state-default').hover(function () { $(this).addClass('ui-state-hover'); }, function () { $(this).removeClass('ui-state-hover'); });
        });
        $('a.processed').each(function () {
            var b = this;
            if (b && typeof (b.click) == 'undefined') {
                b.click = function () {
                    var result = true;
                    if (b.onclick) result = b.onclick();
                    if (typeof (result) == 'undefined' || result) {
                        eval(b.getAttribute('href'));
                    }
                }
            }
        });

        $('input:text, input:password, textarea').focus(function () {
            $(this).addClass('focused');
        }).blur(function () {
            $(this).removeClass('focused');
        });

        $('div.validationsumm').addClass('ui-state-error ui-corner-all').css('padding', '1em').css('margin', '1em');

        $('span.validationerror').each(function () {
            $(this).html('<img src="/cmsimages/spacer.gif" height="16" width="16" title="' + $(this).attr('errormessage') + '" />');
        });

        $('span.validationerrlong').each(function () {
            $(this).html('<img src="/cmsimages/spacer.gif" height="16" width="16" title="' + $(this).attr('errormessage') + '" /> <b>' + $(this).attr('errormessage') + '</b>');
        });

        $('input.pcase').blur(function () {
            var sVal = $(this).val();
            if (sVal.indexOf('.') < 0 && sVal.length > 2) {
                if (sVal.toLowerCase() == sVal || sVal.toUpperCase() == sVal) {
                    var start = sVal.substr(0, 1);
                    var rest = sVal.substr(1);
                    $(this).val(start.toUpperCase() + rest.toLowerCase());
                }
            } else {
                if (sVal.toLowerCase() == sVal && sVal.length > 2) {
                    $(this).val(sVal.toUpperCase());
                }

                if (sVal.length == 1) {
                    $(this).val(sVal.toUpperCase());
                }

                if (sVal.indexOf('.') > 0 && sVal.length == 2) {
                    $(this).val(sVal.toUpperCase());
                }
            }
        });

        $('input.datefld').datepicker();

        $('input.csreadonly').focus(function () {
            $(this).blur();
        });

        $('tr.datarow:not(.nohover), tr.altdatarow:not(.nohover)').live('mouseover mouseout', function (e) {
            if (e.type == 'mouseover') {
                $(this).addClass('datarowhilite');
                var rowid = $(this).attr('data-rowid');
                if (typeof rowid !== 'undefined' && rowid !== false) {
                    var oWrapper = $(this).closest('table');
                    $('tr[data-rowid=' + rowid + ']', oWrapper).addClass('datarowhilite');
                }
            } else {
                $(this).removeClass('datarowhilite');
                var rowid = $(this).attr('data-rowid');
                if (typeof rowid !== 'undefined' && rowid !== false) {
                    var oWrapper = $(this).closest('table');
                    $('tr[data-rowid=' + rowid + ']', oWrapper).removeClass('datarowhilite');
                }
            }
        });

        $('tr.datarow, tr.altdatarow, tr.datarowedit, tr.datarowinsert').each(function () {
            $('td:first', $(this)).addClass('datarowfirst');
            $('td:last', $(this)).addClass('datarowlast');
        });

        $('a.button-save, a.button-delete').each(function () {
            if ($(this).parent().get(0).tagName.toUpperCase() != 'P' || $(this).siblings().count > 0) {
                $(this).wrap('<div></div>');
            }
            $(this).parent().css('margin-left', '25%');
            $(this).mouseover(function () {
                $(this).addClass('ui-state-hover');
            }).mouseout(function () {
                $(this).removeClass('ui-state-hover');
            });
        });

        $('a.button-save').each(function () {
            $(this).addClass('ui-state-default').addClass('ui-corner-all').prepend('<span class="ui-icon ui-icon-disk"/>');
        });

        $('a.button-delete').each(function () {
            $(this).addClass('ui-state-error').addClass('ui-corner-all').prepend('<span class="ui-icon ui-icon-closethick"/>');
        });

        $('div.msg-warning').each(function () {
            var sMsg = $(this).text();
            $(this).hide();
            $.jGrowl(sMsg, { sticky: true });
        });

        $('div.msg-success').each(function () {
            var sMsg = $(this).text();
            $(this).hide();
            $.jGrowl(sMsg);
        });

        $('span.chklistrow').each(function () {

        });
    });

    jQuery(window).load(function() {
        jQuery('#wait').hide();
    });

    jQuery.fn.selectRange = function(start, end) {
        return this.each(function() {
            if (this.setSelectionRange) {
                this.focus();
                this.setSelectionRange(start, end);
            } else if (this.createTextRange) {
                var range = this.createTextRange();
                range.collapse(true);
                range.moveEnd('character', end);
                range.moveStart('character', start);
                range.select();
            }
        });
    };

    jQuery.fn.insertAtCursor = function(myStartVal, myEndVal) {
        return this.each(function() {
            var curValue = jQuery(this).val();

            jQuery(this).focus();

            if (this.setSelectionRange) {
                var scrollPos = jQuery(this).scrollTop(); 
                if (this.selectionStart || this.selectionStart == '0') {
                    var iStartPos = this.selectionStart;
                    var iEndPos = this.selectionEnd;
                    var iSelLen = iEndPos - iStartPos;
                    var sSel = '';
                    if (iSelLen > 0) {
                        sSel = curValue.substring(iStartPos, iStartPos + iSelLen);
                    }
                    var sBefore = curValue.substring(0, iStartPos);
                    var sAfter = curValue.substring(iEndPos);
                    jQuery(this).val(sBefore + myStartVal + sSel + myEndVal + sAfter);
                } else {
                    jQuery(this).val(curValue + myStartVal + myEndVal);
                }
                jQuery(this).scrollTop(scrollPos);
            } else if (this.createTextRange) {
                oSel = document.selection.createRange();
                var selText = oSel.text;
                if (selText != '' && selText != null) {
                    var sSelTrim = jQuery.trim(selText);
                    if (sSelTrim.substring(0, myStartVal.length) != myStartVal) {
                        oSel.text = myStartVal + selText + myEndVal;
                    }
                } else {
                    oSel.text = myStartVal + myEndVal;
                }
            } else {
                jQuery(this).val(curValue + myStartVal + myEndVal);
            }
        });
    };
}

(function($) {
    $.fn.visible = function() {
        return this.each(function() {
            $(this).css('visibility', 'visible');
        });
    };
})(jQuery);

(function($) {
    $.fn.invisible = function() {
        return this.each(function() {
            $(this).css('visibility', 'hidden');
        });
    };
})(jQuery);

(function($) {
    $.fn.hidden = function() {
        return this.each(function() {
            $(this).css('visibility', 'hidden');
        });
    };
})(jQuery);

(function ($) {
    $.fn.inlineDialog = function () {
        return this.each(function () {
            $(this).wrap('<div />');
            var oWrapper = $(this).parent();
            oWrapper.addClass('ui-inline-dialog ui-widget ui-widget-content ui-corner-all');
            var oTitle = $('<div />').addClass('ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix').append('<span>' + $(this).attr('title') + '</span>').addClass('ui-dialog-title');
            oWrapper.prepend(oTitle);
        });
    };
})(jQuery);

(function($) {
    // plugin definition
    $.fn.pageTabs = function(options) {
        var selector = $(this).selector;
        var opts = $.extend({}, $.fn.pageTabs.defaults, options);

        return this.each(function() {
            var $this = $(this);
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
            $(this).addClass('ui-tabs ui-widget ui-widget-content ui-corner-all');
            $('ul:first', $(this)).addClass('ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all');
            $('ul:first>li', $(this)).addClass('ui-state-default ui-corner-top').hover(function() {
                if (!$(this).hasClass('ui-tabs-selected')) { $(this).addClass('ui-state-hover'); }
            }, function() {
                $(this).removeClass('ui-state-hover');
            });
            $('div:first', $(this)).addClass('ui-tabs-panel ui-widget-content ui-corner-bottom');
            if (o.selected >= 0) {
                $('ul:first>li:eq(' + o.selected + ')', $(this)).addClass('ui-tabs-selected ui-state-active');
            }
        });
    };

    // plugin defaults - added as a property on our plugin function
    $.fn.pageTabs.defaults = {
        selected: 0
    };
})(jQuery);

(function ($) {
    // plugin definition
    $.fn.pageButtons = function (options) {
        var selector = $(this).selector;
        var opts = $.extend({}, $.fn.pageButtons.defaults, options);

        return this.each(function () {
            var $this = $(this);
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
            $(this).addClass('ui-widget ui-widget-content ui-corner-all');
            $('ul:first', $(this)).addClass('ui-widget-header ui-corner-all ui-helper-clearfix');
            $('ul:first li', $(this)).css({'float':'left', 'margin':'0 0.5em'});
//            $('ul:first', $(this)).addClass('ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all');
//            $('ul:first>li', $(this)).addClass('ui-state-default ui-corner-top').hover(function () {
//                if (!$(this).hasClass('ui-tabs-selected')) { $(this).addClass('ui-state-hover'); }
//            }, function () {
//                $(this).removeClass('ui-state-hover');
//            });
//            $('div:first', $(this)).addClass('ui-tabs-panel ui-widget-content ui-corner-bottom');
            if (o.selected >= 0) {
                $('ul:first>li:eq(' + o.selected + ') a', $(this)).button();
            }
        });
    };

    // plugin defaults - added as a property on our plugin function
    $.fn.pageButtons.defaults = {
        selected: 0
    };
})(jQuery);

(function($) {
    $.fn.sort = function() {
        return this.pushStack([].sort.apply(this, arguments), []);
    };
})(jQuery);

(function($) {
    $.fn.sortOptions = function(sortCallback) {
        jQuery('option', this).sort(sortCallback).appendTo(this);
        return this;
    };
})(jQuery);

(function($) {
    $.fn.sortOptionsByText = function() {
        var byTextSortCallback = function(x, y) {
            var xText = jQuery(x).text().toUpperCase();
            var yText = jQuery(y).text().toUpperCase();
            return (xText < yText) ? -1 : (xText > yText) ? 1 : 0;
        };
        return this.sortOptions(byTextSortCallback);
    };
})(jQuery);

(function($) {
    $.fn.sortOptionsByValue = function() {
        var byValueSortCallback = function(x, y) {
            var xVal = jQuery(x).val();
            var yVal = jQuery(y).val();
            return (xVal < yVal) ? -1 : (xVal > yVal) ? 1 : 0;
        };
        return this.sortOptions(byValueSortCallback);
    };
})(jQuery);

(function($) {
    $.fn.sortOptionsByTextWithPreserve = function() {
        var foption = $('option:first', $(this));
        if (foption.val() == '' || foption.val() == '0') {
            foption.remove();
        } else {
            foption = null;
        }

        var loption = $('option:last', $(this));
        if (loption.val() == '-1') {
            loption.remove();
        } else {
            loption = null;
        }

        //foption = $('#'+ selectId + ' option:first');
        soptions = $('option', $(this)).sort(function(a, b) {
           return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
        });
        $(this).html(soptions);
        if (foption != null) {
            $(this).prepend(foption);
        }
        if (loption != null) {
            $(this).append(loption);
        }
    };
})(jQuery);

function fixDecimalPlaces(nbr, places) {
    var ret = nbr;
    var iplaces = 0;
    var tmp=nbr.toString();
    if (tmp.indexOf('.')>-1) {
        iplaces = tmp.length-tmp.indexOf('.')-1;
    } else {
        if (ret[0] == '0') {
            ret = ret.substr(1);
        }
    }
    if (iplaces > places) {
        ret = nbr*1;
        ret = ret.toFixed(places).toString();
    }
    return ret;
}

