/** 
* @projectDescription	Javascript library for forms on Aggregame.com
*
* @author	Ian Paterson
*/

/**
 * Toggles page elements related to the provided radio button or checkbox. 
 * Choices must have the same id as the radio button with a '_choice' suffix
 * and must respond to the standard o.toggle() function.
 *
 * @param {Object} The radio <input> object
 */
function toggleSelected(o) {
	if (!(o = _(o))) return;

	if (doOnce) return;
	doOnce = 1;

	var id = o.id.replace(/\d+$/,'');

	var i = 0;

	while (_(id + i) && i < 100) {
		_(id.replace('field_','') + i + '_choice').toggle(_(id + i).checked);
		i++;
	}

	setTimeout('doOnce = 0', 100);
}

function forceSelect(o) {
	o = _(o);

	if (doOnce) return;

	_('field_' + o.id.replace('_choice','')).checked = !_('field_' + o.id.replace('_choice','')).checked;
	_('field_' + o.id.replace('_choice','')).onchange();
}

function blockSelection() {
	doOnce = 1;
	setTimeout('doOnce = 0', 100);
}

function validationAssist(o, pattern) {
	o = _(o);

	if (!o) return;

	if (pattern)
		$.validationPatterns[o.id] = pattern;

	o.parentNode.style.width = o.offsetWidth + 'px';

	o.onkeydown = validation;
}


function validation(e) {
	if (!e) e = window.event;
	var o = e.target || e.srcElement;
	o = _(o);
	if (!o) return;

	var id = o.id.replace('field_', '');
	var bar = _(id + '_validation').child('div');

	if (o.nodeType == 3) // defeat Safari bug
		o = o.parentNode;

	clearTimeout($.validationTimer);

	if (!o.onkeyup)
		o.onkeyup = function() { clearTimeout($.validationTimer); };

	// Try to get the maxlength the right way
	var max = o.getAttribute('maxlength');

	// Get the maxlength this way if the other didn't work
	if (!max) {
		max = o.maxLength;
	}

	setTimeout(function() {
		// Calculate the percent remaining with decimals
		var p = Math.round(1000000 * (o.value.length) / max) / 10000;
		p = (p < 100) ? p : 100;

		if (p > 80) {
			var warn = (p - 80) / 20;
			if (warn > 1) warn = 1;
			var color = {
				r: -17,
				g: -170,
				b: -170
			};

			for (i in color) { color[i] = Math.round(color[i] * warn + 170) || '00' }

			bar.style.backgroundColor = '#' + color.r.toString(16) + color.g.toString(16) + color.b.toString(16);

			for (i in color) { color[i] = color[i] * 1 + 50 }


			bar.style.borderColor = '#' + color.r.toString(16) + color.g.toString(16) + color.b.toString(16);
		}
		else {
			bar.style.backgroundColor = '#AAA';
			bar.style.borderColor = '#EEE';
			bar.style.borderBottomColor = '#CCC';
		}

		if (p > 100) p = 100;

		bar.style.width = p+ '%';
	}, 15);

	// If the browser does not repeat events when the key is held down, use a timeout instead
	$.validationTimer = setTimeout('validationAssist()', 1);

	var key = e.keyCode || e.which;

	var s = document.selection; 
	if (o.selectionStart != undefined) 
		s = o.selectionStart != o.selectionEnd;

	if (o.value.length > max)
		o.value = o.value.substr(0, max);

	if (o.value.length >= max && !(/^(8|3[789]|40|46|1[678]|3[3456]|20)$/.test(key) || e.ctrlKey || e.altKey || s)) {
		return false;
	}

	return true;
}


function adjustRows(o) {
  o = _(o);
	if (!o) return;

	var id = o.id.replace('field_','');

	var d = _('rowhelper_' + id);

	if (!d.id) {
		d = document.createElement('div');
		d.id = 'rowhelper_' + id;
		d.style.position = 'absolute';
		d.style.visibility = 'hidden';
		d.className = o.className;
		d.style.fontSize = o.style.fontSize;
		d.style.width = o.offsetWidth + 'px';
		d.style.fontWeight = 'normal';
		var c = document.createElement('div');
		c.style.overflow = 'hidden';
		c.style.height = '1px';
		o.parentNode.appendChild(c.appendChild(d));

		o.style.overflow = 'hidden';
		o.style.overflowX = 'auto';
	}

	d.style.display = 'block';

	d.innerHTML = o.value.replace(/([<\n])/g,function($1){return ($1 == '<') ? '&lt;' : '<br />'}) + '<br />';

	if (d.offsetHeight >= o.offsetHeight - 16 || d.offsetHeight <= o.offsetHeight - 60) {
		o.style.height = d.offsetHeight + 50 + 'px';
	}

	d.style.display = 'none';

	clearTimeout($.adjustRowTimer);
	$.adjustRowTimer = setTimeout("adjustRows('" + o.id + "')", 100);
}