/*
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;
                formElement.style.background = 'Yellow';
		alert(message);
		formElement.focus();
	} else {
        	formElement.style.background = 'White';
        }

	return _isEmpty;
}
/*
	Check if a form element is not full.
	If it isn't display an alert box and focus
	on the element
*/
function isNotFull(formElement, size, message) {
	formElement.value = trim(formElement.value);

	_isNotFull = false;
	if (formElement.value.length != size) {
		_isNotFull = true;
                formElement.style.background = 'Yellow';
		alert(message);
		formElement.focus();
	} else {
        	formElement.style.background = 'White';
        }

	return _isNotFull;
}

/*
	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 trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function isIncomplete(fld, n) {
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    if (fld.value.length != n) {
        fld.style.background = 'Yellow';
       	return true;
    } else {
        fld.style.background = 'White';
        return false;
    }
}

