// Based on: http://www.quirksmode.org/book/examplescripts/maxlength/
// Modified for a single text area with a preset max length.
// http://www.sandersweb.net/comments/maxlength.js
// (as modified, for original see above webpage) Copyright (C) 2011 David Sanders
// <textarea> does not have a 'maxlength' attribute so this script
// provides a visual count of current and maxlength to aide in filling
// out the form.
// Updated: 2011-07-01

var W3CDOM = document.createElement && document.getElementById;
var maxLength = 480;  //preset max length

function checkMaxLength() {
	var currentLength = this.value.length;
	if (currentLength > maxLength) {
		this.relatedElement.className = 'toomuch';
	} else {
		this.relatedElement.className = '';
	}
	this.relatedElement.firstChild.nodeValue = currentLength;
}

function setMaxLength() {
	if (!W3CDOM) { return; }
	// 'comments' is id of the <textarea>
	var myTextArea = document.getElementById('comments'); 
	var counter = document.createElement('div');
	counter.className = 'counter_max';
	var counterClone = counter.cloneNode(true);
	counterClone.innerHTML = '<span>0</span>/'+maxLength;
	myTextArea.parentNode.insertBefore(counterClone,myTextArea.nextSibling);
	myTextArea.relatedElement = counterClone.getElementsByTagName('span')[0];
	myTextArea.onkeyup = myTextArea.onchange = checkMaxLength;
	myTextArea.onkeyup();
}

function init() {
	setMaxLength();
}

