function Validate(event, strID, strDataType, iPrecision, iScale, strValidChars, bAllowNegatives, sCharCase, bAllowSpaces, iMaxLength)
{	
var TextBox = document.getElementById(strID);
	
	var strCharCode = (window.event)?window.event.keyCode:event.which;
if(strCharCode == 0 || strCharCode == 8)
					return true;
  var caretpos = 0;
	if (typeof TextBox.selectionStart != 'undefined')
		caretpos = TextBox.selectionStart;
	else if (document.selection)
  {
		caretpos = Math.abs(document.selection.createRange().moveStart('character',-1000000));
    if (document.selection.type == 'Text')
      document.selection.clear();
  }

	
  //Check for Maxlength
  if (iMaxLength != 0)
  {
    var iCount = 0;
    var Text = TextBox.value;
    while(Text.indexOf('\n') > -1)
    {
      Text = Text.substring(Text.indexOf('\n') + 1);
      iCount++;
    }
    if (strCharCode != 13 && (TextBox.value.length - iCount*2) >= iMaxLength)
      return false;
  }

  //Checking for integers
	if (strDataType == 'Numeric')
	{	
		if ((strCharCode < 48 || strCharCode > 57) && strCharCode != 8 && strCharCode != 13
			&& strCharCode != 16 && strCharCode != 20 && strCharCode != 9
		    && strCharCode != 17 && strCharCode != 18 && strCharCode != 45)
		{
			var bFlag = 0;
			for (var iCount = 0; iCount < strValidChars.length; iCount++)
			{
				if (strCharCode == strValidChars.charCodeAt(iCount))
				  bFlag = 1;
			}
			if (bFlag == 0)
			  return false;
		}
		else
		{
		  
		  if (bAllowNegatives && strCharCode == 45 && caretpos != 0)
			 return false;
		  else if (!bAllowNegatives && strCharCode == 45)
			 return false;
		  else
			 return true;
		}
	}

	//Checking for chars
	if (strDataType == 'Alpha')
	{	
		var C = TextBox.value;
		if (sCharCase == 'UpperCase')
		{  
			TextBox.value = C.toUpperCase();  
		}
		else if (sCharCase == 'LowerCase')
		{
			TextBox.value = C.toLowerCase();  
		}
		if (((strCharCode < 65 || strCharCode > 90) && (strCharCode < 97 || strCharCode > 122)) && strCharCode != 8 && strCharCode != 13
			&& strCharCode != 16 && strCharCode != 20 && strCharCode != 9 
			&& strCharCode != 18 && strCharCode != 17)
		{	
			var bFlag = 0;
			for (var iCount = 0; iCount < strValidChars.length; iCount++)
			{
				if (strCharCode == strValidChars.charCodeAt(iCount))
				bFlag = 1
			}

			if (bAllowSpaces == true && strCharCode == 32 && TextBox.value.length > 0)
				return true;
			else if (bFlag == 0)
				return false;
		}
		else
		{
			return true;
	  }
	}
	// Checking for AlphaNumeric
	if (strDataType == 'AlphaNumeric')
	{	
			var C = TextBox.value;
		if (sCharCase == 'UpperCase')
		{  
			TextBox.value = C.toUpperCase();  
		}
		else if (sCharCase == 'LowerCase')
		{
			TextBox.value = C.toLowerCase();  
		}
		if (((strCharCode < 65 || strCharCode > 90) && (strCharCode < 97 || strCharCode > 122)) && (strCharCode < 48 || strCharCode > 57) && 
			strCharCode != 8 && strCharCode != 13 && strCharCode != 16 && strCharCode != 20 && strCharCode != 9	&& strCharCode != 18 && 
			strCharCode != 17)
		{	
			var bFlag = 0;
			for (var iCount = 0; iCount < strValidChars.length; iCount++)
			{
				if (strCharCode == strValidChars.charCodeAt(iCount))
				bFlag = 1
			}
			if (bAllowSpaces == true && strCharCode == 32 && TextBox.value.length > 0)
				return true;
			else if (bFlag == 0)
				return false;
		}
		else
		{
			return true;
	    }
	}

	 //Checking for decimals			
				if (strDataType == 'Float')
				{
					if(strCharCode == 0 || strCharCode == 8)
					return true;
					if ((strCharCode < 48 || strCharCode > 57) && strCharCode != 13
						&& strCharCode != 16 && strCharCode != 20 && strCharCode != 9 
						&& strCharCode != 46 && strCharCode != 17 && strCharCode != 18 && strCharCode != 190)
					{
						return false;
					}
					var strControlText = document.getElementById(strID).value;
					if (strCharCode == 46)
					{
						if (strControlText.indexOf('.') != -1)
						{
							return false;
						}
					}
					var iDotIndex = strControlText.indexOf('.');
//					if (iDotIndex != -1)
//					{
//						var strDecimalPart = strControlText.substr((iDotIndex + 1), (strControlText.length - (iDotIndex + 1))) 
//						alert(strDecimalPart);
//alert(iScale);			alert(caretpos);			alert(iDotIndex);
//						if (iScale > 0 && strDecimalPart.length >= iScale && caretpos > iDotIndex)
//						{
//							return false;
//						}
//            else if (caretpos <= iDotIndex && (strControlText.length - 1 - strDecimalPart.length) >= (iPrecision - iScale))
//              return false;
//					}
//					else
	//				{
						//alert(iScale);
						if (iScale > 0 && ((iPrecision - iScale) > 0) && strControlText.length > 0 && strControlText.length >= (iPrecision - iScale))
						{
							if (String.fromCharCode(strCharCode) != '.')
							{
								//alert('Scale Zero');
								return false;
							}
			//			}
					}
					return true;
				}


	if (strDataType == 'ValidChars')
	{
		var bFlag = 0;
		for (var iCount = 0; iCount < strValidChars.length; iCount++)
		{
			if (strCharCode == strValidChars.charCodeAt(iCount))
			bFlag = 1
		}
		if (bFlag == 0)
			return false;
		else
			return true;
	}
	if (strDataType == 'None')
	{
		return true;
	}
}
function ChangeColor(strId, strDataType, sFocusColor){
	if (strDataType == 'Numeric')
		window.status = 'Enter numeric data only';
	if (strDataType == 'Alpha')
		window.status = 'Enter alphabets only'; 
	if (strDataType == 'AlphaNumeric')
		window.status = 'Enter alphanumeric data  only'; 
	if (strDataType == 'Float')
		window.status = 'Enter decimals/float values only'; 
	if (strDataType == 'ValidChars')
		window.status = 'Enter only the valid chars'; 	
	if (strDataType == 'None')	
		window.status = 'Enter any data'; 	
	var TextBoxEx = document.getElementById(strId);
	if (sFocusColor != '0')
	{
	  TextBoxEx.style.backgroundColor = sFocusColor;		
	}
}
function ChangeColorBack(strId, strColor)
{
	var TextBoxEx = document.getElementById(strId);
	if (strColor != 0)
		TextBoxEx.style.backgroundColor = strColor;
	else
		TextBoxEx.style.backgroundColor = 'white';
	window.status = '';
}
