/**************************************************
 *                                                *
 * Written by: kent lynch                         *
 *                                                *
 **************************************************/

// returns the index of the selected radio button (ex: getRadioIndex(this.form.active_ind))
function getRadioIndex(buttonGroup){
	theindex = -1;
	for (var i=0; i < buttonGroup.length; i++){
		if (buttonGroup[i].checked) {
			theindex =  i;
		}
	}
	return theindex;
}
// returns the value of the selected radio button (ex: getRadioValue(this.form.active_ind))
function getRadioValue(buttonGroup){
	var thevalue = "";
	for (var i=0; i < buttonGroup.length; i++){
		if (buttonGroup[i].checked) {
			thevalue = buttonGroup[i].value;
		}
	}
	return thevalue;
}

// returns comma delim list of all values within the radio button group (ex: getRadioValues(this.form.active_ind))
function getRadioValues(buttonGroup){
	var listValues = "";
	for (var i=0; i < buttonGroup.length; i++){
		if(listValues > ""){
			listValues += ",";
		}
		listValues += buttonGroup[i].value;
	}
	return listValues;	
}

function confirmButton(buttonGroup,prevvalue_field,confirmStr){
	var rUstr = "Are you sure you want to "; 
	var oldIndex = "";
	var oldValue = "";
	var currentIndex = "";
	var currentValue = "";

	confirmStr = rUstr + confirmStr;
	oldValue = prevvalue_field.value;
	currentIndex = getRadioIndex(buttonGroup); // returns the selected button's index
	currentValue = getRadioValue(buttonGroup); // returns the selected button's value
	var listValues = getRadioValues(buttonGroup); // returns list of all values for the radio button group
//	alert("listValues: " + listValues + "\ncurrentIndex: " + currentIndex + "\ncurrentValue: " + currentValue);

	var arrayValues = listValues.split(","); // convert returned list into an array, loop thru and get the index of the old value
	for (var i=0; i < arrayValues.length; i++) {
		if(oldValue == arrayValues[i]){
			oldIndex = i;
			break;
		}			
	}
	// make sure the user didn't click the currently selectec button
	if(oldIndex!=currentIndex){
		if(confirm(confirmStr)){
			buttonGroup[currentIndex].checked = true;
			prevvalue_field.value = currentValue;	
			return true;
		}
		else{
			buttonGroup[oldIndex].checked = true;
			return false;
		}
	}
}
function setRadioButton(buttonGroup,theValue){
	if(theValue==""){
		// loop thru and clear all checked buttons
		for (var i=0; i < buttonGroup.length; i++) {
			buttonGroup[i].checked = false;
		}
	}
	else{
		var theIndex = -1;
		var listValues = getRadioValues(buttonGroup);
		var arrayValues = listValues.split(","); // convert returned list into an array
		//	alert(listValues);
		// loop thru and find the index of the value
		for (var i=0; i < arrayValues.length; i++) {
			if(theValue == arrayValues[i]){
				theIndex = i;
				break;
			}			
		}
		if(theIndex > -1){
			buttonGroup[theIndex].checked = true;
		}
	}
}


