var pie = null;

function keyUpHandler()
{
	sum = 0;
	vars = '?';
	
	$$('.percent input.text').each(function(contactValue){
		//contactValue.value = contactValue.value.replace(/[^\d]/g, '');
		
		var intValue = parseInt(contactValue.value);
		if (isNaN(intValue)) {
			intValue = 0;
			contactValue.value = '';
		} else {
			contactValue.value = intValue;
		}
		
		sum += intValue;
		vars += contactValue.name + '=' + intValue + '&';			
	});

	$('option_sum').value = sum;
			
	if (sum > 100) {
		$('error').style.display = 'block';
	} else {
		pie.style.display = 'block';
		$('pieimg').src = '/imagegen/pie' + vars;				
		if (sum == 100) {
			$('error').style.display = 'none';
		}
	}
}

Event.observe(window, 'load', function()
{
	var sum = 0;
	
	var submitbutton = $('bt_submit');
	
	var	vars = '?';
	pie = $('pie');
	
	var gotInputs = false;
  	
	$$('.percent input.text').each(function(contact){
		gotInputs = true;
		Event.observe(contact, 'keyup', keyUpHandler)
	});
	
	if (gotInputs) {
		keyUpHandler();
	}
});