﻿;
(function($)
{

    $.fn.CPIButton = function(options)
    {

        return this.each(function()
        {
            var span = this;
            var opts = $.extend({}, {
                text: "", //What shows up on the button

                // one of the following two sections is required

                // 1.  For buttons that are just links to a new page
                url: null, // destination url
                message: "<h2>Loading...</h2>", // message displayed while loading new page

                // 2.  Buttons that call back javascript on the current page
                callback: null //callback function

            }, options
            );

            var anchor;

            if (opts.text == null || opts.text == "") opts.text = $(this).text();

            if (opts.callback == null)
            {
                anchor = $('<a />').attr("href", opts.url).text(opts.text).click(function()
                {
                    $.blockUI({ message: opts.message });
                    return true;
                });
            }
            else
            {
                anchor = $('<a />').attr("href", "#").text(opts.text).click(function() { opts.callback.call(span); return false; });
            }


            $(this).text('').addClass("image_button").addClass("ui-corner-all").append(anchor);
        });
    }

})(jQuery);

function OnServerError()
{
    $.unblockUI();
    DisplayError("A server error has occurred.  Please try again or contact FAME at info@famemaine.com");
}

function DisplayError(text, title)
{
    if (title == null) title = "Error";

    var validationDiv = $('div.CPIValidation');

    if (validationDiv.length == 1)
    {
        validationDiv.CPIValidation('addError', text);
    }
    else
    {

        $('#dlgMessage')
            .empty()
            .text(text)
            .dialog({
                title: 'Error',
                modal: true,
                buttons: { "OK": function() { $(this).dialog("destroy"); } }
            }).focus();
    }
}

function ConvertNULLToEmptyString(input)
{
    if (input == null) return "";
    return input;
}

function ConvertToAccordian(parentElement)
{
    var headers = parentElement.find(".accordian_header");

    while (headers.length > 0) 
    {
        var testSibling = $(headers[0]).parent();
        var outerDiv = $("<div />").addClass('Accordian_Outer_Div').insertBefore(testSibling);

        var innerDiv = null;



        while (testSibling.length > 0)
        {
            var workingItem = $(testSibling[0]);
            testSibling = testSibling.next();

            var workingChildren = workingItem.children();


            if (workingChildren.length == 1 && workingChildren.hasClass("accordian_header"))
            {
//                workingChildren.removeClass("accordian_header").attr('href', '#').remove();
                workingChildren.removeClass("accordian_header").remove();
                workingItem.remove();  //WRL Add - needed to clear out the original parent of the <a class="accordian_header" /> tag
                var header = $('<h3 />').append(workingChildren).appendTo(outerDiv);
                innerDiv = $('<div />').appendTo(outerDiv);
            }
            else {
                innerDiv.append(workingItem.remove());
            }
        }

        headers = $(".accordian_header");

//        var expandAll = $('<a />').attr('href', '#').text('expand all').attr('float', 'right').attr('color', 'red').click(function()
//        {
//            var sss = $(this).next().find(':not(:has(.selected))');
//            $(this).next().find(':not(:has(.selected))').accordian('activate', 0);
//        });
//        outerDiv.accordion({ active: false, collapsible: true, autoHeight: false }).before(expandAll);
        outerDiv.accordion({ active: false, collapsible: true, autoHeight: false });
    }

}

function isValidEmail(str) {
    return (RegExp(/^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/).test(String(str)));
}

