function _populate(form, parent, all, target, refPrefix, selected)
{
	pRadio = eval("document." + form + "." + parent)
	allList = eval("document." + form + "." + all)
	selectedList = eval("document." + form + "." + target)
	selectedObject = eval("document." + form + "." + selected)
	
	var optionArray

	for(i = 0; i < pRadio.length; i++)
	{	
		//alert(pRadio[i].checked)
		if(pRadio[i].checked)
		{
			//alert(refObjectPrefixStr + pRadio[i].value)
			options = eval("document." + form + "." + refPrefix + pRadio[i].value + ".value")
			//alert(options)
			optionArray = options.split(";")
			//alert(optionArray.length)
			break;
		}
	}
	
	for(i = 0; i < optionArray.length; i++)
	{
		//alert(optionArray[i])
		//eval("allList.options[i]=" + new Option(optionArray[i], optionArray[i]))
		allList.options[i]= new Option(optionArray[i], optionArray[i]+"_value")
	}
	
	// this is for the init after user submited with incomplete information
	selectedArray = selectedObject.value.split(";").sort()
	for(i = 0, j = 0; i < selectedArray.length; i++)
	{
		for(; j < allList.options.length; j++)
		{
			if(selectedArray[i] == allList.options[j].text)
			{
				allList.options[j].selected = true
				break;
			}
		}
	}
	
	moveRecords(allList, selectedList)
	initFilter(allList, selectedList)
}
function _addItems(form, all, target, targetText, targetValue)
{
	_addItemsWithMax(form, all, target, 10, targetText, targetValue)
}

function _addItemsWithMax(form, all, target, maxSelected, targetText, targetValue)
{
	allList = eval("document." + form + "." + all)
	selectedList = eval("document." + form + "." + target)
	
	if(!needMore(allList, selectedList, maxSelected, targetText, targetValue))
	{
		alert("Selected Number exceeds Max")
		return
	}
	
	moveItems(allList, selectedList)

	arrangeList(selectedList, targetText, targetValue)
}

function _deleteItems(form, parent, all, target, targetText, targetValue)
{
	pRadio = eval("document." + form + "." + parent)
	allList = eval("document." + form + "." + all)
	selectedList = eval("document." + form + "." + target)
	
	filter(pRadio, selectedList)
	moveItems(selectedList, allList)
	
	arrangeList(selectedList, targetText, targetValue)
}

function moveItems(fromList, toList)
{
	for(i = 0; i < fromList.length; i++)
	{
		if(fromList.options[i].selected)
		{
			//alert(fromList.options[i].text + " : " + fromList.options[i].value)
			toList.options[toList.length] = new Option(fromList.options[i].text, fromList.options[i].value)
		}
	}
	
	for(i = fromList.length-1; i >= 0; i--)
	{
		if(fromList.options[i].selected)
		{
			fromList.options[i] = null;
		}
	}
	
	sortList(fromList)
	sortList(toList)
	
	//history.go(0)
}

function initFilter(fromList, toList)
{
	for(i = fromList.length-1; i >= 0; i--)
	{
		for(j = 0; j < toList.length; j++)
		{
			if(fromList.options[i].text == toList.options[j].text)
			{
				fromList.options[i] = null;
			}
		}
	}
}

function filter(pRadio, fromList)
{
	state = ''
	
	for(i = 0; i < pRadio.length; i++)
	{	
		//alert(pRadio[i].checked)
		if(pRadio[i].checked)
		{
			//alert(pRadio[i].value)
			state = pRadio[i].value
			break;
		}
	}
	
	for(i = fromList.length-1; i >= 0; i--)
	{
		if(fromList.options[i].selected)
		{
			//alert(fromList.options[i].text)
			
			strArray = (fromList.options[i].text).split(', ')
			//alert(strArray[1])
			/*
			length = (fromList.options[i].text).length
			alert(length)
			alert((fromList.options[i].text).substring(length-2, length))
			if((fromList.options[i].text).substring(length-2, length) != state)
			*/
			if(strArray[1] != state)
			{
				fromList.options[i] = null;
			}
		}
	}
}

