function allowedChars(strTextToValidate, strAllowedChars, blnCaseSensitive){
	//Returns false if strText contains any characters other than those in strAllowedChars.
	var thisChar
	if(blnCaseSensitive == false){
		strTextToValidate = strTextToValidate.toLowerCase();
		strAllowedChars = strAllowedChars.toLowerCase();
	}
	for(i=0;i<strTextToValidate.length;i++){
		thisChar = strTextToValidate.charAt(i);
		if(strAllowedChars.search(thisChar) == -1){
			return false;
		}
	}
}

function validateField(objControl, fieldName, minLength, maxLength){
	//Function validates character length of fields.
	//objControl = object reference for control being validated.
	//fieldName = friendly name of field as it is to be displayed to user.
	//minLength = minimun number of characters allowed.
	//maxLength = maximum number of characters allowed - enter 0 for unlimited.
	var strTemp = objControl.value;
	if(minLength > 0){
		if(strTemp.length == 0){
			alert("Please enter a value for '" + fieldName + "'.");
			objControl.focus();
			return false;
		}
	}
	if(strTemp.length < minLength){
		alert("'" + fieldName + "' must be at least " + minLength + " characters long.");
		objControl.focus();
		return false;
	}
	if(maxLength > 0){
		if(strTemp.length > maxLength){
			alert("'" + fieldName + "' is too long. Maximum length is " + maxLength + " characters.");
			objControl.focus();
			return false;
		}
	}
}

function validateEmailAddress(emailaddress){
	//Returns true if email address is valid; otherwise false.
	var atPos
	var strTemp
	var dotPos
	var i
	
	//Get position of @
	atPos = emailaddress.indexOf("@");
	if(atPos < 1){return false;}
	//Make sure there is something after the @
	strTemp = emailaddress.substr(atPos + 1, emailaddress.length);
	if(strTemp == ""){return false;}
	//Make sure there is only one @
	if(strTemp.indexOf("@")!= -1){return false;}
	//Get pos of . after @
	dotPos = strTemp.indexOf(".")
	//Make sure there is a dot and that it is not immeditately after the @
	if(dotPos == -1||dotPos == 0){return false;}
	//Make sure there are some chars after the dot
	strTemp = strTemp.substr(dotPos + 1, strTemp.length);
	if(strTemp == ""){return false;}
	//Make sure last char is not a dot.
	if(strTemp.substr(strTemp.length-1, strTemp.length) == "."){return false;}
	
	//Check that only basic ascii chars are present (0-127)
	for(i=0;i<emailaddress.length;i++){
		if(emailaddress.charCodeAt(i)>127){return false;}
	}
	
	return true;
}

function validateNumberField(objControl, fieldName, valMin, valMax, allowDecimal, allowBlank){
	//Checks that field value is a number between valMin and valMax.
	//If valMin or valMax are zero length string, then there are no max or min limits.
	//Zero length string is allowed if allowBlank is set to true.
	//If allowDecimal is false, only integers are allowed.
	//Returns false if criteria are not met.
	if(objControl.value == ""){
		if(allowBlank == false){
			alert("Please enter a number for '" + fieldName + "'.");
			objControl.focus();
			return false;
		}
	}else{
		if(isNaN(objControl.value)){
			alert("Please enter a number for '" + fieldName + "'.");
			objControl.focus();
			return false;
		}else{
			if(valMin.toString() != "" && valMax.toString() != ""){
				if(objControl.value < valMin || objControl.value > valMax){
					alert("'" + fieldName + "' should be between " + valMin + " and " + valMax + ".");
					objControl.focus();
					return false;
				}
			}
			if(valMin.toString() != ""){
				if(objControl.value < valMin){
					alert("'" + fieldName + "' should be greater than or equal to " + valMin + ".");
					objControl.focus();
					return false;
				}
			}
			if(valMax.toString() != ""){
				if(objControl.value > valMax){
					alert("'" + fieldName + "' should be less than or equal to " + valMax + ".");
					objControl.focus();
					return false;
				}
			}
			if(allowDecimal == false){
				if(isInteger(objControl.value) == false){
					alert("Please enter a whole number for '" + fieldName + "'.");
					objControl.focus();
					return false;
				}
			}
		}
	}
}

function isInteger(val){
	if(val == ""){return false;}
	if(isNaN(val)){return false;}
	var dotPos = val.indexOf(".");
	if(dotPos != -1){
		strDecimal = val.substr(dotPos + 1,val.length - (dotPos + 1));
		for(var i=0;i<strDecimal.length;i++){
			if(strDecimal.charAt(i) != 0){return false;}
		}
	}
	return true;
}

function strReplace(strString, strFind, strReplace){
	while(strString.indexOf(strFind)!=-1){
		strString = strString.replace(strFind, strReplace);
	}
	return strString;
}