function trimLeft(s) {
	var whitespaces = " \t\n\r";
	for(n = 0; n < s.length; n++) { 
		if (whitespaces.indexOf(s.charAt(n)) == -1) 
			return (n > 0) ? s.substring(n, s.length) : s; 
	}
	return("");
}
function trimRight(s){
	var whitespaces = " \t\n\r";
	for(n = s.length - 1; n  > -1; n--) { 
		if (whitespaces.indexOf(s.charAt(n)) == -1) 
			return (n < (s.length - 1)) ? s.substring(0, n+1) : s; 
	}
	return("");
}
function trim(s) {
	return ((s == null) ? "" : trimRight(trimLeft(s))); 
}
function isBlank(field, ErrorMsg) {
	var strTrimmed = trim(field.value);
	if (strTrimmed.length > 0) return false;
	alert(ErrorMsg);
	field.focus();
	return true;
}
function isBlankRadio(field, ErrorMsg) {
	if (returnSelection(field) == null) {
		alert(ErrorMsg);
		return true;
	} else {
		return false;
	}
}
function isBadURL(field, ErrorMsg) {
	var strTrimmed = trim(field.value);
	if (strTrimmed.length == 0 || 
		strTrimmed.substring(0,7) == 'http://' || 
		strTrimmed.substring(0,6) == 'ftp://' || 
		strTrimmed.substring(0,7) == 'mailto:' || 
		strTrimmed.substring(0,8) == 'https://') return false;
	alert(ErrorMsg);
	field.focus();
	return true;
}
function isNumber(field, ErrorMsg) {
	var strVal = trim(field.value);
	if (strVal.length == 0 || strVal.length > 999) return false;
	var 	x = 0;
	for (i=0;i < strVal.length; i++) { 
		if (strVal.charAt(i) > '0' && strVal.charAt(i) < '9') x++;
	}
	if (strVal.length > x) {
		alert(ErrorMsg);
		field.focus();
		return false;
	} else {
		return true;
	}
}
function isEmail(field, ErrorMsg) {
	var strMsg = ""; 
	var chAt  = '@'; 
	var chDot = '.'; 
	var strEmailAddr = trim(field.value);
	   if (strEmailAddr.length == 0) return true;
	   if (strEmailAddr.indexOf(" ") == -1)
	   {
	       var iFirstAtPos = strEmailAddr.indexOf(chAt);
	       var iLastAtPos = strEmailAddr.lastIndexOf(chAt);
	       if (iFirstAtPos > 0 && iFirstAtPos < (strEmailAddr.length - 1) &&iFirstAtPos == iLastAtPos) {
		   // look for '.' there must be at least one char between '@' and '.'
		   var iDotPos = strEmailAddr.indexOf(chDot, iFirstAtPos + 1);
		   if (iDotPos > (iFirstAtPos + 1) && iDotPos < (strEmailAddr.length -1)) return true;
	       }
	   }
	   alert(ErrorMsg);
	   field.focus();
	   return false;
}

function isFirstOption(field, ErrorMsg) {
	if (field[0].selected) {
		alert(ErrorMsg);
		return true;
	} else {
		return false;
	}
}

function returnSelection(radioButton) {
	var selection=null;
	for(var i=0; i<radioButton.length; i++) {
		if(radioButton[i].checked) {
			selection=radioButton[i].value;
			return selection;
		}
	}
	return selection; 
}



