// JavaScript Document
// Versione 1.0.1
// Creato il 13/10/2009
// Autore Andrea Furlan
function createCharCounter(idField,countDown){
	if (!document.getElementById(idField) || !document.getElementById(idField+'_count')){
		alert("charCounter Setup:\nIl campo specificato non esiste !");
		return false;
	}
	
	if (arguments.length > 1){
		createCharDownCounter(idField,countDown)
		return true;
	}
	
	updateCharCount(idField)
	document.getElementById(idField).onkeydown = function(){
		updateCharCount(idField);
	}
	document.getElementById(idField).onkeyup = function(){
		updateCharCount(idField);
	}
}

function updateCharCount(idField){
	document.getElementById(idField+'_count').value = document.getElementById(idField).value.length;
}

/* Funzioni per gestire il count down di coratteri rimasti */

function createCharDownCounter(idField,countDown){
	updateCharCountDown(idField,countDown);
	/*document.getElementById(idField).onkeydown = function(){
		updateCharCountDown(idField,countDown);
	}*/
	document.getElementById(idField).onkeyup = function(){
		updateCharCountDown(idField,countDown);
	}
}

function updateCharCountDown(idField,countDown){
	if (parseInt(document.getElementById(idField).value.length) > countDown){
		document.getElementById(idField).value = document.getElementById(idField).value.substr(0,countDown);
		alert('Non puoi inserire pił di '+countDown+' caratteri');
	}
	if (document.getElementById(idField+'_count').value)
		document.getElementById(idField+'_count').value = countDown - parseInt(document.getElementById(idField).value.length);
	else
		document.getElementById(idField+'_count').innerHTML = countDown - parseInt(document.getElementById(idField).value.length);
}