// <textarea id="elastic_5" rows=5 cols="40" maxheight="450" minheight="100" style="line-height:12px; font-size:11px; font-family:verdana;">

if (typeof bd_safeaddonload != 'undefined')
{
    bd_safeaddonload(bd_elatic_textareas);
}
else
{
    window.onload = bd_elatic_textareas;
};

function bd_elasticise_textarea(input)
{
    if (input.id == '')
    {
        input.setAttribute("id",'e_' + input.name + Math.round(Math.random() * 1000000) + "_div");
    }

    input.onoldkeyup = input.onkeyup;   
    input.onkeyup = function()
    {
        var id       = this.id; 
        var textarea = document.getElementById(id);
        var div      = document.getElementById(id + '_div');

        var text = textarea.value;
        text = text.replace(/&/g,'&amp;').replace(/  /g, '&nbsp;').replace(/<|>/g, '&gt;').replace(/\n/g, '<br />');
        
        div.innerHTML = text;
        
        var h = div.offsetHeight;
        var lineheight      = parseInt(getstyle(input,'line-height') || getstyle(input,'lineHeight'));
		var minheight   	= parseInt(textarea.getAttribute("minheight"));
        var maxheight	    = parseInt(textarea.getAttribute("maxheight"));
        var extra           = 0;
        maxheight   	    = (parseInt(maxheight / lineheight) * lineheight); // Normalize height
        
        if (isNaN(lineheight)) { lineheight = parseInt(div.style.fontSize); }
        if (typeof this.hasFocus != 'undefined') { if (this.hasFocus == true) { extra = lineheight * 2; } }
        if (isNaN(minheight)) { minheight = 5 * lineheight; }
        if (isNaN(maxheight)) { maxheight = 25 * lineheight; }
        
        if (textarea.className.search('collapse') == -1)
        {
            extra = lineheight * 2;
        }

        if (h < minheight) { h = minheight; }
        if (h < maxheight)
        {
            textarea.style.height   = ((parseInt(h / lineheight) * lineheight) + extra) + "px";
            textarea.style.overflow = "hidden";
        }
        else
        {
            textarea.style.height   = maxheight + "px";
            textarea.style.overflow = "auto";
        }
        
        if (typeof textarea.onoldkeyup == "function")
        {
            textarea.onoldkeyup();
        }
    };

    input.onkeydown = function() { this.onkeyup(); }
    input.onfocus   = function() { this.hasFocus = true; this.onkeyup(); }
    
    if (input.className.search('collapse') != -1)
    {
        input.onblur    = function() { this.hasFocus = false; this.onkeyup(); }
    }
    
    // Create duplicate div section that matches textarea
    var div = document.createElement('div');

    var clone = div.style;
    div.setAttribute("id",input.id + "_div");
    clone.position      = 'absolute';
    clone.wordWrap     = 'break-word';
    clone.top           = "-1000px"; 
    clone.left          = "-1000px";
    clone.paddingTop    = getstyle(input,'padding-top') || getstyle(input,'paddingTop');
    clone.paddingRight  = getstyle(input,'padding-right') || getstyle(input,'paddingRight');
    clone.paddingBottom = getstyle(input,'padding-bottom') || getstyle(input,'paddingBottom');
    clone.paddingLeft   = getstyle(input,'padding-left') || getstyle(input,'paddingLeft');
    clone.fontSize      = getstyle(input,'fontSize') || getstyle(input,'font-size');
    clone.lineHeight    = getstyle(input,'line-height') || getstyle(input,'lineHeight');
    clone.fontFamily    = getstyle(input,'font-family') || getstyle(input,'fontFamily');
    clone.width         = getstyle(input,'width');
    clone.fontWeight    = getstyle(input,'font-weight') || getstyle(input,'fontWeight');
    input.parentNode.insertBefore(div, input);

    // Upddatera
    input.onkeyup();
}

function bd_elatic_textareas()
{
    var textareas = document.getElementsByTagName('textarea');

    for (var i = 0; i < textareas.length; i++)
    {
        var input = textareas[i];

        if ((input.type == 'textarea') && ((input.id.search('elastic') != -1) || (input.className.search('elastic') != -1)))
        {
            bd_elasticise_textarea(input);
        }
    }
};

function getstyle(x,styleProp)
{
	if (x.currentStyle)
	{
		var y = x.currentStyle[styleProp];
	}
	else if (window.getComputedStyle)
	{
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	}

	return y;
};

