﻿$(document).ready(function() {
    createWatermarks();

    //has MS-Ajax, re-setup watermarks on a partial postback
    if (Sys && Sys.WebForms && Sys.WebForms.PageRequestManager && typeof Sys.WebForms.PageRequestManager.getInstance == "function") {
        var _prm = Sys.WebForms.PageRequestManager.getInstance();
        _prm.add_pageLoaded(function() {
            forceClearWatermarks();
            createWatermarks();
        });
    }
});

function createWatermarks() {
    $("input:text[watermark!=null]").watermark();
    $("input:text[watermark!=null]").livequery(function() {
        $(this).watermark();
    });
}

function clearWatermarks() {
    if (Page_IsValid) {
        $("input:text[watermark!=null]").removewatermarks();
    }
}

function forceClearWatermarks() {
    $("input:text[watermark!=null]").removewatermarks();
}

(function($) {
    $.fn.removewatermarks = function(blurClass) {
        if (!blurClass) blurClass = 'blur';
        return this.each(function() {
            var $input = $(this),
            title = $input.attr('watermark');
            if (this.value === title && $input.hasClass(blurClass)) {
                $input.val('').removeClass(blurClass);

 
                
            }
        });
    };
})(jQuery);

(function($) {

    $.fn.watermark = function(blurClass) {
        if (!blurClass) blurClass = 'blur';

        return this.each(function() {
            var $input = $(this),
            title = $input.attr('watermark'),
            $form = $(this.form),
            $win = $(window);

            function remove() {
                if (this.value === title && $input.hasClass(blurClass)) {
                    $input.val('').removeClass(blurClass);

                }

            }

            // only apply logic if the element has the attribute
            if (title) {

                //BUG 32900 - if the clearonload is set to true, set the input to ''
                //prevents watermarks from being appended to the names when users enter them
                //before the page is finished loading.
                
                if ($input.ClearOnLoad == "true"    )
                { $input.val(''); }


                if (this.value === '') {
                    $input.val(title).addClass(blurClass);

                };

                // on blur, set value to title attr if text is blank
                $input.blur(function() {
                    if (this.value === '') {
                        $input.val(title).addClass(blurClass);
                    }
                }).focus(remove).blur(); // now change all inputs to title

                // clear the pre-defined text when form is submitted
                $form.submit(remove);
                $win.unload(remove); // handles Firefox's autocomplete
            }
        });
    };
})(jQuery);

