// these functions will go in a javascript include to be included on each page that needs to process a form

function getCheckBoxVal(myElement) {
    if (myElement.checked==true) {
        return(getNum(myElement.value));
    } else {
        return 0;
    }
}


function getSelectVal(myElement) {
    return(getNum(myElement.value));
}


function getSelectSplitVal(myElement) {
    tArray=myElement.value.split("|");
    if (tArray.length > 1) {
        return getNum(tArray[1]);
    } else {
        return 0;
    }
}



function getRadioVal(myElement) {
    tmpval=0;
    for (i=0;i<myElement.length;i++) {
        if (myElement[i].checked==true) {
            tmpval = getNum(myElement[i].value);
            break;
        }
    }
    return tmpval;
}


function getRadioSplitVal(myElement) {
    tmpval=0;
    for (i=0;i<myElement.length;i++) {
        if (myElement[i].checked==true) {
            tArray=myElement[i].value.split("|");
            if (tArray.length > 1) {
                tmpval = getNum(tArray[1]);
            } else {
                tmpval = 0;
            }
            break;
        }
    }
    return tmpval;
}

function getTextVal(myElement) {
    tmpval=getNum(myElement.value);
    return tmpval;
}

function getNum(x) {
    return isNaN(parseFloat(x)) ? 0 : parseFloat(x)
}


function formatDecimal(amount){
    if (isNaN(parseFloat(amount))) {
        return "0.00";
    } else {
        var tmpString = new String(amount);
        if(tmpString.indexOf(".") == -1) {
            tmpString = tmpString + ".00";
        } else {
            var startDec = tmpString.indexOf(".");
            var strBegin = tmpString.substring(0, startDec);
            var strEnd = tmpString.substring(startDec, tmpString.length);
            if(strEnd.length >= 4){
                var strEnd = strEnd.substring(0,4);
                var endVal = strEnd.charAt(strEnd.length - 1);
                if(endVal >= 5){
                   endVal = ".00" + endVal;
                   strEnd = Number(strEnd) + (.01 - endVal);
                } else { 
                   strEnd = strEnd.substring(0,3);
                }
            }
            if(strEnd.length < 3) {
                tmpString = (Number(strBegin) + Number(strEnd)) + "0";
            } else { 
                tmpString = Number(strBegin) + Number(strEnd);
            }
        }
        return tmpString;
    }
}


// Match drop down box to selected value
// setSelect(this,value)
function setSelect(theObject,a) {
	for (i=0;i<theObject.options.length;i++) {
		if (theObject.options[i].value==a) {
			theObject.selectedIndex=i;
			break;
		}
	}
}


// Match radio button to selected value
// setRadio(this,value)
function setRadio(theObject,a) {
	for (i=0;i<theObject.length;i++) {
	    //alert(theObject[i].value + ":" + a)
		if (theObject[i].value==a) {
			theObject[i].checked=true;
			break;
		}
	}
}


// Match split radio button to selected value
// setSplitRadio(this,value)
function setSplitRadio(theObject,a) {
    //alert(a);
	for (i=0;i<theObject.length;i++) {
	    tArray=theObject[i].value.split("|");
        if (tArray.length > 1) {
            tmpval = tArray[0];
        } else {
            tmpval = "";
        }
	    //alert(tmpval + ":" + a)
		if (tmpval==a) {
			theObject[i].checked=true;
			break;
		}
	}
}


// Check that a radio selection is made
function checkfor1Radio(theForm,q) {
    for (i=0;i<theForm.elements[q].length;i++) {
        if (theForm.elements[q][i].checked==true) {
            //alert(theForm.elements[q][i].value);
            return true;
            break;
        }
    }
    return false;
}


// toggle Checkbox on/off by clicking adjacent text
// useage toggleCheckbox( formname a , elementname b )
function toggleCheckbox(a,b) {
    if (document.forms[a].elements[b].checked==false) {
        document.forms[a].elements[b].checked = true;
    } else {
        document.forms[a].elements[b].checked = false;
    }
}

// toggle to radio button by clicking adjacent text
// usage toggleRadio( formname a, elementname b, button_order c)
function toggleRadio(a,b,c) {
    //alert(a);
    //alert(myForm.name)
    //alert(document.forms[a].elements[b][c].checked);
    if (document.forms[a].elements[b][c].checked==false) {
        document.forms[a].elements[b][c].checked=true;
    }
    //if (a.elements[b][c].checked==false) {
    //    a.elements[b][c].checked=true;
    //}
}

function noUnCheck(myCheckBox) {
    myCheckBox.checked=true;
    alert("You must check this value to continue.");
}


