/*
USAGE::INCLUDE THIS FILE WITH IN JAVASCRIPT CODE
*/


function allChars(st)
{  
    var st_len = st.length; 
    for (var i = 0; i < st_len; i++)
		if (!((st.charAt(i) <= 'z' && st.charAt(i) >= 'a') || (st.charAt(i) <= 'Z' && st.charAt(i) >= 'A')))
			return false;
	return true;       
}  

/*This function return true if the email string
 is valid otherwise false*/
function test1()
{
alert('inside test');
}
function isEmail(strValue) 
{
	//alert('inside isEmail');
	var result;
	var supported = 0;
	
	if (window.RegExp) 
	{
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	
	if (!supported)
	 {
		result = (strValue.indexOf(".") > 2) && (strValue.indexOf("@") > 0);
		if(result)
			return true;
		else
			return false;
	}
	
	//var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	//var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	//result = (!r1.test(strValue) && r2.test(strValue));
	result = filter.test(strValue);
	if(result)
		return true;
	else 
	 return false;
 
}

/*
This function strips the blanks 
from front and back of the string 
and return the striped string 
*/

function StripBlanks(st)
{
	var ret_str = '';
	var st_len = st.length;
	
   for (var i = 0; i < st_len; i++)
		if (st.charAt(i) != ' ')
			break;
		for(var j=st_len-1;j>=0;j--)
				if(st.charAt(j)!=' ')
				            break;

	for (;i<=j; i++)
 		ret_str += st.charAt(i);
            
        	return ret_str;//returning the required string 
}	
			

/*This is a stripblanks replacement as it was not working in certain cases*/
function Trim(STRING)
{
	while(STRING.charAt((STRING.length -1))==" ")
	{
		STRING = STRING.substring(0,STRING.length-1);
	}
	while(STRING.charAt(0)==" ")
	{
		STRING = STRING.replace(STRING.charAt(0),"");
	}
	return STRING;
}


/*Removes whitespaces &nbsp; from leading and Trailing Position of string*/
function TrimNew(str) 
{
  if (!str) return "";
  return str.replace(/^\s+/, "").replace(/\s+$/, "");
}


/*This function checks whether the data is INTEGER 
 or not.  ON success returns true */
function DataIsInt(strOperand) {
	var oldstring = strOperand;
	var newstring = parseInt(oldstring).toString();
	var InpValid=1;
	if (oldstring.length == newstring.length && newstring != "NaN")
	{
		return true;
	}
	else
	{
		return false;
	}
}

/*This function checks whether the data is FLOAT 
 or not.  ON success returns true */

function DataIsFloat(str)
{
var count=0;
str=StripBlanks(str);
 for(var i=0;i<str.length;i++)
    {
  if(str.charAt(i)<='9'&str.charAt(i)>='0')
 
         continue;
 else
    if(str.charAt(i)=='.')
     { count++;
      if (count==2)
   
      {
   
       return false;}
      }
   else 
   {
   return false;
    }
   
   }
   
   return true;
   
    }

function isValidFloat(str, decimalPlaces, isNegativeAllowed)
{
	var strNumberString = '1234567890+-';
	var stringDecPoint = '.';
	var decimalCharNum = 0;
	var dp = false;
	if (str == "")
		str = "0";

	if (isNaN(str))
		return false;
	else if (str.substring(str.length-1, str.length) == '.')
		return false;
	else
	{
		if (isNegativeAllowed == 0 && str < 0)
			return false;

		for (var i=0; i<str.length; i++)
		{ 
			if (strNumberString.indexOf(str.charAt(i)) == -1)
			{  
				if (stringDecPoint.indexOf(str.charAt(i)) == -1)
					return false;

				if (!dp)
				{ 
					dp = true; 
					decimalCharNum++;
				}
			}
			else
			{
				if (decimalCharNum > decimalPlaces)
					return false;

				if (dp)
					decimalCharNum++;
			}
		}
	}

	return true;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}	

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}


