﻿var CH = {
    ApplyInputMasks: function CH_ApplyInputMasks() {
        $(".ssn").mask("999-99-9999");
        $(".phone").mask("(999) 999-9999");
        $(".tollPhone").mask("9 (999) 999-9999");
    },
    ShowAjaxDialog: function CH_ShowAjaxDialog(options) {
        var defaults = {
            width: 600,
            height: 400,
            successHandler: $.noop,
            cancelHandler: $.noop,
            open : $.noop,
            successLabel: "Okay",
            cancelLabel: "Cancel",
            modal : true,
            draggable : false,
            resizable : false,
            closeOnEscape : false
        };

        options = $.extend(true, {}, defaults, options);

        var buttons = {};
        if (options.cancelLabel != "") {
            buttons[options.cancelLabel] = function() {
                options.cancelHandler.apply(this, arguments);
                $(this).dialog('close').dialog('destroy').remove();
            }
        }        
        if (options.successLabel != "") {
            buttons[options.successLabel] = function() {
                options.successHandler.apply(this, arguments);
            };
        }

        $.ajax({
            url: options.url,
            dataType: "html",
            cache: false,
            success: function(data) {
                var d = $(data);
                $("<div />")
				    .attr('title', d.attr('title'))
				    .dialog({
				        modal: options.modal,
				        draggable: options.draggable,
				        resizable: options.resizable,
				        closeOnEscape: options.closeOnEscape,
				        width: options.width,
				        height: options.height,
				        open : options.open,
				        buttons: buttons
				    })
				    .append(d);
            }
        });
    },

    ShowConfirmDialog: function(options) {
        var defaults = {
            width: 400,
            height: 150,
            title: "Confirmation",
            message: "Are you sure?",
            successHandler: $.noop,
            cancelHandler: $.noop,
            successLabel: "Okay",
            cancelLabel: "Cancel"
        };

        options = $.extend(true, {}, defaults, options);

        var buttons = {};
        if (options.cancelLabel != '')
        {
            buttons[options.cancelLabel] = function() {
                options.cancelHandler.apply(this, arguments);
                $(this).dialog('close').dialog('destroy').remove();
            }
        }        
        if (options.successLabel != '')
        {
            buttons[options.successLabel] = function() {
                options.successHandler.apply(this, arguments);
            };
        }

        $("<div />")
		    .attr('title', options.title)
		    .dialog({
		        modal: true,
		        draggable: false,
		        resizable: false,
		        closeOnEscape: false,
		        width: options.width,
		        height: options.height,
		        buttons: buttons
		    })
		    .append($("<span>" + options.message + "</span>"));
    },
    
    RegisterChart : function CH_RegisterChart(options)
    {
		var chart = new FusionCharts(options.url, options.id, options.w.toString(), options.h.toString(), "0", "0");
		var dataUrl = $.param.querystring(options.data, {noCache:(new Date()).valueOf().toString()});
		chart.setDataURL(escape(dataUrl));
		chart.render(options.id);
    },
    
    CalculateAge : function(dob) {
        var myDate = new Date(dob);
        var today = new Date();
        var daysApart = Math.abs(Math.round((today - myDate) / 86400000));
        age = daysApart / 365;
        return Math.floor(age);
    }
};
