



// Compare two options within a list by TEXT
function compareOptionText(a, b) 
{ 
	if (a.text > b.text)
		return 1;
	if (a.text < b.text)
		return -1;
		
	return 0;
}



function setAddButton(bolEmpty) 
{
	var regTrim = /(^\s+)|(\s+$)/g;
	bolEmpty = "boolean" == typeof(bolEmpty) ? bolEmpty : false;
	eNew.value = bolEmpty ? "" : eNew.value.replace(regTrim,"");
	eAdd.disabled = eNew.value.length != 0 ? false : true;
	if (bolEmpty) eNew.focus();
	return false;
}

function setButtons() 
{
	setMoveButtons();
	return false;
}




function setRemoveButton() 
{
	eRemove.disabled = eList.length == 0 || eList.selectedIndex == -1 ? true : false;
	return false;
}

function setMoveButtons() 
{
	eUp.disabled = eList.length < 2 || eList.selectedIndex < 1 ? true : false;
	eDown.disabled = eList.length < 2 || eList.selectedIndex == eList.length - 1  ? true : false;
	return false;
}


function manageItem(bolAdd) 
{
	if (bolAdd) 
	{
		eList[eList.length] = new Option(eNew.value,eNew.value,false,false);
		setAddButton(true);
	} 
	else 
	{
		for (var i=eList.length;i>0;i-=1) 
		{
			if (eList[i - 1].selected) eList.remove(i - 1);
		}
         
	}

	setButtons();
	return false;
}

function moveItem(bolUp) 
{
	var iTarget = eList.selectedIndex;
	if (iTarget == -1) return;
	
	var iSwap = bolUp ? iTarget - 1 : iTarget + 1;
	var szSwap = eList[iSwap].value
	var szTarget = eList[iTarget].value;
	eList[iSwap].value = szTarget;
	eList[iTarget].value = szSwap;
	
	szSwap = eList[iSwap].text;
	szTarget = eList[iTarget].text;
	
	eList[iSwap].text = szTarget;
	eList[iTarget].text = szSwap;

	eList.selectedIndex = iSwap;
	setMoveButtons();
	return false;
}

function moveSelectedOptions(from, to, sorted) 
{
	// do nothing if nothing selected
	if (from.selectedindex == -1)
	{
		return;
	}
	if (sorted == true)
	{
		newDestList = new Array( to.options.length );
		var len = 0;
		for( len = 0; len < to.options.length; len++ ) 
		{
			if ( to.options[ len ] != null )
			{
				newDestList[ len ] = new Option( to.options[ len ].text, to.options[ len ].value, false, false );
			}
		}
	}
	for (var i = 0; i < from.options.length; i++) 
	{
		var o = from.options[i];
		
		if (o.selected) 
		{
			if (sorted == true)
			{
				newDestList[ len ] = new Option( from.options[i].text, from.options[i].value, false, false );
				len++;
			}
			else
			{	
				to.options[to.options.length] = new Option( o.text, o.value, false, false);
			}
		}
	}
	if (sorted == true)
	{
		newDestList.sort(compareOptionText); 
		for ( var j = 0; j < newDestList.length; j++ ) 
		{
			if ( newDestList[ j ] != null )
			{
			to.options[ j ] = newDestList[ j ];
			}
		}
	}
	
	for (var i=(from.options.length-1); i>=0; i--) 
	{
		var o = from.options[i];
		if (o.selected) 
		{
			from.options[i] = null;
		}
	}
	
	from.selectedIndex = -1;
	to.selectedIndex = -1;
}

function validateForm() {
	if (eList.length == 0) 
	return false;
	for (var i=0;i<eList.length;i++) 
	{
		eList[i].selected = true;
	}
	return true;
}



function select_all(object) 
{
	for (iTmp=0; iTmp<object.length; iTmp++) 
		object.options[iTmp].selected = true;
	return true
}


function OpenCalendar(fieldName)
{
	var notoolb = window.open("r-CalPick.aspx?Control=" + fieldName ,"notoolbWindow","height=190, width = 240, menubar = no, toolbar=no,  status=none");
}

function OpenNoToolWindow(urlString, width, height) 
{
	var notoolb = window.open(urlString  ,"notoolbWindow","height=" + height + ", width = " + width + ", menubar = no, toolbar=no,  status=none");
}
