var popUp; 

function OpenCalendar(idname, postBack)
{
	popUp = window.open('Calendar.aspx?formname=' + document.forms[0].name + 
		'&id=' + idname + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack, 
		'popupcal', 
		'width=165,height=208,left=200,top=250');
}

function SetDate(formName, id, newDate, postBack)
{
	eval('var theform = document.' + formName + ';');
	popUp.close();
	theform.elements[id].value = newDate;
	if (postBack)
		__doPostBack(id,'');
}

function Confirm(sMsg)
{
	//debugger
	var bConfirm = window.confirm(sMsg);
	if ( !bConfirm )
		window.event.returnValue = false;
}

function DisplayElement(elementID, display)
{
	var styl;
	if ( display )
		styl = 'inline';
	else
		styl = 'none';
	
	var element = document.getElementById(elementID);
	if ( element != null )
		element.style.display = styl;
}

function Hide(elementID)
{
	var element = document.getElementById(elementID);
	if ( element != null )
		element.style.display = 'none';
}

function Show(elementID)
{
	var element = document.getElementById(elementID);
	if ( element != null )
		element.style.display = 'block';
}


function AutoTab(max, field, digitsOnly) 
{
	//debugger
	if (digitsOnly && !/^\d+$/.test(field.value)) { //digits only
		field.value = '';
		return false;
	}

	if (field.value.length < max) return true; //still room

	// try to find next tabindex
	var i = field.tabIndex;
	var f = field.form;
	var el, e = 0;
	if ( i++ > 1 ) {
		while (el = f.elements[e++]) {
			if (el.tabIndex != null && el.tabIndex == i) {
				el.focus(); //move focus
				return false;
			}
		}		
	}

	//otherwise, find next element on form
	e = 0;
	while (el = f.elements[e++]) if (el == field) break; //find field position
	f.elements[e].focus(); //move focus
	return false;
}

var idlength;
function TabNext(obj, mode, len, nextField)
{
	if (mode == "down") 
	{
		idlength = obj.value.length;
	}
	else if (mode == "up") 
	{
		if(obj.value.length != idlength) 
		{
			idlength = obj.value.length;
			if(idlength==len) 
			{
				nextField.focus();
			}
		}
	}
}


/*
 * Selection list functions
 */
function ClearSelectList(list)
{
	list.options.length = 0;
}

function ClearSelectListItems(list)
{
	var items = list.options;
	var i;
	for (i = items.length-1; i>=0; i--)
	{
		if ( items[i].selected )
			items.remove(i);
	} 
}

function BuildStringFromList(list, useText, delimiter, quote )
{
	//debugger

	// if list is empty, return empty string
	if ( list.options.length == 0 )
		return '';
		
	var items = new Array(list.options.length);
	var i = 0;
	for ( i = 0; i < list.options.length; i++ )
	{
		var token; 
		if ( useText )
			token = list.options[i].text;
		else
			token = list.options[i].value;
		items[i] =  quote + token + quote;
	}
	return items.join(delimiter);
}

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Original:  Fred P -->
<!-- Begin
// Compare two options within a list by VALUES
function compareOptionValues(a, b) 
{ 
  // Radix 10: for numeric values
  // Radix 36: for alphanumeric values
  var sA = parseInt( a.value, 36 );  
  var sB = parseInt( b.value, 36 );  
  return sA - sB;
}


// Compare two options within a list by TEXT
function compareOptionText(a, b) 
{ 
  // Radix 10: for numeric values
  // Radix 36: for alphanumeric values
  var sA = parseInt( a.text, 36 );  
  var sB = parseInt( b.text, 36 );  
  return sA - sB;
}

// adds the comma-delimited list of values to listbox
function PopulateListFromString( list, valueList, idList )
{
	//TODO: implement (not needed yet)
}

// Dual list move function
function moveDualList( srcList, destList, moveAll ) 
{
  // Do nothing if nothing is selected
  if (  ( srcList.selectedIndex == -1 ) && ( moveAll == false )   )
  {
    return;
  }

  newDestList = new Array( destList.options.length );
  var len = 0;
  for( len = 0; len < destList.options.length; len++ ) 
  {
    if ( destList.options[ len ] != null )
    {
      newDestList[ len ] = new Option( destList.options[ len ].text, destList.options[ len ].value, destList.options[ len ].defaultSelected, destList.options[ len ].selected );
    }
  }

  for( var i = 0; i < srcList.options.length; i++ ) 
  { 
    if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) )
    {
       // Statements to perform if option is selected
       // Incorporate into new list
       newDestList[ len ] = new Option( srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected );
       len++;
    }
  }


  // Sort out the new destination list
  //newDestList.sort( compareOptionValues );   // BY VALUES
  newDestList.sort( compareOptionText );   // BY TEXT

  // Populate the destination with the items from the new array
  for ( var j = 0; j < newDestList.length; j++ ) 
  {
    if ( newDestList[ j ] != null )
    {
      destList.options[ j ] = newDestList[ j ];
    }
  }

  // Erase source list selected elements
  for( var i = srcList.options.length - 1; i >= 0; i-- ) 
  { 
    if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) )
    {
       // Erase Source
       srcList.options[i] = null;
    }
  }

} // End of moveDualList()

// Checkbox methods
//

// If me is a checkbox, toggles the CSS style of the row
function Select(me, style) 
{
	if ( me.type != 'checkbox' ) return;
	if ( me.origStyle == null ) return;
	
	var td = me.parentElement;
	if (td == null)	return;

	var tr = td.parentElement;
	if (tr == null)	return;

	if (me.checked)
	{
		tr.className = style;
	}
	else
	{
		tr.className = me.origStyle;
	}
}

// Toggles all checkboxes on form, setting the TR to the supplied CSS style
function ToggleAll(me, style) 
{
	var index = me.name.indexOf('_'); 
	var prefix = me.name.substring(0,index);
	for(i=0; i<document.forms[0].length; i++)
	{
		var o = document.forms[0][i];
		if (o.type == 'checkbox')
		{
			if (me.name != o.name)
			{
				if (o.name.substring(0,prefix.length) == prefix)
				{
					o.checked = me.checked;
					Select(o, style);
				}
			}
		}
	}
}

