// Get checked value from radio button.
function getRadioButtonValue (radio) {
	for (var i = 0; i < radio.length; i++) {
		if (radio[i].checked) {
			break;
		}
    }
    return radio[i].value;
}

// Round numbers to whatever you want!
function roundNumber (number, roundTo) {
	roundedNumber = Math.round(number*Math.pow(10,roundTo))/Math.pow(10,roundTo);
	roundedStr = "" + roundedNumber;
	if (roundedStr.indexOf(".")==-1) {
		roundedStr+=".00"
	}
	beforePeriodStr = roundedStr.substr(0,roundedStr.indexOf("."));
	afterPeriodStr = roundedStr.substr(roundedStr.indexOf("."))
	while (afterPeriodStr.length<3) {
		afterPeriodStr += "0"
	}
	returnNumber = beforePeriodStr + afterPeriodStr;
	return returnNumber;
}

// Removes all characters which appear in regexp bag from string s.
// NOTES:
// 1) bag must be a regexp which matches single characters in isolation,
//    i.e. A or B or C or D or 1 or 2 ...
//    e.g. /\d/g  or /[a-zA-Z]/g
// 2) make sure to append the 'g' modifier (for global search & replace)
//    at the end of the regexp
//    e.g. /\d/g  or /[a-zA-Z]/g
function stripCharsInRE (s, bag) {
	return s.replace(bag, "")
}

// Removes all characters which do NOT appear in string bag from string s.
function stripCharsNotInBag (s, bag) {
	var i;
    var returnString = "";

    for (i = 0; i < s.length; i++) {   
		var c = s.charAt(i);
        if (bag.indexOf(c) != -1) {
			returnString += c;
		}
    }
    return returnString;
}

// Removes all whitespace characters from s.
function stripWhitespace (s) {
	return stripCharsInBag (s, whitespace);
}

// Removes initial (leading) whitespace characters from s.
function stripInitialWhitespace (s) {
	var i = 0;
	while ((i < s.length) && charInString (s.charAt(i), whitespace)) {
       i++;
	}
	return s.substring (i, s.length);
}

/*
Add .00 to values that do not have the decimal.
*/

function addZeros(val) {
	var numStr;
	var numTrail = 0;
	var ptrFlag = 0;
	var i=0;

	numStr = ""+ val;

	for( i=0; numStr.charAt(i); i++)
	{
		if( ptrFlag )
			numTrail++;

		if(  numStr.charAt(i) == "." )
			ptrFlag++;			
	}

	if(ptrFlag == 0 && numTrail == 0 && numStr.length >= 10)
		return numStr;
		
	if(ptrFlag == 0 && numStr.length <= 9)
		numStr = numStr + ".00";

	if( numTrail == 1 && numStr.length <= 11) 
		numStr = numStr + "0";
		
	if(ptrFlag == 1 && numTrail == 0 && numStr.length <= 10)
		numStr = numStr + "00";

	return numStr;
}

/*************************************************************
Recommended use for all of the below functions:
On the JSP page where your form object(s) is located,
write a small function to run "OnLoad" which will call the below functions.
In the method call, pass in the select object and use a 
<jsp:getProperty name=" " property=" "/> to pass in the option value 
to be set.
**************************************************************/


/* setSelectBox()
Description: Use to pre-select an option in a selectbox by 
passing in the targeted selectbox object and the value
used to find the appropriate option.  

valueType: represents what attribute of the select box you 
				are passing in. ** case sensitive **
Can either be:
"value": meaning the value attribute of the <option value=""> tag.
"text": meaning the display text between the <option>"...."</option> tags.

You MUST pass in the selectbox object itself, not it's name.  The 
value "IS" case-sensitive.
*/

function setSelectBox(selectBox, valueType, newValue) {
	if (valueType == "value") {
		for(i=0; i<selectBox.length; i++) {
			if (selectBox.options[i].value == newValue) {
				selectBox.options[i].selected = true;
			}
		}
	} else if (valueType == "text") {
		for(i=0; i<selectBox.length; i++) {
			if (selectBox.options[i].text == newValue) {
				selectBox.options[i].selected = true;
			}
		}
	}
}


/* setRadioButton()
Description: Use to pre-select a radio button on a page.
Radio buttons are designed by each button in a group to have
the same name.  Therefore, only one can be checked at a time.
The browser then creates an array of those buttons with the 
same name.  However, each radio button tag has a value attribute
which can be set to a unique value for each radio button.
To use this function, pass in the radio button object 
and the value of the button you want checked.
*/
function setRadioButton(radioButton, value) {
	for(i=0; i<radioButton.length; i++) {
		if (radioButton[i].value == value) {
			radioButton[i].checked = true;
		}
	}
}


/* setCheckBoxGroup()
Description: Use to pre-select a checkbox on a page.
If you want to select multiple checkboxes at a time, name them
all the same but change each value attribute to a unique value
for that checkbox.  The browser then creates an array of those 
checkboxes with the same name.
To use this function, pass in the checkbox object 
and an array of values of the boxes you want checked. 
*/
function setCheckBox(checkBox, newValue) {
	for(i=0; i<checkBox.length; i++) {
		for (j=0; j<checkBox.length; j++) {
			if (checkBox[i].value == newValue) {
				checkBox[i].checked = true;
			}
		}
	}
}
