/********************************************************

FORM functionality

Enters value in label of radio button into destination box if radio button is selected
Clears radio button if destination box is modified

If Javascript is enabled, show radio buttons. If not, the radio buttons should be hidden.

Requires (usually found in main.js)
-----------

	addLoadHandler
	attachEventListener
	getEventTarget

********************************************************/

/*  todos */
// check for methods I'm using



var drConfig = {};
drConfig.textfieldClass = 'place';
drConfig.radioClass = 'popDestRadio';
drConfig.textfield;
drConfig.radioFieldsetClass = 'popDest';
drConfig.radios = [];
drConfig.radioLabels = [];

addLoadHandler(initTextfieldRadio);

function initTextfieldRadio() {
	if ( typeof document.getElementsByTagName != 'undefined' ) {
		// find textfield that will be cleared, and or set
		// find also radio buttons
		// Note that it will find only the first input element with the classname specified.
		var inputs = document.getElementsByTagName('input');
		var len = inputs.length;
		
		for (var i = 0; i < len; i++) {
			var currInput = inputs[i];
			if ( currInput.className == drConfig.textfieldClass ) {
				drConfig.textfield = currInput;
				continue;
			}
			if (currInput.type == 'radio') {
				if (currInput.className == drConfig.radioClass) {
					// need to use this syntaxt because IE5 don't support array.push();
					drConfig.radios[drConfig.radios.length] = currInput;
					drConfig.radioLabels[drConfig.radioLabels.length] =  currInput.nextSibling;
				}
			} else {
				continue;
			}
		}
		
		// set up eventlisteners
		// if the value of the textfield is changed
		attachEventListener(drConfig.textfield, "keydown", onKeydownTextfield, false);
		// if a radiobutton is selected
		// or if a label is selected
		var len = drConfig.radios.length;
		for ( var i = 0; i < len; i++ ) {
			attachEventListener(drConfig.radios[i], "mousedown", onMouseDownRadio, false);
			attachEventListener(drConfig.radioLabels[i], "mousedown", onMouseDownRadio, false);
		}
		
		// and display radio buttons
		var fieldsets = document.getElementsByTagName('fieldset');
		var len = fieldsets.length;
		for ( var i = 0 ; i < len; i++ ) {
			var currFs = fieldsets[i];
			if (currFs.className == drConfig.radioFieldsetClass ) {
				currFs.style.display = 'block';
			} else {
				continue;
			}
		}
	}	
}

function onKeydownTextfield(event) {
	// find out what key was pressed
	if (typeof event == "undefined" ) {
		event = window.event;
	}
	
	switch (event.keyCode) {
		// for these keys, do nothing
		case 9:		// tab
		case 13:		// enter
		case 16:		// shift
		case 17:		// ctrl
		case 18:		// alt
		case 20:		// caps lock
		case 33:		// page up
		case 34:		// page down
		case 35:		// end
		case 36:		// home
		case 37:		// left arrow
		case 39:		// right arrow		
		case 27:		// esc
		case 38:		// up arrow
		case 40: 	// down arrow
			break;
		// any of these means that the value of the textbox has actually changed.
		case 8:		// backspace
		case 46:		// delete
		default:		// the rest
			// so clear the radios
			var len = drConfig.radios.length;
			for (var i = 0; i < len; i++) {
				drConfig.radios[i].checked = false;
			}
	}
}

// Same function used for both radios and labels
function onMouseDownRadio(event) {
	if (typeof event == "undefined" ) {
		event = window.event;
	}
	
	// find out what the label for the radio button that was clicked is
	var target = getEventTarget(event);
	var radioLabel = '';
	if ( target.tagName.toLowerCase() == 'input' ) {
		var radioLabel = target.nextSibling.firstChild.nodeValue;
	} else if ( target.tagName.toLowerCase() == 'label' ) {
		
		var radioLabel = target.firstChild.nodeValue;
		// also need to set radio button, because Safari 2 doesn't seem to do this automatically
		target.previousSibling.checked = true;
	}
	
	// set value of textbox to that value
	drConfig.textfield.value = radioLabel;
}
