// Please be aware:
// Code placed in this section is global to the entire system and should be restricted
// to javascript used directly by the rendering engine such as:
// * Form
// * Crud
// * Reports

// This is NOT the place to put code that will run on maybe 1 or 2 pages
// rather put this code in its own javascript file and load it on only the
// pages required

function DateToTimestamp(year,month,day){
    var datum = new Date(Date.UTC(year,month-1,day,0, 0, 0));
    return datum.getTime()/1000;
}

$(document).ready(function() {

    // Damn IE
    $(document).pngFix();

/*  // To Be Removed
    if ($.cookie('Communicator_TermsAndConditions'))
    {
        $('input#Terms').attr('checked', true).parent('td').parent('tr').hide();
    }*/

    $("div#number-format").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: false,
        width: 550,
        buttons: {
            Close: function() {
                $(this).dialog('close');
            }
        }
    });

    $("div#contact-support").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: false,
        width: 520,
        open: function(event, ui) {
            if (bValid == true)
            {
                $('form#contact-support-form span.marketing-copy').html(origCFForm);
                $('#contact-support-form')[0].reset();
                $('form#contact-support-form table.form').css('visibility', 'visible');
            }
        },
        buttons: {
            Close: function() {
                $(this).dialog('close');
            }
        }
    });

    $('#nfContactSupport').click(function() {
        $("div#number-format").dialog('close');
        $("div#contact-support").dialog('open');
        $('#Subject').val(1);
    });

    $('span#click-contact-form-throttle').click(function() {
        $("div#contact-support").dialog('open');
        $('#Subject').val(3);
    });

    $('span#click-contact-form').click(function() {
        $("div#contact-support").dialog('open');
        $('#Subject').val(2);
    });

    $('span#number-format-form').click(function() {
        $("div#number-format").dialog('open');
    });

    // Tweak Controls
    $('img#tweak-container-toggle').click(function() {
        $('div#tweak-container').toggle();
    });

    // Setting Billing Profile Class
    var BillingProfile = $('input[name=BillingProfile]').val();
    $('tbody.billing-profile').addClass('billing-profile-' + BillingProfile);

    // Group Toggle
    $('tr[id^=group-toggle-]').click(function() {
        var id = $(this).attr('id').substr(13);
        $(this).children('td').toggleClass('toggled');

        if (document.getElementById(id).style.display == 'none')
        {
            $('tbody#'+id).show();
        }
        else
        {
          $('tbody#'+id).hide()
        }
    });

    $('tbody.crud-search-body').hide();

    $('thead.crud-search-header').click(function() {
        var id = 'tbody[id=crud-search-body-' + $(this).attr('id').substr(19) + ']';
        $(id).toggle();
        $(this).children('tr').children('td').toggleClass('selected');
    });

    // Tooltip Controls
    $("[class*=tooltip]").hover(
        function(e){
            if (this.title)
            {
                this.t = this.title;
                this.title = "";
                $("body").append("<p id='tooltip'>"+ this.t +"</p>");
                $("#tooltip")
                    .css("top",(e.pageY - 10) + "px")
                    .css("left",(e.pageX + 20) + "px")
                    .fadeIn("fast");
            }
        },
        function(){
            if (this.t)
            {
                this.title = this.t;
                $("#tooltip").remove();
            }
        }
    );

    $(".tooltip").mousemove(function(e){
        $("#tooltip")
            .css("top",(e.pageY - 10) + "px")
            .css("left",(e.pageX + 20) + "px");
    });

    // Crud
    // Number Records per page select post
    $("select.SelectNumRecords").change(function() {
        var field = $(this).attr('name');
        window.location = $(this).attr('rel') + '&' + field + '=' + $(this).val();
    });

    // Selection Options
    $('.crud-select-toggle').click(function() {
        var targets = $(this).attr('rel');
        if ($(this).is(':checked'))
        {
            $('input.' + targets).each(function() {
                if (!$(this).is(':disabled'))
                {
                    $(this).attr('checked', true).trigger('change');
                }
            });
        }
        else
        {
            $('input.' + targets).each(function() {
                if (!$(this).is(':disabled'))
                {
                    $(this).attr('checked', false).trigger('change');
                }
            });
        }
    });

    $('span.crud-select-all').click(function() {
        var targets = $(this).attr('rel');
        $('input.' + targets).attr('checked', true).trigger('change');
    });

    $('span.crud-select-none').click(function() {
        var targets = $(this).attr('rel');
        $('input.' + targets).attr('checked', false).trigger('change');
    });

    $('span.crud-select-invert').click(function() {
        var targets = $(this).attr('rel');
        $('input.' + targets).each(function(){
            $(this).attr('checked', !$(this).attr('checked')).trigger('change');
        });

        //$('input.' + targets).attr('checked', !$('input.' + targets).attr('checked')).trigger('change');
    });

    // Report
    // Controls
    $("div.report-form-toggle").click(function() {
        $("div.report-form-hidden").toggle();
        if ($(this).html() == 'Show Form')
        {
            $(this).addClass('report-form-toggle-selected');
            $(this).html('Hide Form');
        }
        else
        {
            $(this).removeClass('report-form-toggle-selected');
            $(this).html('Show Form');
        }
    });

    // Datepicker Control
    $("input.datepicker").change(function() {
        var timestamp = $(this).val();
        var Y = timestamp.substr(0, 4);
        var m = timestamp.substr(5, 2);
        var d = timestamp.substr(8, 2);

        $('input#Raw' + $(this).attr('id')).val(DateToTimestamp(Y, m, d));
    });

    // Export Controls
    function UpdateExportOptions()
    {
        if ($("select.export-options").size())
        {
            var id = 'tbody#export-options-advanced-' + $("select.export-options").attr('id').substr(15);
            if ($("select.export-options").val() == 'Custom')
            {
                $(id).show();
            }
            else
            {
                $(id).hide();
            }
        }
    }

    $("select.export-options").change(function() {
        UpdateExportOptions();
    });
    UpdateExportOptions();
});

