function isEmpty(text) {
	// \w+ any alphanumeric character including the underscore
    if (text.search(/\w+/) == -1) 
        return true;
    else
        return false;
}

function isNumber(text) {
    if (text.search(/^[0-9]+$/) == -1) 
        return false;
    else
        return true;
}

function isEmail(text) {
    if (text.search(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*\@[a-z0-9-]+(\.[a-z0-9-]+)*$/) == -1) 
        return false;
    else
        return true;
}

//tests date is correct and has dd/mm/yyyy format
function isDate (myDate) {
	if (myDate.length == 10) {
		if (myDate.substring(2,3) == '/' && myDate.substring(5,6) == '/') {
			var date  = myDate.substring(0,2);
			var month = myDate.substring(3,5);
			var year  = myDate.substring(6,10);
			var test = new Date(year,month-1,date);
			if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) return true;
		}
	}
	return false;
}

//tests date is correct and has dd/mm/yyyy format
//the date can be null 
function isDate2 (myDate) {
	if (isEmpty(myDate)) return true;
	else return isDate(myDate);
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function numCharsExceed(value, max) {
    if (value.length <= max) return 0;
    else return (value.length - max);    
}

//returns true if all the values for fieldName in theForm
//are distinct numbers
function isNumeration(theForm, fieldName) {
	var numbers = new Array();
	var k = 0;
	for (i=0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].name == fieldName) {
			var num = theForm.elements[i].value;
			if (!isNumber(num)) {
				return false;
			}
			for (j=0; j < numbers.length; j++) {
				if (numbers[j] == num) {
					return false;
				}
			}
			numbers[k] = num;
			k++;				
		}		
	}
	return true;
}


//returns true if all the values for fieldName in theForm
//are not empty
function isFilled(theForm, fieldName) {
	for (i=0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].name == fieldName) {
			var txt = theForm.elements[i].value;
			if (isEmpty(txt)) {
				return false;
			}			
		}		
	}
	return true;
}

//returns true if all the form fields of 
//the specified type are not empty
function isFilledType(theForm, fieldType) {
	for (i=0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].type == fieldType) {
			var txt = theForm.elements[i].value;
			if (isEmpty(txt)) {
				return false;
			}			
		}		
	}
	return true;
}


function focusOnFirst(typeName, theForm ) {
	for (i=0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].type == typeName) {
			theForm.elements[i].focus();
			return;	
		}		
	}
}

function isArray(obj){return(typeof(obj.length)=="undefined")?false:true;}


//for multiple checkboxes with same name, returns true if at least one is checked
function isChecked(theForm, fieldName) {
	for (i=0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].name == fieldName) {
			if (theForm.elements[i].checked) {
				return true;
			}			
		}		
	}
	return false;
}

//for multiple checkboxes with same name, check/uncheck all
function setCheck(theForm, fieldName, value) {
	for (i=0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].name == fieldName) {
				theForm.elements[i].checked=value;
		}		
	}
}

//for multiple checkboxes with same name, invert checked state
function invertCheck(theForm, fieldName) {
	for (i=0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].name == fieldName) {
			theForm.elements[i].checked = !(theForm.elements[i].checked);
		}		
	}
}