function trim(str) {
	if (String(str) == "undefined" || str == "") {
		return "";
	}

	return str.replace(/^\s*/, "").replace(/\s*$/, "");
}

function IsDate(ctrl,mand) {	
	var v = new Array("31","29","31","30","31","30","31","31","30","31","30","31");
	var x, y;
	
	if (IsEmpty(ctrl)) {
	    return !mand;
	}

	ctrl.value = trim(ctrl.value);

	//verify format
	if (!Match(ctrl,"^\s*([0-9]{1,2})[/-]([0-9]{1,2})[/-]([1-9][0-9]{3})\s*$"))	{
		return false;
	}

	x = RegExp.$1;
	y = RegExp.$2;	

	//account for leap years
	if (x < 1 || x > 12) {		
		return false;
	} else if (x == 2 && !(RegExp.$3 % 4)) {
		v[1]--;
    }

	//check bounds
	if (Number(y) < 1 || Number(y) > Number(v[Number(x) - 1])) {
		return false;
    }

	ctrl.value = x + "/" + y + "/" + RegExp.$3;
	
	return true;
}

 function IsEmail(ctrl, mand) {		
	if (IsEmpty(ctrl)) {
	    return !mand;
	}

	ctrl.value = trim(ctrl.value);

	return ctrl.value.search(/^[\w\-_\.]+@([\w\-_]+\.)+\w{2,4}$/) != -1;	
}

 function IsEmpty(ctrl) {
	 return Match(ctrl,"^\s*$");
 }

function IsFloat(ctrl, mand) {
	if (IsEmpty(ctrl)) {
	    return !mand;
	}

	ctrl.value = trim(ctrl.value);

	return Match(ctrl,"^\s*-?[0-9]+(\.[0-9]{1,2})?\s*$");
}

function IsInRange(ctrl, min, max, mand) {
	if (IsEmpty(ctrl)) {
	    return !mand;
	}

	return ctrl.value >= min && ctrl.value <= max;	
}

function IsNumber(ctrl, mand) {
	if (IsEmpty(ctrl)) {
	    return !mand;
	}

	ctrl.value = trim(ctrl.value);

	return Match(ctrl,"^\s*-?[0-9]+\s*$");
}

function IsPostal(ctrl, mand) {
	if (IsEmpty(ctrl)) {
	    return !mand;
	}

	ctrl.value = trim(ctrl.value);	

	return Match(ctrl,"^[a-zA-Z][0-9][a-zA-Z][ -]?[0-9][a-zA-Z][0-9]$");
}

function IsZip(ctrl, mand) {
	if (IsEmpty(ctrl)) {
	    return !mand;
	}

	ctrl.value = trim(ctrl.value);	

	return Match(ctrl,"^[0-9]{5,9}$");
}

function IsSelected(ctrl) {
	for (i = 0; i < ctrl.options.length; i++) {
		if ( ctrl.options[i].selected ) {
			return true;
		}
	}	
	
	return false;
}

function IsSelectedFromGroup(sName, frm) {
	var bItemSelected = false;
	
	with (frm) {
		for (i=0; i < elements.length - 1; i++) {
			if (elements[i].name.search(eval("/^" + sName + "_[0-9a-zA-Z]*$/")) != -1) {
				bItemSelected |= elements[i].checked;
			}
		}
	}

	return bItemSelected;
}

function IsTelno(ctrl, mand){	
	if (IsEmpty(ctrl)) {
	    return !mand;
	}

	ctrl.value = trim(ctrl.value);
	
	return  (Match(ctrl,"^((\\([0-9]{3}\\) ?)|([0-9]{3}-?))[0-9]{3}-[0-9]{4}$"));
}

function IsNonStrictTelno(ctrl, mand) {	
	if (IsEmpty(ctrl)) {
	    return !mand;
	}

	return  (Match(ctrl,"^[0-9x\(\)\\-\+\./\\\\ ]*$"));
}

function Match(ctrl, expr) {	
	return String(ctrl.value).match(new RegExp(expr));
}

function WithinLength(ctrl, len) {
	var str = new String(ctrl.value);	
	
	return (str.length > len)? false : true;	
}

// a general regular expression validation funtion
function validFormat(fieldObj, regExpStr) {	
	if (!IsEmpty(fieldObj))	{
		return (Match(fieldObj, regExpStr));
	} else {
		return true;
	}
}