//check length of text in the passed control
function invalLength(ctl, ctlName, len, msg) {
	if (ctl.value.length > len) {
		if (msg == null)
			msg = "Please limit " + ctlName + " to " + len + " characters. You have used " + ctl.value.length + " characters."
		
		msg = msg.replace(/\[MaxCharactersAllowed\]/, len)
		msg = msg.replace(/\[NumOfCharacters\]/, ctl.value.length)

		alert(msg)
		ctl.select()
		return true
	}

	return false
}


//check whether passed parameter is a valid number
function notNumber(number) {
	number = number.toString()

	for (var i=0; i<number.length; i++) {
		if (number.charAt(i) > "9" || number.charAt(i) < "0") return true
	}

	return false
}


//check whether passed validated control contains any of passes invalid characters (case insensitive)
function invalidChars(validatedControl, validatedName, charString, msg) {
	var validatedString = trim(validatedControl.value).toLowerCase()
	charString = charString.toLowerCase()

	for (var i=0; i<validatedString.length; i++) {
		for (var j=0; j<charString.length; j++) {
			if (validatedString.charAt(i) == charString.charAt(j)) {
			
				if (typeof (Common_Scripts_FuncLib_Space) == "undefined")
					Common_Scripts_FuncLib_Space = "space"
					
				var alertString = charString.split("").join("  ")
				alertString = alertString.split("     ").join("  " + Common_Scripts_FuncLib_Space + "  ")
				
				if (msg == null)
					msg = validatedName + " cannot contain any of the following illegal characters:\n\n " + alertString

				msg = msg.replace(/\[IllegalCharacters\]/, alertString)
				alert(msg)					
				validatedControl.focus()
				validatedControl.select()
				return true
			}
		}
	}

	return false
}


//check whether text-box is empty
function isEmpty(field, fieldName, msg) {
	if (trim(field.value) == "") {
		if (msg == null)
			msg = "Please enter " + fieldName + "."
		
		alert(msg)
		field.focus()
		return true
	}

	return false
}


//check whether selection has been made in the select-box
function notSelected(field, fieldName, msg) {
	if (field.selectedIndex == 0) {
		if (msg == null)
			msg = "Please select " + fieldName + "."
			
		alert(msg)
		field.focus()
		return true
	}

	return false
}


//trim string
function trim(stringToTrim) {
	var i, j

	//left trim
	for(i=0; i<stringToTrim.length; i++) {
		if (stringToTrim.charAt(i) != " ") break
	}

	//right trim
	for(j=stringToTrim.length-1; j>=i; j--) {
		if (stringToTrim.charAt(j) != " ") break
	}

	return stringToTrim.substring(i, j + 1)
}


//validate email address
function notEmail(field, fieldName, msg) {
	var email = trim(field.value)

	if (email == "") return false

	var at = false
	var dot = false

	for (var i=0; i<email.length; i++) {
		if (email.charAt(i) == "@") at = true
		if (email.charAt(i) == "." && at) dot = true
	}

	if (!(at && dot && email.length > 5)) {
		if (msg == null)
			msg = "The " + fieldName + " you entered is not a valid e-mail address."
		
		alert(msg)		
		field.focus()
		field.select()
		return true
	}

	//check for invalid characters
	if (invalidChars(field, "E-mail", ',;|" <>\\/')) return true

	return false
}


//date validation
function validDate(date, ctlDay) {
	return date.getDate() == ctlDay[ctlDay.selectedIndex].text
}

//validate ASCII Character Set 
function charCheck(field, message) {
	var txt = field.value

	for (var i=0; i<txt.length; i++) {
		if (txt.charCodeAt(i) >= 128) {	
			alert(message) 
			field.focus()
			return true
		}
	}

	return false
}


//Checks if selected item has invalid value then select item with gotoIndex
function checkInvalVal(sel, invalVal, gotoIndex) {
	if (sel.options[sel.selectedIndex].value == invalVal) {		
		sel.selectedIndex = gotoIndex
	}
}