﻿/// <reference path="../jquery-1.3.2.js" />
(function ($) {
    $.fn.formatTimeInput = function (options) {
        debug(this);
        // build main options before element iteration
        var opts = $.extend({}, $.fn.formatTimeInput.defaults, options);

        $(this).each(function () {
            $this = $(this);

            $this.blur(function () {
                var timeInput = this.value;

                if (timeInput != '')
                    this.value = $.fn.formatTimeInput.formatTime(timeInput);
            });
        });
    };
    //
    // define and expose the formatTime function
    //
    $.fn.formatTimeInput.formatTime = function (timeInput) {

        var retValue = timeInput.replace(/\,|\.|\:|\;|\-/g, "");

        if (retValue.length == 4) {
            // Ex. 2230 --> 22:30
            retValue = retValue.substring(0, 2) + ':' + retValue.substring(2, 4);
        }
        else if (retValue.length == 3) {
            // Ex 930 --> 09:30
            retValue = '0' + retValue.substring(0, 1) + ':' + retValue.substring(1, 3);
        }
        else if (retValue.length == 2) {
            // Ex 12 --> 12:00
            retValue = retValue.substring(0, 2) + ':00';
        }
        else if (retValue.length == 1) {
            // Ex 12 --> 12:00
            retValue = '0' + retValue.substring(0, 2) + ':00';
        } else {
            retValue = timeInput;
        }

        return retValue;
    };
    //
    // private function for debugging
    //
    function debug($obj) {
        if (window.console && window.console.log)
            window.console.log("input count: " + $obj.size());
    };
    //
    // plugin defaults
    //
    $.fn.formatTimeInput.defaults = {
        invalidClassName: "invalid"
    };
})(jQuery);
