// Set this to true to turn debuging on
var debug = true;



//This function takes to parameters "from" and "to"
//From is the field to take the asset from
//To is the field to add the asset to

function moveItem(from,to)
{
	source = document.getElementById(from);
	dest = document.getElementById(to);
	if(source.selectedIndex > -1){
		destLength = dest.options.length;
		sourceLength = source.options.length;
		dest.options[destLength] = new Option(source.options[source.selectedIndex].text,source.options[source.selectedIndex].value);
		if(debug==true){//alert(source.options[source.selectedIndex].value);
		}
		
		if(source.selectedIndex != 0){
			select = source.selectedIndex - 1;
		}else{
			select = source.selectedIndex;
		}
		
		source.options[source.selectedIndex] = null;
		source.selectedIndex = select;
	}
	
}

var sort = "numeric";

function moveDBComboAsset(from,to)
{
	if(!document.getElementById(from).value)
	{		
		//sourceText = document.getElementById(from+"_ulbTextBox");//"_ctl0_AddressNumber_Unselected_ulbTextBox");
		sourceText = document.getElementById(from+"_dlbTextBox");
		//sourceValue=document.getElementById(from+"_ulbValueHidden");//"_ctl0_AddressNumber_Unselected_ulbValueHidden");
		sourceValue=document.getElementById(from+"_dlbValueHidden");
		dest = document.getElementById(to);	
		destLength = dest.options.length;		
		dest.options[destLength] = new Option(sourceText.value,sourceValue.value);
		
		var isinteger = isInt(sourceValue.value);
		var firstItem = null;	
		var sortTextList = new Array();
		var sortValueList = new Array();
		var sortList = new Array();
		
		for(var x=0; x<dest.options.length;x++)
		{
			sortTextList[x] = new Array()
			sortTextList[x][0] = dest.options[x].text;	
			sortTextList[x][1] = dest.options[x].value;
		}
		
		if(isinteger == true)
			sortTextList.sort(mysortfn);
		else
			sortTextList.sort(compareNum);
		
		// This is really slow when there are large list boxes
		for(var x=0;x<sortTextList.length;x++)
		{		
			dest.options[x].text = sortTextList[x][0];
			dest.options[x].value = sortTextList[x][1];
		}
	}
	else
	{
		source = document.getElementById(from);
		for(var x=0;x<source.length;x++)
		{
			if(source.options[x].selected)
			{
				source.options[x] = null
				if(firstItem == null)
					firstItem = x;
				if(source.options[x + 1])
				{
					if(!source.options[x + 1].selected)
					{
						lastItem = x;
					}
				}
				else
					lastItem = x;
			}
		}	
	}
}

function mysortfn(a,b) {
	var va = a[0].split(":");	
	var vb = b[0].split(":");
	
	if(sort != "numeric")
	{
		va = va[1];
		vb = vb[1];
		if (va<vb) return -1;
		if (va>vb) return 1;
	}else{
		va = va[0];
		vb = vb[0];
		if(isNaN(va) == false && isNaN(vb) == false)
		{
			return(va-vb);
		}else{
			if (va<vb) return -1;
			if (va>vb) return 1;
		}
	}
	
	 //if (a[0]<b[0] ) return -1;
	//if (a[0]>b[0]) return 1;
	//if(sort == "numeric"){	alert(va + " " + vb);}
	if (va<vb) return -1;
	if (va>vb) return 1;
	 return 0;
}

function isInt(x)
{
	return isNaN(x);
}


function compareNum(a,b)
{
	return a[0]-b[0]
}


//This function takes to parameters "from" 
//Deletes the item from the listbox

function deleteItem(from)
{
	source = document.getElementById(from);

	if(source.selectedIndex > -1){
		sourceLength = source.options.length;
		
		if(debug==true){//alert(source.options[source.selectedIndex].value);
		}
		
		if(source.selectedIndex != 0){
			select = source.selectedIndex - 1;
		}else{
			select = source.selectedIndex;
		}
		
		source.options[source.selectedIndex] = null;
		source.selectedIndex = select;
	}
	
}



// This function takes one parameter "field"
// field - is the id of the field to move the selected option down a position
function moveDown(field)
{
	//alert("moveDown");
	source = document.getElementById(field);
	if(source.selectedIndex > -1){
		if(source.selectedIndex != source.options.length - 1){
		
		var selectedNum = source.selectedIndex;
		var nextNum = source.selectedIndex + 1;
		var selectedValue = source.options[selectedNum].value;
		var selectedText = source.options[selectedNum].text;
		var nextValue = source.options[nextNum].value;
		var nextText = source.options[nextNum].text;
		
		source.options[nextNum].text = selectedText;
		source.options[nextNum].value = selectedValue;
		source.options[selectedNum].text = nextText;
		source.options[selectedNum].value = nextValue;
		
		source.selectedIndex = nextNum;
		
		}
	}
}

// This function takes one parameter "field"
// field - is the id of the field to move the selected option up a position
function moveUp(field)
{
//alert("moveUp");
source = document.getElementById(field);

if(source.selectedIndex > -1){
		if(source.selectedIndex != 0){
		
			var selectedNum = source.selectedIndex;
			var nextNum = source.selectedIndex - 1;
			var selectedValue = source.options[selectedNum].value;
			var selectedText = source.options[selectedNum].text;
			var nextValue = source.options[nextNum].value;
			var nextText = source.options[nextNum].text;
			
			source.options[nextNum].text = selectedText;
			source.options[nextNum].value = selectedValue;
			source.options[selectedNum].text = nextText;
			source.options[selectedNum].value = nextValue;
			
			source.selectedIndex = nextNum;
			
		}
	}

}

function selectAllItems(field)
{
	source = document.getElementById(field);
	for(var x=0;x<source.options.length;x++)
	{
		source.options[x].selected = true;
	}
}


//This function takes to parameters "from" and "to"
//From is the field to take the asset from
//To is the field to add the asset to

function moveAsset(from,to)
{
	
	source = document.getElementById(from);
	dest = document.getElementById(to);
	for(var x=0;x<source.length;x++)
	{
		if(source.options[x].selected){
			destLength = dest.options.length;
			sourceLength = source.options.length;		
			
			dest.options[destLength] = new Option(source.options[x].text,source.options[x].value);
			
			var isinteger = isInt(source.options[x].text);			
		}
	}
	var firstItem = null;
	//var sourceLength = source.length;
	for(var x=source.length-1;x>=0;x--)
	{
		if(source.options[x].selected){
			source.options[x] = null
			if(firstItem == null){
				firstItem = x;
			}
			if(source.options[x])
			{
				if(!source.options[x].selected)
				{
					lastItem = x;
				}
			}else{
				lastItem = x;
			}
			
		}
		
	}	
	
	var sortTextList = new Array();
	var sortValueList = new Array();
	var sortList = new Array();
	for(var x=0; x<dest.options.length;x++)
		{
			sortTextList[x] = new Array()
			sortTextList[x][0] = dest.options[x].text;	
			sortTextList[x][1] = dest.options[x].value;
		}
	
	
	if(isinteger == true)
	{
		sortTextList.sort(mysortfn);
	}else{
		sortTextList.sort(compareNum);
		
	}		
	
	// This is really slow when there are large list boxes
	
	for(var x=0;x<sortTextList.length;x++)
	{		
		dest.options[x].text = sortTextList[x][0];
		dest.options[x].value = sortTextList[x][1];
	}
}





		






