function allowOnlyDigitsAndOneDot(thisEvent,val){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (thisEvent) keycode = thisEvent.which;
	//alert("keycode: " + keycode);
	//alert("val="+val);//val contine ce text era inainte de apasarea tastei

	//keycode==8 is backspace and keycode==0 is delete
	if (keycode==8 || keycode==0|| keycode==13) return true;
	
	if (keycode==46)
	{
	var index=val.indexOf(".");
	if (index!=-1) {alert("Only one \".\" is allowed!");return false;}
	else return true;
	}

	
	if (keycode<48 || keycode>57)
	        {
		     alert("Only digits are allowed!");
	         return false;
	        }
	        
	return true;	
}


function allowOnlyDigitsAndOneDotMaxDecimals(thisEvent,thisObject,maxDecimals){
	var val = thisObject.value;
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (thisEvent) keycode = thisEvent.which;
	//alert("keycode: " + keycode);
	//alert("val="+val);//val contine ce text era inainte de apasarea tastei

	//keycode==8 is backspace and keycode==0 is delete
	if (keycode==8 || keycode==0 || keycode==13) return true;
	
	if (keycode==46)
	{
	var index=val.indexOf(".");
	if (index!=-1) {alert("Only one \".\" is allowed!");return false;}
	else return true;
	}

	
	if (keycode<48 || keycode>57)
	        {
		     alert("Only digits are allowed!");
	         return false;
	        }
	        
	var selectedText;
	  // IE version
	  if (document.selection != undefined)
	  {
	    thisObject.focus();
	    var sel = document.selection.createRange();
	    selectedText = sel.text;
	  }
	  // Mozilla version
	  else if (thisObject.selectionStart != undefined)
	  {
	    var startPos = thisObject.selectionStart;
	    var endPos = thisObject.selectionEnd;
	    selectedText = thisObject.value.substring(startPos, endPos)
	  }

	  if (selectedText == "") {
		//if there are decimals, than it cannot be more than maxDecimals decimals
			var index=val.indexOf(".");
			if (index!=-1) {
				var fractionPart = val.substring(index+1);
				//+1 is because the length of fraction part is taken before key press and the last letter is not taken into consideration
				if (fractionPart.length + 1 > maxDecimals) {
					alert("The value must have maximum "+maxDecimals+" decimals!");
					return false;
				}
			}
	  }
	
	return true;	
}



function allowOnlyDigits(thisEvent,val){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (thisEvent) keycode = thisEvent.which;
	//alert("keycode: " + keycode);
	//alert("val="+val);//val contine ce text era inainte de apasarea tastei

	//keycode==8 is backspace and keycode==0 is delete
	if (keycode==8 || keycode==0 || keycode==13) return true;
	
	if (keycode<48 || keycode>57)
	        {
		     alert("Only digits are allowed!");
	         return false;
	        }
	     
	//if there are decimals, than it cannot be more than maxDecimals decimals
	var index=val.indexOf(".");
	if (index!=-1) {
		var fractionPart = val.substring(index+1);
		//+1 is because the length of fraction part is taken before key press and the last letter is not taken into consideration
		if (fractionPart.length + 1 > 0) {
			alert("The value must have 0 decimals!");
			return false;
		}
	}
	
	return true;	
}

function allowOnlyDigitsAndOtherCharacters(thisEvent,val, allowedCharacterCodesArray){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (thisEvent) keycode = thisEvent.which;
	//alert("keycode: " + keycode);
	//alert("val="+val);//val contine ce text era inainte de apasarea tastei

	//keycode==8 is backspace and keycode==0 is delete
	if (keycode==8 || keycode==0 || keycode==13) return true;	
	
	if (allowedCharacterCodesArray != null) {
		var i=0;
		for(i=0; i<allowedCharacterCodesArray.length; i++) { 
			var allowedCharacterCode = allowedCharacterCodesArray[i];
			if ( keycode == allowedCharacterCode) return true;
		}
	}
	
	if (keycode<48 || keycode>57)
	        {
		     alert("Only digits are allowed!");
	         return false;
	        }
	     
	//if there are decimals, than it cannot be more than maxDecimals decimals
	var index=val.indexOf(".");
	if (index!=-1) {
		var fractionPart = val.substring(index+1);
		//+1 is because the length of fraction part is taken before key press and the last letter is not taken into consideration
		if (fractionPart.length + 1 > 0) {
			alert("The value must have 0 decimals!");
			return false;
		}
	}
	
	return true;	
}

