/*-----------------------------------------------------------------------

 (c) 2001 e-point S.A.          http://www.e-point.pl/

  $Id: form.js,v 1.1.1.1 2007/12/13 10:29:09 wooki Exp $
-----------------------------------------------------------------------*/

function getSelectedIndex(obj) {
	obj = (typeof(obj) != "object")? eval(obj) : obj;
//	if(obj == self._UNDEFINED_) return null;
	if(obj.type == "select-one") {
		return obj.selectedIndex;
	} else
	if(obj.type == "radio" || obj.type == "checkbox") {
		if(obj.checked) return 0;
		else return -1;
	} else
	if(obj.length && obj.length > 0 && obj[0].type == "radio") {
		for(var i=0; obj[i]; i++) {
			if(obj[i].checked) return i;
		}
		return -1;
	} else
	if(obj.length && obj.length > 0 && obj[0].type == "checkbox") {
		var result = new Array();
		for(var i=0; obj[i]; i++) {
			if(obj[i].checked) result[result.length] = i;
		}
		return result;
	}
	return null;
}

function getElementValue(obj) {
	obj = (typeof(obj) != "object")? eval(obj) : obj;
//	if(obj == self._UNDEFINED_) return null;
	var index = getSelectedIndex(obj);
	if(index == null || index < 0) return null;
	if(obj.type == "select-one") {
		return obj[obj.selectedIndex].value;
	} else
	if(obj.type == "radio" || obj.type == "checkbox") {
		return obj.value;
	} else
	if(obj.length && obj.length > 0 && obj[0].type == "radio") {
		return obj[index].value;
	} else
	if(obj.length && obj.length > 0 && obj[0].type == "checkbox") {
		var result = new Array();
		for(var i=0; i<index.length; i++) {
			result[result.length] = obj[index[i]].value;
		}
		return result;
	}
	return null;
}

function setSelectedIndex(obj, index) {
	obj = (typeof(obj) != "object")? eval(obj) : obj;
//	if(obj == self._UNDEFINED_) return;
//	if(index == self._UNDEFINED_ || index < 0) return;
	if(obj.type == "select-one") {
		obj.selectedIndex = index;
	} else
	{
		obj[index].checked = true;
	}
}

function setSelectedIndexByValue(obj, value) {
	obj = (typeof(obj) != "object")? eval(obj) : obj;
//	if(obj == self._UNDEFINED_) return;
//	if(value == self._UNDEFINED_) return;
	for(var i=0; obj[i]; i++) {
		if(obj[i].value == value) {
			if(obj.type == "select-one") {
				obj.selectedIndex = i;
			} else
			{
				obj[i].checked = true;
			}
			break;
		}
	}
}
