/*
Program Purpose
**********************************************************************
Used to validate form data,retrieve values, etc.
**********************************************************************

Bug Fix notes or code changes
**********************************************************************
Original Programmer: Jason R. Sebring, 1/3/2002
1)
programmer:
date:
lineNumber:
purpose:
**********************************************************************
*/

function isBlank(str) {
	var blankPattern = /^\s*$/;
	return blankPattern.test(str);
}

 // checks/unchecks all checkboxes in a checkBox group, from a given checkbox
function checkAll(checkControl, checkGroup) {
	var checkAll = checkControl.checked;
	if(checkGroup) {
		checkGroup.checked = checkAll;
	}
	for(var i = 0; i < checkGroup.length; i++) {
		checkGroup[i].checked = checkAll;
	}
}

// checks/unchecks all checkboxes on a form, from a given checkbox
function selectAllCheckBox(frm, checkControl) {
	var checkAll = checkControl.checked;
	for(var i = 0; i < frm.length; i++) {
		if(frm[i].type.toUpperCase() == 'CHECKBOX') {
			frm[i].checked = checkAll;
		}
	}
}

// checks/unchecks all checkboxes on a form, from a given checkbox
function selectSelectAllCheckBox(frm) {
	var checkAll = true;
	for(var i = 0; i < frm.length; i++) {
		if((frm[i].type == 'checkbox') && (frm[i].name != 'selectAll')) {
			if(!frm[i].checked) {
				checkAll = false;
				break;
			}
		}
	}
	if(frm.selectAll) {
		frm.selectAll.checked = checkAll;
	}
}

// uncheckes another checkbox based on if itself is unchecked
function unCheck(checkBoxThis, checkBoxAnother) {
	if(!checkBoxThis.checked) {
		checkBoxAnother.checked = false;
	}
}

// used to handle checkboxes where 'selectAll' exists
function handleCheckBoxes(frm, checkBox) {
	if(checkBox.name == 'selectAll') {
		selectAllCheckBox(frm,checkBox);
	} else if(checkBox.name.indexOf('All') != -1) {
		checkAll(checkBox, frm[checkBox.name.substring(0,(checkBox.name.length-3))]);
		checkGroupAll(frm, checkBox);
		selectSelectAllCheckBox(frm);
	} else {
		checkGroupAll(frm, checkBox);
		selectSelectAllCheckBox(frm);		
	}
}

// binds the onclick event to all the checkboxes of a given form
// used in conjuction with handleCheckboxes
function initCheckBoxes(frm) {
	for(var i = 0; i < frm.length; i++) {
		if(frm[i].type.toUpperCase() == 'CHECKBOX') {
			frm[i].onclick = function(){handleCheckBoxes(frm, this);};
		}
	}
}

// used to check all items in a checkbox group from a given checkBox
function checkGroupAll(frm,checkBox) {
	var allChecked = true;
	if(!checkBox.checked) {
		allChecked = false;
	}
	for(var i = 0; i < frm[checkBox.name].length; i++) {
		if(!frm[checkBox.name][i].checked) {
			allChecked = false;
			break;
		}
	}

	if(frm[checkBox.name + 'All']) {
		frm[checkBox.name + 'All'].checked = allChecked;
	}
}

// checks if the radio group has a checked item or not
function hasCheckedItem(radioGroup) {
	var checked = radioGroup.checked;
	for(var i = 0; i < radioGroup.length; i++) {
		checked = radioGroup[i].checked;
		if(checked) {
			break;
		}
	}
	return checked;
}

// retrieves the checked item within a radio group
function getCheckedItem(radioGroup) {
	var checkedItem = '';
	if(radioGroup.length)
	{
		for(var i = 0; i < radioGroup.length; i++) {
			if(radioGroup[i].checked) {
				checkedItem = radioGroup[i].value;
				break;
			}
		}
	} 
	else
	{
		checkedItem = radioGroup.value;
	}
	return checkedItem;
}

// retrieves the first selected value in a listbox
function getFirstSelectedValue(listBox) {
  
	var intLength,intIndex;
	var value;
    alert(listBox);
    if(listBox != null)
    {
		alert(listBox.length)
		if(listBox.length > 0)
		{
			
			intLength = listBox.length;
			for(intIndex = 0; intIndex < intLength; intIndex++)
			{
				if(listBox[intIndex].selected)
				{
					value = listBox[intIndex].value;
				}
			}
		}
		else
		{
			value = listBox.value;
		    
		}
	}
	return value;
}

// retrieves the first selected text value in a listbox
function getFirstSelectedText(listBox) {
	var intLength, intIndex;
	var text;
	if(listBox.length)
	{
		intLength = listBox.length;
		for(intIndex = 0; intIndex < intLength; intIndex++)
		{
			if(listBox[intIndex].selected)
			{
				text = listBox[intIndex].text;
			}
		}
	}
	else
	{
		text = listBox.text;
	}
	return text;
}
