/*TO BE DEPRICATED, MOVE SOME FUNCTIONS INTO MetafuseBase.js*/

/// <reference path="MetafuseBase.js" />


//sorts a select box by the text as string
function SortSelectByTextAsString(selectElement)
{
	
	if(selectElement.options.length > 1)
	{
		//load the array
		var a = new Array();
		for(var i = 0; i < selectElement.options.length; i++)
		{
			a[a.length] = selectElement.options[i];
		}
	
		//sort the array by text
		a.sort(CompareSelectTextAsString);
		
		//clear the select box elements
		while(selectElement.options.length > 0)
		{
			selectElement.options[0] = null;
		}
		//add the sorted array back to the select	
		for(var i = 0; i < a.length; i++)
		{
			
			selectElement.options[selectElement.options.length] = a[i];
		}
	}
}

function CompareSelectTextAsString(optionA, optionB)
{
	// text comparison
	if(optionA.text == optionB.text)
	{
		return 0;
	}
	else
	{
		if(optionA.text < optionB.text)
		{
			return -1;
		}
		else
		{
			return 1;
		}
	}
}

function SelectBoxMoveSelectedItemsDown(selectElement)
{
	var optionsToMove = new Array();
	var firstMoveOptionIndex = -1;
	var optionOrder = new Array();
	var optionMoved = false;
	
	for(var i = 0; i < selectElement.options.length; i++)
	{
	
		var currentOption = selectElement.options[i];
		
		if(currentOption.selected == true && (i + 1 < selectElement.options.length))
		{
			if(optionsToMove.length == 0)
			{
				firstMoveOptionIndex = i;
			}
			optionsToMove[optionsToMove.length] = currentOption;
	
		}
		else if(optionsToMove.length > 0)
		{
			optionMoved = true;
				
			optionOrder[optionOrder.length] = currentOption;
			
			for(var ii = 0; ii < optionsToMove.length; ii++)
			{	
				optionOrder[optionOrder.length] = optionsToMove[ii];
			}
			
			//clear the options to move
			optionsToMove = new Array();
		}
		else
		{
			optionOrder[optionOrder.length] = currentOption
		}
	}
	
	//if any were moved, rebind the list
	if(optionMoved == true)
	{
		while(selectElement.options.length > 0)
		{
			selectElement.options[0] = null;
		}
		for(var i = 0; i < optionOrder.length; i++)
		{
			selectElement.options[selectElement.options.length] = optionOrder[i];
		}
	}	
}

function SelectBoxMoveSelectedItemsUp(selectElement)
{
	var optionsToMove = new Array();
	var firstMoveOptionIndex = -1;
	var optionOrder = new Array();
	var optionMoved = false;
	
	for(var i = selectElement.options.length - 1; i >= 0; i--)
	{
	
		var currentOption = selectElement.options[i];
		
		if(currentOption.selected == true && (i > 0))
		{
			if(optionsToMove.length == 0)
			{
				firstMoveOptionIndex = i;
			}
			optionsToMove[optionsToMove.length] = currentOption;
	
		}
		else if(optionsToMove.length > 0)
		{
			optionMoved = true;
			optionOrder[optionOrder.length] = currentOption;
			
			for(var ii = 0; ii < optionsToMove.length; ii++)
			{	
				optionOrder[optionOrder.length] = optionsToMove[ii];
			}
			
			//clear the options to move
			optionsToMove = new Array();
		}
		else
		{
			optionOrder[optionOrder.length] = currentOption;
		}
	}
	
	//if any were moved, rebind the list
	if(optionMoved == true)
	{
		while(selectElement.options.length > 0)
		{
			selectElement.options[0] = null;
		}
		for(var i = optionOrder.length - 1; i >= 0 ; i--)
		{
			selectElement.options[selectElement.options.length] = optionOrder[i];
		}
	}	
}


//gets the select box value
function SelectValue(oSelectElement)
{
	var bSelectSelected = false;
	var i;
	for(i=0; i < oSelectElement.options.length; i++)
	{
		if(oSelectElement.options[i].selected == true)
		{
			bSelectSelected = true;
			break;
		}
	}
	if(bSelectSelected == true)
	{
		return oSelectElement.options[i].value;
	}
	else
	{
		return "";
	}
}
//gets the select box text
function SelectText(oSelectElement)
{
	var bSelectSelected = false;
	var i;
	for(i=0; i < oSelectElement.options.length; i++)
	{
		if(oSelectElement.options[i].selected == true)
		{
			bSelectSelected = true;
			break;
		}
	}
	if(bSelectSelected == true)
	{
		return oSelectElement.options[i].text;
	}
	else
	{
		return "";
	}
}
//sets the value of the select element to the variable sValue (a variant)
//returns true is succeeded (element was found)
function SelectValueSet(oSelectElement, sValue)
{
	//for non-MULTIPLE SELECTS
	var i;
	if(oSelectElement.options.length)
	{
		for(i=0; i < oSelectElement.options.length; i++)
		{

			//alert(oSelectElement.options[i].value + " = " + sValue);
	
			if(oSelectElement.options[i].value.toString() == sValue.toString())
			{
			
				oSelectElement.options[i].selected = true;
				return true;
			}
		}
	}
	return false;
}
function IsEmailAddress(sInputString)
{
	var iIndexOfAt;
	var iIndexOfPeriod;
	if(sInputString)
	{
		sInputString = sInputString.toString();
		iIndexOfAt = sInputString.indexOf("@");
		if(iIndexOfAt >= 1)
		{
			iIndexOfPeriod = sInputString.indexOf(".", iIndexOfAt);
			if(iIndexOfPeriod > iIndexOfAt + 1 && iIndexOfPeriod < sInputString.length - 2)
			{
				if(sInputString.indexOf("@", iIndexOfAt + 1) == -1)
				{
					return true;
				}
			}
		}
	}
	return false;
}



