// The functions GetRadioValue, GetSelectValue, AssignSelect, AssignRadio, and CheckIfNumber,
// can have either a string, or an object passed to them as an argument.  This is so if you have only
// one form on your page, you can simply refer to the form element by name.  Similarly, it's also handy to 
// use "this" as an argument/object if the form element is referring to itself when calling the function.
// (See the "This text field has to be a number" example at http://kip.doit.wisc.edu/JavaScriptSamples/Examples.html)
//
// Kip Rood - kjrood@facstaff.wisc.edu 
// 12/7/2001


function GetRadioValue(theRadioName){
	theRadioSet = MakeObject(theRadioName)
	theValue = null
	if(theRadioSet){
		Radio_i = 0
  		while(theRadioSet[Radio_i] && theValue == null){
  			if(theRadioSet[Radio_i].checked){
  				theValue = theRadioSet[Radio_i].value
  			}
  			Radio_i++
  		}
  	}
  	return theValue;	
}

function GetSelectValue(theSelectName){
	theSelectElement = MakeObject(theSelectName)
	if(theSelectElement){
		return theSelectElement.options[theSelectElement.selectedIndex].value;
	}
	else{
		return null;
	}
}

function GetSelectText(theSelectName){
	theSelectElement = MakeObject(theSelectName)
	if(theSelectElement){
		return theSelectElement.options[theSelectElement.selectedIndex].text;
	}
	else{
		return null;
	}
}

function AssignRadio(theRadioName,theValue){
	theRadioSet = MakeObject(theRadioName)
	if(theRadioSet){
		Radio_i = 0
  		while(theRadioSet[Radio_i]){
  			if(theRadioSet[Radio_i].value == theValue){
  				theRadioSet[Radio_i].checked = true
  			}
  			Radio_i++
  		}
  		return true;
  	}
  	else{
  		return null;
  	}	
}

function AssignCheckBox(theCheckBoxName,theValue){
	theCheckBoxSet = MakeObject(theCheckBoxName)
	if(theCheckBoxSet){
		CheckBox_i = 0
  		while(theCheckBoxSet[CheckBox_i]){
  			if(theCheckBoxSet[CheckBox_i].value == theValue){
  				theCheckBoxSet[CheckBox_i].checked = true
  			}
  			CheckBox_i++
  		}
  		return true;
  	}
  	else{
  		return null;
  	}	
}

function AssignSelect(theSelectName,theValue){
	theSelectElement = MakeObject(theSelectName)
	if(theSelectElement){
		Select_i = 0
		while(theSelectElement.options[Select_i]){
			if(theSelectElement.options[Select_i].value == theValue){
				theSelectElement.options[Select_i].selected = true
			}
			Select_i++
		}
		return true;
	}
	else{
		return null;
	}
}

function MakeObject(theStrOrObj){
	if(typeof(theStrOrObj) == "string"){
		theForm = document.forms[0]
		if(eval("theForm." + theStrOrObj)){	
			theObj = eval("theForm." + theStrOrObj)
		}
	} else if(typeof(theStrOrObj) == "object"){
		theObj = theStrOrObj
	}
	return theObj;
}

function FormatAsCurrency(Currency){
	numCurrency = Currency
	//Make currency a string...
	Currency = Currency + ""
	
	thelength = Currency.length
    if(Currency == ""){
    	return Currency
    }
    else if(Currency.indexOf(".",0) != -1){
    	//If there is a decimal point....
    	decimal = Currency.indexOf(".",0) + 1
    	theNumberofDecimalPlaces = thelength - decimal
    	if(theNumberofDecimalPlaces > 2){
    		if(Number(Currency.substring(decimal+2,decimal+3)) >= 5){
    			
    			Currency = Number(Currency.substring(0,decimal+2)) + .01
    			Currency = String(Currency)
			}
			Currency = Currency.substring(0,decimal+2)
    	}
    	else if(theNumberofDecimalPlaces == 2){
    		Currency = Currency
    	}
    	else if(theNumberofDecimalPlaces == 1){
    		Currency = Currency + "0"
    	}
    }
    else{
    	//If there is not a decimal point....
 		Currency = Currency + ".00"
    }	
    return Currency
}

function CheckIfNumber(theObject){
	theObject = MakeObject(theObject)
	theObject.value = myReplace(theObject.value,",","");
	if(isNaN(theObject.value)){
		alert("Please be sure you have entered a number")
		theObject.value = ""
		theObject.focus()
		return false;
	}
	else{
		return true;
	}
}

function CheckIfMinute(theObject){
	if(CheckIfNumber(theObject)){
		theObject = MakeObject(theObject)
		theObject.value = myReplace(theObject.value,",","");
		if(theObject.value < 0 || theObject.value > 59){
			alert("Please enter a number 1 to 59.")
			theObject.value = ""
			theObject.focus()
			return false;
		}
		else{
			return true;
		}
	}
}

function CheckIfHour(theObject){
	if(CheckIfNumber(theObject)){
		theObject = MakeObject(theObject)
		theObject.value = myReplace(theObject.value,",","");
		if(theObject.value < 1 || theObject.value > 12){
			alert("Please enter a number 1 to 12.")
			theObject.value = ""
			theObject.focus()
			return false;
		}
		else{
			return true;
		}
	}
}

function CheckAll(theCheckBoxes,BigCheck){
	theCheckBoxes = MakeObject(theCheckBoxes)
	BigCheck = MakeObject(BigCheck)
	Check_i = 0
	if(theCheckBoxes){
		if(isNaN(theCheckBoxes.length)){
			theCheckBoxes.checked = BigCheck.checked
		}
		else{
			while(theCheckBoxes[Check_i]){
				theCheckBoxes[Check_i].checked = BigCheck.checked
				Check_i++
			}		
		}

	}
}

function myReplace(theString,Find,Replace){
	theCharNum = 0 
	while(theString.indexOf(Find,theCharNum)!=-1){
		theCharNum = theString.indexOf(Find,theCharNum)
		theString = theString.substring(0,theCharNum) + Replace + theString.substring(theCharNum + Find.length,theString.length)
		theCharNum = theCharNum + Replace.length
	}
	return theString
}
