// Get element with specified id
function _e(id) {
	return document.getElementById(id);
}

var _activeForm = null;

function setActiveForm(nameOrIndex) {
	_activeForm = document.forms[nameOrIndex];
}

// Get input control with specified name
function _i(name) {
	if(_activeForm==null) {
		_activeForm = document.forms[0];
	}
	return _activeForm.elements[name];
}

// Get value of input control with specified name
function get(name) {
	var input = _i(name);
	if(typeof(input) == 'undefined') {
		throw new Error("get: unknown form field: " + name);
	}
	if(typeof(input.length) != 'undefined') {
		// we're dealing with multiple input controls sharing
		// the same name or with a RADIO/SELECT input control.
		if(input[0].type == 'radio') {
			return getRadioValue(name);
		}
		if(input.type == 'select-one') {
			return getSelectValue(name);
		}
		throw new Error("get: duplicate name for input " + name + " (type: " + input.type + ")");
	}
	if(input.type == "checkbox") {
		return input.checked;
	}
	return input.value;
}

// set value of input control with specified name
function set(name,val) {
	var input = _i(name);
	if(typeof(input) == 'undefined') {
		throw new Error("set: unknown form field: " + name);
	}
	if(typeof(input.length) != 'undefined') {
		if(input[0].type == 'radio') {
			setRadioValue(name,val);
		} else if(input.type == 'select-one') {
			setSelectValue(name,val);
		} else {
			throw new Error("set: duplicate name for input " + name + " (type: " + input.type + ")");
		}
	} else if(input.type == 'radio') {
		// there is only one radio button in the radio button group
		if(val==input.value) input.checked = true;
		else input.checked = false;
	} else if(input.type == 'checkbox') {
		// when setting a checkbox you must pass a boolean value
		input.checked = val;
	} else {
		input.value = val;
	}
}

// Get value of the currently checked radio button
function getRadioValue(name) {
	var radios = _i(name);
	for(var i=0;i<radios.length;++i) {
		if(radios[i].checked) return radios[i].value;
	}
	return null;
}

// Get index of the currently checked radio button
function getRadioIndex(name) {
	var radios = _i(name);
	for(var i=0;i<radios.length;++i) {
		return i;
	}
	return -1;
}

// Get value of the currently selected option
function getSelectValue(name) {
	return _i(name).options[_i(name).selectedIndex].value;
}

// Get text of the currently selected option
function getSelectText(name) {
	return _i(name).options[_i(name).selectedIndex].text;
}

// Get index of the currently selected option
function getSelectIndex(name) {
	return _i(name).selectedIndex;
}

// Check radio button with specified value and uncheck all others.
// So it's NOT the value of a radio button that is changed; rather
// the button that has the specified value will be checked.
// If data type of value is "number", value will be interpreted as
// an index into the buttons array; otherwise as a search criterium
// for the values in the buttons array.
// Specifying a non-existent value unchecks all radio buttons.
function setRadioValue(name, value) {
	var radios = _i(name);
	if(typeof(value) === "number") {
		for(var i=0;i<radios.length;++i) {
			if(i == value) radios[i].checked = true;
			else radios[i].checked = false;
		}
	} else if(typeof(value) === "string") {
		for(var i=0;i<radios.length;++i) {
			if(radios[i].value === value) radios[i].checked = true;
			else radios[i].checked = false;
		}
	}
}

// Select the SELECT option with the specified value.
function setSelectValue(name,val) {
	var options = _i(name).options;
	for(i=0; i<options.length; ++i) {
		if(options[i].value == val) {
			options[i].selected = true;
		} else {
			options[i].selected = false;
		}
	}
}

// increment value of input control with specified name
function increment(name) {
	var i = parseInt(get(name),10);
	set(name,++i);
}

// decrement value of input control with specified name
function decrement(name) {
	var i = parseInt(get(name),10);
	set(name,--i);
}

function setFormTarget(target) {
	frm = _activeForm;
	if(frm==null) {
		frm = document.forms[0];
	}
	frm.target = target;
}

// submit form with or without first setting form action
function submitForm(action) {
	if(_activeForm==null) {
		_activeForm = document.forms[0];
	}
	if(arguments.length >= 1) {
		_activeForm.action = action;
	}
	_activeForm.submit();
}

// dumps all properties & their values into a string
function dump(obj,separator) {
	var sep = ((arguments.length < 2) ? "; " : separator);
	var str = "";
	for(prop in obj) {
		str += (prop + ": " + obj[prop] + sep);
	}
	return str;
}
