﻿var plainText = { 'color': 'black', 'font-style': 'normal' };
var unsetText = { 'color': '#B0B0B0', 'font-style': 'italic' };

function tip_init_events(e, blankSearchText) {
    $(e).css(plainText);
    $(e).focus(function() {
        if ($(e).val() == blankSearchText)
            $(e).val('').css(plainText);
    });
    $(e).blur(function() {
        
        if ($(e).val() == '')
            $(e).css(unsetText).val(blankSearchText);
        if ($(e).val() == blankSearchText)
            $(e).css(unsetText);
    });
    e.blur();
}

function tip_init(id, blankSearchText) {
    
    var e = $('#' + id);
    tip_init_events(e, blankSearchText);

    try {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_endRequest(function() { tip_init_events($('#' + id), blankSearchText); });
    }
    catch (err) {
        return;
    }
}

// Keep user from entering more than maxLength characters
/* 
#  * 
#  * Textarea Maxlength Setter JQuery Plugin 
#  * Version 1.0 
#  * 
#  * Copyright (c) 2008 Viral Patel 
#  * website : http://viralpatel.net/blogs 
#  * 
#  * Dual licensed under the MIT and GPL licenses: 
#  * http://www.opensource.org/licenses/mit-license.php 
#  * http://www.gnu.org/licenses/gpl.html 
#  * 
*/

$(document).ready(function() {
    //Set maxlength of all the textarea (call plugin)
    $("textarea[maxLength]").keypress(function(event) {
        var key = event.which;
        var maxLength = $(this).attr("maxLength");
        var length = this.value.length;

        //all keys including return.  
        if (key >= 33 || key == 13) {
            if (length >= maxLength)
                event.preventDefault();
        }
        $('#' + this.id + '_remaining_chars').text(maxLength - length);

    }).keyup(function(event) {
        var maxLength = $(this).attr("maxLength");
        var length = this.value.length;



        if (length >= maxLength) {
            if (length > maxLength) {
                var newString = this.value.substring(0, maxLength - 3);
                do {
                    var lastIndex = newString.lastIndexOf(' ');
                    if (lastIndex != -1)
                        var newString = this.value.substring(0, lastIndex);
                } while (lastIndex != -1 && maxLength - lastIndex <= 3)
                if (newString.length <= maxLength - 3)
                    newString += '...';
                this.value = newString;
            }
            event.preventDefault();
        }

        $('#' + this.id + '_remaining_chars').text(maxLength - this.value.length);
    });

});