function filterNumber(textInput, event, precision, scale)
{
	var e = event == null ? window.event : event;
	
	// allow arrows
	if (e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40) return true;
	// allow tab, home, end
	if (e.keyCode == 9 || e.keyCode == 35 || e.keyCode == 36) return true;
	
	var selectionStart = 0;
	var selectedText = "";
	if (textInput.createTextRange) {
		var r = document.selection.createRange().duplicate();
		selectedText = r.text;
		r.moveEnd('character', textInput.value.length);
		if (r.text == '') selectionStart = textInput.value.length;
		else selectionStart =  textInput.value.lastIndexOf(r.text);
	} else {
		selectionStart = textInput.selectionStart;
		selectedText = textInput.value.substring(textInput.selectionStart, textInput.selectionEnd);
	}
	
	// allow editable character processing only if no text is selected 
	//  or the scale is 0 or the selected text does not include the dot
	if (scale == 0 || selectedText.length == 0 || selectedText.indexOf(".") == -1) {
		var dotPosition = textInput.value.indexOf(".");
		var cursorPosition = cursorPosition = selectionStart;
		// allow backspace only if deleting the dot does not break the precision/scale combination
		if (e.keyCode == 8) {
			if (textInput.value.substring(cursorPosition - 1).substring(0,1) == '.') {
				if (textInput.value.length - 1 <= precision - scale) {
					return true;
				}
			} else {
				return true;
			}
		}
		
		// allow delete only if deleting the dot does not break the precision/scale combination
		if (e.keyCode == 46) {
			if (textInput.value.substring(cursorPosition).substring(0,1) == '.') {
				if (textInput.value.length - 1 <= precision - scale) {
					return true;
				}
			} else {
				return true;
			}
		}
		
		// only one 'dot'
		if (e.keyCode == 110 || e.keyCode == 190) {
			if (scale > 0 && dotPosition == -1)  {
				// allow dot only if the right side in below scale and left side < precision - scale
				if (textInput.value.substring(cursorPosition).length <= scale && 
					textInput.value.substring(0,cursorPosition -1).length <= precision - scale) {
					return true;
				}
			}
		}
		
		// keys 0-9 || numpad keys 0-9 
		if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
			if (scale > 0 && dotPosition != -1) {
				// if cursor is after the dot check the number of decimals
				if (dotPosition < cursorPosition) {
					// if number of decimals are belor the max value
					if (textInput.value.substring(dotPosition + 1).length < scale) {
						return true;
					}
				} else {
					// if number of digits are below the max value minus scale
					if (textInput.value.substring(0, dotPosition).length < precision - scale) {
						return true;
					}
				}
			} else {
				// if number of digits are below the max value (or max value - scale)
				if (scale > 0) {
					if (textInput.value.length < precision - scale) {
						return true;
					}
				} else {
					if (textInput.value.length < precision) {
						return true;
					}
				}
			}
		}
	}
	return false;
}

function checkHour(event,hour){
	
	var hourNumber = parseInt(trim(hour));
	if (hourNumber < 0 || hourNumber > 23) {
		alert("Future effective hour must have values between 0 and 23 ");
		return false;
	}
	
	return true;
}

function checkMinute(event,minute){
	
	var minuteNumber = parseInt(trim(minute));
	
	if (minuteNumber < 0 || minuteNumber > 59) {
		alert("Future effective minute must have values between 0 and 59 ");
		return false;
	}
	
	return true;
}

function getASCIIKeyCode(aChar) {
  var character = aChar.substring(0, 1);
  var code = aChar.charCodeAt(0);
  return code;
}

function checkIsNumericInput(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = getASCIIKeyCode(lchar);

  /* Check if the keyed in character is a number */
  if (cCode < getASCIIKeyCode( "0" ) || cCode > getASCIIKeyCode( "9" ) ) {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
  }
  return false;
}