//returns the value of a checkbox element
function CheckBoxValue(oCheckBoxElement)
{
	if(oCheckBoxElement.checked == true)
	{
		return oCheckBoxElement.value;
	}
	else
	{
		return "";
	}
}

function RadioValue(oRadioElement)
{
	var bRadioChecked = false;
	var i;
	if(oRadioElement.length)
	{
		for(i=0; i < oRadioElement.length; i++)
		{
			if(oRadioElement[i].checked == true)
			{
				bRadioChecked = true;
				break;
			}
		}
		if(bRadioChecked == true)
		{
			return oRadioElement[i].value;
		}
	}
	else if(oRadioElement.checked == true)
	{
		return oRadioElement.value;
	}
	return "";
}
function RadioValueSet(oRadioElement, sValue)
{
	var i;
	if(!oRadioElement.length)
	{
	
		if(oRadioElement.value == sValue)
		{
			oRadioElement.checked = true;
			return true;
		}
	}
	else
	{
		
		for(i=0; i<oRadioElement.length; i++)
		{	
			if(oRadioElement[i].value == sValue)
			{
				oRadioElement[i].checked = true;
				return true;
			}
		}
	}      
	return false;   
}

function FormatNumber(vNumber, iDigitsAfterDecimal, bIncludeLeadingDigit, bUserParensForNegativeNumbers, bGroupDigits, iDigitsBeforeDecimal)
{
	var dNumber;
	var sDigitsBeforeDecimal = new String("");
	var sDigitsAfterDecimal = new String("");
	var iDecimals;
	var sSign = "";  // the sign of the number, "" for positive numbers
	var i;
	//fall through and intialize the variables that haven't been initialized yet
	switch(arguments.length)
	{
		case 1:
			iDigitsAfterDecimal = 0;
		case 2:
			bIncludeLeadingDigit = true;
		case 3:
			bUserParensForNegativeNumbers = false;
		case 4:
			bGroupDigits = true;
		case 5:
			iDigitsBeforeDecimal = 0;
	}
	//force the iDigitsAfterDecimal to be at least 0
	if(isNaN(parseInt(iDigitsAfterDecimal)) || iDigitsAfterDecimal < 0)
	{
		iDigitsAfterDecimal = 0;
	}
	dNumber = parseFloat(vNumber);
	//if a valid number wasn't passed in, return ""
	if(isNaN(dNumber))
	{
		return "";
	}
	//round it to iDigitsAfterDecimal
	dNumber = Round(dNumber, iDigitsAfterDecimal);
	//get the sign if negative
	if (dNumber < 0)
	{
		sSign = "-";
	}
	// convert the number to a positive number
	dNumber = Math.abs(dNumber);
	//set sDigitsBeforeDecimal
	if(bIncludeLeadingDigit && dNumber < 1)
	{
		sDigitsBeforeDecimal = "0";
	}
	else if(dNumber >= 1)
	{
		sDigitsBeforeDecimal = parseInt(dNumber) + "";
	}
	//group digits
	if(bGroupDigits)
	{
		var sDigitsBeforeDecimal_tmp = "";
		for(i = 0; i < sDigitsBeforeDecimal.length; i++)
		{
			if(i % 3 == 0 && i > 0)
			{
				sDigitsBeforeDecimal_tmp = "," + sDigitsBeforeDecimal_tmp;
			}
			sDigitsBeforeDecimal_tmp = sDigitsBeforeDecimal.charAt(sDigitsBeforeDecimal.length-i-1) + sDigitsBeforeDecimal_tmp;
		}
		sDigitsBeforeDecimal = sDigitsBeforeDecimal_tmp;
	}
	//set sDigitsAfterDecimal
	if(iDigitsAfterDecimal > 0)
	{
		sDigitsAfterDecimal = Round((dNumber * Math.pow(10, iDigitsAfterDecimal)) - (Math.floor(dNumber) * Math.pow(10, iDigitsAfterDecimal))) + "";
	}
	//add zeros after the decimal point as appropriate
	for(i = sDigitsAfterDecimal.length; i < iDigitsAfterDecimal; i++)
	{
		sDigitsAfterDecimal = "0" + sDigitsAfterDecimal;
	}
	//add zeros before the number as appropriate
	for(i = sDigitsBeforeDecimal.length; i < iDigitsBeforeDecimal; i++)
	{
		sDigitsBeforeDecimal = "0" + sDigitsBeforeDecimal;
	}
	//return () around negative numbers if requested
	if(bUserParensForNegativeNumbers && sSign == "-")
	{
		if(sDigitsAfterDecimal != "")
		{
			return "(" + sDigitsBeforeDecimal + "." + sDigitsAfterDecimal + ")";
		}
		else
		{
			return "(" + sDigitsBeforeDecimal + ")";
		}
	}
	//return with - if negative
	if(sDigitsAfterDecimal != "")
	{
		return sSign + sDigitsBeforeDecimal + "." + sDigitsAfterDecimal;
	}
	else
	{
		return sSign + sDigitsBeforeDecimal;
	}
}

