/* WARNING!!!!   WARNING!!!! WARNING!!!!
Jennifer Dodson: 04/30/2001
This is a modified version of the standard utility.js.  It is for use ONLY on PML.com
*/
							
function checkLength(testStr,minChars,maxChars)
{	if((minChars!=null) && (testStr.length<minChars)) return -1;
	else if ((maxChars!=null) && (testStr.length>maxChars))	return 1;
	else return 0
}
function checkFloatRange(testValue,minValue,maxValue)
{	if((parseFloat(minValue) != null) && (testValue < parseFloat(minValue))) return -1;
	else if ((parseFloat(maxValue) != null) && (testValue > parseFloat(maxValue))) return 1;
	else return 0;
}
function getProperty(curElement,curProperty)
{   var curValue = eval("curElement." + curProperty);
    switch(curValue != null)
	{	case false: return eval("curElement.currentStyle['"+curProperty+"']");
		default: return curValue;
	}
	return curValue;
}
function listFirst(curList)
{	var firstElement;
	var pos = curList.indexOf(",");
	switch(pos>0)
	{	case true: return curList.substring(0,pos);
		case false: return curList;
	}
}
function listRest(curList)
{	var pos=curList.indexOf(",");
	switch(pos>0)
	{	case true: return curList.substring(pos+1,curList.length);
	  	case false: return "";
	}
}
function parseDate(dateString)
{	switch(dateString == null)
	{	case true:	return null;
		default:
			var dateArray = dateString.split("/");
			var parsedDate = new Date(dateArray[2],dateArray[0]-1,dateArray[1],0,0,0,0);
			return parsedDate.valueOf();
	}
}

//removes specified charachers from the string.  Returns the newly modified string.
function stripCharsFromString (curString, charsToRemove)
{	var patternString = "["+charsToRemove+"]";
	var pattern = new RegExp(patternString,'g');
	return curString.replace(pattern,"");
}

//on page visual effect for errors.
function labelColorSwitch (fieldArray,switchBit){
	defaultColor = '#000066';
	switchColor = '#990000';
	for(var i=0;i<fieldArray.length;i++){
		labelID = fieldArray[i].labelID;
		color=defaultColor;
		if(switchBit==1){color=switchColor;}
		document.getElementById(labelID).style.color = color;
	}
}	
		 
//set the default checked option for a group of radio buttons or checkboxes
function setRadioOrCheckbox(e,v){
	formElement = e;
	defaultValue = v;	
	//If there's only one checkbox in the group
	if(formElement.length==null)
	{
		if(formElement.value==defaultValue){
			formElement.checked=true;
			return true;
		}
	}	 
	//if there are multiple items in the group
	else
	{		
		for(i=0;i<=formElement.length;i++){
			if(formElement[i].value==defaultValue){
				formElement[i].checked=true;
				return true;
			}
		}
	}
}

//set the default value on a select box
function setSelectBox(e,v){		
	formElement = e;
	defaultValue = v;
	for(i=0;i<=formElement.options.length-1;i++)
		if(formElement.options[i].value==v)
			formElement.selectedIndex=i;
}

function moneyDisplayHandler(elementList){
		for(i=0;i<elementList.length;i++){
			currentElement = document.getElementById(elementList[i]);
			currentElement.value = formatAsMoney(currentElement.value);
		}
	}
			
//formats number into a percision 2 decimal. 
function formatAsMoney(mnt)  
{
	//mnt is the amount to be formatted.
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}
	
//causes a set of checkboxes to behave like radio buttons
//used on the CAS tabs primarily.
/*formName = the form the fields belong to. 
	fieldNames is an array of field names to have the behavior.  These are the "columns" of buttons.
	clicked = the checkbox that was clicked
	index = the index of checkbox that was clicked. All other boxes in the fields (fieldnames) with that index will be unchecked
*/																		
function radioCheckbox(formName, fieldNames,clicked,index)
{				 
	//if the box was being unchecked, then return false
	if(clicked.checked==false)
	{
 		clicked.checked=true;	
		return false;
	}			 
	 		
	//loop through the field names and check or uncheck appropriately
	for(i=0;i<fieldNames.length;i++)
	{
		//if the field being evaluated is not the field of the box that was checked		 
		if(clicked.name != fieldNames[i])
		{
			// get the checkbox at the index for the field
			temp = eval(formName+"."+fieldNames[i]);			
			//uncheck the checkbox.
			temp[index].checked = false;
		}
	}
}
function calcTotals(inputElementName, outputElement)
{
	var r;
	var total = 0.00;

	var inputElement = document.getElementById(inputElementName);
	if (inputElement!=null)
	{
		for (r=0;r < inputElement.length;r++)
		{
			total+=Math.round(inputElement.options[r].value);
		}
	}
	var o = document.getElementById(outputElement);
	if (o.firstChild) // clear the old sum...
		o.removeChild(o.firstChild);
	// display the new sum...
	o.appendChild(document.createTextNode(formatAsMoney(total)+''));
} 

function changeChildOnChangeOfParent(parentId,childId,childArray,parentValue)
    {	//statesArray[]
    	var index;
    	if(parentId==''){
	    	index = parentValue;
	    }else{
	    	parentSelect = document.getElementById(parentId);
	    	index = parentSelect.value;
	    }
    	childSelect = document.getElementById(childId);
    	if((!childSelect)||(childSelect.options==null))return false;
	
       while (childArray[index].length < childSelect.options.length)
        {
          childSelect.options[(childSelect.options.length - 1)] = null;
        }

       if(childArray[index].length>=1)
        for (k = 0; k < childArray[index].length; k++)
        {

           childSelect.options[k] = new Option(childArray[index][k].text, childArray[index][k].value);
        }
       else
        childSelect.options[childSelect.options.length]=new Option('       ','',0,0);

       // select the first item in theSecondDropDownField
       childSelect.options[0].selected = true;
      }
function calcValues(elementsArray,totalDestination){
		totalDestination = document.getElementById(totalDestination);
		value=0;
		for(i=0;i<elementsArray.length;i++){
			currentElement = document.getElementById(elementsArray[i]);
			currentElementValue = new Number(currentElement.value);
			value = value + currentElementValue;
		}
		value = formatAsMoney(value);
		totalDestination.value = value;
	}
function calcSelectValues(selectObject,totalDestination){
		selectObject = document.getElementById(selectObject);
		totalDestination =  document.getElementById(totalDestination);
		value=0;
		for(i=0;i < selectObject.length;i++){
			optionValue = new Number(selectObject.options[i].value);
			value =value + optionValue;
		}
		value = formatAsMoney(value);
		totalDestination.value = value;
	}
function populateValues(fromList,toList){
		for(i=0;i<fromList.length;i++){
			fromElement = document.getElementById(fromList[i]);
			toElement = document.getElementById(toList[i]);
			value = fromElement.value;
			toElement.value = value;
		}
	}
function setFormAction(formToEffect,formAction){
		formObject = eval("document."+formToEffect);
		formObject.action = formAction;
		formObject.submit();
}