function _doClear(form, all, allText, allValue, target, targetText, targetValue)
{
	targetForm = eval("document." + form)
	allList = eval("document." + form + "." + all)
	selectedList = eval("document." + form + "." + target)
	
	allList.length = 0
	selectedList.length = 0
	allList.options[0]= new Option(allText, allValue)
	selectedList.options[0]= new Option(targetText, targetValue)
	
	targetForm.reset()
}

function _doSubmit(form)
{
	targetForm = eval("document." + form)
	
	targetForm.submit()
}

function needMore(fromList, toList, maxSelected, initTextStr, initValueStr)
{
	//alert("need more")
	//var maxSelectedCount = 10;
	
	//var selected = toList.length
	//alert(toList.length)
	var selected = getItemCount(toList, initTextStr, initValueStr)
	//alert("selected = " + selected)
	if(selected >= maxSelected)
	{
		return false
	}
	
	for(i = 0; i < fromList.length; i++)
	{
		if(fromList.options[i].selected)
		{
			if((selected = selected + 1) > maxSelected)
			{
				return false
			}
		}
	}
	
	return true
}

function sortList(list)
{
	var optionArray = new Array()
	
	for(i = 0; i < list.length; i++)
	{
		optionArray[i] = list.options[i].text
	}
	
	optionArray.sort()
	
	list.length = 0
	for(i = 0; i < optionArray.length; i++)
	{
		list.options[i]= new Option(optionArray[i], optionArray[i]+"_value")
	}
}

function arrangeList(list, initTextStr, initValueStr)
{
	if(list.length == 0)
	{
		list.options[0] = new Option(initTextStr, initValueStr)
		return
	}

	for(i = 0; i < list.length; i++)
	{
		if(list.options[i].text != initTextStr)
		{
			continue
		}
		
		if(list.length > 1)
		{
			list.options[i] = null
		}
		break;
	}
}

function getItemCount(list, initTextStr, initValueStr)
{
	//alert(list.length)
	for(i = 0; i < list.length; i++)
	{
		if(list.options[i].text != initTextStr)
		{
			continue
		}
		
		return list.length-1
	}
	
	return list.length
}





//
//  Given 2 select options, this function moves the selected
//  options from one select box to the other.
//  @param sForm1 - the name of the form containing the selects
//  @param sOptionsSelected - the name of the select pulldown with options
//           selected by the user for addition to the other pulldown
//  @param sOptionsAddedTo - the name of the pulldown that receives the
//           elements.
function fnDualSelectChoice1(sForm1, sOptionsSelected, sOptionsAddedTo) {
	var selAdd = document.forms[sForm1].elements[sOptionsSelected];
	var selRem = document.forms[sForm1].elements[sOptionsAddedTo];

	// Figure out the first blank or null spot to stick the new value
	var iRmIdx = selRem.options.length;
	for (var i = 0; i < selRem.options.length; i++) {
		if ((selRem.options[i].value == "" || selRem.options[i].value == null) &&
			(selRem.options[i].text == "" || selRem.options[i].text == null)) {
			iRmIdx = i;
		}
	}

	for (var i = 0; i < selAdd.options.length; i++) {
		// Find the selected towns and stick them into the select box to be added
		if (selAdd.options[i].selected && selAdd.options[i].value != null && selAdd.options[i].value != '') {
			var opt = new Option(selAdd.options[i].text, selAdd.options[i].value);
			selRem.options[iRmIdx++] = opt;
		}
	}

	// Remove the selected items from the original box
	for (var i =  selAdd.options.length -1; i >= 0; i--) {
		if (selAdd.options[i] == null || selAdd.options[i].value == null || selAdd.options[i].value == '') continue;
		if (selAdd.options[i].selected) {
			selAdd.options[i] = null;
		}
	}

	selRem.options[iRmIdx] = new Option("");

}

