//
// (c) Travel Power Limited 2006-09
//
var com;
if (!com) com={};
else if (typeof com != "object")
	throw new Error("com already exists and is not an object");

if (!com.travelpower) com.travelpower = {}
else if (typeof com.travelpower != "object")
	throw new Error("com.travelpower already exists and is not an object");

com.travelpower.Select = {}
com.travelpower.Select.NoValueSelected = function() {return(null)}

com.travelpower.Select.getLabel = function(thisControl)
{
    /// <summary>Returns a Select Label</summary>
    /// <param name="thisControl" type="string">The Select Control or Id</param>
    /// <returns type="string">The Select Label</returns>
    return (com.travelpower.Select.getData(thisControl, "L"));
}

com.travelpower.Select.getValue = function(thisControl)
{
    /// <summary>Returns a Select Value</summary>
    /// <param name="thisControl" type="string">The Select Control or Id</param>
    /// <returns type="string">The Select Value</returns>
    return (com.travelpower.Select.getData(thisControl, "D"));
}

com.travelpower.Select.setValue = function(thisControl, thisControlValue) 
{
	if (typeof thisControl == "string") thisControl = document.getElementById(thisControl);

	for (var i = 0; i < thisControl.length; i++) 
	{
		if (thisControl[i].value == thisControlValue) 
		{
			thisControl[i].selected = true;
			break;
		}
	}
}

com.travelpower.Select.getData = function(thisControl,dataType)
{
	if (typeof thisControl == "string") thisControl = document.getElementById(thisControl);
	
	if (thisControl==null)
			return(com.travelpower.Select.NoValueSelected());
	
	var lSelectedIndex = thisControl.selectedIndex;
	
	try
	{
		if (lSelectedIndex == null)
			return(com.travelpower.Select.NoValueSelected());
		
		if (lSelectedIndex < 0)
			return(com.travelpower.Select.NoValueSelected());
	}
	catch(er) {return(com.travelpower.Select.NoValueSelected());}
	
	if (dataType=="D")
		return(thisControl.options[lSelectedIndex].value);
	else
		return(thisControl.options[lSelectedIndex].text);
}

com.travelpower.Select.getMultipleLabel = function(thisControl, thisSeparator)
{
	if (!thisSeparator)
		thisSeparator=",";
		
	return(com.travelpower.Select.getMultipleData(thisControl,"L", thisSeparator));
}

com.travelpower.Select.getMultipleValue = function(thisControl, thisSeparator)
{
	if (!thisSeparator)
		thisSeparator=",";
		
	return(com.travelpower.Select.getMultipleData(thisControl,"D", thisSeparator));
}

com.travelpower.Select.getMultipleData = function(thisControl, dataType, thisSeparator) 
{
	if (typeof thisControl == "string") thisControl = document.getElementById(thisControl);
	
	if (thisControl==null)
			return(com.travelpower.Select.NoValueSelected());

	var lReturn="";
	
	for (var i = 0; i < thisControl.length; i++) 
	{
		if (thisControl[i].selected == true) 
		{
			if (dataType=="D")
				lReturn += thisControl[i].value + thisSeparator;
			else
				lReturn += thisControl[i].text + thisSeparator;
		}
	}
	
	return(lReturn.substring(0,lReturn.length-1));
}

com.travelpower.Select.clearOptions = function(thisControl)
{
	if (typeof thisControl == "string") thisControl = document.getElementById(thisControl);

	if (thisControl == null)
		return;

	var lLength = thisControl.length;
	
	for (var i = 0; i < lLength; i++)
	{
		thisControl.remove(0);
	}
}


var gNoValueSelected=null;

function getRadioValue(thisControl) 
{
	if (typeof thisControl == "string") thisControl = document.getElementsByName(thisControl);

	for (var i = 0; i < thisControl.length; i++)
		if (thisControl[i].checked)
			return(thisControl[i].value);
	return(null);
}

function setRadioValue(thisControl, thisControlValue) 
{
	if (typeof thisControl == "string") thisControl = document.getElementsByName(thisControl);

	for (var i = 0; i < thisControl.length; i++) {
		if (thisControl[i].value == thisControlValue) {
			thisControl[i].checked = true;
			break;
		}
	}
}

function clearRadio(thisControl) {
	for (var i = 0; i < thisControl.length; i++)
		thisControl[i].checked = false;
}

function setSelectedValue(thisControl, thisControlValue) {
	if (typeof thisControl == "string") thisControl = document.getElementById(thisControl);

	for (var i = 0; i < thisControl.length; i++) {
		if (thisControl[i].value == thisControlValue) {
			thisControl[i].selected = true;
			break;
		}
	}
}

function hasSelectedValue(thisControl, thisControlValue) {
	if (typeof thisControl == "string") thisControl = document.getElementById(thisControl);

	for (var i = 0; i < thisControl.length; i++) {
		if (thisControl[i].value == thisControlValue) {
			return(true);
		}
	}
	return(false);
}

function getSelectedValue(thisControl)
{
	//alert("thisControl:"+thisControl);
	if (typeof thisControl == "string") thisControl = document.getElementById(thisControl);
	
	if (thisControl==null)
			return(gNoValueSelected);
	
	var lSelectedIndex = thisControl.selectedIndex;
	
	try
	{
		if (lSelectedIndex == null)
			return(gNoValueSelected);
		
		if (lSelectedIndex < 0)
			return(gNoValueSelected);
	}
	catch(er) {return(gNoValueSelected);}
	
	return(thisControl.options[lSelectedIndex].value);
}

function getSelectedLabel(thisControl)
{
	if (typeof thisControl == "string") thisControl = document.getElementById(thisControl);

	if (thisControl == null)
		return (gNoValueSelected);

	if (thisControl.selectedIndex == undefined)
		return (gNoValueSelected);

	if (thisControl.selectedIndex < 0)
			return(gNoValueSelected);

	return(thisControl.options[thisControl.selectedIndex].text);
}

function addSelectOption(selectObject, optionValue, optionText, selectedStatus, addWhere)
{
	if (!addWhere)
		addWhere = "E";
		
	if (typeof selectObject == "string") selectObject = document.getElementById(selectObject);

	var lNewOption
	if (addWhere == "B")
	{
		lNewOption = 0;
		
		try {selectObject.options[selectObject.options.length] = new Option("","")}
		catch (er) {alert(er);}
		
		for (var feOption=selectObject.options.length-1; feOption > 0; feOption--)
		{
			selectObject.options[feOption].value = selectObject.options[feOption-1].value;
			selectObject.options[feOption].text = selectObject.options[feOption-1].text;
		}
	}
	else
	{	
		lNewOption = selectObject.options.length;
		try {selectObject.options[lNewOption] = new Option("","")}
		catch (er) {alert(er);}
	}
	
	selectObject.options[lNewOption].value = optionValue;
	selectObject.options[lNewOption].text = optionText;
	selectObject.options[lNewOption].selected = selectedStatus;
}

function removeSelectOption(thisControl, thisControlValue)
{
	if (typeof thisControl == "string") thisControl = document.getElementById(thisControl);

	for (var i = 0; i < thisControl.length; i++) {
		if (thisControl[i].value == thisControlValue) {
			thisControl.remove(i);
			break;
		}
	}
}

//alert("TPFormRadio.js parsed");

