/*
 * Sets the given radio element to the specified value.
 */
function checkRadio( radioField, value ) {

	for( var n = 0; n < radioField.length; n++) {
		if( radioField[n].value == value ) {
			radioField[n].checked = true;
			break;
		} else {
			radioField[n].checked = false;
		}
	}

}

/*
 * Returns teh value of the specified radio element.
 */
function getRadioValue( radioField ) {
	for( var n = 0; n < radioField.length; n++) {
		if( radioField[n].checked == true ) {
			return radioField[n].value;
		}
	}

}

/*
 * Sets the given checkbox element to the specified value.
 */
function checkBox( checkField, value ) {

	if( checkField ) {

		if( value ) {
			for( var x = 0; x < checkField.length; x++ ) {
				if( checkField[x].value == value ) {
					// the winner!
					if( checkField[x].checked ) checkField[x].checked = false;
					else checkField[x].checked = true;
					break;
				}
			}

		} else {
			if( checkField.checked ) checkField.checked = false;
			else checkField.checked = true;
		}
	}
}

/*
 * Selects the element with the specified value in the given select field.
 */
function selectOption( selectField, value ) {

	if( selectField ) {

		if( value ) {
			for( var x = 0; x < selectField.options.length; x++ ) {
				if( selectField.options[x].value == value ) {
					selectField.selectedIndex = x;
					break;
				}
			}

		}
	}
}

/*
 * Returns the value of the selected element for the specified select field.
 */
function getSelectValue( selectField ) {
	return( selectField.options[ selectField.selectedIndex ].value );
}

/*
 * Returns the sring specified with whitespace on both sides removed.
 */
function trim( str ) {

	if( !str ) {
		return "";
	}

	var newstr = "";
	var idx = 0;

	for( var i = 0; i < str.length; i++ ) {
		if( str.charAt( i ) == ' ' ) {
			idx++;
		} else {
			break;
		}
	}

	if( idx == str.length ) return "";

	var idxr = str.length-1;
	for( var i = str.length-1; i >= 0; i-- ) {
		if( str.charAt( i ) == ' ' ) {
			idxr--;
		} else {
			break;
		}
	}
	return str.substring( idx, idxr+1 );
}