function isValidDate(strValue,required)
{
	var result;
	var elems = strValue.split("/");
	result = (elems.length == 3);
	if (result)
	{
		var strTempDate;
		strTempDate = Date.parse(strValue);
		if(isNaN(strTempDate))
			result = false;
		var month = parseInt(parseFloat(elems[0]));
		var day = parseInt(parseFloat(elems[1]));
		var year = parseInt(parseFloat(elems[2]));
		if(result)
		{
			if (month==2) 
			{
				if ( ( (year%4==0) && (year%100 != 0) ) || (year%400==0) ) 
				{
					if (day > 29)
					{
						result = false; 
					}
				}
				else if (day > 28) 
				{ 
					result = false;
				}
			}
			if ((month==4)||(month==6)||(month==9)||(month==11)) 
			{
				if (day > 30) 
				{ 
					result = false; 
				}
			}
			else if (day > 31) 
			{ 
				result = false; 
			}
			if(result)
				result = (month > 0) && (month < 13);
			if(result)
				result = ((year.toString().length == 2) || (year.toString().length == 4));
			if(result)
			{
				if((year>2999) || (year<=1900))
					result=false;
			}
		}
	}
	return result;
}

  /*Purpose  : Validates string to contain valid numbers*/
function ValidateForNumbers(stringToValidate)
{ 
	var strValidNumbers = "0123456789";
	if((stringToValidate.indexOf("-")== 0) || (stringToValidate.indexOf("+")== 0))
	{
		stringToValidate = stringToValidate.substr(1,(stringToValidate.length-1));
	}
	/*if(stringToValidate.indexOf("0")== 0 && stringToValidate.length > 1)
	{
		return false;
		//stringToValidate = stringToValidate.substr(1,(stringToValidate.length-1));
	}*/
	for(var outercounter = 0; outercounter < stringToValidate.length; outercounter++)
	{
		var character = stringToValidate.charAt(outercounter);
		for(var innercounter = 0;innercounter < strValidNumbers.length; innercounter++)
		{
			if(character == strValidNumbers.charAt(innercounter))
				break;
		}    
		if(innercounter == strValidNumbers.length)
		{
			return false;
			break;
		}
	}
	if(outercounter == stringToValidate.length)
	{
		return true;
	}
}

 /* Purpose  : Validates string for a valid Integer value*/
function ValidateForInteger(NumberToValidate)
{ 
	if (ValidateForNumbers(NumberToValidate))
	{
		var val = parseInt(NumberToValidate);
		if (val != 'NaN')
			if ((NumberToValidate < -999999999) || (NumberToValidate > 999999999))
				return false;
			else
				return true;
		else
			return false;
	}
	else
		return false;
}

/*
  Purpose  : Validates string for a valid positive Integer value*/
function ValidateForPositiveInteger(NumberToValidate)
{ 
	if (ValidateForNumbers(NumberToValidate))
	{
		if ((NumberToValidate < -999999999) || (NumberToValidate > 999999999))
			return false;
		else if(NumberToValidate.indexOf("-")== 0)
			return false;
		else
			return true;
	}
	else
		return false;
}

/*

Purpose : Checks for press of Numeric Key only for an integer Textbox
*/

function checkNumericKey()
{
	var KeyAscii=event.keyCode;
	if(KeyAscii >= 48 && KeyAscii <= 57) 
		event.returnValue = true;
	else
		event.returnValue = false;
}

function checkKeyForFloatValues()
{
	var KeyAscii=event.keyCode;
	if((KeyAscii >= 48 && KeyAscii <= 57) || (KeyAscii == 46))
		event.returnValue = true;
	else
		event.returnValue = false;
}

/*
(Escape invalid Characters)
*/
function EscapeInvalidCharacters(strToEncode)
{
	strToEncode = strToEncode.replace(/&/g,'&amp;');
	strToEncode = strToEncode.replace(/</g,"&lt;");
	strToEncode = strToEncode.replace(/>/g,"&gt;");
	strToEncode = strToEncode.replace(/'/g,"\'");
	return strToEncode;
}