
/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function addOptionList(optSrcId, OptTargetId) {
	
	//alert(optSrcId);
	var src = document.getElementById(optSrcId);
	var target = document.getElementById(OptTargetId);
	
	for( i = src.options.length -1; i >= 0; i-- ) {
		//alert(i);
		if( src[i].selected ) {
			//alert("sel");
			addOption(target, src[i].text, src[i].value);
			removeOption( src, i);
		}
	}
	//sortlist(src);
	//sortlist(target);
}


function addOption(obj, text, value) {
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	obj.options.add(optn)
}


function removeOption(obj, i) {
	obj.remove(i);
}


function sortlist(objId) {
	
	var lb = objId;
	
	//var lb = document.getElementById('mylist');
	arrTexts = new Array();

	for(i=0; i<lb.length; i++)  {
	  arrTexts[i] = lb.options[i].text;
	}

	arrTexts.sort();

	for(i=0; i<lb.length; i++)  {
	  lb.options[i].text = arrTexts[i];
	  lb.options[i].value = arrTexts[i];
	}
}


function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}