// This function removes the values from one select box from the values in the other select box
function fnRemoveOverlapFromSelectBoxes(formName, targetSelectBoxName, selectBoxWithValuesToRemoveName) {
	var targetSelectBox = document.forms[formName].elements[targetSelectBoxName];
	var selectBoxWithValuesToRemove = document.forms[formName].elements[selectBoxWithValuesToRemoveName];

	for (var i = 0; i < selectBoxWithValuesToRemove.options.length; i++) {
		var removeValue = selectBoxWithValuesToRemove.options[i].value;
		for (var j = 0; j < targetSelectBox.options.length; j++) {
			if (removeValue == targetSelectBox.options[j].value) {
				targetSelectBox.options[j] = null;
			}
		}

	}
	
}


// This function selects all the values for a select box
function fnSelectAllValues(formName, selectBoxName) {
	var selectBox = document.forms[formName].elements[selectBoxName];
	for (var i = 0; i < selectBox.options.length; i++) {
		if (selectBox.options[i].value != null && selectBox.options[i].value != "") {
			selectBox.options[i].selected = true;
		}
	} 
}

// Returns the selected value of a select box
function fnGetSelectedValue(formName, selectBoxName) {
	var selectBox = document.forms[formName].elements[selectBoxName];
	for (var i = 0; i < selectBox.options.length; i++) {
		if (selectBox.options[i].selected == true) {
			return selectBox.options[i].value;
		}
	}
	return null;
}

// Returns the selected text at a select box
function fnGetSelectedText(formName, selectBoxName) {
	var selectBox = document.forms[formName].elements[selectBoxName];
	for (var i = 0; i < selectBox.options.length; i++) {
		if (selectBox.options[i].selected == true) {
			return selectBox.options[i].text;
		}
	}
	return null;
}


// This function removes a value from a select box
function fnRemoveSelectedOptions(formName, targetSelectBoxName) {
	var targetSelectBox = document.forms[formName].elements[targetSelectBoxName];
	for (var i = 0; i < targetSelectBox.options.length; i++) {
		if (targetSelectBox.options[i].selected) {
			targetSelectBox.options[i] = null;
			i--;
		}
	}
	
}


// This function adds a value into select box
function fnAddSelectOption(formName, targetSelectBoxName, value, text) {
	var targetSelectBox = document.forms[formName].elements[targetSelectBoxName];
	var isAdded = false;

	// Check to see if the option is already there
	for (var i = 0; i < targetSelectBox.options.length; i++) {
		if (targetSelectBox.options[i].value == value &&
			targetSelectBox.options[i].text == text) { 		
			return;
		}
	}

	for (var i = 0; i < targetSelectBox.options.length; i++) {
		if (targetSelectBox.options[i].value == '') {
			targetSelectBox.options[i].value = value;
			targetSelectBox.options[i].text = text;			
			isAdded = true;
		}
	}
	if (isAdded == false) {
		targetSelectBox.length = targetSelectBox.length + 1;
		targetSelectBox.options[targetSelectBox.length - 1].value = value;
		targetSelectBox.options[targetSelectBox.length - 1].text = text;
		isAdded = true;
	}
}



// This function adds a value into select box
function fnRemoveAllSelectBoxOptions(formName, targetSelectBoxName) {
	var targetSelectBox = document.forms[formName].elements[targetSelectBoxName];

	targetSelectBox.length = 0;
/*	for (var i = 0; i < targetSelectBox.options.length; i++) {
		targetSelectBox.options[i].value = null;
		targetSelectBox.options[i].text = null;
	}
	*/
}


function fnLimitSelectBoxSelections(sForm1, selectList, maxSelected, message) {
	var selectBox = document.forms[sForm1].elements[selectList];

	// Check to make sure that adding this will not case the limit to exceed 10
	var countItemsSelected = 0;
	for (var i = 0; i < selectBox.options.length; i++) {
		if (selectBox.options[i].selected == true) {
			countItemsSelected++;
		}
	}	
	if (countItemsSelected > maxSelected) {
		alert(maxSelected);
		return false;
	}
	return true;
}

