﻿jQuery.fn.setDefaultVal = function(defaultVal) {
    var item = $(this);

    // If it is a textarea then we assign "inner text" to the "value".
    // This is needed, because Firefox has issue which happens on ctrl-R,
    // when item.val() returns the value from the previously
    // loaded page instance.
    if (this.get(0).tagName.toLowerCase() == "textarea") { item.val(item.text()); }
    
    if (!defaultVal) { defaultVal = item.val(); }
    if (!item.data("defaultVal")) {
        // Save the default value.
        item.data("defaultVal", defaultVal);
        // If there is no value at this moment.
        if (item.val().length == 0) {
            // Add the default value.
            item.addClass("default");
            item.val(defaultVal);
        }
    }
    this.focus(function() {
        // Make sure that the control has default value.
        if (item.data("defaultVal")) {
            if (item.hasClass("default")) { item.val("").removeClass("default"); }
        }
    });
    this.blur(function() {
        // Make sure that the control has default value.
        if (item.data("defaultVal")) {
            if (item.val().length == 0) {
                item.addClass("default");
                item.val(item.data("defaultVal"));
            }
        }
    });
};
// Works on par with jQuery.fn.setDefaultVal().
jQuery.fn.getValue = function() {
var item = $(this);
    // If item has default class then there is no value.
    if (item.hasClass("default")) { return ""; }
    // Otherwise there is.
    return item.val();
};
