/**************************************************************************
 * Limits the Chars of Textarea Elements or Input Elements
 *
 * @Author: Michael Schwarz (M.Schwarz@php-vision.de)
 * @Author: Slightly modified by Daniel Scheel
 * @Date: 14/04/2006											
 * @param: Obj     Das zu überwachende Element
 * @param: maxChr  Maximale Zeichen Anzahl
 * @param: counter ID eines HTML Obj. für Aktuellen Counterstand 
 * @Version: 0.5 ( available on www.php-vision.de ) 
 * -----------------------------------------------------------------------
 * Usage :
 *  <script type="text/javascript" src="textarea-char-limit.js"> </script>  
 *  Currently <span id="counter_field">0</span> Chars of 250 entered<br />
 *  <textarea onfocus="textlimit(this,250,'counter_field');">  </textarea>
 * -----------------------------------------------------------------------
 * Donate to Paypal Account M.Schwarz@php-vision.de
/*************************************************************************/ 

function textlimit(Obj, maxcount, counter)
{
	function c_len()
	{	
		if (this.value.length > this.maxcount && this.maxcount != 0) {
			return false;
		}
		update_count(this);	
	}
	
	function upd_count()
	{
		update_count(this);
	}
	
	function update_count(elem)
	{
		if (elem.counter != null) {
			cnt = getObj(elem.counter);
			cnt.value = elem.maxcount - elem.value.length;
		}
	}
	
	function check()
	{
		if (this.value.length  > this.maxcount && this.maxcount != 0) {
			this.value = this.value.substr(0, this.maxcount);
		}
	    update_count(this); 
	}
	
	Obj.counter   = counter;	  Obj.maxcount  = maxcount;
	Obj.onkeypress= c_len;		Obj.onkeydown = upd_count;	
	Obj.onkeyup   = check;		Obj.onfocus   = check;
	Obj.onblur    = check;		
	Obj.onmouseup = check;

	update_count(Obj);
	
	function getObj(ref) 
	{
		//W3C/ 
		if (document.getElementById(ref) != null) { 
			return document.getElementById(ref);
		//IE5/
		} else if (document.all[ref] != null) { 
			return document.all[ref];
		//NN4/
		} else if (document.layers[ref] != null) { 
			return document.layers[ref];
		//ERR/
		} else {
			alert('Object "' + ref + '" not Found')
		}
	}
}

function update_count_hc(maxcount)
{
	document.getElementById("counter_field").value = maxcount - document.getElementById("comment").value.length;
}
